118 lines
3.2 KiB
JavaScript
Raw Normal View History

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() {
noteDetailEl.summernote('editor.saveRange');
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) {
const noteTitle = getNoteTitle(noteId);
2017-11-04 14:43:20 -04:00
linkTitleEl.val(noteTitle);
}
2017-11-04 14:43:20 -04:00
autoCompleteEl.autocomplete({
source: getAutocompleteItems(glob.allNoteIds),
minLength: 0,
change: () => {
const val = autoCompleteEl.val();
const noteId = getNodeIdFromLabel(val);
if (noteId) {
setDefaultLinkTitle(noteId);
}
},
// this is called when user goes through autocomplete list with keyboard
// at this point the item isn't selected yet so we use supplied ui.item to see where the cursor is
focus: (event, ui) => {
const noteId = getNodeIdFromLabel(ui.item.value);
setDefaultLinkTitle(noteId);
}
2017-11-04 14:43:20 -04:00
});
}
2017-11-04 14:43:20 -04:00
formEl.submit(() => {
let val = autoCompleteEl.val();
2017-11-04 14:43:20 -04:00
const noteId = getNodeIdFromLabel(val);
2017-11-04 14:43:20 -04:00
if (noteId) {
const linkTitle = linkTitleEl.val();
2017-11-04 14:43:20 -04:00
dialogEl.dialog("close");
2017-11-04 14:43:20 -04:00
noteDetailEl.summernote('editor.restoreRange');
2017-11-04 14:43:20 -04:00
noteDetailEl.summernote('createLink', {
text: linkTitle,
url: 'app#' + noteId,
isNewWindow: true
});
}
2017-11-04 14:43:20 -04:00
return false;
});
2017-11-04 14:43:20 -04:00
// when click on link popup, in case of internal link, just go the the referenced note instead of default behavior
// of opening the link in new window/tab
$(document).on('click', 'div.popover-content a, div.ui-tooltip-content', goToInternalNote);
$(document).on('dblclick', '.note-editable a, div.ui-tooltip-content', goToInternalNote);
2017-11-04 14:43:20 -04:00
function goToInternalNote(e, callback) {
const targetUrl = $(e.target).attr("href");
2017-11-04 14:43:20 -04:00
const noteId = getNoteIdFromLink(targetUrl);
2017-11-04 14:43:20 -04:00
if (noteId !== null) {
getNodeByKey(noteId).setActive();
2017-11-04 14:43:20 -04:00
// this is quite ugly hack, but it seems like we can't close the tooltip otherwise
$("[role='tooltip']").remove();
2017-11-04 14:43:20 -04:00
e.preventDefault();
2017-09-26 23:23:03 -04:00
2017-11-04 14:43:20 -04:00
if (callback) {
callback();
}
2017-09-26 23:23:03 -04:00
}
}
2017-11-04 14:43:20 -04:00
function getNoteIdFromLink(url) {
const noteIdMatch = /app#([A-Za-z0-9]{12})/.exec(url);
2017-11-04 14:43:20 -04:00
if (noteIdMatch === null) {
return null;
}
else {
return noteIdMatch[1];
}
}
2017-11-04 14:43:20 -04:00
function getNodeIdFromLabel(label) {
const noteIdMatch = / \(([A-Za-z0-9]{12})\)/.exec(label);
if (noteIdMatch !== null) {
return noteIdMatch[1];
}
2017-11-04 14:43:20 -04:00
return null;
}
2017-11-04 14:43:20 -04:00
$(document).bind('keydown', 'alt+l', showDialog);
return {
showDialog
};
})();