From aa14ea7ac47417a624849a4a1dfdbde12f71e248 Mon Sep 17 00:00:00 2001 From: yflory Date: Wed, 16 Nov 2016 18:11:44 +0100 Subject: [PATCH] Use the old store format --- www/file/fileObject.js | 62 ++++++++++++++++++++++++++++++------------ www/file/main.js | 19 ++++++++----- 2 files changed, 56 insertions(+), 25 deletions(-) diff --git a/www/file/fileObject.js b/www/file/fileObject.js index 62be76029..0db53d9ab 100644 --- a/www/file/fileObject.js +++ b/www/file/fileObject.js @@ -214,16 +214,20 @@ define([ var unsortedFiles = getUnsortedFiles().slice(); var trashFiles = getTrashFiles().slice(); var toRemove = []; - Object.keys(files[FILES_DATA]).forEach(function (f) { + files[FILES_DATA].forEach(function (arr) { + var f = arr.href; if (rootFiles.indexOf(f) === -1 && unsortedFiles.indexOf(f) === -1 && trashFiles.indexOf(f) === -1) { - toRemove.push(f); + toRemove.push(arr); } }); toRemove.forEach(function (f) { - debug("Removing", f, "from filesData"); - delete files[FILES_DATA][f]; + var idx = files[FILES_DATA].indexOf(f); + if (idx !== -1) { + debug("Removing", f, "from filesData"); + files[FILES_DATA].splice(idx, 1); + } }); }; @@ -267,13 +271,28 @@ define([ return findElement(files, parentPath); }; + 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 (!href || !files[FILES_DATA][href]) { + var data = getFileData(href); + if (!href || !data) { error("getTitle called with a non-existing href: ", href); return; } - return files[FILES_DATA][href].title; + return data.title; }; var pushToTrash = function (name, element, path) { @@ -518,8 +537,8 @@ define([ }; var addPad = exp.addPad = function (href, data) { - if (Object.keys(files[FILES_DATA]).indexOf(href) === -1) { - files[FILES_DATA][href] = data; + if (!getFileData(href)) { + files[FILES_DATA].push(data); } var unsortedFiles = getUnsortedFiles().slice(); var rootFiles = getRootFiles().slice(); @@ -548,7 +567,7 @@ define([ 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[FILES_DATA])) { debug("FILES_DATA was not an array"); files[FILES_DATA] = []; } if (!$.isArray(files[UNSORTED])) { debug("UNSORTED was not an array"); files[UNSORTED] = []; } var fixRoot = function (element) { @@ -600,19 +619,26 @@ define([ var rootFiles = getRootFiles().slice(); var unsortedFiles = getUnsortedFiles().slice(); var trashFiles = getTrashFiles().slice(); - for (var el in fd) { - if (typeof(fd[el]) !== "object") { - debug("An element in filesData was not an object.", fd[el]); - delete fd[el]; + var toClean = []; + fd.forEach(function (el, idx) { + if (typeof(el) !== "object") { + debug("An element in filesData was not an object.", el); + toClean.push(el); } else { - if (rootFiles.indexOf(el) === -1 - && unsortedFiles.indexOf(el) === -1 - && trashFiles.indexOf(el) === -1) { + if (rootFiles.indexOf(el.href) === -1 + && unsortedFiles.indexOf(el.href) === -1 + && trashFiles.indexOf(el.href) === -1) { debug("An element in filesData was not in ROOT, UNSORTED or TRASH.", el); - files[UNSORTED].push(el); + files[UNSORTED].push(el.href); } } - } + }); + toClean.forEach(function (el) { + var idx = fd.indexOf(el); + if (idx !== -1) { + fd.splice(idx, 1); + } + }); }; fixFilesData(files[FILES_DATA]); diff --git a/www/file/main.js b/www/file/main.js index bdcc88019..cedfc8ad0 100644 --- a/www/file/main.js +++ b/www/file/main.js @@ -365,7 +365,7 @@ define([ }; var openFile = function (fileEl) { - window.location.hash = fileEl; + window.open(fileEl); }; var refresh = function () { @@ -611,11 +611,11 @@ define([ $span.html(''); $span.append($name); - if (typeof(files[FILES_DATA][element]) === "undefined") { + if (!filesOp.getFileData(element)) { return; } var hrefData = Cryptpad.parsePadUrl(element); - var data = files[FILES_DATA][element]; + var data = filesOp.getFileData(element); var type = Messages.type[hrefData.type] || hrefData.type; var $title = $('', {'class': 'title listElement', title: data.title}).text(data.title); var $type = $('', {'class': 'date listElement', title: type}).text(type); @@ -820,7 +820,11 @@ define([ var $fhType = $('', {'class': 'date'}).text(Messages.table_type); var $fhAdate = $('', {'class': 'date'}).text(Messages.fm_lastAccess); var $fhCdate = $('', {'class': 'date'}).text(Messages.fm_creation); - $fihElement.append($fhName).append($fhTitle).append($fhType).append($fhAdate).append($fhCdate); + $fihElement.append($fhName); + if (displayTitle) { + $fihElement.append($fhTitle); + } + $fihElement.append($fhType).append($fhAdate).append($fhCdate); return $fileHeader; }; @@ -830,12 +834,13 @@ define([ // Unsorted element are represented by "href" in an array: they don't have a filename // and they don't hav a hierarchical structure (folder/subfolders) - var displayUnsorted = function ($container, $fileHeader) { + var displayUnsorted = function ($container) { var unsorted = files[UNSORTED]; if (allFilesSorted()) { return; } + var $fileHeader = getFileListHeader(false); $container.append($fileHeader); unsorted.forEach(function (href, idx) { - var file = files[FILES_DATA][href]; + var file = filesOp.getFileData(href); if (!file) { debug("getUnsortedFiles returns an element not present in filesData: ", href); return; @@ -939,7 +944,7 @@ define([ var $fileHeader = getFileListHeader(true); if (isUnsorted) { - displayUnsorted($list, $fileHeader); + displayUnsorted($list); } else if (isTrashRoot) { displayTrashRoot($list, $folderHeader, $fileHeader); } else {