From b4864b96ac6974899f9b799fd91c60ee6d988b6e Mon Sep 17 00:00:00 2001 From: yflory Date: Tue, 22 Aug 2017 14:28:17 +0200 Subject: [PATCH] Add a recent pads category in the drive --- www/common/userObject.js | 9 +++++ www/drive/main.js | 71 ++++++++++++++++++++++++++++++++++++++-- 2 files changed, 77 insertions(+), 3 deletions(-) diff --git a/www/common/userObject.js b/www/common/userObject.js index 6b4a31bbe..431e9c9dc 100644 --- a/www/common/userObject.js +++ b/www/common/userObject.js @@ -413,6 +413,15 @@ define([ }); return ret; }; + exp.getRecentPads = function () { + var allFiles = files[FILES_DATA]; + var sorted = Object.keys(allFiles) + .sort(function (a,b) { + return allFiles[a].atime < allFiles[b].atime; + }) + .map(function (str) { return Number(str); }); + return sorted; + }; /** * OPERATIONS diff --git a/www/drive/main.js b/www/drive/main.js index 4ecdbc5c1..07ed2dd7c 100644 --- a/www/drive/main.js +++ b/www/drive/main.js @@ -51,6 +51,8 @@ define([ var TEMPLATE_NAME = Messages.fm_templateName; var TRASH = "trash"; var TRASH_NAME = Messages.fm_trashName; + var RECENT = "recent"; + var RECENT_NAME = "Recent pads"; var LOCALSTORAGE_LAST = "cryptpad-file-lastOpened"; var LOCALSTORAGE_OPENED = "cryptpad-file-openedFolders"; @@ -233,9 +235,10 @@ define([ // Categories dislayed in the menu // _WORKGROUP_ : do not display unsorted - var displayedCategories = [ROOT, TRASH, SEARCH]; + var displayedCategories = [ROOT, TRASH, SEARCH, RECENT]; if (AppConfig.enableTemplates) { displayedCategories.push(TEMPLATE); } if (isWorkgroup()) { displayedCategories = [ROOT, TRASH, SEARCH]; } + var virtualCategories = [SEARCH, RECENT]; if (!APP.loggedIn) { displayedCategories = [FILES_DATA]; @@ -1259,6 +1262,7 @@ define([ case TEMPLATE: pName = TEMPLATE_NAME; break; case FILES_DATA: pName = FILES_DATA_NAME; break; case SEARCH: pName = SEARCH_NAME; break; + case RECENT: pName = RECENT_NAME; break; default: pName = name; } return pName; @@ -1318,6 +1322,9 @@ define([ case FILES_DATA: msg = Messages.fm_info_allFiles; break; + case RECENT: + msg = Messages.fm_info_recent || 'TODO'; + break; default: msg = undefined; } @@ -1805,6 +1812,50 @@ define([ }); }; + var displayRecent = function ($list) { + var filesList = filesOp.getRecentPads(); + var limit = 20; + var i = 0; + filesList.forEach(function (id) { + if (i >= 20) { return; } + // Check path (pad exists and not in trash) + var paths = filesOp.findFile(id); + if (!paths.length) { return; } + var path = paths[0]; + if (filesOp.isPathIn(path, [TRASH])) { return; } + // Display the pad + var file = filesOp.getFileData(id); + if (!file) { + //debug("Unsorted or template returns an element not present in filesData: ", href); + file = { title: Messages.fm_noname }; + //return; + } + var $icon = getFileIcon(id); + var ro = filesOp.isReadOnlyFile(id); + // ro undefined mens it's an old hash which doesn't support read-only + var roClass = typeof(ro) === 'undefined' ? ' noreadonly' : ro ? ' readonly' : ''; + var $element = $('
  • ', { + 'class': 'file-element element element-row' + roClass, + }); + addFileData(id, $element); + $element.prepend($icon).dblclick(function () { + openFile(id); + }); + $element.data('path', path); + $element.click(function(e) { + e.stopPropagation(); + onElementClick(e, $element, path); + }); + $element.contextmenu(openDefaultContextMenu); + $element.data('context', $defaultContextMenu); + /*if (draggable) { + addDragAndDropHandlers($element, path, false, false); + }*/ + $list.append($element); + i++; + }); + }; + // Display the selected directory into the content part (rightside) // NOTE: Elements in the trash are not using the same storage structure as the others // _WORKGROUP_ : do not change the lastOpenedFolder value in localStorage @@ -1833,9 +1884,11 @@ define([ var isTemplate = filesOp.comparePath(path, [TEMPLATE]); var isAllFiles = filesOp.comparePath(path, [FILES_DATA]); var isSearch = path[0] === SEARCH; + var isRecent = path[0] === RECENT; + var isVirtual = virtualCategories.indexOf(path[0]) !== -1; - var root = isSearch ? undefined : filesOp.find(path); - if (!isSearch && typeof(root) === "undefined") { + var root = isVirtual ? undefined : filesOp.find(path); + if (!isVirtual && typeof(root) === "undefined") { log(Messages.fm_unknownFolderError); debug("Unable to locate the selected directory: ", path); var parentPath = path.slice(); @@ -1921,6 +1974,8 @@ define([ displayTrashRoot($list, $folderHeader, $fileHeader); } else if (isSearch) { displaySearch($list, path[1]); + } else if (isRecent) { + displayRecent($list); } else { $dirContent.contextmenu(openContentContextMenu); if (filesOp.hasSubfolder(root)) { $list.append($folderHeader); } @@ -2093,6 +2148,15 @@ define([ $container.append($trashList); }; + var createRecent = function ($container, path) { + var $icon = $templateIcon.clone(); //TODO + var isOpened = filesOp.comparePath(path, currentPath); + var $element = createTreeElement(RECENT_NAME, $icon, [RECENT], false, false, false, isOpened); + $element.addClass('root'); + var $list = $('