Add translations, ability to select multiple elements, clean the code
parent
f05efbb113
commit
d77970975c
@ -0,0 +1,453 @@
|
||||
define([
|
||||
'/customize/messages.js',
|
||||
'/common/cryptpad-common.js',
|
||||
'/bower_components/jquery/dist/jquery.min.js',
|
||||
], function (Messages, Cryptpad) {
|
||||
var $ = window.jQuery;
|
||||
var module = {};
|
||||
|
||||
var ROOT = "root";
|
||||
var UNSORTED = "unsorted";
|
||||
var FILES_DATA = "filesData";
|
||||
var TRASH = "trash";
|
||||
var NEW_FOLDER_NAME = Messages.fm_newFolder;
|
||||
|
||||
var init = module.init = function (files, config) {
|
||||
var DEBUG = config.DEBUG || false;
|
||||
var logging = console.log;
|
||||
var log = config.log || logging;
|
||||
var logError = config.logError || logging;
|
||||
var debug = config.debug || logging;
|
||||
|
||||
var exp = {};
|
||||
|
||||
var error = exp.error = function() {
|
||||
exp.fixFiles();
|
||||
console.error.apply(arguments);
|
||||
};
|
||||
|
||||
var comparePath = exp.comparePath = function (a, b) {
|
||||
if (!a || !b || !$.isArray(a) || !$.isArray(b)) { return false; }
|
||||
if (a.length !== b.length) { return false; }
|
||||
var result = true;
|
||||
var i = a.length - 1;
|
||||
while (result && i >= 0) {
|
||||
result = a[i] === b[i];
|
||||
i--;
|
||||
}
|
||||
return result;
|
||||
};
|
||||
|
||||
var deleteFromObject = function (path) {
|
||||
var parentPath = path.slice();
|
||||
var key = parentPath.pop();
|
||||
var parentEl = findElement(files, parentPath);
|
||||
if (path.length === 4 && path[0] === TRASH) {
|
||||
files[TRASH][path[1]].splice(path[2], 1);
|
||||
} else if (path[0] === UNSORTED) {
|
||||
parentEl.splice(key, 1);
|
||||
} else {
|
||||
delete parentEl[key];
|
||||
}
|
||||
};
|
||||
|
||||
var isPathInRoot = exp.isPathInRoot = function (path) {
|
||||
return path[0] && path[0] === ROOT;
|
||||
};
|
||||
var isPathInUnsorted = exp.isPathInUnsorted = function (path) {
|
||||
return path[0] && path[0] === UNSORTED;
|
||||
};
|
||||
var isPathInTrash = exp.isPathInTrash = function (path) {
|
||||
return path[0] && path[0] === TRASH;
|
||||
};
|
||||
|
||||
var isFile = exp.isFile = function (element) {
|
||||
return typeof(element) === "string";
|
||||
};
|
||||
|
||||
var isFolder = exp.isFolder = function (element) {
|
||||
return typeof(element) !== "string";
|
||||
};
|
||||
|
||||
var isFolderEmpty = exp.isFolderEmpty = function (element) {
|
||||
if (typeof(element) !== "object") { return false; }
|
||||
return Object.keys(element).length === 0;
|
||||
};
|
||||
|
||||
var hasSubfolder = exp.hasSubfolder = function (element, trashRoot) {
|
||||
if (typeof(element) !== "object") { return false; }
|
||||
var subfolder = 0;
|
||||
for (var f in element) {
|
||||
if (trashRoot) {
|
||||
if ($.isArray(element[f])) {
|
||||
element[f].forEach(function (el, idx) {
|
||||
subfolder += isFolder(el.element) ? 1 : 0;
|
||||
});
|
||||
}
|
||||
} else {
|
||||
subfolder += isFolder(element[f]) ? 1 : 0;
|
||||
}
|
||||
}
|
||||
return subfolder;
|
||||
};
|
||||
|
||||
var hasFile = exp.hasFile = function (element, trashRoot) {
|
||||
if (typeof(element) !== "object") { return false; }
|
||||
var file = 0;
|
||||
for (var f in element) {
|
||||
if (trashRoot) {
|
||||
if ($.isArray(element[f])) {
|
||||
element[f].forEach(function (el, idx) {
|
||||
file += isFile(el.element) ? 1 : 0;
|
||||
});
|
||||
}
|
||||
} else {
|
||||
file += isFile(element[f]) ? 1 : 0;
|
||||
}
|
||||
}
|
||||
return file;
|
||||
};
|
||||
|
||||
var isSubpath = exp.isSubpath = function (path, parentPath) {
|
||||
var pathA = parentPath.slice();
|
||||
var pathB = path.slice(0, pathA.length);
|
||||
return comparePath(pathA, pathB);
|
||||
};
|
||||
|
||||
var getAvailableName = function (parentEl, name) {
|
||||
if (typeof(parentEl[name]) === "undefined") { return name; }
|
||||
var newName = name;
|
||||
var i = 1;
|
||||
while (typeof(parentEl[newName]) !== "undefined") {
|
||||
newName = name + "_" + i;
|
||||
i++;
|
||||
}
|
||||
return newName;
|
||||
};
|
||||
|
||||
var compareFiles = function (fileA, fileB) {
|
||||
// Compare string, might change in the future
|
||||
return fileA === fileB;
|
||||
};
|
||||
|
||||
var isInTrashRoot = function (path) {
|
||||
return path[0] === TRASH && path.length === 4;
|
||||
};
|
||||
|
||||
var getTrashElementData = exp.getTrashElementData = function (trashPath) {
|
||||
if (!isInTrashRoot) {
|
||||
debug("Called getTrashElementData on a element not in trash root: ", trashpath);
|
||||
return;
|
||||
}
|
||||
var parentPath = trashPath.slice();
|
||||
parentPath.pop();
|
||||
return findElement(files, parentPath);
|
||||
};
|
||||
|
||||
var getUnsortedFiles = exp.getUnsortedFiles = function () {
|
||||
return files[UNSORTED];
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
// Data from filesData
|
||||
var getTitle = function (href) {
|
||||
if (!files[FILES_DATA][href]) {
|
||||
error("getTitle called with a non-existing href: ", href);
|
||||
return;
|
||||
}
|
||||
return files[FILES_DATA][href].title;
|
||||
};
|
||||
|
||||
|
||||
// Find an element in a object following a path, resursively
|
||||
var findElement = exp.findElement = function (root, pathInput) {
|
||||
if (!pathInput) {
|
||||
error("Invalid path:\n", pathInput, "\nin root\n", root);
|
||||
return;
|
||||
}
|
||||
if (pathInput.length === 0) { return root; }
|
||||
var path = pathInput.slice();
|
||||
var key = path.shift();
|
||||
if (typeof root[key] === "undefined") {
|
||||
debug("Unable to find the key '" + key + "' in the root object provided:\n", root);
|
||||
return;
|
||||
}
|
||||
return findElement(root[key], path);
|
||||
};
|
||||
|
||||
var moveElement = exp.moveElement = function (elementPath, newParentPath, cb) {
|
||||
if (comparePath(elementPath, newParentPath)) { return; } // Nothing to do...
|
||||
if (newParentPath[0] && newParentPath[0] === TRASH) {
|
||||
debug("Moving to trash is forbidden. You have to use the removeElement function");
|
||||
return;
|
||||
}
|
||||
var element = findElement(files, elementPath);
|
||||
|
||||
var newParent = findElement(files, newParentPath);
|
||||
|
||||
if (isPathInUnsorted(newParentPath)) {
|
||||
if (isFolder(element)) {
|
||||
log(Messages.fo_moveUnsortedError);
|
||||
return;
|
||||
} else {
|
||||
if (files[UNSORTED].indexOf(element) === -1) {
|
||||
files[UNSORTED].push(element);
|
||||
}
|
||||
deleteFromObject(elementPath);
|
||||
cb && cb();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
var name;
|
||||
|
||||
if (isPathInUnsorted(elementPath)) {
|
||||
name = getTitle(element);
|
||||
} else if (elementPath.length === 4 && elementPath[0] === TRASH) {
|
||||
// Element from the trash root: elementPath = [TRASH, "{dirName}", 0, 'element']
|
||||
name = elementPath[1];
|
||||
} else {
|
||||
name = elementPath[elementPath.length-1];
|
||||
}
|
||||
var newName = !isPathInRoot(elementPath) ? getAvailableName(newParent, name) : name;
|
||||
|
||||
if (typeof(newParent[newName]) !== "undefined") {
|
||||
log("A file with the same name already exist at the new location. Rename the file and try again.");
|
||||
return;
|
||||
}
|
||||
|
||||
newParent[newName] = element;
|
||||
deleteFromObject(elementPath);
|
||||
cb && cb();
|
||||
};
|
||||
|
||||
var createNewFolder = exp.createNewFolder = function (folderPath, name, cb) {
|
||||
var parentEl = findElement(files, folderPath);
|
||||
var folderName = getAvailableName(parentEl, name || NEW_FOLDER_NAME);
|
||||
parentEl[folderName] = {};
|
||||
var newPath = folderPath.slice();
|
||||
newPath.push(folderName);
|
||||
cb({
|
||||
newPath: newPath
|
||||
});
|
||||
};
|
||||
|
||||
var pushToTrash = function (name, element, path) {
|
||||
var trash = findElement(files, [TRASH]);
|
||||
|
||||
if (typeof(trash[name]) === "undefined") {
|
||||
trash[name] = [];
|
||||
}
|
||||
var trashArray = trash[name];
|
||||
var trashElement = {
|
||||
element: element,
|
||||
path: path
|
||||
};
|
||||
trashArray.push(trashElement);
|
||||
};
|
||||
// Move to trash
|
||||
var removeElement = exp.removeElement = function (path, cb, force) {
|
||||
if (!path || path.length < 2 || path[0] === TRASH) {
|
||||
debug("Calling removeElement from a wrong path: ", path);
|
||||
return;
|
||||
}
|
||||
var element = findElement(files, path);
|
||||
var key = path[path.length - 1];
|
||||
var name = isPathInUnsorted(path) ? getTitle(element) : key;
|
||||
var andThen = function () {
|
||||
var parentPath = path.slice();
|
||||
parentPath.pop();
|
||||
pushToTrash(name, element, parentPath);
|
||||
deleteFromObject(path);
|
||||
cb && cb();
|
||||
};
|
||||
if (force) {
|
||||
andThen();
|
||||
return;
|
||||
}
|
||||
Cryptpad.confirm("Are you sure you want to move " + name + " to the trash?", function(res) {
|
||||
if (!res) { return; }
|
||||
andThen();
|
||||
});
|
||||
};
|
||||
|
||||
var removeFromTrashArray = function (element, name) {
|
||||
var array = files[TRASH][name];
|
||||
if (!array || !$.isArray(array)) { return; }
|
||||
// Remove the element from the trash array
|
||||
var index = array.indexOf(element);
|
||||
if (index > -1) {
|
||||
array.splice(index, 1);
|
||||
}
|
||||
// Remove the array is empty to have a cleaner object in chainpad
|
||||
if (array.length === 0) {
|
||||
delete files[TRASH][name];
|
||||
}
|
||||
};
|
||||
|
||||
var restoreTrash = exp.restoreTrash = function (path, cb) {
|
||||
if (!path || path.length !== 4 || path[0] !== TRASH) {
|
||||
debug("restoreTrash was called from an element not in the trash root: ", path);
|
||||
return;
|
||||
}
|
||||
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);
|
||||
removeFromTrashArray(parentEl, path[1]);
|
||||
cb();
|
||||
}
|
||||
return;
|
||||
}
|
||||
// Find the new parent element
|
||||
var newParentEl = findElement(files, newPath);
|
||||
var name = getAvailableName(newParentEl, path[1]);
|
||||
// Move the element
|
||||
newParentEl[name] = element;
|
||||
removeFromTrashArray(parentEl, path[1]);
|
||||
cb();
|
||||
};
|
||||
|
||||
// Delete permanently
|
||||
var removeFromTrash = exp.removeFromTrash = function (path, cb) {
|
||||
if (!path || path.length < 4 || path[0] !== TRASH) { return; }
|
||||
// Remove the last element from the path to get the parent path and the element name
|
||||
var parentPath = path.slice();
|
||||
var name;
|
||||
if (path.length === 4) { // Trash root
|
||||
name = path[1];
|
||||
parentPath.pop();
|
||||
var parentElement = findElement(files, parentPath);
|
||||
removeFromTrashArray(parentElement, name);
|
||||
cb();
|
||||
return;
|
||||
}
|
||||
name = parentPath.pop();
|
||||
var parentEl = findElement(files, parentPath);
|
||||
if (typeof(parentEl[name]) === "undefined") {
|
||||
logError("Unable to locate the element to remove from trash: ", path);
|
||||
return;
|
||||
}
|
||||
delete parentEl[name];
|
||||
cb();
|
||||
};
|
||||
|
||||
var emptyTrash = exp.emptyTrash = function (cb) {
|
||||
files[TRASH] = {};
|
||||
cb();
|
||||
};
|
||||
|
||||
|
||||
var renameElement = exp.renameElement = function (path, newName, cb) {
|
||||
if (path.length <= 1) {
|
||||
logError('Renaming `root` is forbidden');
|
||||
return;
|
||||
}
|
||||
if (!newName || newName.trim() === "") { return; }
|
||||
// Copy the element path and remove the last value to have the parent path and the old name
|
||||
var element = findElement(files, path);
|
||||
var parentPath = path.slice();
|
||||
var oldName = parentPath.pop();
|
||||
if (oldName === newName) {
|
||||
return;
|
||||
}
|
||||
var parentEl = findElement(files, parentPath);
|
||||
if (typeof(parentEl[newName]) !== "undefined") {
|
||||
log(Messages.fo_existingNameError);
|
||||
return;
|
||||
}
|
||||
parentEl[newName] = element;
|
||||
delete parentEl[oldName];
|
||||
cb();
|
||||
};
|
||||
|
||||
var fixFiles = exp.fixFiles = function () {
|
||||
// Explore the tree and check that everything is correct:
|
||||
// * 'root', 'trash' and 'filesData' exist and are objects
|
||||
// * Folders are objects
|
||||
// * Files are href
|
||||
// * Trash root contains only arrays, each element of the array is an object {element:.., path:..}
|
||||
// * Data (title, cdate, adte) are stored in filesData. filesData contains only href keys linking to object with title, cdate, adate.
|
||||
// * Dates (adate, cdate) can be parsed/formatted
|
||||
debug("Cleaning file system...");
|
||||
|
||||
// Create a backup
|
||||
if (typeof(localStorage.oldFileSystem) === "undefined") {
|
||||
localStorage.oldFileSystem = '[]';
|
||||
}
|
||||
var before = JSON.stringify(files);
|
||||
|
||||
if (typeof(files[ROOT]) !== "object") { debug("ROOT was not an object"); files[ROOT] = {}; }
|
||||
if (typeof(files[TRASH]) !== "object") { debug("TRASH was not an object"); files[TRASH] = {}; }
|
||||
if (typeof(files[FILES_DATA]) !== "object") { debug("FILES_DATA was not an object"); files[FILES_DATA] = {}; }
|
||||
if (!$.isArray(files[UNSORTED])) { debug("UNSORTED was not an array"); files[UNSORTED] = []; }
|
||||
var fixRoot = function (element) {
|
||||
for (var el in element) {
|
||||
if (!isFile(element[el]) && !isFolder(element[el])) {
|
||||
debug("An element in ROOT was not a folder nor a file. ", element[el]);
|
||||
delete element[el];
|
||||
} else if (isFolder(element[el])) {
|
||||
fixRoot(element[el]);
|
||||
}
|
||||
}
|
||||
};
|
||||
fixRoot(files[ROOT]);
|
||||
var fixTrashRoot = function (tr) {
|
||||
var toClean;
|
||||
for (var el in tr) {
|
||||
if (!$.isArray(tr[el])) {
|
||||
debug("An element in TRASH root is not an array. ", tr[el]);
|
||||
delete tr[el];
|
||||
} else {
|
||||
toClean = [];
|
||||
tr[el].forEach(function (obj, idx) {
|
||||
if (typeof(obj) !== "object") { toClean.push(idx); return; }
|
||||
if (!isFile(obj.element) && !isFolder(obj.element)) { toClean.push(idx); return; }
|
||||
if (!$.isArray(obj.path)) { toClean.push(idx); return; }
|
||||
});
|
||||
for (var i = toClean.length-1; i>=0; i--) {
|
||||
tr[el].splice(toClean[i], 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
fixTrashRoot(files[TRASH]);
|
||||
var fixFilesData = function (fd) {
|
||||
for (var el in fd) {
|
||||
if (typeof(fd[el]) !== "object") {
|
||||
debug("An element in filesData was not an object. ", fd[el]);
|
||||
delete fd[el];
|
||||
}
|
||||
};
|
||||
};
|
||||
fixFilesData(files[FILES_DATA]);
|
||||
var fixUnsorted = function (us) {
|
||||
var toClean = [];
|
||||
us.forEach(function (el, idx) {
|
||||
if (!isFile(el)) {
|
||||
toClean.push(idx);
|
||||
}
|
||||
});
|
||||
};
|
||||
fixUnsorted(files[UNSORTED]);
|
||||
|
||||
if (JSON.stringify(files) !== before) {
|
||||
var backup = JSON.parse(localStorage.oldFileSystem);
|
||||
backup.push(files);
|
||||
localStorage.oldFileSystem = JSON.stringify(backup);
|
||||
debug("Your file system was corrupted. It has been cleaned so that the file manager application can be used");
|
||||
return;
|
||||
}
|
||||
debug("File system was clean");
|
||||
};
|
||||
fixFiles();
|
||||
|
||||
return exp;
|
||||
};
|
||||
|
||||
return module;
|
||||
});
|
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue