Refresh the directory if needed when the object has changed

pull/1/head
yflory 8 years ago
parent 118829617f
commit d6e1215166

@ -71,12 +71,6 @@ define(function () {
}
};
Store.addPad = function () {};
Store.forgetPad = function (href, cb) {
};
var changeHandlers = Store.changeHandlers = [];
Store.change = function (f) {

@ -20,6 +20,12 @@ define([
*/
var $ = window.jQuery;
// When set to true, USE_FS_STORE becomes the default store, but the localStorage store is
// still loaded for migration purpose. When false, the localStorage is used.
var USE_FS_STORE = true;
var storeToUse = USE_FS_STORE ? FS : Store;
var common = {
User: User,
};
@ -36,8 +42,8 @@ define([
var getStore = common.getStore = function (legacy) {
if (!legacy && userStore) { return userStore; }
if (legacy) { return store; }
if (!legacy && fsStore) { return fsStore; }
if ((!USE_FS_STORE || legacy) && store) { return store; }
if (USE_FS_STORE && !legacy && fsStore) { return fsStore; }
throw new Error("Store is not ready!");
};
@ -379,6 +385,9 @@ define([
};
// STORAGE
var forgetFSPad = function (href, cb) {
getStore().forgetPad(href, cb);
};
var forgetPad = common.forgetPad = function (href, cb, legacy) {
var parsed = parsePadUrl(href);
@ -389,6 +398,10 @@ define([
}
getStore(legacy).keys(function (err, keys) {
if (err) {
cb(err);
return;
}
var toRemove = keys.filter(function (k) {
return k.indexOf(parsed.hash) === 0;
});
@ -403,6 +416,26 @@ define([
});
};
if (USE_FS_STORE && !legacy) {
// TODO implement forgetPad in store.js
forgetFSPad(href, callback);
return;
}
getRecentPads(function (err, recentPads) {
setRecentPads(recentPads.filter(function (pad) {
var p = parsePadUrl(pad.href);
// find duplicates
if (parsed.hash === p.hash && parsed.type === p.type) {
console.log("Found a duplicate");
return;
}
return true;
}), callback, legacy);
}, legacy);
if (typeof(getStore(legacy).forgetPad) === "function") {
// TODO implement forgetPad in store.js
getStore(legacy).forgetPad(href, callback);
@ -457,8 +490,7 @@ define([
if (!contains) {
var data = makePad(href, name);
renamed.push(data);
if (typeof(getStore(legacy).addPad) === "function") {
//TODO implement addPad in store.js
if (USE_FS_STORE && typeof(getStore().addPad) === "function") {
getStore().addPad(href);
}
}
@ -533,8 +565,11 @@ define([
f(void 0, env);
};
FS.ready(function (err, store) {
fsStore = common.store = env.store = store;
storeToUse.ready(function (err, store) {
common.store = env.store = store;
if (USE_FS_STORE) {
fsStore = store;
}
$(function() {
// Race condition : if document.body is undefined when alertify.js is loaded, Alertify

@ -86,11 +86,7 @@ li {
text-decoration: underline;
}
#tree #trashTree {
margin-top: 2em;
}
#tree #unsortedTree {
#tree #trashTree, #tree #unsortedTree, #tree #allfilesTree {
margin-top: 2em;
}

@ -322,6 +322,7 @@ define([
appStatus._onReady.forEach(function (h) {
h();
});
_onReady = [];
}
}
};
@ -857,6 +858,28 @@ define([
});
};
var displayAllFiles = function ($container) {
var allfiles = files[FILES_DATA];
if (allfiles.length === 0) { return; }
var $fileHeader = getFileListHeader(false);
$container.append($fileHeader);
allfiles.forEach(function (file, idx) {
var $icon = $fileIcon.clone();
var $name = $('<span>', { 'class': 'file-element element' });
addFileData(file.href, file.title, $name, false);
var $element = $('<li>', {
draggable: false
}).append($icon).append($name).dblclick(function () {
openFile(file.href);
});
$element.click(function(e) {
e.stopPropagation();
onElementClick($element);
});
$container.append($element);
});
};
var displayTrashRoot = function ($list, $folderHeader, $fileHeader) {
// Elements in the trash are JS arrays (several elements can have the same name)
[true,false].forEach(function (folder) {
@ -890,7 +913,8 @@ define([
// Display the selected directory into the content part (rightside)
// NOTE: Elements in the trash are not using the same storage structure as the others
var displayDirectory = module.displayDirectory = function (path) {
var displayDirectory = module.displayDirectory = function (path, force) {
if (!appStatus.isReady && !force) { return; }
appStatus.ready(false);
currentPath = path;
$content.html("");
@ -899,6 +923,7 @@ define([
}
var isTrashRoot = filesOp.comparePath(path, [TRASH]);
var isUnsorted = filesOp.comparePath(path, [UNSORTED]);
var isAllFiles = filesOp.comparePath(path, [FILES_DATA]);
var root = filesOp.findElement(files, path);
if (typeof(root) === "undefined") {
@ -906,7 +931,7 @@ define([
debug("Unable to locate the selected directory: ", path);
var parentPath = path.slice();
parentPath.pop();
displayDirectory(parentPath);
displayDirectory(parentPath, true);
return;
}
@ -938,6 +963,8 @@ define([
if (isUnsorted) {
displayUnsorted($list);
} else if (isAllFiles) {
displayAllFiles($list);
} else if (isTrashRoot) {
displayTrashRoot($list, $folderHeader, $fileHeader);
} else {
@ -1075,6 +1102,15 @@ define([
$container.append($unsortedList);
};
var createAllFiles = function ($container, path) {
var $icon = $unsortedIcon.clone();
var isOpened = filesOp.comparePath(path, currentPath);
var $allfilesElement = createTreeElement(FILES_DATA_NAME, $icon, [FILES_DATA], false, false, isOpened);
$allfilesElement.addClass('root');
var $allfilesList = $('<ul>', { id: 'allfilesTree' }).append($allfilesElement);
$container.append($allfilesList);
};
var createTrash = function ($container, path) {
var $icon = filesOp.isFolderEmpty(files[TRASH]) ? $trashEmptyIcon.clone() : $trashIcon.clone();
var isOpened = filesOp.comparePath(path, currentPath);
@ -1099,10 +1135,9 @@ define([
$tree.html('');
createTree($tree, [ROOT]);
createUnsorted($tree, [UNSORTED]);
createAllFiles($tree, [FILES_DATA]);
createTrash($tree, [TRASH]);
};
module.displayDirectory(currentPath);
//resetTree(); //already called by displayDirectory
var hideMenu = module.hideMenu = function () {
$contextMenu.hide();
@ -1251,6 +1286,31 @@ define([
moveElements(paths, [TRASH], false, refresh);
}
});
files.on('change', [], function () {
var path = arguments[2];
if ((filesOp.isPathInUnsorted(currentPath) && filesOp.isPathInUnsorted(path)) ||
(path.length >= currentPath.length && filesOp.isSubpath(path, currentPath)) ||
(filesOp.isPathInTrash(currentPath) && filesOp.isPathInTrash(path))) {
// Reload after 50ms to make sure all the change events have been received
window.setTimeout(function () {
module.displayDirectory(currentPath);
}, 50);
}
module.resetTree();
}).on('remove', [], function () {
var path = arguments[1];
if ((filesOp.isPathInUnsorted(currentPath) && filesOp.isPathInUnsorted(path)) ||
(path.length >= currentPath.length && filesOp.isSubpath(path, currentPath)) ||
(filesOp.isPathInTrash(currentPath) && filesOp.isPathInTrash(path))) {
// Reload after 50ms to make sure all the change events have been received
window.setTimeout(function () {
module.displayDirectory(currentPath);
}, 50); }
module.resetTree();
});
module.displayDirectory(currentPath);
};
/*

Loading…
Cancel
Save