diff --git a/www/common/fileObject.js b/www/common/fileObject.js index f19bdc2d4..c52c8bbac 100644 --- a/www/common/fileObject.js +++ b/www/common/fileObject.js @@ -299,6 +299,67 @@ define([ checkDeletedFiles(); }; + // Permanently delete multiple files at once using a list of paths + // NOTE: We have to be careful when removing elements from arrays (trash root, unsorted or template) + var deleteHrefs = function (hrefs) { + hrefs.forEach(function (obj) { + var idx = files[obj.root].indexOf(obj.href); + files[obj.root].splice(idx, 1); + }); + }; + var deleteMultipleTrashRoot = function (roots) { + roots.forEach(function (obj) { + var idx = files[TRASH][obj.name].indexOf(obj.el); + files[TRASH][obj.name].splice(idx, 1); + }); + }; + var deleteMultiplePermanently = exp.deletePathsPermanently = function (paths) { + var hrefPaths = paths.filter(isPathInHrefArray); + var rootPaths = paths.filter(isPathInRoot); + var trashPaths = paths.filter(isPathInTrash); + + var hrefs = []; + hrefPaths.forEach(function (path) { + var href = exp.findElement(files, path); + hrefs.push({ + root: path[0], + href: href + }); + }); + deleteHrefs(hrefs); + + rootPaths.forEach(function (path) { + var parentPath = path.slice(); + var key = parentPath.pop(); + var parentEl = exp.findElement(files, parentPath); + parentEl[key] = undefined; + delete parentEl[key]; + }); + + var trashRoot = []; + trashPaths.forEach(function (path) { + var parentPath = path.slice(); + var key = parentPath.pop(); + var parentEl = exp.findElement(files, parentPath); + // Trash root: we have array here, we can't just splice with the path otherwise we might break the path + // of another element in the loop + console.log(path); + if (path.length === 4) { + trashRoot.push({ + name: path[1], + el: parentEl + }); + return; + } + // Trash but not root: it's just a tree so remove the key + parentEl[key] = undefined; + delete parentEl[key]; + }); + deleteMultipleTrashRoot(trashRoot); + + checkDeletedFiles(); + }; + // Find an element in a object following a path, resursively // NOTE: it is always used with an absolute path and root === files in our code var findElement = exp.findElement = function (root, pathInput) { diff --git a/www/drive/main.js b/www/drive/main.js index d9d7d6c3a..2027523fe 100644 --- a/www/drive/main.js +++ b/www/drive/main.js @@ -1641,10 +1641,8 @@ define([ }); // 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/unsorted/template - } + //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) { @@ -1653,9 +1651,7 @@ define([ Cryptpad.confirm(msg, function(res) { if (!res) { return; } - paths.forEach(function(p) { - cb(p); - }); + filesOp.deletePathsPermanently(paths); refresh(); }); return;