define([
"jquery",
"underscore",
"utils",
"classes/Extension",
"mousetrap",
"fileSystem",
"text!html/documentSelectorSettingsBlock.html",
], function($, _, utils, Extension, mousetrap, fileSystem, documentSelectorSettingsBlockHTML) {
var documentSelector = new Extension("documentSelector", 'Document Selector');
documentSelector.settingsBlock = documentSelectorSettingsBlockHTML;
documentSelector.defaultConfig = {
orderBy: "title",
shortcutPrevious: "Ctrl+[",
shortcutNext: "Ctrl+]"
};
documentSelector.onLoadSettings = function() {
utils.setInputValue("#select-document-selector-orderby", documentSelector.config.orderBy);
utils.setInputValue("#input-document-selector-shortcut-previous", documentSelector.config.shortcutPrevious);
utils.setInputValue("#input-document-selector-shortcut-next", documentSelector.config.shortcutNext);
};
documentSelector.onSaveSettings = function(newConfig, event) {
newConfig.orderBy = utils.getInputValue("#select-document-selector-orderby");
newConfig.shortcutPrevious = utils.getInputTextValue("#input-document-selector-shortcut-previous", event);
newConfig.shortcutNext = utils.getInputTextValue("#input-document-selector-shortcut-next", event);
};
var fileMgr = undefined;
documentSelector.onFileMgrCreated = function(fileMgrParameter) {
fileMgr = fileMgrParameter;
};
var dropdownElt = undefined;
var liMap = undefined;
var liArray = undefined;
var sortFunction = undefined;
var selectFileDesc = undefined;
var buildSelector = function() {
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) {
return attributes.provider.providerId;
}).each(function(attributes) {
var classes = 'icon-provider-' + attributes.provider.providerId;
if(attributes.isRealtime === true) {
classes += " realtime";
}
result.push('');
});
result.push(" ");
result.push(fileDesc.title);
return result.join("");
}
liMap = {};
dropdownElt.empty();
var documentPanelSelectorElt = $(".document-panel > .panel-content > .list-group");
documentPanelSelectorElt.find('.list-group-item').remove();
_.chain(fileSystem).sortBy(sortFunction).each(function(fileDesc) {
var a = $('').html(composeTitle(fileDesc));
var documentPanelItemElt = a.clone().addClass('list-group-item action-close-panel');
documentPanelItemElt.add(a).click(function() {
if(!liMap[fileDesc.fileIndex].is(".disabled")) {
fileMgr.selectFile(fileDesc);
}
else {
$("#wmd-input").focus();
}
});
var li = $("").append(a);
liMap[fileDesc.fileIndex] = li;
if(fileDesc === selectFileDesc) {
li.addClass("disabled");
documentPanelItemElt.addClass("active");
}
dropdownElt.append(li);
documentPanelSelectorElt.append(documentPanelItemElt);
});
liArray = _.values(liMap);
};
documentSelector.onFileSelected = function(fileDesc) {
selectFileDesc = fileDesc;
buildSelector();
};
documentSelector.onFileCreated = buildSelector;
documentSelector.onFileDeleted = buildSelector;
documentSelector.onTitleChanged = buildSelector;
documentSelector.onSyncExportSuccess = buildSelector;
documentSelector.onSyncRemoved = buildSelector;
documentSelector.onNewPublishSuccess = buildSelector;
documentSelector.onPublishRemoved = buildSelector;
// Filter for search input in file selector
function filterFileSelector(filter) {
var liList = $(".file-selector > li");
liList.show();
if(filter) {
var words = filter.toLowerCase().split(/\s+/);
liList.each(function() {
var fileTitle = $(this).text().toLowerCase();
if(_.some(words, function(word) {
return fileTitle.indexOf(word) === -1;
})) {
$(this).hide();
}
});
}
}
documentSelector.onReady = function() {
if(documentSelector.config.orderBy == "title") {
sortFunction = function(fileDesc) {
return fileDesc.title.toLowerCase();
};
}
else if(documentSelector.config.orderBy == "mru") {
sortFunction = function(fileDesc) {
return -fileDesc.selectTime;
};
}
dropdownElt = $('