', { id: 'trashTree', 'class': 'category2' }).append($trashElement);
$container.append($trashList);
};
var resetTree = module.resetTree = function () {
$tree.html('');
createTree($tree, [ROOT]);
if (!isWorkgroup()) {
createUnsorted($tree, [UNSORTED]);
createTemplate($tree, [TEMPLATE]);
createAllFiles($tree, [FILES_DATA]);
}
createTrash($tree, [TRASH]);
};
var hideMenu = module.hideMenu = function () {
$contextMenu.hide();
$trashTreeContextMenu.hide();
$trashContextMenu.hide();
$contentContextMenu.hide();
$defaultContextMenu.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();
};
var getReadOnlyUrl = APP.getRO = function (href) {
if (!filesOp.isFile(href)) { return; }
var i = href.indexOf('#') + 1;
var hash = href.slice(i);
var base = href.slice(0, i);
var hrefsecret = Cryptpad.getSecrets(hash);
var viewHash = Cryptpad.getViewHashFromKeys(hrefsecret.channel, hrefsecret.keys);
return base + viewHash;
};
$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('open_ro')) {
var el = filesOp.findElement(files, path);
if (filesOp.isFolder(el)) { return; }
var roUrl = getReadOnlyUrl(el);
openFile(roUrl);
}
else if ($(this).hasClass('newfolder')) {
var onCreated = function (info) {
module.newFolder = info.newPath;
module.displayDirectory(path);
};
filesOp.createNewFolder(path, null, onCreated);
}
module.hideMenu();
});
$defaultContextMenu.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('open')) {
$element.dblclick();
}
else if ($(this).hasClass('open_ro')) {
var el = filesOp.findElement(files, path);
if (filesOp.isFolder(el)) { return; }
var roUrl = getReadOnlyUrl(el);
openFile(roUrl);
}
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);
hideNewButton();
});
$(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) {
// "Del"
if (e.which === 46) {
if (filesOp.isPathInFilesData(currentPath)) { return; } // We can't remove elements directly from filesData
var $selected = $iframe.find('.selected');
if (!$selected.length) { return; }
var paths = [];
var isTrash = filesOp.isPathInTrash(currentPath);
$selected.each(function (idx, elmt) {
if (!$(elmt).data('path')) { return; }
paths.push($(elmt).data('path'));
});
// If we are in the trash or anon pad or if we are holding the "shift" key, delete permanently,
if (isTrash || e.shiftKey) {
//var cb = filesOp.removeFromTrash; // We're in the trash
//if (!isTrash) { cb = filesOp.deletePathPermanently; } // We're in root
var msg = Messages._getKey("fm_removeSeveralPermanentlyDialog", [paths.length]);
if (paths.length === 1) {
msg = Messages.fm_removePermanentlyDialog;
}
Cryptpad.confirm(msg, function(res) {
if (!res) { return; }
filesOp.deletePathsPermanently(paths);
refresh();
});
return;
}
// else move to trash
moveElements(paths, [TRASH], false, refresh);
}
});
$(ifrw).contextmenu(function () {
module.hideMenu();
return false;
});
var onRefresh = {
refresh: function() {
if (onRefresh.to) {
window.clearTimeout(onRefresh.to);
}
onRefresh.to = window.setTimeout(refresh, 500);
}
};
proxy.on('change', [], function (o, n, p) {
var path = arguments[2];
if (path[0] !== 'drive') { return false; }
path = path.slice(1);
var cPath = currentPath.slice();
if ((filesOp.isPathInUnsorted(cPath) && filesOp.isPathInUnsorted(path)) ||
(filesOp.isPathInTemplate(cPath) && filesOp.isPathInTemplate(path)) ||
(path.length >= cPath.length && filesOp.isSubpath(path, cPath)) ||
(filesOp.isPathInTrash(cPath) && 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) {
if (filesOp.isPathInHrefArray(cPath)) {
onRefresh.refresh();
} else {
refreshFilesData();
}
}
module.resetTree();
return false;
}).on('remove', [], function (o, p) {
var path = arguments[1];
var cPath = currentPath.slice();
if ((filesOp.isPathInUnsorted(cPath) && filesOp.isPathInUnsorted(path)) ||
(filesOp.isPathInTemplate(cPath) && filesOp.isPathInTemplate(path)) ||
(path.length >= cPath.length && filesOp.isSubpath(path, cPath)) ||
(filesOp.isPathInTrash(cPath) && 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;
});
$iframe.find('#tree').mousedown(function (e) {
if (APP.resizeTree) { return; }
APP.resizeTree = window.setInterval(function () {
$driveToolbar.find('.leftside').width($tree.width());
}, 100);
});
$(ifrw).mouseup(function (e) {
window.clearInterval(APP.resizeTree);
APP.resizeTree = undefined;
});
refresh();
};
var setName = APP.setName = function (newName) {
if (typeof(newName) !== 'string') { return; }
var myUserNameTemp = Cryptpad.fixHTML(newName.trim());
if(myUserNameTemp.length > 32) {
myUserNameTemp = myUserNameTemp.substr(0, 32);
}
var myUserName = myUserNameTemp;
Cryptpad.setAttribute('username', myUserName, function (err, data) {
if (err) {
logError("Couldn't set username", err);
return;
}
APP.userName.lastName = myUserName;
APP.$displayName.text(myUserName);
});
};
// don't initialize until the store is ready.
Cryptpad.ready(function () {
var storeObj = Cryptpad.getStore().getProxy && Cryptpad.getStore().getProxy().proxy ? Cryptpad.getStore().getProxy() : undefined;
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) || Cryptpad.getUserHash() || 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 proxy;
if (storeObj && !window.location.hash.slice(1)) { proxy = storeObj.proxy; }
else {
var rt = window.rt = module.rt = Listmap.create(listmapConfig);
proxy = rt.proxy;
}
var onCreate = 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 && !localStorage.FS_hash && !Cryptpad.getUserHash() && !window.location.hash) {
localStorage.FS_hash = editHash;
}
module.patchText = TextPatcher.create({
realtime: realtime,
logging: true,
});
var userList = APP.userList = info.userList;
APP.userName = {};
var config = {
displayed: ['useradmin', 'language', 'spinner', 'lag', 'state'],
readOnly: readOnly,
ifrw: window,
common: Cryptpad,
userName: {
setName: setName,
lastName: APP.userName
},
hideShare: true
};
var toolbar = APP.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.userAdmin);
APP.$displayName = $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;
//TODO change text & transalte
Cryptpad.alert(Messages._getKey('fm_alert_backupUrl', [url]));
$('#fm_backupUrl').val(url);
$('#fm_backupUrl').click(function () {
$(this).select();
});
});
$userBlock.append($backupButton);
}
};
var onReady = function () {
module.files = proxy;
if (JSON.stringify(proxy) === '{}') {
var store = Cryptpad.getStore(true);
var drive = proxy.drive = {};
store.get(Cryptpad.storageKey, function (err, s) {
drive[FILES_DATA] = s;
initLocalStorage();
init(proxy);
APP.userList.onChange();
Cryptpad.removeLoadingScreen();
});
return;
}
if (!proxy.drive || typeof(proxy.drive) !== 'object') { proxy.drive = {}; }
initLocalStorage();
init(proxy);
APP.userList.onChange();
Cryptpad.removeLoadingScreen();
};
var onDisconnect = function (info) {
setEditable(false);
if (APP.refresh) { APP.refresh(); }
APP.toolbar.failed();
Cryptpad.alert(Messages.common_connectionLost);
};
var onReconnect = function (info) {
setEditable(true);
if (APP.refresh) { APP.refresh(); }
APP.toolbar.reconnecting(info.myId);
Cryptpad.findOKButton().click();
};
if (storeObj && !window.location.hash) {
onCreate(storeObj.info);
onReady();
} else {
proxy.on('create', function (info) {
onCreate(info);
}).on('ready', function () {
onReady();
});
}
proxy.on('disconnect', function () {
onDisconnect();
});
proxy.on('reconnect', function (info) {
onReconnect(info);
});
});
Cryptpad.onError(function (info) {
if (info) {
onConnectError();
}
});
});