Stackedit/js/extensions/document-selector.js

72 lines
2.1 KiB
JavaScript
Raw Normal View History

2013-05-27 19:45:33 +00:00
define([
"jquery",
2013-05-28 23:41:09 +00:00
"underscore",
"file-system"
], function($, _, fileSystem) {
2013-05-26 22:59:17 +00:00
var documentSelector = {
extensionId: "documentSelector",
extensionName: "Document selector",
2013-05-27 19:45:33 +00:00
settingsBloc: '<p>Builds the "Open document" dropdown menu.</p>'
2013-05-26 22:59:17 +00:00
};
2013-05-27 19:45:33 +00:00
var fileMgr = undefined;
documentSelector.onFileMgrCreated = function(fileMgrParameter) {
fileMgr = fileMgrParameter;
};
var liMap = undefined;
var buildSelector = function() {
2013-05-26 22:59:17 +00:00
function composeTitle(fileDesc) {
var result = [];
var syncAttributesList = _.values(fileDesc.syncLocations);
var publishAttributesList = _.values(fileDesc.publishLocations);
var attributesList = syncAttributesList.concat(publishAttributesList);
_.chain(attributesList).sortBy(function(attributes) {
2013-05-27 19:45:33 +00:00
return attributes.provider.providerId;
2013-05-26 22:59:17 +00:00
}).each(function(attributes) {
2013-05-27 19:45:33 +00:00
result.push('<i class="icon-' + attributes.provider.providerId + '"></i>');
2013-05-26 22:59:17 +00:00
});
result.push(" ");
result.push(fileDesc.title);
return result.join("");
}
2013-05-27 19:45:33 +00:00
liMap = {};
2013-05-26 22:59:17 +00:00
$("#file-selector li:not(.stick)").empty();
2013-05-27 19:45:33 +00:00
_.chain(
fileSystem
).sortBy(function(fileDesc) {
return fileDesc.title.toLowerCase();
}).each(function(fileDesc) {
var a = $('<a href="#">').html(composeTitle(fileDesc)).click(function() {
2013-05-27 22:13:41 +00:00
if(!liMap[fileDesc.fileIndex].is(".disabled")) {
2013-05-27 19:45:33 +00:00
fileMgr.selectFile(fileDesc);
}
});
2013-05-26 22:59:17 +00:00
var li = $("<li>").append(a);
2013-05-27 19:45:33 +00:00
liMap[fileDesc.fileIndex] = li;
2013-05-26 22:59:17 +00:00
$("#file-selector").append(li);
});
};
2013-05-27 19:45:33 +00:00
documentSelector.onFileSelected = function(fileDesc) {
if(liMap === undefined) {
buildSelector();
}
$("#file-selector li:not(.stick)").removeClass("disabled");
liMap[fileDesc.fileIndex].addClass("disabled");
2013-05-26 22:59:17 +00:00
};
2013-05-27 19:45:33 +00:00
documentSelector.onFileCreated = buildSelector;
documentSelector.onFileDeleted = buildSelector;
documentSelector.onTitleChanged = buildSelector;
documentSelector.onSyncExportSuccess = buildSelector;
documentSelector.onSyncRemoved = buildSelector;
documentSelector.onNewPublishSuccess = buildSelector;
documentSelector.onPublishRemoved = buildSelector;
2013-05-26 22:59:17 +00:00
return documentSelector;
});