trilium/public/javascripts/dialogs/add_link.js

115 lines
3.3 KiB
JavaScript
Raw Normal View History

"use strict";
2017-11-05 02:43:20 +08:00
const addLink = (function() {
2017-12-22 10:54:25 +08:00
const dialogEl = $("#add-link-dialog");
const formEl = $("#add-link-form");
2017-11-05 02:43:20 +08:00
const autoCompleteEl = $("#note-autocomplete");
const linkTitleEl = $("#link-title");
2017-12-22 10:54:25 +08:00
const clonePrefixEl = $("#clone-prefix");
const linkTitleFormGroup = $("#add-link-title-form-group");
const prefixFormGroup = $("#add-link-prefix-form-group");
2017-11-05 02:43:20 +08:00
function showDialog() {
2017-11-05 05:03:15 +08:00
glob.activeDialog = dialogEl;
2017-12-22 10:54:25 +08:00
$('input:radio[name="add-link-type"]').filter('[value="html"]').attr('checked', true);
linkTitleEl.val('');
clonePrefixEl.val('');
linkTitleFormGroup.show();
prefixFormGroup.hide();
2017-11-05 02:43:20 +08:00
dialogEl.dialog({
modal: true,
2017-12-22 10:54:25 +08:00
width: 700
2017-11-05 02:43:20 +08:00
});
2017-11-05 02:43:20 +08:00
autoCompleteEl.val('').focus();
linkTitleEl.val('');
2017-11-05 02:43:20 +08:00
function setDefaultLinkTitle(noteId) {
2017-12-03 02:54:16 +08:00
const noteTitle = noteTree.getNoteTitle(noteId);
2017-11-05 02:43:20 +08:00
linkTitleEl.val(noteTitle);
}
2017-11-05 02:43:20 +08:00
autoCompleteEl.autocomplete({
2017-11-20 08:39:39 +08:00
source: noteTree.getAutocompleteItems(),
2017-11-05 02:43:20 +08:00
minLength: 0,
change: () => {
const val = autoCompleteEl.val();
2017-12-03 02:54:16 +08:00
const notePath = link.getNodePathFromLabel(val);
const noteId = treeUtils.getNoteIdFromNotePath(notePath);
2017-11-05 02:43:20 +08:00
if (noteId) {
setDefaultLinkTitle(noteId);
}
},
// this is called when user goes through autocomplete list with keyboard
2017-11-21 12:51:28 +08:00
// at this point the item isn't selected yet so we use supplied ui.item to see WHERE the cursor is
2017-11-05 02:43:20 +08:00
focus: (event, ui) => {
2017-12-03 02:54:16 +08:00
const notePath = link.getNodePathFromLabel(ui.item.value);
const noteId = treeUtils.getNoteIdFromNotePath(notePath);
setDefaultLinkTitle(noteId);
}
2017-11-05 02:43:20 +08:00
});
}
2017-11-05 02:43:20 +08:00
formEl.submit(() => {
2017-11-20 11:31:30 +08:00
const value = autoCompleteEl.val();
2017-11-20 11:31:30 +08:00
const notePath = link.getNodePathFromLabel(value);
2017-12-22 10:54:25 +08:00
const noteId = treeUtils.getNoteIdFromNotePath(notePath);
2017-11-20 11:31:30 +08:00
if (notePath) {
2017-12-22 10:54:25 +08:00
const linkType = $("input[name='add-link-type']:checked").val();
if (linkType === 'html') {
const linkTitle = linkTitleEl.val();
dialogEl.dialog("close");
link.addLinkToEditor(linkTitle, '#' + notePath);
}
else if (linkType === 'selected-to-current') {
const prefix = clonePrefixEl.val();
2017-12-22 10:54:25 +08:00
treeChanges.cloneNoteTo(noteId, noteEditor.getCurrentNoteId(), prefix);
2017-12-22 10:54:25 +08:00
dialogEl.dialog("close");
}
else if (linkType === 'current-to-selected') {
const prefix = clonePrefixEl.val();
treeChanges.cloneNoteTo(noteEditor.getCurrentNoteId(), noteId, prefix);
dialogEl.dialog("close");
}
2017-11-05 02:43:20 +08:00
}
2017-11-05 02:43:20 +08:00
return false;
});
2017-12-22 10:54:25 +08:00
$("input[name='add-link-type']").change(function() {
if (this.value === 'html') {
linkTitleFormGroup.show();
prefixFormGroup.hide();
}
else {
linkTitleFormGroup.hide();
prefixFormGroup.show();
}
});
2017-12-19 12:41:13 +08:00
$(document).bind('keydown', 'ctrl+l', e => {
showDialog();
e.preventDefault();
});
2017-11-05 02:43:20 +08:00
return {
showDialog
};
})();