2020-01-25 09:56:08 +01:00
|
|
|
import treeService from '../services/tree.js';
|
2018-08-16 21:02:42 +02:00
|
|
|
import noteAutocompleteService from "../services/note_autocomplete.js";
|
2019-06-10 22:45:03 +02:00
|
|
|
import utils from "../services/utils.js";
|
2018-03-25 11:09:17 -04:00
|
|
|
|
|
|
|
const $dialog = $("#add-link-dialog");
|
|
|
|
const $form = $("#add-link-form");
|
2019-11-05 23:11:25 +01:00
|
|
|
const $autoComplete = $("#add-link-note-autocomplete");
|
2018-03-25 11:09:17 -04:00
|
|
|
const $linkTitle = $("#link-title");
|
2020-03-22 11:41:00 +01:00
|
|
|
const $addLinkTitleSettings = $("#add-link-title-settings");
|
2019-11-05 22:40:44 +01:00
|
|
|
const $addLinkTitleFormGroup = $("#add-link-title-form-group");
|
2017-11-04 17:03:15 -04:00
|
|
|
|
2020-02-15 22:12:05 +01:00
|
|
|
/** @var TextTypeWidget */
|
|
|
|
let textTypeWidget;
|
2020-02-02 11:14:44 +01:00
|
|
|
|
2020-02-15 22:12:05 +01:00
|
|
|
export async function showDialog(widget) {
|
|
|
|
textTypeWidget = widget;
|
|
|
|
|
2020-03-22 11:41:00 +01:00
|
|
|
$addLinkTitleSettings.toggle(!textTypeWidget.hasSelection());
|
|
|
|
|
|
|
|
updateTitleFormGroupVisibility();
|
|
|
|
$addLinkTitleSettings.find('input[type=radio]').on('change', updateTitleFormGroupVisibility);
|
2017-12-21 21:54:25 -05:00
|
|
|
|
2020-04-11 09:47:19 +02:00
|
|
|
// with selection hyper link is implied
|
|
|
|
if (textTypeWidget.hasSelection()) {
|
|
|
|
$addLinkTitleSettings.find("input[value='hyper-link']").prop("checked", true);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$addLinkTitleSettings.find("input[value='reference-link']").prop("checked", true);
|
|
|
|
}
|
|
|
|
|
2020-02-09 10:00:13 +01:00
|
|
|
utils.openDialog($dialog);
|
2017-09-09 12:06:15 -04:00
|
|
|
|
2019-11-09 17:45:22 +01:00
|
|
|
$autoComplete.val('').trigger('focus');
|
2018-03-25 11:09:17 -04:00
|
|
|
$linkTitle.val('');
|
2017-09-09 12:06:15 -04:00
|
|
|
|
2018-04-12 20:03:23 -04:00
|
|
|
async function setDefaultLinkTitle(noteId) {
|
2020-01-25 09:56:08 +01:00
|
|
|
const noteTitle = await treeService.getNoteTitle(noteId);
|
2018-03-25 11:09:17 -04:00
|
|
|
|
|
|
|
$linkTitle.val(noteTitle);
|
|
|
|
}
|
2017-09-09 12:06:15 -04:00
|
|
|
|
2018-11-07 17:16:33 +01:00
|
|
|
noteAutocompleteService.initNoteAutocomplete($autoComplete);
|
2018-07-26 16:24:08 +02:00
|
|
|
|
2018-11-07 17:16:33 +01:00
|
|
|
$autoComplete.on('autocomplete:selected', function(event, suggestion, dataset) {
|
|
|
|
if (!suggestion.path) {
|
|
|
|
return false;
|
|
|
|
}
|
2018-03-25 11:09:17 -04:00
|
|
|
|
2020-01-25 09:56:08 +01:00
|
|
|
const noteId = treeService.getNoteIdFromNotePath(suggestion.path);
|
2017-09-09 12:06:15 -04:00
|
|
|
|
2018-11-07 17:16:33 +01:00
|
|
|
if (noteId) {
|
|
|
|
setDefaultLinkTitle(noteId);
|
|
|
|
}
|
|
|
|
});
|
2018-03-25 11:09:17 -04:00
|
|
|
|
2018-11-07 17:16:33 +01:00
|
|
|
$autoComplete.on('autocomplete:cursorchanged', function(event, suggestion, dataset) {
|
2020-01-25 09:56:08 +01:00
|
|
|
const noteId = treeService.getNoteIdFromNotePath(suggestion.path);
|
2018-07-26 16:24:08 +02:00
|
|
|
|
2020-03-31 21:47:15 +02:00
|
|
|
if (noteId) {
|
|
|
|
setDefaultLinkTitle(noteId);
|
|
|
|
}
|
2018-03-25 11:09:17 -04:00
|
|
|
});
|
2018-07-26 16:24:08 +02:00
|
|
|
|
2018-11-07 17:16:33 +01:00
|
|
|
noteAutocompleteService.showRecentNotes($autoComplete);
|
2018-03-25 11:09:17 -04:00
|
|
|
}
|
2017-09-09 12:06:15 -04:00
|
|
|
|
2020-03-22 11:41:00 +01:00
|
|
|
function getLinkType() {
|
|
|
|
return $addLinkTitleSettings.find('input[type=radio]:checked').val();
|
|
|
|
}
|
|
|
|
|
|
|
|
function updateTitleFormGroupVisibility() {
|
|
|
|
const visible = getLinkType() === 'hyper-link';
|
|
|
|
|
|
|
|
$addLinkTitleFormGroup.toggle(visible);
|
|
|
|
}
|
|
|
|
|
2019-11-09 17:45:22 +01:00
|
|
|
$form.on('submit', () => {
|
2018-11-07 17:16:33 +01:00
|
|
|
const notePath = $autoComplete.getSelectedPath();
|
2017-09-09 12:06:15 -04:00
|
|
|
|
2018-03-25 11:09:17 -04:00
|
|
|
if (notePath) {
|
2019-11-05 22:40:44 +01:00
|
|
|
$dialog.modal('hide');
|
2017-12-21 21:54:25 -05:00
|
|
|
|
2020-03-22 11:41:00 +01:00
|
|
|
const linkTitle = getLinkType() === 'reference-link' ? null : $linkTitle.val();
|
|
|
|
|
|
|
|
textTypeWidget.addLink(notePath, linkTitle);
|
2018-03-25 11:09:17 -04:00
|
|
|
}
|
2018-11-07 17:16:33 +01:00
|
|
|
else {
|
|
|
|
console.error("No path to add link.");
|
|
|
|
}
|
2017-09-09 12:06:15 -04:00
|
|
|
|
2018-03-25 11:09:17 -04:00
|
|
|
return false;
|
2020-02-02 10:41:43 +01:00
|
|
|
});
|