Dateiverwaltung für die WebBox
ulrich
2017-02-20 c509a016b6156e34034500803f8e18a2a9529940
web/ui/ui.js
@@ -1,5 +1,355 @@
var cm;
var pfad = '';
function fm_init() {
  $('.codeeditor-space').hide();
  $('.code-editor-container').hide();
  $('#newTextFile').on('click', fm_menu_neue_textdatei);
  $('#newFolder').on('click', fm_menu_neuer_ordner);
  $('#editTextFile').on('click', fm_menu_textdatei_bearbeiten);
  $('#saveFile').on('click', fm_menu_datei_speichern);
  $('#closeFile').on('click', fm_menu_datei_schliessen);
  $('#saveModal').on('hidden.bs.modal', function (e) {
    $('#modal_ok').attr('onclick','').unbind('click');
  });
  $('#logout').click(fm_logout);
  fm_get_login();
  fm_get_list('');
}
function fm_menu_neue_textdatei() {
  $('#dateiansicht').hide();
  $('.codeeditor-space').show();
  $('.code-editor-container').show();
  fm_code_edit('Test');
}
function fm_menu_textdatei_bearbeiten() {
  /*
  $('#dateiansicht').hide();
  $('.codeeditor-space').show();
  $('.code-editor-container').show();
  fm_code_edit('Test');
  */
 fm_get_file_content();
}
function fm_menu_datei_schliessen() {
  $('.codeeditor-space').hide();
  $('.code-editor-container').hide();
  cm.toTextArea();
  $('#dateiansicht').show();
}
function fm_dateiwahl() {
  var elem = this;
  //console.log('datei gewaehlt ' + $(elem).text().trim());
  if($(elem).children(0).hasClass('fa-folder')) {
    var ordner = $(elem).text().trim();
    if(pfad.length > 0) {
      pfad = pfad + '/' + ordner;
    } else {
      pfad = ordner;
    }
    fm_get_list(pfad);
  } else if($(elem).children(0).hasClass('fa-file')) {
    $('.datei-gewaehlt').removeClass('datei-gewaehlt');
    $(elem).children(0).addClass('datei-gewaehlt');
  } else {
    //console.log('kein folder oder file...');
  }
}
function fm_menu_neuer_ordner() {
  fm_get_new_folder();
}
function fm_bc_click() {
  var elem = this;
  var bcPfad = $(elem).attr('rpath');
  //console.log(relPath);
  if(bcPfad !== undefined) {
    pfad = bcPfad;
    fm_get_list(bcPfad);
  } else {
    pfad = '';
    fm_get_list('');
  }
  
}
  //console.log($(elem).text() + ' ' + $(elem).attr('rpath') + ' ' + $(elem).hasClass('active'));
}
/* ----- API Calls ------------- */
function fm_get_login() {
  var m = '?c=de.uhilger.um.pub.SessionManager&m=getSessionUser';
  var u = '../../um/pub' + m;
  fm_get(u, "json", function(resp) {
    $('#userMenu').text(resp.UserData.firstName);
  });
}
function fm_get_new_folder() {
  $('#modal_ok').click(function() {
    // hier speichern
    var m = '?c=de.uhilger.filecms.api.FileMgr&m=newFolder&p=' + pfad + '&p=' + $('#dateiname').val();
    var u = '../svc' + m;
    fm_get(u, "json", function(resp) {
    });
    /*
    var m = '?c=de.uhilger.filecms.api.FileMgr&m=saveTextFile';
    var u = '../svc' + m;
    fm_post(u, {p1: pfad, p2: $('#dateiname').val(), p3: cm.getValue()}, function(resp) {
    });
    */
  });
  $('.modal-title').text('Neuer Ordner');
  $('#dialogfrage').text("Name?");
  $('#dateiname').val('');
  $('#dateiname').attr('placeholder', 'Ordnername');
  $('#saveModal').modal({
    keyboard: false,
    show: true
  });
}
function fm_get_file_content() {
  var fname = $('.datei-gewaehlt').text();
  //console.log(pfad + '/' + fname);
  /*
  var m = '?c=de.uhilger.filecms.api.FileMgr&m=list&p=' + relPath;
  var u = '../svc' + m;
  fm_get(u, "json", function(resp) {
  });
  */
}
// http://localhost:8079/file-cms/svc?c=de.uhilger.filecms.api.FileMgr&f=JSONNICE&m=list&p=
function fm_get_list(relPfad) {
  var m = '?c=de.uhilger.filecms.api.FileMgr&m=list&p=' + relPfad;
  var u = '../svc' + m;
  fm_get(u, "json", function(resp) {
    if(resp.List[0].FileRef !== undefined) {
      var files = new Array();
      if(resp.List[0].FileRef instanceof Array) {
        for(var i = 0; i < resp.List[0].FileRef.length; i++) {
          files.push(new FileRef(resp.List[0].FileRef[i]));
        }
      } else {
        files.push(new FileRef(resp.List[0].FileRef));
      }
      var fl = new FileList(files);
      var template = $('#tpl-kacheln').html();
      Mustache.parse(template);   // optional, speeds up future uses
      $('.figure').attr('onclick','').unbind('click');
      $('#dateien').empty();
      $('#dateien').html(Mustache.render(template, fl));
      $('.figure').click(fm_dateiwahl);
    } else {
      $('#dateien').empty();
    }
    var template;
    $('.breadcrumb-item').attr('onclick','').unbind('click');
    $('#bcnav').empty();
    var dirList = new Array();
    var rp = '';
    //console.log("'" + relPfad + "'");
    var dirs = relPfad.split('/');
    //console.log(dirs.length);
    dirList.push(new BcrFile(rp, 'Home'));
    if(relPfad.length > 0) {
      for(var i = 0; i < dirs.length - 1; i++) {
        if(rp.length > 0 ) {
          dirList.push(new BcrFile(rp + '/' + dirs[i], dirs[i]));
          rp = rp + '/' + dirs[i];
        } else {
          dirList.push(new BcrFile(dirs[i], dirs[i]));
          rp = dirs[i];
        }
      }
      var bl = new BcrFiles(dirList);
      if(dirList.length > 0) {
        template = $('#tpl-bcr').html();
        Mustache.parse(template);   // optional, speeds up future uses
        $('#bcnav').html(Mustache.render(template, bl));
      }
      if(dirs.length > 0) {
        dirList.push(new BcrFile(rp + '/' + dirs[dirs.length-1], dirs[dirs.length-1]));
        template = $('#tpl-bcr2').html();
        Mustache.parse(template);   // optional, speeds up future uses
        $('#bcnav').append(Mustache.render(template, dirList[dirList.length-1]));
      } else {
        template = $('#tpl-bcr2').html();
        Mustache.parse(template);   // optional, speeds up future uses
        $('#bcnav').append(Mustache.render(template, dirList[0]));
      }
      $('#bcnav').append($('#tpl-bcr3').html());
    } else {
      pfad = '';
      template = $('#tpl-bcr2').html();
      Mustache.parse(template);   // optional, speeds up future uses
      $('#bcnav').append(Mustache.render(template, dirList[0]));
    }
    $('.breadcrumb-item').click(fm_bc_click);
  });
}
function fm_menu_datei_speichern() {
  $('#modal_ok').click(function() {
    // hier speichern
    var m = '?c=de.uhilger.filecms.api.FileMgr&m=saveTextFile';
    var u = '../svc' + m;
    fm_post(u, {p1: pfad, p2: $('#dateiname').val(), p3: cm.getValue()}, function(resp) {
    });
  });
  $('.modal-title').text('Datei speichern');
  $('#dialogfrage').text("Dateiname?");
  $('#dateiname').val('');
  $('#dateiname').attr('placeholder', 'Dateiname');
  $('#saveModal').modal({
    keyboard: false,
    show: true
  });
  // FileRef saveTextFile(String relPath, String fileName, String contents)
  /*
  var t = new Template(-2, $('#filename').val(), self.cm.getValue(), 3);
  var u = '../api/tr/?c=de.uhilger.webbox.api.ContentApi&m=newTemplate';
  self.post(u, {p: self.serialise(t)}, function (resp) {
    self.isnew = false;
    self.editid = resp.Template.id;
    $('#contlist').append(self.buildContListItem(3, resp.Template.name, resp.Template.id));
    $('#templateeditor').addClass('hidden');
  });
  */
}
function fm_logout() {
  var m = '?c=de.uhilger.filecms.pub.SessionManager&m=expireSession';
  var u = '../pub' + m;
  fm_get(u, "text", function(resp) {
    $('#userMenu').text('nicht angemeldet');
    window.location.href = '../logout.html';
  });
}
/* ---- codemirror editor handling -------- */
function fm_code_edit(content) {
  //var windowHeight = $(window).height();
  //$("editspace").empty();
  //self.cm.toTextArea();
  cm = CodeMirror.fromTextArea(document.getElementById("editspace"), {
    lineNumbers: true,
    mode: "xml",
    viewportMargin : Infinity,
    extraKeys: {
        "F9": function(cm) {
        cm.setOption("fullScreen", !cm.getOption("fullScreen"));
      },
        "Esc": function(cm) {
        if (cm.getOption("fullScreen")) cm.setOption("fullScreen", false);
      }
    }
  });
  cm.setValue(content);
}
/* -------- helper functions ----------- */
function fm_get(u, dtype, scallback) {
  $.ajax({
    url: u,
    type: "GET",
    dataType: dtype,
    success: scallback,
    error: function (xhr, status, errorThrown) {
      alert("Error: " + errorThrown + " Status: " + status + " URL: " + u);
    },
    complete: function (xhr, status) {
      //console.log( "The request is complete!" );
    }
  });
}
function fm_post(u, d, scallback) {
  $.ajax({
    url: u,
    data: d,
    type: "POST",
    dataType: "json",
    success: scallback,
    error: function (xhr, status, errorThrown) {
      $('#fehler').html("Error: " + errorThrown + " Status: " + status);
    },
    complete: function (xhr, status) {
      //alert( "The request is complete!" );
    }
  });
}
function fm_serialise(obj) {
  return '{"' + obj.constructor.name + '":' + JSON.stringify(obj) + '}';
}
/* ----- Objekte ----- */
function FileList(fl) {
  this.files = fl;
}
function FileRef(obj) {
  var self = this;
  this.fr = obj;
  this.typeClass = function() {
    if(self.fr.isDirectory) {
      return 'fa-folder';
    } else {
      return 'fa-file';
    }
  };
  this.fileName = function() {
    var namen = self.fr.absolutePath.split('/');
    if(namen.length > 0) {
      return namen[namen.length - 1];
    } else {
      return self.fr.absolutePath;
    }
  };
}
function BcrFiles(fl) {
  this.files = fl;
}
function BcrFile(rp, n) {
  this.relPath = rp;
  this.fName = n;
}