86 lines
2.5 KiB
JavaScript
Raw Normal View History

"use strict";
2017-11-04 14:43:20 -04:00
const addLink = (function() {
const dialogEl = $("#insert-link-dialog");
const formEl = $("#insert-link-form");
const autoCompleteEl = $("#note-autocomplete");
const noteDetailEl = $('#note-detail');
const linkTitleEl = $("#link-title");
function showDialog() {
2017-11-04 17:03:15 -04:00
glob.activeDialog = dialogEl;
2017-11-04 14:43:20 -04:00
dialogEl.dialog({
modal: true,
width: 500
});
2017-11-04 14:43:20 -04:00
autoCompleteEl.val('').focus();
linkTitleEl.val('');
2017-11-04 14:43:20 -04:00
function setDefaultLinkTitle(noteId) {
2017-12-02 13:54:16 -05:00
const noteTitle = noteTree.getNoteTitle(noteId);
2017-11-04 14:43:20 -04:00
linkTitleEl.val(noteTitle);
}
2017-11-04 14:43:20 -04:00
autoCompleteEl.autocomplete({
2017-11-19 19:39:39 -05:00
source: noteTree.getAutocompleteItems(),
2017-11-04 14:43:20 -04:00
minLength: 0,
change: () => {
const val = autoCompleteEl.val();
2017-12-02 13:54:16 -05:00
const notePath = link.getNodePathFromLabel(val);
const noteId = treeUtils.getNoteIdFromNotePath(notePath);
2017-11-04 14:43:20 -04:00
if (noteId) {
setDefaultLinkTitle(noteId);
}
},
// this is called when user goes through autocomplete list with keyboard
2017-11-20 23:51:28 -05:00
// at this point the item isn't selected yet so we use supplied ui.item to see WHERE the cursor is
2017-11-04 14:43:20 -04:00
focus: (event, ui) => {
2017-12-02 13:54:16 -05:00
const notePath = link.getNodePathFromLabel(ui.item.value);
const noteId = treeUtils.getNoteIdFromNotePath(notePath);
setDefaultLinkTitle(noteId);
}
2017-11-04 14:43:20 -04:00
});
}
2017-11-04 14:43:20 -04:00
formEl.submit(() => {
2017-11-19 22:31:30 -05:00
const value = autoCompleteEl.val();
2017-11-19 22:31:30 -05:00
const notePath = link.getNodePathFromLabel(value);
2017-11-19 22:31:30 -05:00
if (notePath) {
2017-11-04 14:43:20 -04:00
const linkTitle = linkTitleEl.val();
2017-11-04 14:43:20 -04:00
dialogEl.dialog("close");
2017-12-02 13:54:16 -05:00
const editor = noteEditor.getEditor();
const doc = editor.document;
// doc.enqueueChanges( () => {
// // const link = '<a href="#' + notePath + '" target="_blank">' + linkTitle + '</a>';
//
// editor.data.insertContent({
// linkHref: '#' + notePath
// }, doc.selection);
// } );
// noteDetailEl.summernote('createLink', {
// text: linkTitle,
// url: 'app#' + notePath,
// isNewWindow: true
// });
2017-11-04 14:43:20 -04:00
}
2017-11-04 14:43:20 -04:00
return false;
});
2017-11-04 14:43:20 -04:00
$(document).bind('keydown', 'alt+l', showDialog);
return {
showDialog
};
})();