2013-05-27 19:45:33 +00:00
|
|
|
define([
|
|
|
|
"jquery",
|
2013-05-28 23:41:09 +00:00
|
|
|
"underscore",
|
2013-08-18 00:02:23 +00:00
|
|
|
"crel",
|
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",
|
2013-08-18 00:02:23 +00:00
|
|
|
], function($, _, crel, utils, Extension, mousetrap, fileSystem, documentSelectorSettingsBlockHTML) {
|
2013-05-26 22:59:17 +00:00
|
|
|
|
2013-08-21 00:16:10 +00:00
|
|
|
var documentSelector = new Extension("documentSelector", 'Document Selector', true, true);
|
2013-06-22 23:48:57 +00:00
|
|
|
documentSelector.settingsBlock = documentSelectorSettingsBlockHTML;
|
|
|
|
documentSelector.defaultConfig = {
|
2013-08-22 19:10:57 +00:00
|
|
|
orderBy: "mru",
|
2013-06-22 23:48:57 +00:00
|
|
|
shortcutPrevious: "Ctrl+[",
|
|
|
|
shortcutNext: "Ctrl+]"
|
2013-06-10 21:22:32 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
documentSelector.onLoadSettings = function() {
|
2013-07-24 23:20:56 +00:00
|
|
|
utils.setInputValue("#select-document-selector-orderby", documentSelector.config.orderBy);
|
2013-06-10 21:22:32 +00:00
|
|
|
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
|
|
|
};
|
|
|
|
|
2013-09-09 23:32:24 +00:00
|
|
|
var aceEditor = undefined;
|
|
|
|
documentSelector.onAceCreated = function(aceEditorParam) {
|
|
|
|
aceEditor = aceEditorParam;
|
|
|
|
};
|
|
|
|
|
2013-05-29 19:55:23 +00:00
|
|
|
var fileMgr = undefined;
|
|
|
|
documentSelector.onFileMgrCreated = function(fileMgrParameter) {
|
|
|
|
fileMgr = fileMgrParameter;
|
|
|
|
};
|
|
|
|
|
2013-08-18 00:02:23 +00:00
|
|
|
var liEltTmpl = [
|
|
|
|
'<li class="<%= isCurrent ? "disabled" : "" %>" data-file-index="<%= fileDesc.fileIndex %>">',
|
|
|
|
' <a href="#">',
|
|
|
|
' <%= fileDesc.composeTitle() %>',
|
|
|
|
' </a>',
|
|
|
|
'</li>'
|
|
|
|
].join('');
|
2013-08-10 11:34:30 +00:00
|
|
|
var dropdownElt = undefined;
|
2013-08-18 00:02:23 +00:00
|
|
|
var liEltMap = undefined;
|
|
|
|
var liEltList = undefined;
|
2013-06-09 09:49:19 +00:00
|
|
|
var sortFunction = undefined;
|
2013-06-16 10:47:35 +00:00
|
|
|
var selectFileDesc = undefined;
|
2013-08-20 22:40:19 +00:00
|
|
|
var selectedLi = undefined;
|
2013-09-12 23:25:25 +00:00
|
|
|
var $editorElt = undefined;
|
2013-05-29 19:55:23 +00:00
|
|
|
var buildSelector = function() {
|
2013-08-18 00:02:23 +00:00
|
|
|
var liListHtml = _.chain(fileSystem).sortBy(sortFunction).reduce(function(result, fileDesc) {
|
|
|
|
return result + _.template(liEltTmpl, {
|
|
|
|
fileDesc: fileDesc,
|
|
|
|
isCurrent: fileDesc === selectFileDesc
|
|
|
|
});
|
|
|
|
}, '').value();
|
|
|
|
dropdownElt.innerHTML = liListHtml;
|
2013-05-29 19:55:23 +00:00
|
|
|
|
2013-08-18 00:02:23 +00:00
|
|
|
liEltList = [];
|
|
|
|
liEltMap = {};
|
|
|
|
_.each(dropdownElt.querySelectorAll('li'), function(liElt) {
|
|
|
|
var $liElt = $(liElt);
|
|
|
|
liEltList.push($liElt);
|
|
|
|
var fileDesc = fileSystem[$liElt.data('fileIndex')];
|
|
|
|
liEltMap[fileDesc.fileIndex] = $liElt;
|
|
|
|
$liElt.find("a").click(function() {
|
2013-08-20 22:40:19 +00:00
|
|
|
selectedLi = undefined;
|
2013-08-18 00:02:23 +00:00
|
|
|
if(!$liElt.hasClass("disabled")) {
|
2013-05-29 19:55:23 +00:00
|
|
|
fileMgr.selectFile(fileDesc);
|
|
|
|
}
|
2013-09-12 23:25:25 +00:00
|
|
|
else if(aceEditor !== undefined) {
|
2013-09-09 23:32:24 +00:00
|
|
|
aceEditor.focus();
|
2013-06-10 21:22:32 +00:00
|
|
|
}
|
2013-09-12 23:25:25 +00:00
|
|
|
else {
|
|
|
|
$editorElt.focus();
|
|
|
|
}
|
2013-05-29 19:55:23 +00:00
|
|
|
});
|
|
|
|
});
|
2013-08-18 00:02:23 +00:00
|
|
|
|
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
|
|
|
documentSelector.onReady = function() {
|
2013-09-12 23:25:25 +00:00
|
|
|
$editorElt = $('#wmd-input');
|
|
|
|
|
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-08-18 00:02:23 +00:00
|
|
|
dropdownElt = crel('ul', {
|
|
|
|
class: 'dropdown-menu dropdown-file-selector'
|
2013-05-29 19:55:23 +00:00
|
|
|
});
|
2013-08-18 00:02:23 +00:00
|
|
|
document.querySelector('.ui-layout-resizer-north').appendChild(crel('div', crel('div', {
|
|
|
|
'data-toggle': 'dropdown'
|
|
|
|
}), dropdownElt));
|
|
|
|
var $dropdownElt = $(dropdownElt).dropdown();
|
|
|
|
|
2013-08-22 00:19:59 +00:00
|
|
|
var $documentPanelTogglerElt = $('.document-panel .collapse-button');
|
|
|
|
$documentPanelTogglerElt.prop("title", _.template("<%= title %> <%= shortcutPrevious %> <%= shortcutNext %>", {
|
|
|
|
title: $documentPanelTogglerElt.prop("title"),
|
2013-08-10 11:34:30 +00:00
|
|
|
shortcutPrevious: documentSelector.config.shortcutPrevious,
|
|
|
|
shortcutNext: documentSelector.config.shortcutNext
|
|
|
|
}));
|
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-09-14 16:59:40 +00:00
|
|
|
console.log('keydown')
|
2013-08-20 22:40:19 +00:00
|
|
|
if(selectedLi === undefined) {
|
2013-08-18 00:02:23 +00:00
|
|
|
$dropdownElt.dropdown('toggle');
|
2013-08-20 22:40:19 +00:00
|
|
|
selectedLi = liEltMap[selectFileDesc.fileIndex];
|
2013-06-09 09:49:19 +00:00
|
|
|
}
|
2013-08-20 22:40:19 +00:00
|
|
|
var liIndex = _.indexOf(liEltList, selectedLi) - 1;
|
2013-06-09 09:49:19 +00:00
|
|
|
if(liIndex === -2) {
|
|
|
|
liIndex = -1;
|
|
|
|
}
|
2013-08-20 22:40:19 +00:00
|
|
|
selectedLi = liEltList[(liIndex + liEltList.length) % liEltList.length];
|
2013-06-09 09:49:19 +00:00
|
|
|
_.defer(function() {
|
2013-08-20 22:40:19 +00:00
|
|
|
selectedLi.find("a").focus();
|
2013-06-09 09:49:19 +00:00
|
|
|
});
|
|
|
|
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-09-14 16:59:40 +00:00
|
|
|
console.log('keydown')
|
2013-08-20 22:40:19 +00:00
|
|
|
if(selectedLi === undefined) {
|
2013-08-18 00:02:23 +00:00
|
|
|
$dropdownElt.dropdown('toggle');
|
2013-08-20 22:40:19 +00:00
|
|
|
selectedLi = liEltMap[selectFileDesc.fileIndex];
|
2013-06-09 09:49:19 +00:00
|
|
|
}
|
2013-08-20 22:40:19 +00:00
|
|
|
var liIndex = _.indexOf(liEltList, selectedLi) + 1;
|
|
|
|
selectedLi = liEltList[liIndex % liEltList.length];
|
2013-06-09 09:49:19 +00:00
|
|
|
_.defer(function() {
|
2013-08-20 22:40:19 +00:00
|
|
|
selectedLi.find("a").focus();
|
2013-06-09 09:49:19 +00:00
|
|
|
});
|
|
|
|
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-09-14 16:59:40 +00:00
|
|
|
console.log('keyup')
|
2013-08-20 22:40:19 +00:00
|
|
|
if(selectedLi !== undefined) {
|
|
|
|
selectedLi.find("a").click();
|
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
|
|
|
});
|