', { id: 'trashTree' }).append($trashElement);
$container.append($trashList);
};
var resetTree = module.resetTree = function () {
$tree.html('');
createTree($tree, [ROOT]);
createUnsorted($tree, [UNSORTED]);
createAllFiles($tree, [FILES_DATA]);
createTrash($tree, [TRASH]);
};
var hideMenu = module.hideMenu = function () {
$contextMenu.hide();
$trashTreeContextMenu.hide();
$trashContextMenu.hide();
$contentContextMenu.hide();
};
var stringifyPath = function (path) {
if (!$.isArray(path)) { return; }
var rootName = function (s) {
var prettyName;
switch (s) {
case ROOT:
prettyName = ROOT_NAME;
break;
case UNSORTED:
prettyName = UNSORTED_NAME;
break;
case FILES_DATA:
prettyName = FILES_DATA_NAME;
break;
case TRASH:
prettyName = TRASH_NAME;
break;
default:
prettyName = s;
}
return prettyName;
};
var $div = $('');
var i = 0;
var space = 10;
path.forEach(function (s) {
if (i === 0) { s = rootName(s); }
$div.append($('', {'style': 'margin: 0 0 0 ' + i * space + 'px;'}).text(s));
$div.append($('
'));
i++;
});
return $div.html();
};
$contextMenu.on("click", "a", function(e) {
e.stopPropagation();
var path = $(this).data('path');
var $element = $(this).data('element');
if (!$element || !path || path.length < 2) {
log(Messages.fm_forbidden);
debug("Directory context menu on a forbidden or unexisting element. ", $element, path);
return;
}
if ($(this).hasClass("rename")) {
displayRenameInput($element, path);
}
else if($(this).hasClass("delete")) {
moveElements([path], [TRASH], false, refresh);
}
else if ($(this).hasClass('open')) {
$element.dblclick();
}
else if ($(this).hasClass('newfolder')) {
var onCreated = function (info) {
module.newFolder = info.newPath;
module.displayDirectory(path);
};
filesOp.createNewFolder(path, null, onCreated);
}
module.hideMenu();
});
$contentContextMenu.on('click', 'a', function (e) {
e.stopPropagation();
var path = $(this).data('path');
if ($(this).hasClass("newfolder")) {
var onCreated = function (info) {
module.newFolder = info.newPath;
refresh();
};
filesOp.createNewFolder(path, null, onCreated);
}
else if ($(this).hasClass("newdoc")) {
var type = $(this).data('type') || 'pad';
$(this).attr('href','/' + type + '/#?path=' + encodeURIComponent(path));
}
module.hideMenu();
});
$trashTreeContextMenu.on('click', 'a', function (e) {
e.stopPropagation();
var path = $(this).data('path');
var $element = $(this).data('element');
if (!$element || !filesOp.comparePath(path, [TRASH])) {
log(Messages.fm_forbidden);
debug("Trash tree context menu on a forbidden or unexisting element. ", $element, path);
return;
}
if ($(this).hasClass("empty")) {
Cryptpad.confirm(Messages.fm_emptyTrashDialog, function(res) {
if (!res) { return; }
filesOp.emptyTrash(refresh);
});
}
module.hideMenu();
});
$trashContextMenu.on('click', 'a', function (e) {
e.stopPropagation();
var path = $(this).data('path');
var $element = $(this).data('element');
if (!$element || !path || path.length < 2) {
log(Messages.fm_forbidden);
debug("Trash context menu on a forbidden or unexisting element. ", $element, path);
return;
}
var name = path[path.length - 1];
if ($(this).hasClass("remove")) {
if (path.length === 4) { name = path[1]; }
Cryptpad.confirm(Messages._getKey("fm_removePermanentlyDialog", [name]), function(res) {
if (!res) { return; }
filesOp.removeFromTrash(path, refresh);
});
}
else if ($(this).hasClass("restore")) {
if (path.length === 4) { name = path[1]; }
Cryptpad.confirm(Messages._getKey("fm_restoreDialog", [name]), function(res) {
if (!res) { return; }
filesOp.restoreTrash(path, refresh);
});
}
else if ($(this).hasClass("properties")) {
if (path.length !== 4) { return; }
var element = filesOp.getTrashElementData(path);
var sPath = stringifyPath(element.path);
Cryptpad.alert('' + Messages.fm_originalPath + ":
" + sPath);
}
module.hideMenu();
});
$(ifrw).on('click', function (e) {
if (e.which !== 1) { return ; }
removeSelected(e);
removeInput(e);
module.hideMenu(e);
});
$(ifrw).on('drag drop', function (e) {
removeInput(e);
module.hideMenu(e);
});
$(ifrw).on('mouseup drop', function (e) {
$iframe.find('.droppable').removeClass('droppable');
});
$(ifrw).on('keydown', function (e) {
pressKey(e.which, true);
});
$(ifrw).on('keyup', function (e) {
pressKey(e.which, false);
});
$(ifrw).on('keydown', function (e) {
if (e.which === 46) {
var $selected = $iframe.find('.selected');
if (!$selected.length) { return; }
var paths = [];
$selected.each(function (idx, elmt) {
if (!$(elmt).data('path')) { return; }
paths.push($(elmt).data('path'));
});
// If we are in the trash or if we are holding the "shift" key, delete permanently,
// else move to trash
if (filesOp.isPathInTrash(currentPath) || e.shiftKey) {
var todo = filesOp.removeFromTrash;
if (!filesOp.isPathInTrash(currentPath)) {
// If we are not in the trash, we just have to remove the key from root/unsorted
todo = filesOp.deletePathPermanently;
}
// If we are already in the trash, delete the elements permanently
var msg = Messages._getKey("fm_removeSeveralPermanentlyDialog", [paths.length]);
if (paths.length === 1) {
var path = paths[0];
var element = filesOp.findElement(files, path);
var name = filesOp.isInTrashRoot(path) ? path[1] : (filesOp.isPathInUnsorted(path) ? filesOp.getTitle(element) : path[path.length - 1]);
msg = Messages._getKey("fm_removePermanentlyDialog", [name]);
}
Cryptpad.confirm(msg, function(res) {
if (!res) { return; }
paths.forEach(function(p) {
todo(p);
});
refresh();
});
return;
}
moveElements(paths, [TRASH], false, refresh);
}
});
var onRefresh = {
refresh: function() {
if (onRefresh.to) {
window.clearTimeout(onRefresh.to);
}
onRefresh.to = window.setTimeout(refresh, 500);
}
};
files.on('change', [], function (o, n, p) {
var path = arguments[2];
if ((filesOp.isPathInUnsorted(currentPath) && filesOp.isPathInUnsorted(path)) ||
(path.length >= currentPath.length && filesOp.isSubpath(path, currentPath)) ||
(filesOp.isPathInTrash(currentPath) && filesOp.isPathInTrash(path))) {
// Reload after a few ms to make sure all the change events have been received
onRefresh.refresh();
} else if (path.length && path[0] === FILES_DATA) {
refreshFilesData();
}
module.resetTree();
return false;
}).on('remove', [], function (o, p) {
var path = arguments[1];
if ((filesOp.isPathInUnsorted(currentPath) && filesOp.isPathInUnsorted(path)) ||
(path.length >= currentPath.length && filesOp.isSubpath(path, currentPath)) ||
(filesOp.isPathInTrash(currentPath) && filesOp.isPathInTrash(path))) {
// Reload after a few to make sure all the change events have been received
onRefresh.to = window.setTimeout(refresh, 500);
}
module.resetTree();
return false;
});
refresh();
};
// don't initialize until the store is ready.
Cryptpad.ready(function () {
Cryptpad.styleAlerts();
if (window.location.hash && window.location.hash === "#iframe") {
$iframe.find('body').addClass('iframe');
window.location.hash = "";
APP.homePageIframe = true;
}
var hash = window.location.hash.slice(1) || localStorage.FS_hash;
var secret = Cryptpad.getSecrets(hash);
var readOnly = APP.readOnly = secret.keys && !secret.keys.editKeyStr;
var listmapConfig = module.config = {
data: {},
websocketURL: Cryptpad.getWebsocketURL(),
channel: secret.channel,
readOnly: readOnly,
validateKey: secret.keys.validateKey || undefined,
crypto: Crypto.createEncryptor(secret.keys),
logging: false
};
var rt = window.rt = module.rt = Listmap.create(listmapConfig);
rt.proxy.on('create', function (info) {
var realtime = module.realtime = info.realtime;
var editHash = APP.editHash = !readOnly ? Cryptpad.getEditHashFromKeys(info.channel, secret.keys) : undefined;
var viewHash = APP.viewHash = Cryptpad.getViewHashFromKeys(info.channel, secret.keys);
APP.hash = readOnly ? viewHash : editHash;
if (!readOnly && (!window.location.hash || !localStorage.FS_hash)) {
localStorage.FS_hash = editHash;
}
module.patchText = TextPatcher.create({
realtime: realtime,
logging: true,
});
var userList = APP.userList = info.userList;
var config = {
readOnly: readOnly,
ifrw: window,
common: Cryptpad,
hideShare: true
};
var toolbar = info.realtime.toolbar = Toolbar.create(APP.$bar, info.myID, info.realtime, info.getLag, userList, config);
var $bar = APP.$bar;
var $rightside = $bar.find('.' + Toolbar.constants.rightside);
var $userBlock = $bar.find('.' + Toolbar.constants.username);
if (APP.homePageIframe) {
var $linkToMain = $bar.find('.cryptpad-link a');
$linkToMain.attr('href', '#');
$linkToMain.attr('title', '');
$linkToMain.css('cursor', 'default');
$linkToMain.off('click');
}
if (!readOnly) {
var $backupButton = Cryptpad.createButton('', true);
$backupButton.on('click', function() {
var url = window.location.origin + window.location.pathname + '#' + editHash;
Cryptpad.alert("Backup URL for this pad. It is highly recommended that you do not share it with other people.
Anybody with that URL can remove all the files in your file manager.
" + url);
});
$userBlock.append($backupButton);
}
}).on('ready', function () {
module.files = rt.proxy;
if (JSON.stringify(rt.proxy) === '{}') {
var store = Cryptpad.getStore(true);
store.get(Cryptpad.storageKey, function (err, s) {
rt.proxy[FILES_DATA] = s;
initLocalStorage();
init(rt.proxy);
});
return;
}
initLocalStorage();
init(rt.proxy);
APP.userList.onChange();
})
.on('disconnect', function (info) {
setEditable(false);
console.error('err');
Cryptpad.alert(Messages.common_connectionLost);
});
});
});