/*
Dateiverwaltung - File management in your browser
Copyright (C) 2017 Ulrich Hilger, http://uhilger.de
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see .
*/
var cm;
var pfad = '';
var modus = 'kacheln';
var openFileName = '';
var ed;
var openEditor;
var userid;
var tmo;
var tmo2;
var cutCopySrcDir;
var cutCopyFiles;
var cutCopyOperation;
var loc;
var PERS_DIR = "Persoenlich";
var PUB_DIR = "Oeffentlich";
var DAV_DIR = "Austausch";
var BASE_DIR = "$basis";
var DATA_DIR = "$daten";
var WWW_DIR = "www";
var compilerIssues;
var fm_slideshow = false;
function fm_init() {
$("#mce-editor").hide();
$('.codeeditor-space').hide();
$('.code-editor-container').hide();
$('.sued').hide();
//$('.ost').attr('flex-basis', 0);
$('#newTab').on('click', fm_newTab);
$('#newTextFile').on('click', fm_menu_neue_textdatei);
$('#newDoc').on('click', fm_menu_neues_dokument);
$('#newFolder').on('click', fm_get_new_folder);
$('#viewFile').on('click', fm_view_file);
$('#viewSlideshow').on('click', fm_view_slideshow);
$('#editTextFile').on('click', function() {
fm_get_file_content('text');
});
$('#editDocFile').on('click', function() {
fm_get_file_content('dok');
});
$('#saveFile').on('click', fm_menu_datei_speichern);
$('#saveFileAs').on('click', fm_menu_datei_speichern_unter);
$('#closeFile').on('click', fm_menu_datei_schliessen);
$('#renameFile').on('click', fm_menu_datei_umbenennen);
$('#m-zip').on('click', fm_menu_ordner_packen);
$('#m-unzip').on('click', fm_menu_datei_entpacken);
$('#m-html-export').on('click', fm_menu_html_export);
$('#m-del').on('click', fm_menu_delete);
$('#m-cut').on('click', fm_menu_cut);
$('#m-copy').on('click', fm_menu_copy);
$('#m-paste').on('click', fm_menu_paste);
$('#m-shrink').on('click', fm_menu_shrink);
$('#m-rotate').on('click', fm_menu_rotate);
$('#m-toggle-slideshow').on('click', fm_toggle_show_slideshow);
$('#m-test').on('click', fm_menu_test);
$('#m-test-2').on('click', fm_menu_compile);
$('#m-test-3').on('click', fm_menu_compile_all);
$('#m-compile-results').on('click', fm_fusszeile_zeigen);
$('#m-build-app').on('click', fm_menu_build_app);
$('#m-ant-build').on('click', fm_menu_ant_build);
$('#saveModal').on('hidden.bs.modal', function (e) {
$('#modal_ok').attr('onclick','').unbind('click');
});
$('#logout').click(fm_logout);
$('#fusszeile-schliessen').on('click', fm_fusszeile_schliessen);
fm_get_login();
var parsedUrl = new URL(window.location.href);
var wunschPfad = parsedUrl.searchParams.get("pfad");
if(wunschPfad !== null && wunschPfad.length > 0) {
//if(wunschPfad.length > 0) {
pfad = wunschPfad;
fm_get_list(wunschPfad);
fm_init_uploader();
//} else {
// fm_get_list('');
// fm_init_uploader();
//}
} else {
fm_get_list('');
fm_init_uploader();
}
loc = window.location.protocol + '//' + window.location.host;
}
function fm_newTab() {
window.open('/file-cms/ui/?pfad=' + pfad, '_blank');
}
function fm_toggle_show_slideshow() {
if(fm_slideshow) {
$('#m-toggle-slideshow').text('Diashow einschalten');
fm_slideshow = false;
} else {
$('#m-toggle-slideshow').text('Diashow ausschalten');
fm_slideshow = true;
}
}
function fm_resize_editor() {
window.clearTimeout(tmo);
tmo = window.setTimeout(function () {
try {
var myHeight = $('.zentrum').height() - $('.nord').height() - 4;
ed.theme.resizeTo('100%', myHeight); // sets the dimensions of the editable area
} catch (err) {
}
}, 200);
}
function fm_fusszeile_schliessen() {
$('.sued').hide();
}
function fm_fusszeile_zeigen() {
$('.sued').show();
}
function fm_get_path(uid) {
//console.log('pfad: ' + pfad);
var restdir;
if(pfad.indexOf(PUB_DIR) > -1) {
restdir = pfad.substr(PUB_DIR.length);
} else if(pfad.indexOf(PERS_DIR) > -1) {
restdir = pfad.substr(PERS_DIR.length);
} else if(pfad.indexOf(BASE_DIR) > -1) {
restdir = pfad.substr(BASE_DIR.length);
} else if(pfad.indexOf(DATA_DIR) > -1) {
restdir = pfad.substr(DATA_DIR.length);
} else if(pfad.indexOf(DAV_DIR) > -1) {
restdir = pfad.substr(DAV_DIR.length);
}
if(restdir !== undefined && restdir.startsWith('/')) {
restdir = restdir.substr(1);
if(restdir.indexOf(WWW_DIR) > -1) {
restdir = restdir.replace(WWW_DIR, 'data');
}
}
var pdir = fm_get_base(uid);
// console.log('fm_get_path path: ' + pdir + "/" + restdir);
if(restdir.length > 1) {
return pdir + "/" + restdir;
} else {
return pdir;
}
}
function fm_get_base(uid) {
//console.log('pfad: ' + pfad);
var pdir;
if(pfad.indexOf(PUB_DIR) > -1) {
pdir = '/data/' + uid;
} else if(pfad.indexOf(PERS_DIR) > -1) {
pdir = '/home/' + uid;
} else if(pfad.indexOf(BASE_DIR) > -1) {
pdir = '';
} else if(pfad.indexOf(DATA_DIR) > -1) {
pdir = '';
}
//console.log('fm_get_base base: ' + pdir + uid);
return pdir;
}
function fm_dok_editor_init(uid) {
var base = fm_get_path(uid);
edCount = 0;
//console.log("fm_dok_editor_init calling tinymce.init with base: " + base + "/");
/*
* vgl.
* http://stackoverflow.com/questions/4651676/how-do-i-remove-tinymce-and-then-re-add-it
*/
//tinymce.EditorManager.execCommand('mceRemoveControl',true, 'textarea.text-editor');
//tinymce.EditorManager.execCommand('mceAddControl',true, editor_id);
if(ed !== undefined) {
ed.destroy();
}
tinymce.remove('textarea.text-editor');
tinymce.EditorManager.editors = [];
/*
* Konfiguration TinyMCE
*/
tinymce.init({
content_css : "/file-cms/ui/mce.css",
//content_css: "/jslib/bootstrap/css/bootstrap.min.css",
selector: "textarea.text-editor",
statusbar: false,
menubar: false,
plugins: 'advlist charmap code image link lists media table print preview save table textcolor importcss',
toolbar: 'undo redo | styleselect | image table | link unlink | bullist numlist | outdent indent | code',
/*
menu: {
file: {title: 'File', items: 'savevers | exit'},
edit: {title: 'Edit', items: 'undo redo | cut copy paste pastetext | selectall'},
view: {title: 'View', items: 'visualaid | code | link image media | template hr'},
},
*/
resize: 'both',
importcss_append: true,
width: "100%",
height: '100%',
relative_urls : true,
convert_urls : false,
document_base_url : base + "/",
setup: function (editor) {
ed = editor;
}
});
$(window).on('resize', fm_resize_editor);
fm_resize_editor();
}
/* ----- Uploader Anfang ----------*/
var dateien = new Array();
function fm_init_uploader() {
var dropzone = document.getElementById("dateien");
dropzone.ondragover = dropzone.ondragenter = function(event) {
event.stopPropagation();
event.preventDefault();
};
dropzone.ondrop = function(event) {
event.stopPropagation();
event.preventDefault();
var fileList = event.dataTransfer.files;
for (var i=0; i] bei der jede ArrayList nur ein Element hat
return liste;
*/
return fm_dateiliste_bilden(gewaehlte);
}
function fm_dateiliste_bilden(gewaehlte) {
var fnames = new Array();
var i = 0;
while(i < gewaehlte.length) {
var str = { "java.lang.String": $(gewaehlte[i]).text()};
fnames.push(str);
//console.log('loeschen ' + pfad + ' ' + $(gewaehlte[i]).text());
//i += 2;
i++;
}
var liste = '{"List":' + JSON.stringify(fnames) + '}';
// 'liste' enthaelt:
// {"List":[{"java.lang.String":"test3.txt"},{"java.lang.String":"Test1.txt"},{"java.lang.String":"Test2.txt"}]}
// und wird auf dem Server zu einer List[ArrayList] bei der jede ArrayList nur ein Element hat
return liste;
}
function fm_menu_html_export() {
fm_export_html();
}
/* --------- Dateifunktionen Ende ---------- */
function fm_menu_test() {
var w = $('.ost').width();
if(w == 0) {
$('.ost').width('20%');
$('.ost').text('Hallo Welt!');
} else {
$('.ost').empty();
$('.ost').width(0);
}
}
function fm_menu_compile() {
fm_compile('0', fm_mark_compile_results_in_editor);
}
function fm_menu_compile_all() {
fm_compile_all();
}
function fm_menu_build_app() {
fm_build_app();
}
function fm_menu_ant_build() {
fm_ant_build();
}
function fm_mark_compile_results_in_editor(resp) {
cm.clearGutter("breakpoints");
if(resp.List[0].CompilerIssue !== undefined) {
cm.setGutterMarker(0, "breakpoints", makeMarker("Code hat Fehler"));
var lno;
var eMsg;
if(resp.List[0].CompilerIssue instanceof Array) {
var issueNo = 0;
while(issueNo < resp.List[0].CompilerIssue.length) {
// console.log(resp.List[0].CompilerIssue[issueNo].kind);
// console.log(resp.List[0].CompilerIssue[issueNo].lineNumber);
// console.log(resp.List[0].CompilerIssue[issueNo].sourceName);
// console.log(resp.List[0].CompilerIssue[issueNo].message);
lno = resp.List[0].CompilerIssue[issueNo].lineNumber;
eMsg = resp.List[0].CompilerIssue[issueNo].kind + ' ' + resp.List[0].CompilerIssue[issueNo].message;
cm.setGutterMarker(lno-1, "breakpoints", makeMarker(eMsg));
issueNo++;
}
} else {
lno = resp.List[0].CompilerIssue.lineNumber;
eMsg = resp.List[0].CompilerIssue.kind + ' ' + resp.List[0].CompilerIssue.message;
cm.setGutterMarker(lno-1, "breakpoints", makeMarker(eMsg));
}
}
}
function fm_ansicht_umschalten() {
if($('#ansicht').children(0).hasClass('fa-th-list')) {
$('#ansicht').children(0).addClass('fa-th-large');
$('#ansicht').children(0).removeClass('fa-th-list');
modus = 'liste';
} else {
$('#ansicht').children(0).addClass('fa-th-list');
$('#ansicht').children(0).removeClass('fa-th-large');
modus = 'kacheln';
}
fm_get_list(pfad);
}
function fm_set_modus() {
if(modus == 'kacheln') {
$('#ansicht').children(0).addClass('fa-th-list');
$('#ansicht').children(0).removeClass('fa-th-large');
} else {
$('#ansicht').children(0).addClass('fa-th-large');
$('#ansicht').children(0).removeClass('fa-th-list');
}
}
function fm_menu_neue_textdatei() {
fm_text_edit('Neue Datei');
}
function fm_menu_neues_dokument() {
fm_dok_edit('');
}
function fm_menu_datei_schliessen() {
if(openEditor === 'text') {
if(!cm.getDoc().isClean()) {
fm_ask_for_save();
} else {
fm_do_close();
}
} else {
if(tinymce.activeEditor.undoManager.hasUndo()) {
fm_ask_for_save();
} else {
fm_do_close();
}
}
}
function fm_ask_for_save() {
// hier Abfrage ob gespeichert werden soll
$('#confirmOk').click(function() {
$('#confirmOk').attr('onclick','').unbind('click');
fm_menu_datei_speichern(function() {
fm_do_close();
});
});
$('#confirmCancel').click(function() {
$('#confirmCancel').attr('onclick','').unbind('click');
});
$('#confirmClose').show();
$('#confirmClose').click(function() {
$('#confirmClose').attr('onclick','').unbind('click');
fm_do_close();
});
$('#confirmOk').text("Speichern");
$('#confirmOk').addClass('btn-primary');
$('#confirmOk').removeClass('btn-danger');
$('#confirmModalTitle').text("Schliessen");
$('#confirmModalBody').text("Speichern vor dem Schliessen?");
$('#confirmModal').modal({
keyboard: false,
show: true
});
}
function fm_do_close() {
$('.codeeditor-space').hide();
$('.code-editor-container').hide();
$("#mce-editor").hide();
if(cm !== undefined) {
cm.toTextArea();
}
openFileName = '';
openEditor = '';
fm_get_list(pfad);
fm_filectls_show();
}
function fm_dateiwahl(ev) {
var elem = this;
//console.log('dateiwahl nach click, event shift: ' + ev.shiftKey + ", event ctrl: " + ev.ctrlKey);
if(modus == 'kacheln') {
// Kacheln
if($(elem).children(0).hasClass('fa-folder')) {
if(/*ev.shiftKey || */ ev.ctrlKey) {
$(elem).find('.dateiname').addClass('datei-gewaehlt');
} else {
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('datei')) {
if(/*ev.shiftKey || */ ev.ctrlKey) {
// mehrere Dateien sollen gewaehlt werden
} else {
$('.datei-gewaehlt').removeClass('datei-gewaehlt');
}
$(elem).find('.dateiname').addClass('datei-gewaehlt');
} else {
//console.log('kein folder oder file...');
}
} else {
// Liste
if($(elem).find('.datei-elem').children(0).hasClass('fa-file-o')) {
// Datei
if(/*ev.shiftKey || */ ev.ctrlKey) {
// mehrere Dateien sollen gewaehlt werden
} else {
$('.table-info').removeClass('table-info');
$('.datei-gewaehlt').removeClass('datei-gewaehlt');
}
$(elem).addClass('table-info');
$(elem).find('.dateiname').addClass('datei-gewaehlt');
} else {
// Ordner
if(/*ev.shiftKey || */ ev.ctrlKey) {
$(elem).children(0).addClass('datei-gewaehlt');
} else {
var ordner = $(elem).find('.dateiname').text();
if(pfad.length > 0) {
pfad = pfad + '/' + ordner;
} else {
pfad = ordner;
}
fm_get_list(pfad);
}
}
}
}
function fm_bc_click() {
var elem = this;
var bcPfad = $(elem).attr('rpath');
if(bcPfad !== undefined) {
pfad = bcPfad;
fm_get_list(bcPfad);
} else {
pfad = '';
fm_get_list('');
}
}
function fm_text_edit(content, mode) {
fm_filectls_hide();
$('.codeeditor-space').show();
$('.code-editor-container').show();
fm_code_edit(content, mode);
openEditor = 'text';
}
function fm_dok_edit(content) {
fm_filectls_hide();
fm_dok_editor_init(userid);
$("#mce-editor").show();
window.clearTimeout(tmo2);
tmo2 = window.setTimeout(function () {
try {
ed.setContent(content);
openEditor = 'dok';
} catch (err) {
}
}, 200);
}
function fm_filectls_hide() {
$('#zentrum_bc').hide();
$('#dateien').hide();
$('#editMenu').addClass('disabled');
}
function fm_filectls_show() {
$('#zentrum_bc').show();
$('#dateien').show();
$('#editMenu').removeClass('disabled');
}
/* ----- API Calls ------------- */
function fm_view_file() {
// http://localhost:8080/file-cms/ui/#
// http://localhost:8080/file-cms/data/admin/usw.
var userid = $('#userMenu').text();
var gewaehlte = $('.datei-gewaehlt');
var fname = $(gewaehlte[0]).text();
var path = fm_get_path(userid);
window.open(path + '/' + fname, '_blank');
}
function fm_view_slideshow() {
// https://localhost:8443/file-cms/fotos/?o=ulrich/bilder/diashow/ordnername
var userid = $('#userMenu').text();
var gewaehlte = $('.datei-gewaehlt');
var fname = $(gewaehlte[0]).text();
var path = fm_get_path(userid);
var restdir = path;
if(path.indexOf('/data/') > -1) {
restdir = path.substr('/data/'.length);
}
//window.open('/file-cms/fotos/?o=' + userid + '/' + path + '/' + fname, '_blank');
window.open('/file-cms/fotos/?o=' + restdir + '/' + fname, '_blank');
}
function fm_menu_datei_umbenennen() {
var gewaehlte = $('.datei-gewaehlt');
var fname = $(gewaehlte[0]).text();
$('#modal_ok').click(function() {
// hier umbenennen
fm_rename_file(fname, pfad, $('#dateiname').val());
});
$('#saveModalTitle').text('Datei umbenennen');
$('#dialogfrage').text("Neuer Dateiname?");
if(fname !== undefined) {
$('#dateiname').val(fname);
$('#dateiname').attr('placeholder', 'Dateiname');
$('#saveModal').modal({
keyboard: false,
show: true
});
}
}
function fm_menu_datei_entpacken() {
var gewaehlte = $('.datei-gewaehlt');
var fname = $(gewaehlte[0]).text();
fm_unzip_file(fname);
}
/**
* Anname: Man ist in dem Ordner, der gepackt werden soll.
* Dann ist das Archiv anschliessend im Eltern-Ordner.
*/
function fm_menu_ordner_packen() {
//var gewaehlte = $('.datei-gewaehlt');
//var fname = $(gewaehlte[0]).text();
//console.log("Pfad: " + pfad + ", Datei: " + fname);
fm_pack_folder();
}
/*
* Hier merkt sich die Dateiverwalting die markierten Dateien fuer
* eine Dateioperation. Bei cut wird in der 'paste' Operation auf dem Server
* fuer die 'gemerkten' Dateien ein Move gemacht. Die Dateien werden
* vom gewaehlten Ort zum Zielort verschoben.
*
* @returns {undefined}
*/
function fm_cut_files() {
cutCopySrcDir = pfad;
cutCopyFiles = fm_gewaehlte_dateien();
cutCopyOperation = 'cut';
}
/*
* Hier merkt sich die Dateiverwalting die markierten Dateien fuer
* eine Dateioperation. Bei copy wird in der 'paste' Operation auf dem Server
* fuer die 'gemerkten' Dateien eine Kopie gemacht. Die Dateien werden
* am gewaehlten Ort behalten und am Zielort wird eine Kopier der
* gewaehlten DAteien erstellt
*
* @returns {undefined}
*/
function fm_copy_files() {
cutCopySrcDir = pfad;
cutCopyFiles = fm_gewaehlte_dateien();
cutCopyOperation = 'copy';
}
function fm_render_list(fl) {
if(modus == 'kacheln') {
// Kachelansicht
var template = $('#tpl-kacheln').html();
Mustache.parse(template); // optional, speeds up future uses
$('.datei-zeile').attr('onclick','').unbind('click');
$('#dateien').empty();
$('#dateien').html(Mustache.render(template, fl));
$('.figure').click(fm_dateiwahl);
} else {
// Listenansicht
var template = $('#tpl-liste').html();
Mustache.parse(template); // optional, speeds up future uses
$('.figure').attr('onclick','').unbind('click');
$('#dateien').empty();
$('#dateien').html(Mustache.render(template, fl));
$('.datei-zeile').click(fm_dateiwahl);
}
/*
if(modus == 'kacheln') {
// Kachelansicht
$.get('tpl-kacheln.txt', function(template) {
$('#dateien').empty();
$('#dateien').html(Mustache.render(template, fl));
$('.figure').click(fm_dateiwahl);
});
$('.datei-zeile').attr('onclick','').unbind('click');
} else {
// Listenansicht
$.get('tpl-liste.txt', function(template) {
$('#dateien').empty();
$('#dateien').html(Mustache.render(template, fl));
$('.datei-zeile').click(fm_dateiwahl);
});
$('.figure').attr('onclick','').unbind('click');
}
*/
}
function fm_menu_datei_speichern(callback) {
//var fname = $('.datei-gewaehlt').text();
var fname = openFileName;
if(fname !== undefined && fname !== '') {
fm_save_file(fname, 'saveTextFile', callback);
} else {
fm_menu_datei_speichern_unter(callback);
}
}
function fm_menu_datei_speichern_unter(callback) {
$('#modal_ok').click(function() {
$('#modal_ok').attr('onclick','').unbind('click');
// hier speichern
fm_save_file($('#dateiname').val(), 'saveTextFileAs', callback);
});
$('#saveModalTitle').text('Datei speichern');
$('#dialogfrage').text("Dateiname?");
var fname = $('.datei-gewaehlt').text();
if(fname !== undefined) {
$('#dateiname').val(fname);
} else {
$('#dateiname').val('');
}
$('#dateiname').attr('placeholder', 'Dateiname');
$('#saveModal').modal({
keyboard: false,
show: true
});
}
/* ---- codemirror editor handling -------- */
/*
function htmlDecode(value){
return $('').html(value).text();
}
*/
function fm_code_edit(content, m) {
cm = CodeMirror.fromTextArea(document.getElementById("editspace"), {
lineNumbers: true,
lineWrapping: true,
gutters: ["CodeMirror-linenumbers", "breakpoints"],
mode: m,
viewportMargin : Infinity,
tabSize: 2,
extraKeys: {
"F9": function(cm) {
cm.setOption("fullScreen", !cm.getOption("fullScreen"));
},
"Esc": function(cm) {
if (cm.getOption("fullScreen")) cm.setOption("fullScreen", false);
},
".": function(cm) {
console.log('dot pressed: perhaps look up class or method name');
/*
* Hier kann man eine Funktion ausloesen, die fuer das
* Wort vor dem Punkt (Name der Klasse) eine Liste mit
* Vorschlaegen fuer Methodennamen einblendet.
*/
/*
* CodeMirror.Pass laesst das Zeichen zum Editorinhalt durch,
* verhindert aber das Ausloesen von 'keyHandled'
*/
return CodeMirror.Pass;
}
}
});
//cm.setValue(htmlDecode(content));
//cm.setValue(content);
cm.setValue(unescapeHtml(content));
cm.getDoc().markClean();
cm.on("gutterClick", function(theEditor, lineNumber) {
var info = theEditor.lineInfo(lineNumber);
//--lineNumber;
//console.log(info.gutterMarkers.breakpoints.message);
//var marker = info.gutterMarkers.breakpoints;
//$(marker).tooltip('toggle');
});
/*
cm.on("keyHandled", function(theEditor, keyName, event){
console.log('cm.keyHandled keyName: ' + keyName + ', event.type: ' + event.type);
});
*/
}
function makeMarker(msg) {
var marker = document.createElement("div");
marker.style.color = "#822";
marker.innerHTML = "●";
//marker.message = msg;
$(marker).tooltip({
placement: 'right',
title: msg,
offset: '0 -30'
});
return marker;
}
/* -------- helper functions ----------- */
function fm_serialise(obj) {
return '{"' + obj.constructor.name + '":' + JSON.stringify(obj) + '}';
}
function escapeHtml(text) {
text = text.replace(/\u228/g,'ä');
text = text.replace(/\u246/g,'ö');
text = text.replace(/\u252/g,'ü');
text = text.replace(/\u196/g,'Ä');
text = text.replace(/\u214/g,'Ö');
text = text.replace(/\u220/g,'Ü');
text = text.replace(/\u223/g,'ß');
text = text.replace(/\u26/g,'&');
return text;
}
function unescapeHtml(text) {
text = text.replace(/ä/g, String.fromCharCode(228));
text = text.replace(/ö/g, String.fromCharCode(246));
text = text.replace(/ü/g, String.fromCharCode(252));
text = text.replace(/Ä/g, String.fromCharCode(196));
text = text.replace(/Ö/g, String.fromCharCode(214));
text = text.replace(/Ü/g, String.fromCharCode(220));
text = text.replace(/ß/g, String.fromCharCode(223));
text = text.replace(/&/g, String.fromCharCode(26));
return text;
}