Stackedit/js/extensions/documentSelector.js

199 lines
7.6 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",
2013-06-10 21:22:32 +00:00
"utils",
2013-06-22 23:48:57 +00:00
"classes/Extension",
2013-06-10 21:22:32 +00:00
"mousetrap",
"fileSystem",
2013-06-22 23:48:57 +00:00
"text!html/documentSelectorSettingsBlock.html",
], function($, _, utils, Extension, mousetrap, fileSystem, documentSelectorSettingsBlockHTML) {
2013-05-26 22:59:17 +00:00
2013-06-22 23:48:57 +00:00
var documentSelector = new Extension("documentSelector", 'Document Selector');
documentSelector.settingsBlock = documentSelectorSettingsBlockHTML;
documentSelector.defaultConfig = {
orderBy: "title",
shortcutPrevious: "Ctrl+[",
shortcutNext: "Ctrl+]"
2013-06-10 21:22:32 +00:00
};
documentSelector.onLoadSettings = function() {
utils.setInputValue("#select-document-selector-orderby", documentSelector.config.sortBy);
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);
2013-05-29 19:55:23 +00:00
};
var fileMgr = undefined;
documentSelector.onFileMgrCreated = function(fileMgrParameter) {
fileMgr = fileMgrParameter;
};
var liMap = undefined;
2013-06-09 09:49:19 +00:00
var liArray = undefined;
var sortFunction = undefined;
2013-06-16 10:47:35 +00:00
var selectFileDesc = undefined;
2013-05-29 19:55:23 +00:00
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) {
2013-07-21 14:38:53 +00:00
var classes = 'icon-' + attributes.provider.providerId;
if(attributes.isRealtime === true) {
classes += " realtime";
}
result.push('<i class="' + classes + '"></i>');
2013-05-29 19:55:23 +00:00
});
result.push(" ");
result.push(fileDesc.title);
return result.join("");
}
liMap = {};
$("#file-selector li:not(.stick)").empty();
2013-06-09 09:49:19 +00:00
_.chain(fileSystem).sortBy(sortFunction).each(function(fileDesc) {
2013-05-29 19:55:23 +00:00
var a = $('<a href="#">').html(composeTitle(fileDesc)).click(function() {
if(!liMap[fileDesc.fileIndex].is(".disabled")) {
fileMgr.selectFile(fileDesc);
}
2013-06-10 21:22:32 +00:00
else {
$("#wmd-input").focus();
}
2013-05-29 19:55:23 +00:00
});
var li = $("<li>").append(a);
liMap[fileDesc.fileIndex] = li;
2013-06-16 10:47:35 +00:00
if(fileDesc === selectFileDesc) {
li.addClass("disabled");
}
2013-05-29 19:55:23 +00:00
$("#file-selector").append(li);
});
2013-06-09 09:49:19 +00:00
liArray = _.values(liMap);
2013-05-29 19:55:23 +00:00
};
documentSelector.onFileSelected = function(fileDesc) {
2013-06-09 09:49:19 +00:00
selectFileDesc = fileDesc;
buildSelector();
2013-05-29 19:55:23 +00:00
};
documentSelector.onFileCreated = buildSelector;
documentSelector.onFileDeleted = buildSelector;
documentSelector.onTitleChanged = buildSelector;
documentSelector.onSyncExportSuccess = buildSelector;
documentSelector.onSyncRemoved = buildSelector;
documentSelector.onNewPublishSuccess = buildSelector;
documentSelector.onPublishRemoved = buildSelector;
2013-06-03 22:19:52 +00:00
2013-05-29 19:55:23 +00:00
// Filter for search input in file selector
function filterFileSelector(filter) {
var liList = $("#file-selector li:not(.stick)");
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() {
2013-06-10 21:22:32 +00:00
if(documentSelector.config.orderBy == "title") {
2013-06-09 09:49:19 +00:00
sortFunction = function(fileDesc) {
return fileDesc.title.toLowerCase();
};
}
2013-06-10 21:22:32 +00:00
else if(documentSelector.config.orderBy == "mru") {
2013-06-09 09:49:19 +00:00
sortFunction = function(fileDesc) {
return -fileDesc.selectTime;
};
}
2013-06-10 21:22:32 +00:00
var shortcutLi = undefined;
2013-05-29 19:55:23 +00:00
$(".action-open-file").click(function() {
2013-06-10 21:22:32 +00:00
if($("#file-selector").parent().is(".open")) {
2013-06-09 09:49:19 +00:00
return;
}
2013-05-29 19:55:23 +00:00
filterFileSelector();
2013-06-10 21:22:32 +00:00
if(shortcutLi !== undefined) {
2013-06-09 09:49:19 +00:00
return;
}
2013-05-29 19:55:23 +00:00
_.defer(function() {
$("#file-search").val("").focus();
});
2013-06-10 21:22:32 +00:00
}).prop("title", _.template("<%= title %> <%= shortcutPrevious %> <%= shortcutNext %>", {
title: $(".action-open-file").prop("title"),
shortcutPrevious: documentSelector.config.shortcutPrevious,
shortcutNext: documentSelector.config.shortcutNext
}));
2013-06-02 00:38:23 +00:00
$("#file-search").keyup(function(e) {
if(e.which == 13 || e.which == 27) {
$(this).parent().click();
}
else {
filterFileSelector($(this).val());
}
2013-05-29 19:55:23 +00:00
}).click(function(event) {
event.stopPropagation();
});
2013-06-09 09:49:19 +00:00
// Handle key shortcut
2013-06-22 23:48:57 +00:00
var shortcutPrevious = documentSelector.config.shortcutPrevious.toLowerCase();
mousetrap.bind(shortcutPrevious, function() {
2013-06-09 09:49:19 +00:00
if(shortcutLi === undefined) {
2013-06-10 21:22:32 +00:00
$("#file-selector").parent().is(".open") || $(".action-open-file").click();
2013-06-09 09:49:19 +00:00
shortcutLi = liMap[selectFileDesc.fileIndex];
}
var liIndex = _.indexOf(liArray, shortcutLi) - 1;
if(liIndex === -2) {
liIndex = -1;
}
shortcutLi = liArray[(liIndex + liArray.length) % liArray.length];
_.defer(function() {
shortcutLi.find("a").focus();
});
return false;
});
2013-06-22 23:48:57 +00:00
var shortcutNext = documentSelector.config.shortcutNext.toLowerCase();
2013-06-10 21:22:32 +00:00
mousetrap.bind(documentSelector.config.shortcutNext.toLowerCase(), function() {
2013-06-09 09:49:19 +00:00
if(shortcutLi === undefined) {
2013-06-10 21:22:32 +00:00
$("#file-selector").parent().is(".open") || $(".action-open-file").click();
2013-06-09 09:49:19 +00:00
shortcutLi = liMap[selectFileDesc.fileIndex];
}
var liIndex = _.indexOf(liArray, shortcutLi) + 1;
shortcutLi = liArray[liIndex % liArray.length];
_.defer(function() {
shortcutLi.find("a").focus();
});
return false;
});
2013-06-22 23:48:57 +00:00
var delimiter1 = shortcutPrevious.indexOf("+");
var shortcutSelect1 = delimiter1 === -1 ? shortcutPrevious : shortcutPrevious.substring(0, delimiter1);
var delimiter2 = shortcutNext.indexOf("+");
var shortcutSelect2 = delimiter2 === -1 ? shortcutNext : shortcutNext.substring(0, delimiter2);
mousetrap.bind([
shortcutSelect1,
shortcutSelect2
], function() {
2013-06-09 09:49:19 +00:00
if(shortcutLi !== undefined) {
shortcutLi.find("a").click();
shortcutLi = undefined;
2013-06-03 22:19:52 +00:00
}
2013-06-09 09:49:19 +00:00
}, "keyup");
2013-05-29 19:55:23 +00:00
};
return documentSelector;
2013-05-26 22:59:17 +00:00
});