trilium/src/public/javascripts/dialogs/add_link.js

137 lines
3.9 KiB
JavaScript
Raw Normal View History

"use strict";
2017-11-05 02:43:20 +08:00
const addLink = (function() {
const $dialog = $("#add-link-dialog");
const $form = $("#add-link-form");
const $autoComplete = $("#note-autocomplete");
const $linkTitle = $("#link-title");
const $clonePrefix = $("#clone-prefix");
const $linkTitleFormGroup = $("#add-link-title-form-group");
const $prefixFormGroup = $("#add-link-prefix-form-group");
const $linkTypes = $("input[name='add-link-type']");
const $linkTypeHtml = $linkTypes.filter('input[value="html"]');
function setLinkType(linkType) {
$linkTypes.each(function () {
$(this).prop('checked', $(this).val() === linkType);
});
linkTypeChanged();
}
2017-11-05 02:43:20 +08:00
function showDialog() {
glob.activeDialog = $dialog;
2017-11-05 05:03:15 +08:00
if (noteEditor.getCurrentNoteType() === 'text') {
$linkTypeHtml.prop('disabled', false);
2017-12-22 10:54:25 +08:00
setLinkType('html');
}
else {
$linkTypeHtml.prop('disabled', true);
2017-12-22 10:54:25 +08:00
setLinkType('selected-to-current');
}
2017-12-22 10:54:25 +08:00
$dialog.dialog({
2017-11-05 02:43:20 +08:00
modal: true,
2017-12-22 10:54:25 +08:00
width: 700
2017-11-05 02:43:20 +08:00
});
$autoComplete.val('').focus();
$clonePrefix.val('');
$linkTitle.val('');
2017-11-05 02:43:20 +08:00
function setDefaultLinkTitle(noteId) {
2018-03-25 09:39:15 +08:00
const noteTitle = treeService.getNoteTitle(noteId);
$linkTitle.val(noteTitle);
2017-11-05 02:43:20 +08:00
}
$autoComplete.autocomplete({
2018-03-25 09:39:15 +08:00
source: treeService.getAutocompleteItems(),
2017-11-05 02:43:20 +08:00
minLength: 0,
change: () => {
const val = $autoComplete.val();
2017-12-03 02:54:16 +08:00
const notePath = link.getNodePathFromLabel(val);
if (!notePath) {
return;
}
2017-12-03 02:54:16 +08:00
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
});
}
$form.submit(() => {
const value = $autoComplete.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 = $linkTitle.val();
2017-12-22 10:54:25 +08:00
$dialog.dialog("close");
2017-12-22 10:54:25 +08:00
link.addLinkToEditor(linkTitle, '#' + notePath);
}
else if (linkType === 'selected-to-current') {
const prefix = $clonePrefix.val();
2018-01-14 07:02:41 +08:00
cloning.cloneNoteTo(noteId, noteEditor.getCurrentNoteId(), prefix);
$dialog.dialog("close");
2017-12-22 10:54:25 +08:00
}
else if (linkType === 'current-to-selected') {
const prefix = $clonePrefix.val();
2017-12-22 10:54:25 +08:00
2018-01-14 07:02:41 +08:00
cloning.cloneNoteTo(noteEditor.getCurrentNoteId(), noteId, prefix);
2017-12-22 10:54:25 +08:00
$dialog.dialog("close");
2017-12-22 10:54:25 +08:00
}
2017-11-05 02:43:20 +08:00
}
2017-11-05 02:43:20 +08:00
return false;
});
function linkTypeChanged() {
const value = $linkTypes.filter(":checked").val();
if (value === 'html') {
$linkTitleFormGroup.show();
$prefixFormGroup.hide();
2017-12-22 10:54:25 +08:00
}
else {
$linkTitleFormGroup.hide();
$prefixFormGroup.show();
2017-12-22 10:54:25 +08:00
}
}
$linkTypes.change(linkTypeChanged);
2017-12-22 10:54:25 +08:00
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
};
})();