Stackedit/public/res/classes/FolderDescriptor.js

48 lines
1.6 KiB
JavaScript
Raw Normal View History

2013-08-12 00:10:26 +00:00
define([
"underscore",
"utils",
"fileSystem"
], function(_, utils, fileSystem) {
2013-08-11 00:52:27 +00:00
function FolderDescriptor(folderIndex, name, fileList) {
this.folderIndex = folderIndex;
this._name = name || localStorage[folderIndex + ".name"];
2013-08-12 00:10:26 +00:00
// Retrieve file list from localStorage
this.fileList = {};
_.each(utils.retrieveIndexArray(folderIndex + ".files"), function(fileIndex) {
try {
var fileDesc = fileSystem[fileIndex];
fileDesc.folder = this;
this.fileList[fileIndex] = fileDesc;
}
catch(e) {
// localStorage can be corrupted
// Remove file from folder
utils.removeIndexFromArray(folderIndex + ".files", fileIndex);
}
}, this);
2013-08-11 00:52:27 +00:00
Object.defineProperty(this, 'name', {
get: function() {
return this._name;
},
set: function(name) {
this._name = name;
localStorage[this.folderIndex + ".name"] = name;
}
});
}
2013-08-12 00:10:26 +00:00
2013-08-11 00:52:27 +00:00
FolderDescriptor.prototype.addFile = function(fileDesc) {
2013-08-12 00:10:26 +00:00
fileDesc.folder = this;
2013-08-11 00:52:27 +00:00
utils.appendIndexToArray(this.folderIndex + ".files", fileDesc.fileIndex);
this.fileList[fileDesc.fileIndex] = fileDesc;
};
2013-08-12 00:10:26 +00:00
2013-08-11 00:52:27 +00:00
FolderDescriptor.prototype.removeFile = function(fileDesc) {
2013-08-12 00:10:26 +00:00
fileDesc.folder = undefined;
2013-08-11 00:52:27 +00:00
utils.removeIndexFromArray(this.folderIndex + ".files", fileDesc.fileIndex);
delete this.fileList[fileDesc.fileIndex];
};
2013-08-12 00:10:26 +00:00
2013-08-11 00:52:27 +00:00
return FolderDescriptor;
});