From 8e4e0bd543cb1b8f02f3502327e6fc2e44f0f0aa Mon Sep 17 00:00:00 2001 From: zadam Date: Sun, 25 Aug 2019 22:31:02 +0200 Subject: [PATCH] fix global ko references --- src/public/javascripts/desktop.js | 2 -- src/public/javascripts/dialogs/attributes.js | 36 +++++++++++++++++-- src/public/javascripts/services/link.js | 15 -------- .../javascripts/services/note_autocomplete.js | 12 ------- 4 files changed, 33 insertions(+), 32 deletions(-) diff --git a/src/public/javascripts/desktop.js b/src/public/javascripts/desktop.js index 0748bf2c0..f17740ca9 100644 --- a/src/public/javascripts/desktop.js +++ b/src/public/javascripts/desktop.js @@ -168,6 +168,4 @@ entrypoints.registerEntrypoints(); noteTooltipService.setupGlobalTooltip(); -linkService.init(); - noteAutocompleteService.init(); diff --git a/src/public/javascripts/dialogs/attributes.js b/src/public/javascripts/dialogs/attributes.js index 51246041c..ee52f3ea6 100644 --- a/src/public/javascripts/dialogs/attributes.js +++ b/src/public/javascripts/dialogs/attributes.js @@ -4,7 +4,9 @@ import infoService from "../services/info.js"; import treeUtils from "../services/tree_utils.js"; import attributeAutocompleteService from "../services/attribute_autocomplete.js"; import utils from "../services/utils.js"; +import linkService from "../services/link.js"; import libraryLoader from "../services/library_loader.js"; +import noteAutocompleteService from "../services/note_autocomplete.js"; const $dialog = $("#attributes-dialog"); const $saveAttributesButton = $("#save-attributes-button"); @@ -255,15 +257,43 @@ function AttributesModel() { let attributesModel; +function initKoPlugins() { + ko.bindingHandlers.noteLink = { + init: async function (element, valueAccessor, allBindings, viewModel, bindingContext) { + const noteId = ko.unwrap(valueAccessor()); + + if (noteId) { + const link = await linkService.createNoteLink(noteId); + + $(element).append(link); + } + } + }; + + ko.bindingHandlers.noteAutocomplete = { + init: function (element, valueAccessor, allBindings, viewModel, bindingContext) { + noteAutocompleteService.initNoteAutocomplete($(element)); + + $(element).setSelectedPath(bindingContext.$data.selectedPath); + + $(element).on('autocomplete:selected', function (event, suggestion, dataset) { + bindingContext.$data.selectedPath = $(element).val().trim() ? suggestion.path : ''; + }); + } + }; +} + export async function showDialog() { utils.closeActiveDialog(); await libraryLoader.requireLibrary(libraryLoader.KNOCKOUT); - attributesModel = new AttributesModel(); - // lazily apply bindings on first use - if (!ko.dataFor($dialog[0])) { + if (!attributesModel) { + attributesModel = new AttributesModel(); + + initKoPlugins(); + ko.applyBindings(attributesModel, $dialog[0]); } diff --git a/src/public/javascripts/services/link.js b/src/public/javascripts/services/link.js index 25422f33d..20a904812 100644 --- a/src/public/javascripts/services/link.js +++ b/src/public/javascripts/services/link.js @@ -94,20 +94,6 @@ function addTextToEditor(text) { } } -function init() { - ko.bindingHandlers.noteLink = { - init: async function(element, valueAccessor, allBindings, viewModel, bindingContext) { - const noteId = ko.unwrap(valueAccessor()); - - if (noteId) { - const link = await createNoteLink(noteId); - - $(element).append(link); - } - } - }; -} - function tabContextMenu(e) { const $link = $(e.target); @@ -175,6 +161,5 @@ export default { createNoteLink, addLinkToEditor, addTextToEditor, - init, goToLink }; \ No newline at end of file diff --git a/src/public/javascripts/services/note_autocomplete.js b/src/public/javascripts/services/note_autocomplete.js index 7dced1bf4..2d5c9536e 100644 --- a/src/public/javascripts/services/note_autocomplete.js +++ b/src/public/javascripts/services/note_autocomplete.js @@ -132,18 +132,6 @@ function init() { .toggleClass("disabled", !path.trim()) .attr(SELECTED_PATH_KEY, path); // we also set attr here so tooltip can be displayed }; - - ko.bindingHandlers.noteAutocomplete = { - init: function (element, valueAccessor, allBindings, viewModel, bindingContext) { - initNoteAutocomplete($(element)); - - $(element).setSelectedPath(bindingContext.$data.selectedPath); - - $(element).on('autocomplete:selected', function (event, suggestion, dataset) { - bindingContext.$data.selectedPath = $(element).val().trim() ? suggestion.path : ''; - }); - } - }; } export default {