Stackedit/public/res/classes/FileDescriptor.js

152 lines
6.2 KiB
JavaScript
Raw Normal View History

2013-09-09 00:08:55 +00:00
define([
"underscore",
"utils",
2013-11-05 23:03:38 +00:00
"storage",
2014-03-20 00:24:56 +00:00
], function(_, utils, storage) {
2013-06-16 10:47:45 +00:00
function FileDescriptor(fileIndex, title, syncLocations, publishLocations) {
this.fileIndex = fileIndex;
2013-11-05 23:03:38 +00:00
this._title = title || storage[fileIndex + ".title"];
this._editorScrollTop = parseInt(storage[fileIndex + ".editorScrollTop"]) || 0;
2014-03-18 01:10:22 +00:00
this._editorStart = parseInt(storage[fileIndex + ".editorEnd"]) || 0;
2013-11-05 23:03:38 +00:00
this._editorEnd = parseInt(storage[fileIndex + ".editorEnd"]) || 0;
this._previewScrollTop = parseInt(storage[fileIndex + ".previewScrollTop"]) || 0;
this._selectTime = parseInt(storage[fileIndex + ".selectTime"]) || 0;
2014-03-23 02:33:41 +00:00
this._discussionList = JSON.parse(storage[fileIndex + ".discussionList"] || '{}');
2013-06-16 10:47:45 +00:00
this.syncLocations = syncLocations || {};
this.publishLocations = publishLocations || {};
Object.defineProperty(this, 'title', {
get: function() {
return this._title;
},
set: function(title) {
this._title = title;
2013-11-05 23:03:38 +00:00
storage[this.fileIndex + ".title"] = title;
2013-06-16 10:47:45 +00:00
}
});
Object.defineProperty(this, 'content', {
get: function() {
2013-11-05 23:03:38 +00:00
return storage[this.fileIndex + ".content"];
2013-06-16 10:47:45 +00:00
},
set: function(content) {
2013-11-05 23:03:38 +00:00
storage[this.fileIndex + ".content"] = content;
2013-06-16 10:47:45 +00:00
}
});
Object.defineProperty(this, 'editorScrollTop', {
get: function() {
return this._editorScrollTop;
},
set: function(editorScrollTop) {
this._editorScrollTop = editorScrollTop;
2013-11-05 23:03:38 +00:00
storage[this.fileIndex + ".editorScrollTop"] = editorScrollTop;
2013-06-16 10:47:45 +00:00
}
});
2014-03-18 01:10:22 +00:00
Object.defineProperty(this, 'editorStart', {
get: function() {
return this._editorStart;
},
set: function(editorStart) {
this._editorStart = editorStart;
storage[this.fileIndex + ".editorStart"] = editorStart;
}
});
Object.defineProperty(this, 'editorEnd', {
get: function() {
return this._editorEnd;
},
set: function(editorEnd) {
this._editorEnd = editorEnd;
storage[this.fileIndex + ".editorEnd"] = editorEnd;
}
});
2013-06-16 10:47:45 +00:00
Object.defineProperty(this, 'previewScrollTop', {
get: function() {
return this._previewScrollTop;
},
set: function(previewScrollTop) {
this._previewScrollTop = previewScrollTop;
2013-11-05 23:03:38 +00:00
storage[this.fileIndex + ".previewScrollTop"] = previewScrollTop;
2013-06-16 10:47:45 +00:00
}
});
Object.defineProperty(this, 'selectTime', {
get: function() {
return this._selectTime;
},
set: function(selectTime) {
this._selectTime = selectTime;
2013-11-05 23:03:38 +00:00
storage[this.fileIndex + ".selectTime"] = selectTime;
2013-06-16 10:47:45 +00:00
}
});
2014-03-23 02:33:41 +00:00
Object.defineProperty(this, 'discussionList', {
get: function() {
return this._discussionList;
},
set: function(discussionList) {
this._discussionList = discussionList;
storage[this.fileIndex + ".discussionList"] = JSON.stringify(discussionList);
}
});
2014-03-27 00:20:08 +00:00
Object.defineProperty(this, 'discussionListJSON', {
get: function() {
2014-03-28 00:49:49 +00:00
return storage[this.fileIndex + ".discussionList"] || '{}';
2014-03-27 00:20:08 +00:00
},
set: function(discussionList) {
this._discussionList = JSON.parse(discussionList);
storage[this.fileIndex + ".discussionList"] = discussionList;
}
});
2013-06-16 10:47:45 +00:00
}
2013-09-09 00:08:55 +00:00
2013-06-16 10:47:45 +00:00
FileDescriptor.prototype.addSyncLocation = function(syncAttributes) {
utils.storeAttributes(syncAttributes);
utils.appendIndexToArray(this.fileIndex + ".sync", syncAttributes.syncIndex);
this.syncLocations[syncAttributes.syncIndex] = syncAttributes;
};
2013-09-09 00:08:55 +00:00
2013-06-16 10:47:45 +00:00
FileDescriptor.prototype.removeSyncLocation = function(syncAttributes) {
utils.removeIndexFromArray(this.fileIndex + ".sync", syncAttributes.syncIndex);
delete this.syncLocations[syncAttributes.syncIndex];
2013-11-05 23:03:38 +00:00
storage.removeItem(syncAttributes.syncIndex);
2013-06-16 10:47:45 +00:00
};
2013-09-09 00:08:55 +00:00
2013-06-16 10:47:45 +00:00
FileDescriptor.prototype.addPublishLocation = function(publishAttributes) {
utils.storeAttributes(publishAttributes);
utils.appendIndexToArray(this.fileIndex + ".publish", publishAttributes.publishIndex);
this.publishLocations[publishAttributes.publishIndex] = publishAttributes;
};
2013-09-09 00:08:55 +00:00
2013-06-16 10:47:45 +00:00
FileDescriptor.prototype.removePublishLocation = function(publishAttributes) {
utils.removeIndexFromArray(this.fileIndex + ".publish", publishAttributes.publishIndex);
delete this.publishLocations[publishAttributes.publishIndex];
2013-11-05 23:03:38 +00:00
storage.removeItem(publishAttributes.publishIndex);
2013-06-16 10:47:45 +00:00
};
2013-09-09 00:08:55 +00:00
2013-08-11 00:52:05 +00:00
FileDescriptor.prototype.composeTitle = function() {
var result = [];
_.chain(this.syncLocations).sortBy(function(attributes) {
return attributes.provider.providerId;
}).each(function(attributes) {
var classes = 'icon-provider-' + attributes.provider.providerId;
attributes.isRealtime === true && (classes += ' realtime');
result.push('<i class="' + classes + '"></i>');
});
if(_.size(this.syncLocations) !== 0) {
result.push('<i class="icon-refresh title-icon-category"></i>');
}
_.chain(this.publishLocations).sortBy(function(attributes) {
2013-08-11 00:52:05 +00:00
return attributes.provider.providerId;
}).each(function(attributes) {
var classes = 'icon-provider-' + attributes.provider.providerId;
attributes.isRealtime === true && (classes += ' realtime');
result.push('<i class="' + classes + '"></i>');
2013-08-11 00:52:05 +00:00
});
if(_.size(this.publishLocations) !== 0) {
2014-03-17 02:01:46 +00:00
result.push('<i class="icon-upload title-icon-category"></i>');
}
2013-11-03 22:24:17 +00:00
result.push(_.escape(this.title));
2013-08-11 00:52:05 +00:00
return result.join('');
};
2013-09-09 00:08:55 +00:00
2013-06-16 10:47:45 +00:00
return FileDescriptor;
2014-03-20 00:24:56 +00:00
});