Stackedit/public/res/classes/FolderDescriptor.js

49 lines
1.6 KiB
JavaScript
Raw Normal View History

2013-08-12 00:10:26 +00:00
define([
"underscore",
"utils",
2013-11-05 23:03:38 +00:00
"storage",
2013-08-12 00:10:26 +00:00
"fileSystem"
2013-11-05 23:03:38 +00:00
], function(_, utils, storage, fileSystem) {
2013-08-11 00:52:27 +00:00
2013-11-07 23:10:38 +00:00
function FolderDescriptor(folderIndex, name) {
2013-08-11 00:52:27 +00:00
this.folderIndex = folderIndex;
2013-11-05 23:03:38 +00:00
this._name = name || storage[folderIndex + ".name"];
// Retrieve file list from storage
2013-08-12 00:10:26 +00:00
this.fileList = {};
_.each(utils.retrieveIndexArray(folderIndex + ".files"), function(fileIndex) {
try {
var fileDesc = fileSystem[fileIndex];
fileDesc.folder = this;
this.fileList[fileIndex] = fileDesc;
}
catch(e) {
2013-11-05 23:03:38 +00:00
// storage can be corrupted
2013-08-12 00:10:26 +00:00
// 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;
2013-11-05 23:03:38 +00:00
storage[this.folderIndex + ".name"] = name;
2013-08-11 00:52:27 +00:00
}
});
}
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;
});