Refactor fileObject

pull/1/head
yflory 8 years ago
parent 5212539a16
commit c1e0316d43

@ -506,8 +506,9 @@ load pinpad dynamically only after you know that it will be needed */
}); });
return templates; return templates;
}; };
var addTemplate = common.addTemplate = function (href) { var addTemplate = common.addTemplate = function (data) {
getStore().addTemplate(href); getStore().pushData(data);
getStore().addPad(data.href, ['template']);
}; };
var isTemplate = common.isTemplate = function (href) { var isTemplate = common.isTemplate = function (href) {

@ -90,20 +90,22 @@ define([
ret.pushData = filesOp.pushData; ret.pushData = filesOp.pushData;
ret.addPad = function (href, path, name) { ret.addPad = function (href, path, name) {
filesOp.addPad(href, path, name); filesOp.add(href, path, name);
}; };
ret.forgetPad = function (href, cb) { ret.forgetPad = function (href, cb) {
filesOp.forgetPad(href); filesOp.forget(href);
cb(); cb();
}; };
ret.addTemplate = function (href) {
filesOp.addTemplate(href);
};
ret.listTemplates = function () { ret.listTemplates = function () {
return filesOp.listTemplates(); var templateFiles = filesOp.getFiles(['template']);
var res = [];
templateFiles.forEach(function (f) {
var data = filesOp.getFileData(f);
res.push(JSON.parse(JSON.stringify(data)));
});
return res;
}; };
ret.getProxy = function () { ret.getProxy = function () {

@ -0,0 +1,839 @@
define([
'/bower_components/jquery/dist/jquery.min.js',
], function () {
var $ = window.jQuery;
var module = {};
var ROOT = module.ROOT = "root";
var UNSORTED = module.UNSORTED = "unsorted";
var TRASH = module.TRASH = "trash";
var TEMPLATE = module.TEMPLATE = "template";
var init = module.init = function (files, config) {
var Cryptpad = config.Cryptpad;
var Messages = Cryptpad.Messages;
var FILES_DATA = Cryptpad.storageKey;
var NEW_FOLDER_NAME = Messages.fm_newFolder;
var NEW_FILE_NAME = Messages.fm_newFile;
// Logging
var DEBUG = config.DEBUG || false;
var logging = function () {
console.log.apply(console, arguments);
};
var log = config.log || logging;
var logError = config.logError || logging;
var debug = config.debug || logging;
var error = exp.error = function() {
exp.fixFiles();
console.error.apply(console, arguments);
};
// TODO: workgroup
var workgroup = config.workgroup;
var exp = {};
/*
* UTILS
*/
var getStructure = exp.getStructure = function () {
var a = {};
a[ROOT] = {};
a[UNSORTED] = [];
a[TRASH] = {};
a[FILES_DATA] = [];
a[TEMPLATE] = [];
return a;
};
var compareFiles = function (fileA, fileB) { return fileA === fileB; };
var isFile = exp.isFile = function (element) {
return typeof(element) === "string";
};
var isReadOnlyFile = exp.isReadOnlyFile = function (element) {
if (!isFile(element)) { return false; }
var parsed = Cryptpad.parsePadUrl(element);
if (!parsed) { return false; }
var hash = parsed.hash;
var pHash = Cryptpad.parseHash(hash);
if (pHash && !pHash.mode) { return; }
return pHash && pHash.mode === 'view';
};
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;
var addSubfolder = function (el, idx) {
subfolder += isFolder(el.element) ? 1 : 0;
};
for (var f in element) {
if (trashRoot) {
if ($.isArray(element[f])) {
element[f].forEach(addSubfolder);
}
} 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;
var addFile = function (el, idx) {
file += isFile(el.element) ? 1 : 0;
};
for (var f in element) {
if (trashRoot) {
if ($.isArray(element[f])) {
element[f].forEach(addFile);
}
} else {
file += isFile(element[f]) ? 1 : 0;
}
}
return file;
};
// Get data from AllFiles (Cryptpad_RECENTPADS)
var getFileData = exp.getFileData = function (file) {
if (!file) { return; }
var res;
files[FILES_DATA].some(function(arr) {
var href = arr.href;
if (href === file) {
res = arr;
return true;
}
return false;
});
return res;
};
// Data from filesData
var getTitle = exp.getTitle = function (href) {
if (workgroup) { debug("No titles in workgroups"); return; }
var data = getFileData(href);
if (!href || !data) {
error("getTitle called with a non-existing href: ", href);
return;
}
return data.title;
};
// PATHS
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 isSubpath = exp.isSubpath = function (path, parentPaths) {
var pathA = parentPath.slice();
var pathB = path.slice(0, pathA.length);
return comparePath(pathA, pathB);
};
var isPathIn = function (path, categories) {
return categories.some(function (c) {
return Array.isArray(path) && path[0] === c;
});
};
var isInTrashRoot = exp.isInTrashRoot = function (path) {
return path[0] === TRASH && path.length === 4;
};
// FIND
var compareFiles = function (fileA, fileB) { return fileA === fileB; };
var 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:", root);
return;
}
return findElement(root[key], path);
};
var find = exp.find = function (path) {
return findElement(files, path);
};
// GET FILES
var getFilesRecursively = function (root, arr) {
for (var e in root) {
if (isFile(root[e])) {
if(arr.indexOf(root[e]) === -1) { arr.push(root[e]); }
} else {
getFilesRecursively(root[e], arr);
}
}
};
var _getFiles = {};
_getFiles['array'] = function (cat) {
if (!files[cat]) {
files[cat] = [];
}
return files[cat].slice();
};
_getFiles[UNSORTED] = function () {
return _getFiles['array'](UNSORTED);
};
_getFiles[TEMPLATE] = function () {
return _getFiles['array'](TEMPLATE);
};
_getFiles['hrefArray'] = function () {
var ret = [];
ret = ret.concat(_getFiles[UNSORTED]);
ret = ret.concat(_getFiles[TEMPLATE]);
return Cryptpad.deduplicateString(ret);
};
_getFiles[ROOT] = function () {
var ret = [];
getFilesRecursively(files[ROOT], ret);
return ret;
};
_getFiles[TRASH] = function () {
var root = files[TRASH];
var ret = [];
var addFiles = function (el, idx) {
if (isFile(el.element)) {
if(ret.indexOf(el.element) === -1) { ret.push(el.element); }
} else {
getFilesRecursively(el.element, ret);
}
};
for (var e in root) {
if (!$.isArray(root[e])) {
error("Trash contains a non-array element");
return;
}
root[e].forEach(addFiles);
}
return ret;
};
_getFiles[FILES_DATA] = function () {
var ret = [];
files[FILES_DATA].forEach(function (el) {
if (el.href && ret.indexOf(el.href) === -1) {
ret.push(el.href);
}
});
return ret;
};
var getFiles = function (categories) {
var ret = [];
categories.forEach(function (c) {
if (typeof _getFiles[c] === "function") {
ret = ret.concat(_getFiles[c]);
}
});
return Cryptpad.deduplicateString(ret);
};
// SEARCH
var _findFileInRoot = function (path, href) {
if (!isPathIn([ROOT, TRASH])) { return []; }
var paths = [];
var root = find(path);
var addPaths = function (p) {
if (paths.indexOf(p) === -1) {
paths.push(p);
}
};
if (isFile(root)) {
if (compareFiles(href, root)) {
if (paths.indexOf(path) === -1) {
paths.push(path);
}
}
return paths;
}
for (var e in root) {
var nPath = path.slice();
nPath.push(e);
_findFileInRoot(nPath, href).forEach(addPaths);
}
return paths;
};
var _findFileInHrefArray = function (rootName, href) {
var unsorted = files[rootName].slice();
var ret = [];
var i = -1;
while ((i = unsorted.indexOf(href, i+1)) !== -1){
ret.push([rootName, i]);
}
return ret;
};
var _findFileInTrash = function (path, href) {
var root = find(path);
var paths = [];
var addPaths = function (p) {
if (paths.indexOf(p) === -1) {
paths.push(p);
}
};
if (path.length === 1 && typeof(root) === 'object') {
Object.keys(root).forEach(function (key) {
var arr = root[key];
if (!Array.isArray(arr)) { return; }
var nPath = path.slice();
nPath.push(key);
_findFileInTrash(nPath, href).forEach(addPaths);
});
}
if (path.length === 2) {
if (!Array.isArray(root)) { return []; }
root.forEach(function (el, i) {
var nPath = path.slice();
nPath.push(i);
nPath.push('element');
if (isFile(el.element)) {
if (compareFiles(href, el.element)) {
addPaths(nPath);
}
return;
}
_findFileInTrash(nPath, href).forEach(addPaths);
});
}
if (path.length >= 4) {
_findFileInRoot(path, href).forEach(addPaths);
}
return paths;
};
var findFile = function (href) {
var rootpaths = _findFileInRoot([ROOT], href);
var unsortedpaths = _findFileInHrefArray(UNSORTED, href);
var templatepaths = _findFileInHrefArray(TEMPLATE, href);
var trashpaths = _findFileInTrash([TRASH], href);
return rootpaths.concat(unsortedpaths, templatepaths, trashpaths);
};
var search = exp.search = function (value) {
if (typeof(value) !== "string") { return []; }
var res = [];
// Search in ROOT
var findIn = function (root) {
Object.keys(root).forEach(function (k) {
if (isFile(root[k])) {
if (k.toLowerCase().indexOf(value.toLowerCase()) !== -1) {
res.push(root[k]);
}
return;
}
findIn(root[k]);
});
};
findIn(files[ROOT]);
// Search in TRASH
var trash = files[TRASH];
Object.keys(trash).forEach(function (k) {
if (k.toLowerCase().indexOf(value.toLowerCase()) !== -1) {
trash[k].forEach(function (el) {
if (isFile(el.element)) {
res.push(el.element);
}
});
}
trash[k].forEach(function (el) {
if (isFolder(el.element)) {
findIn(el.element);
}
});
});
// Search title
var allFilesList = files[FILES_DATA].slice();
allFilesList.forEach(function (t) {
if (t.title && t.title.toLowerCase().indexOf(value.toLowerCase()) !== -1) {
res.push(t.href);
}
});
// Search Href
var href = Cryptpad.getRelativeHref(value);
if (href) {
res.push(href);
}
res = Cryptpad.deduplicateString(res);
var ret = [];
res.forEach(function (l) {
var paths = findFile(l);
ret.push({
paths: findFile(l),
data: exp.getFileData(l)
});
});
return ret;
};
/**
* OPERATIONS
*/
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;
};
// FILES DATA
var pushFileData = exp.pushData = function (data) {
Cryptpad.pinPads([Cryptpad.hrefToHexChannelId(data.href)], function (e, hash) {
console.log(hash);
});
files[FILES_DATA].push(data);
};
var spliceFileData = exp.removeData = function (idx) {
var data = files[FILES_DATA][idx];
if (typeof data === "object") {
Cryptpad.unpinPads([Cryptpad.hrefToHexChannelId(data.href)], function (e, hash) {
console.log(hash);
});
}
files[FILES_DATA].splice(idx, 1);
};
// MOVE
var pushToTrash = function (name, element, path) {
var trash = files[TRASH];
if (typeof(trash[name]) === "undefined") { trash[name] = []; }
var trashArray = trash[name];
var trashElement = {
element: element,
path: path
};
trashArray.push(trashElement);
};
var copyElement = function (elementPath, newParentPath) {
if (comparePath(elementPath, newParentPath)) { return; } // Nothing to do...
var element = find(elementPath);
var newParent = find(newParentPath);
// Never move a folder in one of its children
if (isSubpath(newParentPath, elementPath)) {
log(Messages.fo_moveFolderToChildError);
return;
}
// Move to Trash
if (isPathIn(newParentPath, [TRASH])) {
if (!elementPath || elementPath.length < 2 || elementPath[0] === TRASH) {
debug("Can't move an element from the trash to the trash: ", path);
return;
}
var key = elementPath[elementPath.length - 1];
var name = isPathIn(elementPath, ['hrefArray']) ? getTitle(element) : key;
var parentPath = elementPath.slice();
parentPath.pop();
pushToTrash(name, element, parentPath);
return true;
}
// Move to hrefArray
if (isPathIn(newParentPath, ['hrefArray'])) {
if (isFolder(element)) {
log(Messages.fo_moveUnsortedError);
return;
} else {
if (elementPath[0] === newParentPath[0]) { return; }
var fileRoot = newParentPath[0];
if (files[fileRoot].indexOf(element) === -1) {
files[fileRoot].push(element);
}
return true;
}
}
// Move to root
var name;
if (isPathIn(elementPath, ['hrefArray'])) {
name = getTitle(element);
} else if (isInTrashRoot(elementPath)) {
// 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(Messages.fo_unavailableName);
return;
}
newParent[newName] = element;
return true;
};
var move = exp.move = function (paths, newPath, cb) {
// Copy the elements to their new location
var toRemove = [];
paths.forEach(function (p) {
var parentPath = p.slice();
parentPath.pop();
if (comparePath(parentPath, newPath)) { return; }
copyElement(p, newPath);
toRemove.push(p);
});
exp.delete(toRemove, cb);
};
// ADD
var add = exp.add = function (href, path, name, cb) {
if (!href) { return; }
var newPath = path, parentEl;
if (path && !Array.isArray(path)) {
newPath = decodeURIComponent(path).split(',');
}
// Add to href array
if (path && isPathIn(newPath, ['hrefArray'])) {
parentEl = find(newPath);
parentEl.push(href);
return;
}
// Add to root
if (path && isPathIn(newPath, [ROOT]) && name) {
parentEl = find(newPath);
if (parentEl) {
var newName = getAvailableName(parentEl, name);
parentEl[newName] = href;
return;
}
}
// No path: push to unsorted
var filesList = getFiles([ROOT, TRASH, 'hrefArray']);
if (filesList.indexOf(href) === -1) { files[UNSORTED].push(href); }
if (typeof cb === "function") { cb(); }
};
var addFile = exp.addFile = function (filePath, name, type, cb) {
var parentEl = findElement(files, filePath);
var fileName = getAvailableName(parentEl, name || NEW_FILE_NAME);
var href = '/' + type + '/#' + Cryptpad.createRandomHash();
parentEl[fileName] = href;
pushFileData({
href: href,
title: fileName,
atime: +new Date(),
ctime: +new Date()
});
var newPath = filePath.slice();
newPath.push(fileName);
cb({
newPath: newPath
});
};
var addFolder = exp.addFolder = function (folderPath, name, cb) {
var parentEl = find(folderPath);
var folderName = getAvailableName(parentEl, name || NEW_FOLDER_NAME);
parentEl[folderName] = {};
var newPath = folderPath.slice();
newPath.push(folderName);
cb({
newPath: newPath
});
};
// FORGET (move with href not path)
var forget = exp.forget = function (href) {
var paths = findFile(href);
move(paths, [TRASH]);
};
// DELETE
// 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 removePadAttribute = function (f) {
Object.keys(files).forEach(function (key) {
var hash = f.indexOf('#') !== -1 ? f.slice(f.indexOf('#') + 1) : null;
if (hash && key.indexOf(hash) === 0) {
debug("Deleting pad attribute in the realtime object");
files[key] = undefined;
delete files[key];
}
});
};
var checkDeletedFiles = function () {
// Nothing in FILES_DATA for workgroups
if (workgroup) { return; }
var filesList = getFiles[ROOT, 'hrefArray', TRASH];
var toRemove = [];
files[FILES_DATA].forEach(function (arr) {
var f = arr.href;
if (filesList.indexOf(f) === -1) {
toRemove.push(arr);
}
});
toRemove.forEach(function (f) {
var idx = files[FILES_DATA].indexOf(f);
if (idx !== -1) {
debug("Removing", f, "from filesData");
spliceFileData(idx);
removePadAttribute(f.href);
}
});
};
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 = 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 = find(path);
hrefs.push({
root: path[0],
href: href
});
});
deleteHrefs(hrefs);
rootPaths.forEach(function (path) {
var parentPath = path.slice();
var key = parentPath.pop();
var parentEl = find(parentPath);
parentEl[key] = undefined;
delete parentEl[key];
});
var trashRoot = [];
trashPaths.forEach(function (path) {
var parentPath = path.slice();
var key = parentPath.pop();
var parentEl = find(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
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();
};
var deletePath = exp.delete = function (paths, cb) {
deletePathsPermanently(paths);
if (typeof cb === "function") { cb(); }
};
var emptyTrash = exp.emptyTrash = function (cb) {
files[TRASH] = {};
checkDeletedFiles();
if(cb) { cb(); }
};
// RENAME
var rename = exp.rename = 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 = find(path);
var parentPath = path.slice();
var oldName = parentPath.pop();
if (oldName === newName) {
return;
}
var parentEl = find(parentPath);
if (typeof(parentEl[newName]) !== "undefined") {
log(Messages.fo_existingNameError);
return;
}
parentEl[newName] = element;
parentEl[oldName] = undefined;
delete parentEl[oldName];
cb();
};
/**
* INTEGRITY CHECK
*/
var fixFiles = exp.fixFiles = function () {
// Explore the tree and check that everything is correct:
// * 'root', 'trash', 'unsorted' and 'filesData' exist and are objects
// * ROOT: Folders are objects, files are href
// * TRASH: Trash root contains only arrays, each element of the array is an object {element:.., path:..}
// * FILES_DATA: - 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
// - All files in filesData should be either in 'root', 'trash' or 'unsorted'. If that's not the case, copy the fily to 'unsorted'
// * UNSORTED: Contains only files (href), and does not contains files that are in ROOT
debug("Cleaning file system...");
var before = JSON.stringify(files);
var fixRoot = function (elem) {
if (typeof(files[ROOT]) !== "object") { debug("ROOT was not an object"); files[ROOT] = {}; }
var element = elem || files[ROOT];
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]);
element[el] = undefined;
delete element[el];
} else if (isFolder(element[el])) {
fixRoot(element[el]);
}
}
};
var fixTrashRoot = function () {
if (typeof(files[TRASH]) !== "object") { debug("TRASH was not an object"); files[TRASH] = {}; }
var tr = files[TRASH];
var toClean;
var addToClean = 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 el in tr) {
if (!$.isArray(tr[el])) {
debug("An element in TRASH root is not an array. ", tr[el]);
tr[el] = undefined;
delete tr[el];
} else {
toClean = [];
tr[el].forEach(addToClean);
for (var i = toClean.length-1; i>=0; i--) {
tr[el].splice(toClean[i], 1);
}
}
}
};
var fixUnsorted = function () {
if (!Array.isArray(files[UNSORTED])) { debug("UNSORTED was not an array"); files[UNSORTED] = []; }
files[UNSORTED] = Cryptpad.deduplicateString(files[UNSORTED].slice());
var us = files[UNSORTED];
var rootFiles = getFiles([ROOT, TEMPLATE]).slice();
var toClean = [];
us.forEach(function (el, idx) {
if (!isFile(el) || rootFiles.indexOf(el) !== -1) {
toClean.push(idx);
}
});
toClean.forEach(function (idx) {
us.splice(idx, 1);
});
};
var fixTemplate = function () {
if (!Array.isArray(files[TEMPLATE])) { debug("TEMPLATE was not an array"); files[TEMPLATE] = []; }
files[TEMPLATE] = Cryptpad.deduplicateString(files[TEMPLATE].slice());
var us = files[TEMPLATE];
var rootFiles = getFiles([ROOT, UNSORTED]).slice();
var toClean = [];
us.forEach(function (el, idx) {
if (!isFile(el) || rootFiles.indexOf(el) !== -1) {
toClean.push(idx);
}
});
toClean.forEach(function (idx) {
us.splice(idx, 1);
});
};
var fixFilesData = function () {
if (!$.isArray(files[FILES_DATA])) { debug("FILES_DATA was not an array"); files[FILES_DATA] = []; }
var fd = files[FILES_DATA];
var rootFiles = getFiles([ROOT, TRASH, 'hrefArray']);
var toClean = [];
fd.forEach(function (el, idx) {
if (!el || typeof(el) !== "object") {
debug("An element in filesData was not an object.", el);
toClean.push(el);
return;
}
if (rootFiles.indexOf(el.href) === -1) {
debug("An element in filesData was not in ROOT, UNSORTED or TRASH.", el);
files[UNSORTED].push(el.href);
return;
}
});
toClean.forEach(function (el) {
var idx = fd.indexOf(el);
if (idx !== -1) {
spliceFileData(idx);
}
});
};
fixRoot();
fixTrashRoot();
if (!workgroup) {
fixUnsorted();
fixTemplate();
fixFilesData();
}
if (JSON.stringify(files) !== before) {
debug("Your file system was corrupted. It has been cleaned so that the pads you visit can be stored safely");
return;
}
debug("File system was clean");
};
return exp;
};
return module;
});

