Add support for templates in the file manager

pull/1/head
yflory 8 years ago
parent 1772cc93c8
commit 9b6c9e3891

@ -86,6 +86,14 @@ define([
cb();
};
Store.addTemplate = function (href) {
filesOp.addTemplate(href);
};
Store.listTemplates = function () {
return filesOp.listTemplates();
};
Store.getProxy = function () {
return exp;
};

@ -172,6 +172,7 @@ define(function () {
out.fm_trashName = "Trash";
out.fm_unsortedName = "Unsorted files";
out.fm_filesDataName = "All files";
out.fm_templateName = "Templates";
out.fm_newFolder = "New folder";
out.fm_newFolderButton = "NEW FOLDER";
out.fm_folderName = "Folder name";

@ -451,6 +451,21 @@ define([
return localStorage[attr];
};
// STORAGE - TEMPLATES
var listTemplates = common.listTemplates = function (type) {
var allTemplates = getStore().listTemplates();
if (!type) { return allTemplates; }
var templates = allTemplates.filter(function (f) {
var parsed = parsePadUrl(f.href);
return parsed.type === type;
});
return templates;
};
var addTemplate = common.addTemplate = function (href) {
getStore().addTemplate(href);
};
// STORAGE
/* fetch and migrate your pad history from localStorage */

@ -97,7 +97,7 @@ li {
text-decoration: underline;
}
#tree #trashTree, #tree #unsortedTree, #tree #allfilesTree {
#tree #trashTree, #tree #unsortedTree, #tree #templateTree, #tree #allfilesTree {
margin-top: 2em;
}

@ -9,6 +9,7 @@ define([
var UNSORTED = "unsorted";
var FILES_DATA = "filesData";
var TRASH = "trash";
var TEMPLATE = "template";
var NEW_FOLDER_NAME = Messages.fm_newFolder;
var init = module.init = function (files, config) {
@ -46,6 +47,12 @@ define([
var isPathInUnsorted = exp.isPathInUnsorted = function (path) {
return path[0] && path[0] === UNSORTED;
};
var isPathInTemplate = exp.isPathInTemplate = function (path) {
return path[0] && path[0] === TEMPLATE;
};
var isPathInHrefArray = exp.isPathInHrefArray = function (path) {
return isPathInUnsorted(path) || isPathInTemplate(path);
};
var isPathInTrash = exp.isPathInTrash = function (path) {
return path[0] && path[0] === TRASH;
};
@ -162,6 +169,13 @@ define([
return files[UNSORTED].slice();
};
var getTemplateFiles = exp.getTemplateFiles = function () {
if (!files[TEMPLATE]) {
files[TEMPLATE] = [];
}
return files[TEMPLATE].slice();
};
var getFilesRecursively = function (root, arr) {
for (var e in root) {
if (isFile(root[e])) {
@ -229,13 +243,15 @@ define([
var checkDeletedFiles = function () {
var rootFiles = getRootFiles();
var unsortedFiles = getUnsortedFiles();
var templateFiles = getTemplateFiles();
var trashFiles = getTrashFiles();
var toRemove = [];
files[FILES_DATA].forEach(function (arr) {
var f = arr.href;
if (rootFiles.indexOf(f) === -1
&& unsortedFiles.indexOf(f) === -1
&& trashFiles.indexOf(f) === -1) {
&& trashFiles.indexOf(f) === -1
&& templateFiles.indexOf(f) === -1) {
toRemove.push(arr);
}
});
@ -339,7 +355,7 @@ define([
}
var element = findElement(files, path);
var key = path[path.length - 1];
var name = isPathInUnsorted(path) ? getTitle(element) : key;
var name = isPathInHrefArray(path) ? getTitle(element) : key;
var parentPath = path.slice();
parentPath.pop();
pushToTrash(name, element, parentPath);
@ -347,7 +363,6 @@ define([
if (cb) { cb(); }
};
//TODO add suport for TEMPLATE here
var moveElement = exp.moveElement = function (elementPath, newParentPath, cb, keepOld) {
if (comparePath(elementPath, newParentPath)) { return; } // Nothing to do...
if (isPathInTrash(newParentPath)) {
@ -364,14 +379,15 @@ define([
return;
}
if (isPathInUnsorted(newParentPath)) { //TODO || TEMPLATE
if (isPathInHrefArray(newParentPath)) {
if (isFolder(element)) {
log(Messages.fo_moveUnsortedError);
log(Messages.fo_moveUnsortedError); //TODO or template
return;
} else {
if (isPathInUnsorted(elementPath)) { return; }
if (files[UNSORTED].indexOf(element) === -1) {
files[UNSORTED].push(element);
if (elementPath[0] === newParentPath[0]) { return; }
var fileRoot = newParentPath[0];
if (files[fileRoot].indexOf(element) === -1) {
files[fileRoot].push(element);
}
if (!keepOld) { deleteFromObject(elementPath); }
if(cb) { cb(); }
@ -381,7 +397,7 @@ define([
var name;
if (isPathInUnsorted(elementPath)) {
if (isPathInHrefArray(elementPath)) {
name = getTitle(element);
} else if (isInTrashRoot(elementPath)) {
// Element from the trash root: elementPath = [TRASH, "{dirName}", 0, 'element']
@ -403,13 +419,16 @@ define([
// "Unsorted" is an array of href: we can't move several of them using "moveElement" in a
// loop because moveElement removes the href from the array and it changes the path for all
// the other elements. We have to move them all and then remove them from unsorted
var moveUnsortedElements = exp.moveUnsortedElements = function (paths, newParentPath, cb) {
var moveHrefArrayElements = exp.moveHrefArrayElements = function (paths, newParentPath, cb) {
if (!paths || paths.length === 0) { return; }
if (isPathInUnsorted(newParentPath)) { return; }
//if (isPathInHrefArray(newParentPath)) { return; }
var elements = {};
// Get the elements
paths.forEach(function (p) {
if (!isPathInUnsorted(p)) { return; }
// Here we move only files from array categories (unsorted, template...)
if (!isPathInHrefArray(p)) { return; }
// And we check that we don't want to move to the same location
if (p[0] === newParentPath[0]) { return; }
var el = findElement(files, p);
if (el) { elements[el] = p; }
});
@ -419,22 +438,21 @@ define([
});
// Remove the elements from their old location
Object.keys(elements).forEach(function (el) {
var idx = files[UNSORTED].indexOf(el);
var fileRoot = elements[el][0];
var idx = files[fileRoot].indexOf(el);
if (idx !== -1) {
files[UNSORTED].splice(idx, 1);
files[fileRoot].splice(idx, 1);
}
});
if (cb) { cb(); }
};
var moveElements = exp.moveElements = function (paths, newParentPath, cb) {
var unsortedPaths = paths.filter(function (p) {
return p[0] === UNSORTED;
});
moveUnsortedElements(unsortedPaths, newParentPath);
var unsortedPaths = paths.filter(isPathInHrefArray);
moveHrefArrayElements(unsortedPaths, newParentPath);
// Copy the elements to their new location
paths.forEach(function (p) {
if (isPathInUnsorted(p)) { return; }
if (isPathInHrefArray(p)) { return; }
moveElement(p, newParentPath, null);
});
if(cb) { cb(); }
@ -477,12 +495,13 @@ define([
var element = findElement(files, path);
var parentEl = getTrashElementData(path);
var newPath = parentEl.path;
if (isPathInUnsorted(newPath)) {
if (files[UNSORTED].indexOf(element) === -1) {
files[UNSORTED].push(element);
if (isPathInHrefArray(newPath)) {
var fileRoot = newPath[0];
if (files[fileRoot].indexOf(element) === -1) {
files[fileRoot].push(element);
}
removeFromTrashArray(parentEl, path[1]);
cb();
}
return;
}
// Find the new parent element
@ -576,7 +595,12 @@ define([
var unsortedFiles = getUnsortedFiles();
var rootFiles = getRootFiles();
var trashFiles = getTrashFiles();
if (path && name) {
var templateFiles = getTemplateFiles();
if (path && isPathInHrefArray(path)) {
var parentEl = findElement(files, newPath);
parentEl.push(href);
}
else if (path && name) {
var newPath = decodeURIComponent(path).split(',');
var parentEl = findElement(files, newPath);
if (parentEl) {
@ -585,11 +609,38 @@ define([
return;
}
}
if (unsortedFiles.indexOf(href) === -1 && rootFiles.indexOf(href) === -1 && trashFiles.indexOf(href) === -1) {
if (unsortedFiles.indexOf(href) === -1 && rootFiles.indexOf(href) === -1&& templateFiles.indexOf(href) === -1 && trashFiles.indexOf(href) === -1) {
files[UNSORTED].push(href);
}
};
// addTemplate is called when we want to add a new pad, never visited, to the templates list
// first, we must add it to FILES_DATA, so the input has to be an fileDAta object
var addTemplate = exp.addTemplate = function (fileData) {
if (typeof fileData !== "object" || !fileData.href || !fileData.title) { return; }
var href = fileData.href;
var test = files[FILES_DATA].some(function (o) {
o.href === href;
});
if (!test) {
files[FILES_DATA].push(fileData);
}
if (files[TEMPLATE].indexOf(href) === -1) {
files[TEMPLATE].push(href);
}
};
var listTemplates = exp.listTemplates = function (type) {
var templateFiles = getTemplateFiles();
var res = [];
templateFiles.forEach(function (f) {
var data = getFileData(f);
res.push(JSON.parse(JSON.stringify(data)));
});
return res;
};
var uniq = function (a) {
var seen = {};
return a.filter(function(item) {
@ -614,6 +665,7 @@ define([
if (typeof(files[TRASH]) !== "object") { debug("TRASH was not an object"); files[TRASH] = {}; }
if (!$.isArray(files[FILES_DATA])) { debug("FILES_DATA was not an array"); files[FILES_DATA] = []; }
if (!$.isArray(files[UNSORTED])) { debug("UNSORTED was not an array"); files[UNSORTED] = []; }
if (!$.isArray(files[TEMPLATE])) { debug("TEMPLATE was not an array"); files[TEMPLATE] = []; }
var fixRoot = function (element) {
for (var el in element) {
@ -653,16 +705,42 @@ define([
var fixUnsorted = function (us) {
var rootFiles = getRootFiles().slice();
var templateFiles = getTemplateFiles();
var toClean = [];
us.forEach(function (el, idx) {
if (!isFile(el) || rootFiles.indexOf(el) !== -1) {
if (!isFile(el) || rootFiles.indexOf(el) !== -1 || templateFiles.indexOf(el) !== -1) {
toClean.push(idx);
}
});
toClean.forEach(function (el) {
var idx = us.indexOf(el);
if (idx !== -1) {
us.splice(idx, 1);
}
});
};
files[UNSORTED] = uniq(files[UNSORTED]);
fixUnsorted(files[UNSORTED]);
var fixTemplate = function (us) {
var rootFiles = getRootFiles().slice();
var unsortedFiles = getUnsortedFiles();
var toClean = [];
us.forEach(function (el, idx) {
if (!isFile(el) || rootFiles.indexOf(el) !== -1 || unsortedFiles.indexOf(el) !== -1) {
toClean.push(idx);
}
});
toClean.forEach(function (el) {
var idx = us.indexOf(el);
if (idx !== -1) {
us.splice(idx, 1);
}
});
};
files[TEMPLATE] = uniq(files[TEMPLATE]);
fixUnsorted(files[TEMPLATE]);
var fixFilesData = function (fd) {
var rootFiles = getRootFiles();
var unsortedFiles = getUnsortedFiles();

@ -18,14 +18,15 @@ define([
var $iframe = $('#pad-iframe').contents();
var ifrw = $('#pad-iframe')[0].contentWindow;
Cryptpad.addLoadingScreen();
//Cryptpad.addLoadingScreen();
var onConnectError = function (info) {
Cryptpad.errorLoadingScreen(Messages.websocketError);
};
var APP = window.APP = {
$bar: $iframe.find('#toolbar'),
editable: false
editable: false,
Cryptpad: Cryptpad
};
var ROOT = "root";
@ -34,6 +35,8 @@ define([
var UNSORTED_NAME = Messages.fm_unsortedName;
var FILES_DATA = Cryptpad.storageKey;
var FILES_DATA_NAME = Messages.fm_filesDataName;
var TEMPLATE = "template";
var TEMPLATE_NAME = Messages.fm_templateName;
var TRASH = "trash";
var TRASH_NAME = Messages.fm_trashName;
var LOCALSTORAGE_LAST = "cryptpad-file-lastOpened";
@ -204,6 +207,7 @@ define([
var $fileIcon = $('<span>', {"class": "fa fa-file-text-o file"});
var $upIcon = $('<span>', {"class": "fa fa-arrow-circle-up"});
var $unsortedIcon = $('<span>', {"class": "fa fa-files-o"});
var $templateIcon = $('<span>', {"class": "fa fa-cubes"});
var $trashIcon = $('<span>', {"class": "fa fa-trash"});
var $trashEmptyIcon = $('<span>', {"class": "fa fa-trash-o"});
var $collapseIcon = $('<span>', {"class": "fa fa-minus-square-o expcol"});
@ -396,7 +400,7 @@ define([
var openDirectoryContextMenu = function (e) {
var $element = $(e.target).closest('li');
$contextMenu.find('li').show();
if ($element.hasClass('file-element')) {
if ($element.find('.file-element').length) {
$contextMenu.find('a.newfolder').parent('li').hide();
}
openContextMenu(e, $contextMenu);
@ -670,6 +674,7 @@ define([
if (name === ROOT && path.length === 1) { name = ROOT_NAME; }
else if (name === TRASH && path.length === 1) { name = TRASH_NAME; }
else if (name === UNSORTED && path.length === 1) { name = UNSORTED_NAME; }
else if (name === TEMPLATE && path.length === 1) { name = TEMPLATE_NAME; }
else if (name === FILES_DATA && path.length === 1) { name = FILES_DATA_NAME; }
else if (filesOp.isPathInTrash(path)) { name = getTrashTitle(path); }
var $title = $('<h1>').text(name);
@ -787,17 +792,25 @@ define([
var SORT_FOLDER_DESC = 'sortFoldersDesc';
var SORT_FILE_BY = 'sortFilesBy';
var SORT_FILE_DESC = 'sortFilesDesc';
var getSortFileDesc = function () {
return Cryptpad.getLSAttribute(SORT_FILE_DESC) === "true";
};
var getSortFolderDesc = function () {
return Cryptpad.getLSAttribute(SORT_FOLDER_DESC) === "true";
};
var onSortByClick = function (e) {
var $span = $(this);
var value;
if ($span.hasClass('foldername')) {
value = Cryptpad.getLSAttribute(SORT_FOLDER_DESC);
value = getSortFolderDesc();
Cryptpad.setLSAttribute(SORT_FOLDER_DESC, value ? false : true);
refresh();
return;
}
value = Cryptpad.getLSAttribute(SORT_FILE_BY);
var descValue = Cryptpad.getLSAttribute(SORT_FILE_DESC);
var descValue = getSortFileDesc();
if ($span.hasClass('filename')) {
if (value === '') {
descValue = descValue ? false : true;
@ -807,7 +820,7 @@ define([
}
} else {
var found = false;
['title', 'atime', 'ctime'].forEach(function (c) {
['title', 'type', 'atime', 'ctime'].forEach(function (c) {
if (!found && $span.hasClass(c)) {
found = true;
if (value === c) { descValue = descValue ? false : true; }
@ -826,7 +839,7 @@ define([
var addFolderSortIcon = function ($list) {
var $icon = $sortAscIcon.clone();
if (Cryptpad.getLSAttribute(SORT_FOLDER_DESC)) {
if (getSortFolderDesc()) {
$icon = $sortDescIcon.clone();
}
if (typeof(Cryptpad.getLSAttribute(SORT_FOLDER_DESC)) !== "undefined") {
@ -845,7 +858,7 @@ define([
};
var addFileSortIcon = function ($list) {
var $icon = $sortAscIcon.clone();
if (Cryptpad.getLSAttribute(SORT_FILE_DESC)) {
if (getSortFileDesc()) {
$icon = $sortDescIcon.clone();
}
var classSorted;
@ -860,7 +873,7 @@ define([
var $fihElement = $('<span>', {'class': 'element'}).appendTo($fileHeader);
var $fhName = $('<span>', {'class': 'name filename'}).text(Messages.fm_fileName).click(onSortByClick);
var $fhTitle = $('<span>', {'class': 'title '}).text(Messages.fm_title).click(onSortByClick);
var $fhType = $('<span>', {'class': 'type'}).text(Messages.table_type);
var $fhType = $('<span>', {'class': 'type'}).text(Messages.table_type).click(onSortByClick);
var $fhAdate = $('<span>', {'class': 'atime'}).text(Messages.fm_lastAccess).click(onSortByClick);
var $fhCdate = $('<span>', {'class': 'ctime'}).text(Messages.fm_creation).click(onSortByClick);
$fihElement.append($fhName);
@ -891,6 +904,10 @@ define([
if (prop) {
var element = useHref || useData ? el : root[el];
var e = useData ? element : filesOp.getFileData(element);
if (prop === 'type') {
var hrefData = Cryptpad.parsePadUrl(e.href);
return hrefData.type;
}
if (prop === 'atime' || prop === 'ctime') {
return new Date(e[prop]);
}
@ -933,20 +950,20 @@ define([
};
// Unsorted element are represented by "href" in an array: they don't have a filename
// and they don't hav a hierarchical structure (folder/subfolders)
var displayUnsorted = function ($container) {
var unsorted = files[UNSORTED];
if (allFilesSorted()) { return; }
var displayHrefArray = function ($container, rootName) {
var unsorted = files[rootName];
if (rootName === UNSORTED && allFilesSorted()) { return; }
var $fileHeader = getFileListHeader(false);
$container.append($fileHeader);
var keys = unsorted;
var sortedFiles = sortElements(false, [UNSORTED], keys, Cryptpad.getLSAttribute(SORT_FILE_BY), !Cryptpad.getLSAttribute(SORT_FILE_DESC), true);
var sortedFiles = sortElements(false, [rootName], keys, Cryptpad.getLSAttribute(SORT_FILE_BY), !getSortFileDesc(), true);
sortedFiles.forEach(function (href) {
var file = filesOp.getFileData(href);
if (!file) {
debug("getUnsortedFiles returns an element not present in filesData: ", href);
debug("Unsorted or template returns an element not present in filesData: ", href);
return;
}
var idx = files[UNSORTED].indexOf(href);
var idx = files[rootName].indexOf(href);
var $icon = $fileIcon.clone();
var $name = $('<span>', { 'class': 'file-element element' });
addFileData(href, file.title, $name, false);
@ -955,7 +972,7 @@ define([
}).append($icon).append($name).dblclick(function () {
openFile(href);
});
var path = [UNSORTED, idx];
var path = [rootName, idx];
$element.data('path', path);
$element.click(function(e) {
e.stopPropagation();
@ -972,7 +989,7 @@ define([
var $fileHeader = getFileListHeader(false);
$container.append($fileHeader);
var keys = allfiles;
var sortedFiles = sortElements(false, [FILES_DATA], keys, Cryptpad.getLSAttribute(SORT_FILE_BY), !Cryptpad.getLSAttribute(SORT_FILE_DESC), false, true);
var sortedFiles = sortElements(false, [FILES_DATA], keys, Cryptpad.getLSAttribute(SORT_FILE_BY), !getSortFileDesc(), false, true);
sortedFiles.forEach(function (file) {
var $icon = $fileIcon.clone();
var $name = $('<span>', { 'class': 'file-element element' });
@ -1013,8 +1030,8 @@ define([
});
});
});
var sortedFolders = sortTrashElements(true, filesList, null, !Cryptpad.getLSAttribute(SORT_FOLDER_DESC));
var sortedFiles = sortTrashElements(false, filesList, Cryptpad.getLSAttribute(SORT_FILE_BY), !Cryptpad.getLSAttribute(SORT_FILE_DESC));
var sortedFolders = sortTrashElements(true, filesList, null, !getSortFolderDesc());
var sortedFiles = sortTrashElements(false, filesList, Cryptpad.getLSAttribute(SORT_FILE_BY), !getSortFileDesc());
if (filesOp.hasSubfolder(root, true)) { $list.append($folderHeader); }
sortedFolders.forEach(function (f) {
var $element = createElement([TRASH], f.spath, root, true);
@ -1039,6 +1056,7 @@ define([
}
var isTrashRoot = filesOp.comparePath(path, [TRASH]);
var isUnsorted = filesOp.comparePath(path, [UNSORTED]);
var isTemplate = filesOp.comparePath(path, [TEMPLATE]);
var isAllFiles = filesOp.comparePath(path, [FILES_DATA]);
var root = filesOp.findElement(files, path);
@ -1077,8 +1095,8 @@ define([
var $folderHeader = getFolderListHeader();
var $fileHeader = getFileListHeader(true);
if (isUnsorted) {
displayUnsorted($list);
if (isUnsorted || isTemplate) {
displayHrefArray($list, path[0]);
} else if (isAllFiles) {
displayAllFiles($list);
} else if (isTrashRoot) {
@ -1089,8 +1107,8 @@ define([
if (filesOp.hasSubfolder(root)) { $list.append($folderHeader); }
// display sub directories
var keys = Object.keys(root);
var sortedFolders = sortElements(true, path, keys, null, !Cryptpad.getLSAttribute(SORT_FOLDER_DESC));
var sortedFiles = sortElements(false, path, keys, Cryptpad.getLSAttribute(SORT_FILE_BY), !Cryptpad.getLSAttribute(SORT_FILE_DESC));
var sortedFolders = sortElements(true, path, keys, null, !getSortFolderDesc());
var sortedFiles = sortElements(false, path, keys, Cryptpad.getLSAttribute(SORT_FILE_BY), !getSortFileDesc());
sortedFolders.forEach(function (key) {
if (filesOp.isFile(root[key])) { return; }
var $element = createElement(path, key, root, true);
@ -1211,6 +1229,15 @@ define([
$container.append($unsortedList);
};
var createTemplate = function ($container, path) {
var $icon = $templateIcon.clone();
var isOpened = filesOp.comparePath(path, currentPath);
var $element = createTreeElement(TEMPLATE_NAME, $icon, [TEMPLATE], false, true, false, isOpened);
$element.addClass('root');
var $list = $('<ul>', { id: 'templateTree' }).append($element);
$container.append($list);
};
var createAllFiles = function ($container, path) {
var $icon = $unsortedIcon.clone();
var isOpened = filesOp.comparePath(path, currentPath);
@ -1244,6 +1271,7 @@ define([
$tree.html('');
createTree($tree, [ROOT]);
createUnsorted($tree, [UNSORTED]);
createTemplate($tree, [TEMPLATE]);
createAllFiles($tree, [FILES_DATA]);
createTrash($tree, [TRASH]);
};
@ -1427,7 +1455,7 @@ define([
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]);
var name = filesOp.isInTrashRoot(path) ? path[1] : (filesOp.isPathInHrefArray(path) ? filesOp.getTitle(element) : path[path.length - 1]);
msg = Messages._getKey("fm_removePermanentlyDialog", [name]);
}
Cryptpad.confirm(msg, function(res) {
@ -1454,6 +1482,7 @@ define([
files.on('change', [], function (o, n, p) {
var path = arguments[2];
if ((filesOp.isPathInUnsorted(currentPath) && filesOp.isPathInUnsorted(path)) ||
(filesOp.isPathInTemplate(currentPath) && filesOp.isPathInTemplate(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
@ -1466,6 +1495,7 @@ define([
}).on('remove', [], function (o, p) {
var path = arguments[1];
if ((filesOp.isPathInUnsorted(currentPath) && filesOp.isPathInUnsorted(path)) ||
(filesOp.isPathInTemplate(currentPath) && filesOp.isPathInTemplate(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
@ -1478,8 +1508,6 @@ define([
refresh();
};
// don't initialize until the store is ready.
Cryptpad.ready(function () {
var storeObj = Cryptpad.getStore().getProxy && Cryptpad.getStore().getProxy().proxy ? Cryptpad.getStore().getProxy() : undefined;

Loading…
Cancel
Save