From b10b0048f31e7ec5b828aa4c86d070ad8bf08115 Mon Sep 17 00:00:00 2001 From: azivner Date: Tue, 27 Mar 2018 22:42:46 -0400 Subject: [PATCH] split out library loader --- src/public/javascripts/dialogs/add_link.js | 1 - .../javascripts/dialogs/edit_tree_prefix.js | 12 ++-- src/public/javascripts/dialogs/note_source.js | 2 +- .../javascripts/dialogs/recent_notes.js | 28 ++++---- src/public/javascripts/dialogs/settings.js | 22 +++---- src/public/javascripts/dialogs/sql_console.js | 3 +- src/public/javascripts/services/bootstrap.js | 5 +- .../javascripts/services/library_loader.js | 64 +++++++++++++++++++ .../javascripts/services/note_detail_code.js | 3 +- .../javascripts/services/note_detail_text.js | 4 +- src/public/javascripts/services/utils.js | 62 ------------------ 11 files changed, 109 insertions(+), 97 deletions(-) create mode 100644 src/public/javascripts/services/library_loader.js diff --git a/src/public/javascripts/dialogs/add_link.js b/src/public/javascripts/dialogs/add_link.js index 5ad638580..90cc6b8b5 100644 --- a/src/public/javascripts/dialogs/add_link.js +++ b/src/public/javascripts/dialogs/add_link.js @@ -1,4 +1,3 @@ -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'; diff --git a/src/public/javascripts/dialogs/edit_tree_prefix.js b/src/public/javascripts/dialogs/edit_tree_prefix.js index e4f69c9b1..656999497 100644 --- a/src/public/javascripts/dialogs/edit_tree_prefix.js +++ b/src/public/javascripts/dialogs/edit_tree_prefix.js @@ -30,14 +30,18 @@ async function showDialog() { $noteTitle.html(noteTitle); } -$form.submit(() => { +async function savePrefix() { const prefix = $treePrefixInput.val(); - server.put('tree/' + branchId + '/set-prefix', { - prefix: prefix - }).then(() => treeService.setPrefix(branchId, prefix)); + await server.put('tree/' + branchId + '/set-prefix', { prefix: prefix }); + + await treeService.setPrefix(branchId, prefix); $dialog.dialog("close"); +} + +$form.submit(() => { + savePrefix(); return false; }); diff --git a/src/public/javascripts/dialogs/note_source.js b/src/public/javascripts/dialogs/note_source.js index e09bfd64a..062345201 100644 --- a/src/public/javascripts/dialogs/note_source.js +++ b/src/public/javascripts/dialogs/note_source.js @@ -29,7 +29,7 @@ function formatNode(node, level) { const indentAfter = new Array(level - 1).join(' '); let textNode; - for (let i = 0; i < node.children.length; i++) { + for (const i = 0; i < node.children.length; i++) { textNode = document.createTextNode('\n' + indentBefore); node.insertBefore(textNode, node.children[i]); diff --git a/src/public/javascripts/dialogs/recent_notes.js b/src/public/javascripts/dialogs/recent_notes.js index 41fed8013..a60a692f1 100644 --- a/src/public/javascripts/dialogs/recent_notes.js +++ b/src/public/javascripts/dialogs/recent_notes.js @@ -27,6 +27,21 @@ function addRecentNote(branchId, notePath) { }, 1500); } +async function getNoteTitle(notePath) { + let noteTitle; + + try { + noteTitle = await treeUtils.getNotePathTitle(notePath); + } + catch (e) { + noteTitle = "[error - can't find note title]"; + + messagingService.logError("Could not find title for notePath=" + notePath + ", stack=" + e.stack); + } + + return noteTitle; +} + async function showDialog() { glob.activeDialog = $dialog; @@ -44,19 +59,8 @@ async function showDialog() { const items = []; for (const notePath of recNotes) { - let noteTitle; - - try { - noteTitle = await treeUtils.getNotePathTitle(notePath); - } - catch (e) { - noteTitle = "[error - can't find note title]"; - - messagingService.logError("Could not find title for notePath=" + notePath + ", stack=" + e.stack); - } - items.push({ - label: noteTitle, + label: await getNoteTitle(notePath), value: notePath }); } diff --git a/src/public/javascripts/dialogs/settings.js b/src/public/javascripts/dialogs/settings.js index 283067f1e..f0d5e2ce2 100644 --- a/src/public/javascripts/dialogs/settings.js +++ b/src/public/javascripts/dialogs/settings.js @@ -8,10 +8,10 @@ import infoService from "../services/info.js"; const $dialog = $("#settings-dialog"); const $tabs = $("#settings-tabs"); -const settingModules = []; +const tabHandlers = []; -function addModule(module) { - settingModules.push(module); +function addTabHandler(handler) { + tabHandlers.push(handler); } async function showDialog() { @@ -26,9 +26,9 @@ async function showDialog() { $tabs.tabs(); - for (const module of settingModules) { - if (module.settingsLoaded) { - module.settingsLoaded(settings); + for (const handler of tabHandlers) { + if (handler.settingsLoaded) { + handler.settingsLoaded(settings); } } } @@ -47,7 +47,7 @@ export default { saveSettings }; -addModule((function() { +addTabHandler((function() { const $form = $("#change-password-form"); const $oldPassword = $("#old-password"); const $newPassword1 = $("#new-password1"); @@ -93,7 +93,7 @@ addModule((function() { }; })()); -addModule((function() { +addTabHandler((function() { const $form = $("#protected-session-timeout-form"); const $protectedSessionTimeout = $("#protected-session-timeout-in-seconds"); const settingName = 'protected_session_timeout'; @@ -117,7 +117,7 @@ addModule((function() { }; })()); -addModule((function () { +addTabHandler((function () { const $form = $("#note-revision-snapshot-time-interval-form"); const $timeInterval = $("#note-revision-snapshot-time-interval-in-seconds"); const settingName = 'note_revision_snapshot_time_interval'; @@ -137,7 +137,7 @@ addModule((function () { }; })()); -addModule((async function () { +addTabHandler((async function () { const $appVersion = $("#app-version"); const $dbVersion = $("#db-version"); const $buildDate = $("#build-date"); @@ -154,7 +154,7 @@ addModule((async function () { return {}; })()); -addModule((async function () { +addTabHandler((async function () { const $forceFullSyncButton = $("#force-full-sync-button"); const $fillSyncRowsButton = $("#fill-sync-rows-button"); const $anonymizeButton = $("#anonymize-button"); diff --git a/src/public/javascripts/dialogs/sql_console.js b/src/public/javascripts/dialogs/sql_console.js index bd69bc091..139ece629 100644 --- a/src/public/javascripts/dialogs/sql_console.js +++ b/src/public/javascripts/dialogs/sql_console.js @@ -1,4 +1,5 @@ import utils from '../services/utils.js'; +import libraryLoader from '../services/library_loader.js'; import server from '../services/server.js'; import infoService from "../services/info.js"; @@ -25,7 +26,7 @@ function showDialog() { async function initEditor() { if (!codeEditor) { - await utils.requireLibrary(utils.CODE_MIRROR); + await libraryLoader.requireLibrary(libraryLoader.CODE_MIRROR); CodeMirror.keyMap.default["Shift-Tab"] = "indentLess"; CodeMirror.keyMap.default["Tab"] = "indentMore"; diff --git a/src/public/javascripts/services/bootstrap.js b/src/public/javascripts/services/bootstrap.js index ffb58b447..f29a2a5cb 100644 --- a/src/public/javascripts/services/bootstrap.js +++ b/src/public/javascripts/services/bootstrap.js @@ -30,6 +30,7 @@ import entrypoints from './entrypoints.js'; import tooltip from './tooltip.js'; import bundle from "./bundle.js"; import treeCache from "./tree_cache.js"; +import libraryLoader from "./library_loader.js"; // required for CKEditor image upload plugin window.glob.getCurrentNode = treeService.getCurrentNode; @@ -37,8 +38,8 @@ window.glob.getHeaders = server.getHeaders; // required for ESLint plugin window.glob.getCurrentNote = noteDetailService.getCurrentNote; -window.glob.requireLibrary = utils.requireLibrary; -window.glob.ESLINT = utils.ESLINT; +window.glob.requireLibrary = libraryLoader.requireLibrary; +window.glob.ESLINT = libraryLoader.ESLINT; window.onerror = function (msg, url, lineNo, columnNo, error) { const string = msg.toLowerCase(); diff --git a/src/public/javascripts/services/library_loader.js b/src/public/javascripts/services/library_loader.js new file mode 100644 index 000000000..f0b919200 --- /dev/null +++ b/src/public/javascripts/services/library_loader.js @@ -0,0 +1,64 @@ +const CKEDITOR = {"js": ["libraries/ckeditor/ckeditor.js"]}; + +const CODE_MIRROR = { + js: [ + "libraries/codemirror/codemirror.js", + "libraries/codemirror/addon/mode/loadmode.js", + "libraries/codemirror/addon/fold/xml-fold.js", + "libraries/codemirror/addon/edit/matchbrackets.js", + "libraries/codemirror/addon/edit/matchtags.js", + "libraries/codemirror/addon/search/match-highlighter.js", + "libraries/codemirror/mode/meta.js", + "libraries/codemirror/addon/lint/lint.js", + "libraries/codemirror/addon/lint/eslint.js" + ], + css: [ + "libraries/codemirror/codemirror.css", + "libraries/codemirror/addon/lint/lint.css" + ] +}; + +const ESLINT = {js: ["libraries/eslint.js"]}; + +async function requireLibrary(library) { + if (library.css) { + library.css.map(cssUrl => requireCss(cssUrl)); + } + + if (library.js) { + for (const scriptUrl of library.js) { + await requireScript(scriptUrl); + } + } +} + +const dynamicallyLoadedScripts = []; + +async function requireScript(url) { + if (!dynamicallyLoadedScripts.includes(url)) { + dynamicallyLoadedScripts.push(url); + + return await $.ajax({ + url: url, + dataType: "script", + cache: true + }) + } +} + +async function requireCss(url) { + const css = Array + .from(document.querySelectorAll('link')) + .map(scr => scr.href); + + if (!css.includes(url)) { + $('head').append($('').attr('href', url)); + } +} + +export default { + requireLibrary, + CKEDITOR, + CODE_MIRROR, + ESLINT +} \ No newline at end of file diff --git a/src/public/javascripts/services/note_detail_code.js b/src/public/javascripts/services/note_detail_code.js index b92c1d77e..cdd46fcae 100644 --- a/src/public/javascripts/services/note_detail_code.js +++ b/src/public/javascripts/services/note_detail_code.js @@ -1,4 +1,5 @@ import utils from "./utils.js"; +import libraryLoader from "./library_loader.js"; import bundleService from "./bundle.js"; import infoService from "./info.js"; import server from "./server.js"; @@ -11,7 +12,7 @@ const $executeScriptButton = $("#execute-script-button"); async function show() { if (!codeEditor) { - await utils.requireLibrary(utils.CODE_MIRROR); + await libraryLoader.requireLibrary(libraryLoader.CODE_MIRROR); CodeMirror.keyMap.default["Shift-Tab"] = "indentLess"; CodeMirror.keyMap.default["Tab"] = "indentMore"; diff --git a/src/public/javascripts/services/note_detail_text.js b/src/public/javascripts/services/note_detail_text.js index 18c691bb5..3314818c4 100644 --- a/src/public/javascripts/services/note_detail_text.js +++ b/src/public/javascripts/services/note_detail_text.js @@ -1,4 +1,4 @@ -import utils from "./utils.js"; +import libraryLoader from "./library_loader.js"; import noteDetailService from './note_detail.js'; const $noteDetailText = $('#note-detail-text'); @@ -7,7 +7,7 @@ let textEditor = null; async function show() { if (!textEditor) { - await utils.requireLibrary(utils.CKEDITOR); + await libraryLoader.requireLibrary(libraryLoader.CKEDITOR); textEditor = await BalloonEditor.create($noteDetailText[0], {}); diff --git a/src/public/javascripts/services/utils.js b/src/public/javascripts/services/utils.js index fd1f33aa8..74d780221 100644 --- a/src/public/javascripts/services/utils.js +++ b/src/public/javascripts/services/utils.js @@ -89,64 +89,6 @@ function formatLabel(attr) { return str; } -const CKEDITOR = {"js": ["libraries/ckeditor/ckeditor.js"]}; - -const CODE_MIRROR = { - js: [ - "libraries/codemirror/codemirror.js", - "libraries/codemirror/addon/mode/loadmode.js", - "libraries/codemirror/addon/fold/xml-fold.js", - "libraries/codemirror/addon/edit/matchbrackets.js", - "libraries/codemirror/addon/edit/matchtags.js", - "libraries/codemirror/addon/search/match-highlighter.js", - "libraries/codemirror/mode/meta.js", - "libraries/codemirror/addon/lint/lint.js", - "libraries/codemirror/addon/lint/eslint.js" - ], - css: [ - "libraries/codemirror/codemirror.css", - "libraries/codemirror/addon/lint/lint.css" - ] -}; - -const ESLINT = {js: ["libraries/eslint.js"]}; - -async function requireLibrary(library) { - if (library.css) { - library.css.map(cssUrl => requireCss(cssUrl)); - } - - if (library.js) { - for (const scriptUrl of library.js) { - await requireScript(scriptUrl); - } - } -} - -const dynamicallyLoadedScripts = []; - -async function requireScript(url) { - if (!dynamicallyLoadedScripts.includes(url)) { - dynamicallyLoadedScripts.push(url); - - return await $.ajax({ - url: url, - dataType: "script", - cache: true - }) - } -} - -async function requireCss(url) { - const css = Array - .from(document.querySelectorAll('link')) - .map(scr => scr.href); - - if (!css.includes(url)) { - $('head').append($('').attr('href', url)); - } -} - function getHost() { const url = new URL(window.location.href); return url.protocol + "//" + url.hostname + ":" + url.port; @@ -212,10 +154,6 @@ export default { stopWatch, formatValueWithWhitespace, formatLabel, - requireLibrary, - CKEDITOR, - CODE_MIRROR, - ESLINT, getHost, download, toObject,