@ -661,7 +661,7 @@ define([
} }
// Unsorted or template // Unsorted or template
if (filesOp.isPathInUnsorted(path) || filesOp.isPathInTemplate(path)) { if (filesOp.isPathInUnsorted(path) || filesOp.isPathInTemplate(path)) {
var file = filesOp.findElement(files, path); var file = filesOp.find(path);
if (filesOp.isFile(file) && filesOp.getTitle(file)) { if (filesOp.isFile(file) && filesOp.getTitle(file)) {
return filesOp.getTitle(file); return filesOp.getTitle(file);
} }
@ -688,7 +688,7 @@ define([
var msg = Messages._getKey('fm_removeSeveralDialog', [paths.length]); var msg = Messages._getKey('fm_removeSeveralDialog', [paths.length]);
if (paths.length === 1) { if (paths.length === 1) {
var path = paths[0]; var path = paths[0];
var name = path[0] === UNSORTED ? filesOp.getTitle(filesOp.findElement(files, path)) : path[path.length - 1]; var name = path[0] === UNSORTED ? filesOp.getTitle(filesOp.find(path)) : path[path.length - 1];
msg = Messages._getKey('fm_removeDialog', [name]); msg = Messages._getKey('fm_removeDialog', [name]);
} }
Cryptpad.confirm(msg, function (res) { Cryptpad.confirm(msg, function (res) {
@ -707,7 +707,7 @@ define([
$selected.each(function (idx, elmt) { $selected.each(function (idx, elmt) {
var ePath = $(elmt).data('path'); var ePath = $(elmt).data('path');
if (ePath) { if (ePath) {
var val = filesOp.findElement(files, ePath); var val = filesOp.find(ePath);
if (!val) { return; } // Error? A ".selected" element in not in the object if (!val) { return; } // Error? A ".selected" element in not in the object
paths.push({ paths.push({
path: ePath, path: ePath,
@ -721,7 +721,7 @@ define([
} else { } else {
removeSelected(); removeSelected();
$element.addClass('selected'); $element.addClass('selected');
var val = filesOp.findElement(files, path); var val = filesOp.find(path);
if (!val) { return; } // The element in not in the object if (!val) { return; } // The element in not in the object
paths = [{ paths = [{
path: path, path: path,
@ -749,7 +749,7 @@ define([
var movedPaths = []; var movedPaths = [];
var importedElements = []; var importedElements = [];
oldPaths.forEach(function (p) { oldPaths.forEach(function (p) {
var el = filesOp.findElement(files, p.path); var el = filesOp.find(p.path);
if (el && (stringify(el) === stringify(p.value.el) || !p.value || !p.value.el)) { if (el && (stringify(el) === stringify(p.value.el) || !p.value || !p.value.el)) {
movedPaths.push(p.path); movedPaths.push(p.path);
} else { } else {
@ -882,7 +882,7 @@ define([
newPath.push(key); newPath.push(key);
} }
var element = filesOp.findElement(files, newPath); var element = filesOp.find(newPath);
var $icon = !isFolder ? getFileIcon(element) : undefined; var $icon = !isFolder ? getFileIcon(element) : undefined;
var ro = filesOp.isReadOnlyFile(element); var ro = filesOp.isReadOnlyFile(element);
// ro undefined mens it's an old hash which doesn't support read-only // ro undefined mens it's an old hash which doesn't support read-only
@ -1119,17 +1119,17 @@ define([
refresh(); refresh();
}; };
$block.find('a.newFolder').click(function () { $block.find('a.newFolder').click(function () {
filesOp.createNewFolder(currentPath, null, onCreated); filesOp.addFolder(currentPath, null, onCreated); // TODO START HERE
}); });
$block.find('a.newdoc').click(function (e) { $block.find('a.newdoc').click(function (e) {
var type = $(this).attr('data-type') || 'pad'; var type = $(this).attr('data-type') || 'pad';
var name = Cryptpad.getDefaultName({type: type}); var name = Cryptpad.getDefaultName({type: type});
filesOp.createNewFile(currentPath, name, type, onCreated); filesOp.addFile(currentPath, name, type, onCreated);
}); });
} else { } else {
$block.find('a.newdoc').click(function (e) { $block.find('a.newdoc').click(function (e) {
var type = $(this).attr('data-type') || 'pad'; var type = $(this).attr('data-type') || 'pad';
sessionStorage[Cryptpad.newPadPathKey] = filesOp.isPathInTrash(currentPath) ? '' : currentPath; sessionStorage[Cryptpad.newPadPathKey] = filesOp.isPathIn(currentPath, [TRASH]) ? '' : currentPath;
window.open('/' + type + '/'); window.open('/' + type + '/');
}); });
} }
@ -1251,11 +1251,11 @@ define([
}; };
var allFilesSorted = function () { var allFilesSorted = function () {
return filesOp.getUnsortedFiles().length === 0; return filesOp.getFiles([UNSORTED]).length === 0;
}; };
var sortElements = function (folder, path, oldkeys, prop, asc, useHref, useData) { var sortElements = function (folder, path, oldkeys, prop, asc, useHref, useData) {
var root = filesOp.findElement(files, path); var root = filesOp.find(path);
var test = folder ? filesOp.isFolder : filesOp.isFile; var test = folder ? filesOp.isFolder : filesOp.isFile;
var keys; var keys;
if (!useData) { if (!useData) {
@ -1478,7 +1478,7 @@ define([
var $atime = $('<td>', {'class': 'col2'}).text(new Date(r.data.atime).toLocaleString()); var $atime = $('<td>', {'class': 'col2'}).text(new Date(r.data.atime).toLocaleString());
var $ctimeName = $('<td>', {'class': 'label2'}).text(Messages.fm_creation); var $ctimeName = $('<td>', {'class': 'label2'}).text(Messages.fm_creation);
var $ctime = $('<td>', {'class': 'col2'}).text(new Date(r.data.ctime).toLocaleString()); var $ctime = $('<td>', {'class': 'col2'}).text(new Date(r.data.ctime).toLocaleString());
if (filesOp.isPathInHrefArray(path)) { if (filesOp.isPathIn(path, ['hrefArray'])) {
path.pop(); path.pop();
path.push(r.data.title); path.push(r.data.title);
} }
@ -1522,14 +1522,14 @@ define([
if (!path || path.length === 0) { if (!path || path.length === 0) {
path = [ROOT]; path = [ROOT];
} }
var isInRoot = filesOp.isPathInRoot(path); var isInRoot = filesOp.isPathIn(path, [ROOT]);
var isTrashRoot = filesOp.comparePath(path, [TRASH]); var isTrashRoot = filesOp.comparePath(path, [TRASH]);
var isUnsorted = filesOp.comparePath(path, [UNSORTED]); var isUnsorted = filesOp.comparePath(path, [UNSORTED]);
var isTemplate = filesOp.comparePath(path, [TEMPLATE]); var isTemplate = filesOp.comparePath(path, [TEMPLATE]);
var isAllFiles = filesOp.comparePath(path, [FILES_DATA]); var isAllFiles = filesOp.comparePath(path, [FILES_DATA]);
var isSearch = path[0] === SEARCH; var isSearch = path[0] === SEARCH;
var root = isSearch ? undefined : filesOp.findElement(files, path); var root = isSearch ? undefined : filesOp.find(path);
if (!isSearch && typeof(root) === "undefined") { if (!isSearch && typeof(root) === "undefined") {
log(Messages.fm_unknownFolderError); log(Messages.fm_unknownFolderError);
debug("Unable to locate the selected directory: ", path); debug("Unable to locate the selected directory: ", path);
@ -1642,11 +1642,11 @@ define([
var $el = $(e); var $el = $(e);
if ($el.data('path')) { if ($el.data('path')) {
var path = $el.data('path'); var path = $el.data('path');
var element = filesOp.findElement(files, path); var element = filesOp.find(path);
if (!filesOp.isFile(element)) { return; } if (!filesOp.isFile(element)) { return; }
var data = filesOp.getFileData(element); var data = filesOp.getFileData(element);
if (!data) { return; } if (!data) { return; }
if (filesOp.isPathInHrefArray(path)) { $el.find('.name').attr('title', data.title).text(data.title); } if (filesOp.isPathIn(path, ['hrefArray'])) { $el.find('.name').attr('title', data.title).text(data.title); }
$el.find('.title').attr('title', data.title).text(data.title); $el.find('.title').attr('title', data.title).text(data.title);
$el.find('.atime').attr('title', getDate(data.atime)).text(getDate(data.atime)); $el.find('.atime').attr('title', getDate(data.atime)).text(getDate(data.atime));
$el.find('.ctime').attr('title', getDate(data.ctime)).text(getDate(data.ctime)); $el.find('.ctime').attr('title', getDate(data.ctime)).text(getDate(data.ctime));
@ -1700,7 +1700,7 @@ define([
}; };
var createTree = function ($container, path) { var createTree = function ($container, path) {
var root = filesOp.findElement(files, path); var root = filesOp.find(path);
// don't try to display what doesn't exist // don't try to display what doesn't exist
if (!root) { return; } if (!root) { return; }
@ -1930,7 +1930,7 @@ define([
} }
else if ($(this).hasClass('open_ro')) { else if ($(this).hasClass('open_ro')) {
paths.forEach(function (p) { paths.forEach(function (p) {
var el = filesOp.findElement(files, p.path); var el = filesOp.find(p.path);
if (filesOp.isFolder(el)) { return; } if (filesOp.isFolder(el)) { return; }
var roUrl = getReadOnlyUrl(el); var roUrl = getReadOnlyUrl(el);
openFile(roUrl, false); openFile(roUrl, false);
@ -1942,11 +1942,11 @@ define([
module.newFolder = info.newPath; module.newFolder = info.newPath;
module.displayDirectory(paths[0].path); module.displayDirectory(paths[0].path);
}; };
filesOp.createNewFolder(paths[0].path, null, onCreated); filesOp.addFolder(paths[0].path, null, onCreated);
} }
else if ($(this).hasClass("properties")) { else if ($(this).hasClass("properties")) {
if (paths.length !== 1) { return; } if (paths.length !== 1) { return; }
var el = filesOp.findElement(files, paths[0].path); var el = filesOp.find(paths[0].path);
var prop = getProperties(el); var prop = getProperties(el);
Cryptpad.alert('', undefined, true); Cryptpad.alert('', undefined, true);
$('.alertify .msg').html(prop); $('.alertify .msg').html(prop);
@ -1972,8 +1972,8 @@ define([
} }
else if ($(this).hasClass('open_ro')) { else if ($(this).hasClass('open_ro')) {
paths.forEach(function (p) { paths.forEach(function (p) {
var el = filesOp.findElement(files, p.path); var el = filesOp.find(p.path);
if (filesOp.isPathInFilesData(p.path)) { el = el.href; } if (filesOp.isPathIn(p.path, [FILES_DATA])) { el = el.href; }
if (!el || filesOp.isFolder(el)) { return; } if (!el || filesOp.isFolder(el)) { return; }
var roUrl = getReadOnlyUrl(el); var roUrl = getReadOnlyUrl(el);
openFile(roUrl, false); openFile(roUrl, false);
@ -1986,7 +1986,7 @@ define([
} }
else if ($(this).hasClass("properties")) { else if ($(this).hasClass("properties")) {
if (paths.length !== 1) { return; } if (paths.length !== 1) { return; }
var el = filesOp.findElement(files, paths[0].path); var el = filesOp.find(paths[0].path);
var prop = getProperties(el); var prop = getProperties(el);
Cryptpad.alert('', undefined, true); Cryptpad.alert('', undefined, true);
$('.alertify .msg').html(prop); $('.alertify .msg').html(prop);
@ -2004,12 +2004,12 @@ define([
refresh(); refresh();
}; };
if ($(this).hasClass("newfolder")) { if ($(this).hasClass("newfolder")) {
filesOp.createNewFolder(path, null, onCreated); filesOp.addFolder(path, null, onCreated);
} }
else if ($(this).hasClass("newdoc")) { else if ($(this).hasClass("newdoc")) {
var type = $(this).data('type') || 'pad'; var type = $(this).data('type') || 'pad';
var name = Cryptpad.getDefaultName({type: type}); var name = Cryptpad.getDefaultName({type: type});
filesOp.createNewFile(path, name, type, onCreated); filesOp.addFile(path, name, type, onCreated);
} }
module.hideMenu(); module.hideMenu();
}); });
@ -2046,7 +2046,7 @@ define([
if (path.length === 4) { name = path[1]; } if (path.length === 4) { name = path[1]; }
Cryptpad.confirm(Messages._getKey("fm_removePermanentlyDialog", [name]), function(res) { Cryptpad.confirm(Messages._getKey("fm_removePermanentlyDialog", [name]), function(res) {
if (!res) { return; } if (!res) { return; }
filesOp.removeFromTrash(path, refresh); filesOp.removeFromTrash(path, refresh); // TODO END HERE
}); });
return; return;
} }

Loading…
Cancel
Save