2018-03-26 01:41:29 +08:00
|
|
|
import treeService from '../services/tree.js';
|
|
|
|
import cloningService from '../services/cloning.js';
|
|
|
|
import linkService from '../services/link.js';
|
|
|
|
import noteDetailService from '../services/note_detail.js';
|
2018-03-26 01:02:39 +08:00
|
|
|
import treeUtils from '../services/tree_utils.js';
|
2018-03-25 23:09:17 +08:00
|
|
|
|
|
|
|
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);
|
|
|
|
});
|
2017-11-05 02:43:20 +08:00
|
|
|
|
2018-03-25 23:09:17 +08:00
|
|
|
linkTypeChanged();
|
|
|
|
}
|
2017-11-05 05:03:15 +08:00
|
|
|
|
2018-03-25 23:09:17 +08:00
|
|
|
async function showDialog() {
|
|
|
|
glob.activeDialog = $dialog;
|
2017-12-22 10:54:25 +08:00
|
|
|
|
2018-03-26 01:41:29 +08:00
|
|
|
if (noteDetailService.getCurrentNoteType() === 'text') {
|
2018-03-25 23:09:17 +08:00
|
|
|
$linkTypeHtml.prop('disabled', false);
|
2017-12-22 10:54:25 +08:00
|
|
|
|
2018-03-25 23:09:17 +08:00
|
|
|
setLinkType('html');
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$linkTypeHtml.prop('disabled', true);
|
2017-12-22 10:54:25 +08:00
|
|
|
|
2018-03-25 23:09:17 +08:00
|
|
|
setLinkType('selected-to-current');
|
|
|
|
}
|
2017-09-10 00:06:15 +08:00
|
|
|
|
2018-03-25 23:09:17 +08:00
|
|
|
$dialog.dialog({
|
|
|
|
modal: true,
|
|
|
|
width: 700
|
|
|
|
});
|
2017-09-10 00:06:15 +08:00
|
|
|
|
2018-03-25 23:09:17 +08:00
|
|
|
$autoComplete.val('').focus();
|
|
|
|
$clonePrefix.val('');
|
|
|
|
$linkTitle.val('');
|
2017-09-10 00:06:15 +08:00
|
|
|
|
2018-03-25 23:09:17 +08:00
|
|
|
function setDefaultLinkTitle(noteId) {
|
|
|
|
const noteTitle = treeService.getNoteTitle(noteId);
|
|
|
|
|
|
|
|
$linkTitle.val(noteTitle);
|
|
|
|
}
|
2017-09-10 00:06:15 +08:00
|
|
|
|
2018-03-25 23:09:17 +08:00
|
|
|
$autoComplete.autocomplete({
|
|
|
|
source: await treeService.getAutocompleteItems(),
|
|
|
|
minLength: 0,
|
|
|
|
change: () => {
|
|
|
|
const val = $autoComplete.val();
|
2018-03-26 01:41:29 +08:00
|
|
|
const notePath = linkService.getNodePathFromLabel(val);
|
2018-03-25 23:09:17 +08:00
|
|
|
if (!notePath) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const noteId = treeUtils.getNoteIdFromNotePath(notePath);
|
2017-09-10 00:06:15 +08:00
|
|
|
|
2018-03-25 23:09:17 +08:00
|
|
|
if (noteId) {
|
2017-10-12 09:20:05 +08:00
|
|
|
setDefaultLinkTitle(noteId);
|
2017-09-10 00:06:15 +08:00
|
|
|
}
|
2018-03-25 23:09:17 +08:00
|
|
|
},
|
|
|
|
// 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) => {
|
2018-03-26 01:41:29 +08:00
|
|
|
const notePath = linkService.getNodePathFromLabel(ui.item.value);
|
2018-03-25 23:09:17 +08:00
|
|
|
const noteId = treeUtils.getNoteIdFromNotePath(notePath);
|
|
|
|
|
|
|
|
setDefaultLinkTitle(noteId);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2017-09-10 00:06:15 +08:00
|
|
|
|
2018-03-25 23:09:17 +08:00
|
|
|
$form.submit(() => {
|
|
|
|
const value = $autoComplete.val();
|
2017-09-10 00:06:15 +08:00
|
|
|
|
2018-03-26 01:41:29 +08:00
|
|
|
const notePath = linkService.getNodePathFromLabel(value);
|
2018-03-25 23:09:17 +08:00
|
|
|
const noteId = treeUtils.getNoteIdFromNotePath(notePath);
|
2017-09-10 00:06:15 +08:00
|
|
|
|
2018-03-25 23:09:17 +08:00
|
|
|
if (notePath) {
|
|
|
|
const linkType = $("input[name='add-link-type']:checked").val();
|
2017-12-22 10:54:25 +08:00
|
|
|
|
2018-03-25 23:09:17 +08:00
|
|
|
if (linkType === 'html') {
|
|
|
|
const linkTitle = $linkTitle.val();
|
2017-12-22 10:54:25 +08:00
|
|
|
|
2018-03-25 23:09:17 +08:00
|
|
|
$dialog.dialog("close");
|
2017-12-22 10:54:25 +08:00
|
|
|
|
2018-03-26 01:41:29 +08:00
|
|
|
linkService.addLinkToEditor(linkTitle, '#' + notePath);
|
2018-03-25 23:09:17 +08:00
|
|
|
}
|
|
|
|
else if (linkType === 'selected-to-current') {
|
|
|
|
const prefix = $clonePrefix.val();
|
2017-09-10 00:06:15 +08:00
|
|
|
|
2018-03-26 01:41:29 +08:00
|
|
|
cloningService.cloneNoteTo(noteId, noteDetailService.getCurrentNoteId(), prefix);
|
2017-09-10 00:06:15 +08:00
|
|
|
|
2018-03-25 23:09:17 +08:00
|
|
|
$dialog.dialog("close");
|
|
|
|
}
|
|
|
|
else if (linkType === 'current-to-selected') {
|
|
|
|
const prefix = $clonePrefix.val();
|
2017-12-22 10:54:25 +08:00
|
|
|
|
2018-03-26 01:41:29 +08:00
|
|
|
cloningService.cloneNoteTo(noteDetailService.getCurrentNoteId(), noteId, prefix);
|
2017-12-22 10:54:25 +08:00
|
|
|
|
2018-03-25 23:09:17 +08:00
|
|
|
$dialog.dialog("close");
|
2017-11-05 02:43:20 +08:00
|
|
|
}
|
2018-03-25 23:09:17 +08:00
|
|
|
}
|
2017-09-10 00:06:15 +08:00
|
|
|
|
2018-03-25 23:09:17 +08:00
|
|
|
return false;
|
|
|
|
});
|
2017-09-10 00:06:15 +08:00
|
|
|
|
2018-03-25 23:09:17 +08:00
|
|
|
function linkTypeChanged() {
|
|
|
|
const value = $linkTypes.filter(":checked").val();
|
2018-01-24 09:14:10 +08:00
|
|
|
|
2018-03-25 23:09:17 +08:00
|
|
|
if (value === 'html') {
|
|
|
|
$linkTitleFormGroup.show();
|
|
|
|
$prefixFormGroup.hide();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$linkTitleFormGroup.hide();
|
|
|
|
$prefixFormGroup.show();
|
2018-01-24 09:14:10 +08:00
|
|
|
}
|
2018-03-25 23:09:17 +08:00
|
|
|
}
|
2018-01-24 09:14:10 +08:00
|
|
|
|
2018-03-25 23:09:17 +08:00
|
|
|
$linkTypes.change(linkTypeChanged);
|
2017-12-22 10:54:25 +08:00
|
|
|
|
2018-03-25 23:09:17 +08:00
|
|
|
export default {
|
|
|
|
showDialog
|
|
|
|
};
|