add link converted to module

This commit is contained in:
azivner 2017-11-04 14:43:20 -04:00
parent 979acc8b4a
commit c723bfc3ac

View file

@ -1,26 +1,32 @@
$(document).bind('keydown', 'alt+l', () => {
$("#note-autocomplete").val('');
$("#link-title").val('');
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");
const noteDetail = $('#note-detail');
noteDetail.summernote('editor.saveRange');
function showDialog() {
noteDetailEl.summernote('editor.saveRange');
$("#insert-link-dialog").dialog({
dialogEl.dialog({
modal: true,
width: 500
});
autoCompleteEl.val('').focus();
linkTitleEl.val('');
function setDefaultLinkTitle(noteId) {
const noteTitle = getNoteTitle(noteId);
$("#link-title").val(noteTitle);
linkTitleEl.val(noteTitle);
}
$("#note-autocomplete").autocomplete({
autoCompleteEl.autocomplete({
source: getAutocompleteItems(glob.allNoteIds),
minLength: 0,
change: () => {
const val = $("#note-autocomplete").val();
const val = autoCompleteEl.val();
const noteId = getNodeIdFromLabel(val);
if (noteId) {
@ -35,22 +41,21 @@ $(document).bind('keydown', 'alt+l', () => {
setDefaultLinkTitle(noteId);
}
});
});
}
$("#insert-link-form").submit(() => {
let val = $("#note-autocomplete").val();
formEl.submit(() => {
let val = autoCompleteEl.val();
const noteId = getNodeIdFromLabel(val);
if (noteId) {
const linkTitle = $("#link-title").val();
const noteDetail = $('#note-detail');
const linkTitle = linkTitleEl.val();
$("#insert-link-dialog").dialog("close");
dialogEl.dialog("close");
noteDetail.summernote('editor.restoreRange');
noteDetailEl.summernote('editor.restoreRange');
noteDetail.summernote('createLink', {
noteDetailEl.summernote('createLink', {
text: linkTitle,
url: 'app#' + noteId,
isNewWindow: true
@ -58,14 +63,14 @@ $("#insert-link-form").submit(() => {
}
return false;
});
});
// 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);
// 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);
function goToInternalNote(e, callback) {
function goToInternalNote(e, callback) {
const targetUrl = $(e.target).attr("href");
const noteId = getNoteIdFromLink(targetUrl);
@ -82,9 +87,9 @@ function goToInternalNote(e, callback) {
callback();
}
}
}
}
function getNoteIdFromLink(url) {
function getNoteIdFromLink(url) {
const noteIdMatch = /app#([A-Za-z0-9]{12})/.exec(url);
if (noteIdMatch === null) {
@ -93,9 +98,9 @@ function getNoteIdFromLink(url) {
else {
return noteIdMatch[1];
}
}
}
function getNodeIdFromLabel(label) {
function getNodeIdFromLabel(label) {
const noteIdMatch = / \(([A-Za-z0-9]{12})\)/.exec(label);
if (noteIdMatch !== null) {
@ -103,4 +108,11 @@ function getNodeIdFromLabel(label) {
}
return null;
}
}
$(document).bind('keydown', 'alt+l', showDialog);
return {
showDialog
};
})();