Add translations, ability to select multiple elements, clean the code

pull/1/head
yflory 8 years ago
parent f05efbb113
commit d77970975c

@ -156,6 +156,33 @@ define(function () {
out.poll_titleHint = "Titre";
out.poll_descriptionHint = "Description";
// File manager
out.fm_rootName = "Mes documents";
out.fm_trashName = "Corbeille";
out.fm_unsortedName = "Fichiers non triés";
out.fm_filesDataName = "Tous les fichiers";
out.fm_newFolder = "Nouveau dossier";
out.fm_newFolderButton = "NOUVEAU DOSSIER";
out.fm_folderName = "Nom du dossier";
out.fm_numberOfFolders = "# de dossiers";
out.fm_numberOfFiles = "# de fichiers";
out.fm_fileName = "Nom du fichier";
out.fm_title = "Titre";
out.fm_lastAccess = "Dernier accès";
out.fm_creation = "Création";
out.fm_forbidden = "Action interdite";
out.fm_emptyTrashDialog = "Êtes-vous sûr de vouloir vider la corbeille ?";
out.fm_removePermanentlyDialog = "Êtes-vous sûr de vouloir supprimer {0} de la corbeille de manière permanente ?";
out.fm_restoreDialog = "Êtes-vous sûr de vouloir restaurer {0} à son emplacement précédent ?";
out.fm_removeSeveralDialog = "Êtes-vous sûr de vouloir déplacer ces {0} éléments vers la corbeille ?"
out.fm_unknownFolderError = "Le dossier sélectionné ou le dernier dossier visité n'existe plus. Ouverture du dossier parent...";
out.fm_contextMenuError = "Impossible d'ouvrir le menu contextuel pour cet élément. Si le problème persiste, essayez de rechercher la page.";
out.fm_selectError = "Impossible de sélectionner l'élément ciblé. Si le problème persiste, essayez de recharger la page.";
out.fo_moveUnsortedError = "La liste des éléments non triés ne peut pas contenir de dossiers.";
out.fo_existingNameError = "Ce nom est déjà utilisé dans ce répertoire. Veuillez en choisir un autre.";
// index.html
out.main_p1 = 'CryptPad est l\'éditeur collaboratif en temps réel <strong>zero knowledge</strong>. Le chiffrement est effectué depuis votre navigateur, ce qui protège les données contre le serveur, le cloud, et la NSA. La clé de chiffrement est stockée dans l\'<a href="https://fr.wikipedia.org/wiki/Identificateur_de_fragment">identifieur de fragment</a> de l\'URL qui n\'est jamais envoyée au serveur mais est accessible depuis javascript, de sorte qu\'en partageant l\'URL, vous donnez l\'accès au pad à ceux qui souhaitent participer.';

@ -156,6 +156,33 @@ define(function () {
out.poll_titleHint = "Title";
out.poll_descriptionHint = "Description";
// File manager
out.fm_rootName = "My documents";
out.fm_trashName = "Trash";
out.fm_unsortedName = "Unsorted files";
out.fm_filesDataName = "All files";
out.fm_newFolder = "New folder";
out.fm_newFolderButton = "NEW FOLDER";
out.fm_folderName = "Folder name";
out.fm_numberOfFolders = "# of folders";
out.fm_numberOfFiles = "# of files";
out.fm_fileName = "File name";
out.fm_title = "Title";
out.fm_lastAccess = "Last access";
out.fm_creation = "Creation";
out.fm_forbidden = "Forbidden action";
out.fm_emptyTrashDialog = "Are you sure you want to empty the trash?";
out.fm_removePermanentlyDialog = "Are you sure you want to remove {0} from the trash permanently?";
out.fm_removeSeveralDialog = "Are you sure you want to move these {0} elements to the trash?"
out.fm_restoreDialog = "Are you sure you want to restore {0} to its previous location?";
out.fm_unknownFolderError = "The selected or last visited directory no longer exist. Opening the parent folder...";
out.fm_contextMenuError = "Unable to open the context menu for that element. If the problem persist, try to reload the page.";
out.fm_selectError = "Unable to select the targetted element. If the problem persist, try to reload the page.";
out.fo_moveUnsortedError = "You can't move a folder to the list of unsorted pads";
out.fo_existingNameError = "Name already used in that directory. Please choose another one.";
// index.html
out.main_p1 = 'CryptPad is the <strong>zero knowledge</strong> realtime collaborative editor. Encryption carried out in your web browser protects the data from the server, the cloud, and the NSA. The secret encryption key is stored in the URL <a href="https://en.wikipedia.org/wiki/Fragment_identifier">fragment identifier</a> which is never sent to the server but is available to javascript so by sharing the URL, you give authorization to others who want to participate.';

@ -12,7 +12,7 @@ html, body {
}
.fa {
min-width: 17px;
/*min-width: 17px;*/
margin-right: 3px;
font-family: FontAwesome;
}
@ -201,7 +201,6 @@ li {
margin: 10px 10px;
width: 140px;
text-align: center;
height: 70px;
vertical-align: top;
}

@ -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…
Cancel
Save