From 3fe9218ea8f510f89e5147823cfb2fe02d96a1a5 Mon Sep 17 00:00:00 2001 From: zadam Date: Tue, 21 May 2019 20:24:40 +0200 Subject: [PATCH] refactoring/cleanup --- .../services/frontend_script_api.js | 6 -- .../javascripts/services/note_detail.js | 72 ++++++++++--------- .../javascripts/services/note_detail_image.js | 2 +- src/public/javascripts/services/note_type.js | 2 +- .../javascripts/services/protected_session.js | 2 +- .../javascripts/services/tab_context.js | 6 +- src/public/javascripts/services/tab_row.js | 1 - src/public/javascripts/services/tree.js | 19 +---- 8 files changed, 47 insertions(+), 63 deletions(-) diff --git a/src/public/javascripts/services/frontend_script_api.js b/src/public/javascripts/services/frontend_script_api.js index 0e9a8bc92..281b9e8a4 100644 --- a/src/public/javascripts/services/frontend_script_api.js +++ b/src/public/javascripts/services/frontend_script_api.js @@ -257,12 +257,6 @@ function FrontendScriptApi(startNote, currentNote, originEntity = null, tabConte */ this.createNoteLink = linkService.createNoteLink; - /** - * @method - * @returns {string} content of active note (loaded into right pane) - */ - this.getActiveNoteContent = noteDetailService.getActiveNoteContent; - /** * @method * @returns {NoteFull} active note (loaded into right pane) diff --git a/src/public/javascripts/services/note_detail.js b/src/public/javascripts/services/note_detail.js index 6fc197ba3..5570b4304 100644 --- a/src/public/javascripts/services/note_detail.js +++ b/src/public/javascripts/services/note_detail.js @@ -17,24 +17,6 @@ const $savedIndicator = $(".saved-indicator"); let detailLoadedListeners = []; -/** @return {NoteFull} */ -function getActiveNote() { - const activeContext = getActiveTabContext(); - return activeContext ? activeContext.note : null; -} - -function getActiveNoteId() { - const activeNote = getActiveNote(); - - return activeNote ? activeNote.noteId : null; -} - -function getActiveNoteType() { - const activeNote = getActiveNote(); - - return activeNote ? activeNote.type : null; -} - async function reload() { // no saving here @@ -67,10 +49,6 @@ async function switchToNote(notePath) { openTabsChanged(); } -function getActiveNoteContent() { - return getActiveTabContext().getComponent().getContent(); -} - function onNoteChange(func) { return getActiveTabContext().getComponent().onNoteChange(func); } @@ -115,17 +93,40 @@ function getActiveTabContext() { return tabContexts.find(tc => tc.tabId === tabId); } -function isActive(tabContext) { - return tabContext.$tab[0] === tabRow.activateTab; +/** @return {NoteFull} */ +function getActiveNote() { + const activeContext = getActiveTabContext(); + return activeContext ? activeContext.note : null; } -async function activateTabContext(tabContext) { - await tabRow.activateTab(tabContext.$tab[0]); +function getActiveNoteId() { + const activeNote = getActiveNote(); + + return activeNote ? activeNote.noteId : null; } -/** @returns {TabContext} */ -function getTabContext(tabId) { - return tabContexts.find(tc => tc.tabId === tabId); +function getActiveNoteType() { + const activeNote = getActiveNote(); + + return activeNote ? activeNote.type : null; +} + +async function switchToTab(tabId, notePath) { + const tabContext = tabContexts.find(tc => tc.tabId === tabId); + + if (!tabContext) { + await loadNoteDetail(notePath, { + newTab: true, + tabId: tabId, + activate: true + }); + } else { + await tabContext.activate(); + + if (notePath && tabContext.notePath !== notePath) { + await loadNoteDetail(notePath); + } + } } async function showTab(tabId) { @@ -348,7 +349,13 @@ $tabContentsContainer.on("dragover", e => e.preventDefault()); $tabContentsContainer.on("dragleave", e => e.preventDefault()); $tabContentsContainer.on("drop", e => { - importDialog.uploadFiles(getActiveNoteId(), e.originalEvent.dataTransfer.files, { + const activeNote = getActiveNote(); + + if (!activeNote) { + return; + } + + importDialog.uploadFiles(activeNote.noteId, e.originalEvent.dataTransfer.files, { safeImport: true, shrinkImages: true, textImportedAsText: true, @@ -502,7 +509,6 @@ export default { loadNote, loadNoteDetail, getActiveNote, - getActiveNoteContent, getActiveNoteType, getActiveNoteId, focusOnTitle, @@ -510,12 +516,10 @@ export default { saveNotesIfChanged, onNoteChange, addDetailLoadedListener, - getTabContext, + switchToTab, getTabContexts, getActiveTabContext, getActiveEditor, - isActive, - activateTabContext, clearOpenTabsTask, filterTabs, openEmptyTab, diff --git a/src/public/javascripts/services/note_detail_image.js b/src/public/javascripts/services/note_detail_image.js index 4439084b6..49d7cb986 100644 --- a/src/public/javascripts/services/note_detail_image.js +++ b/src/public/javascripts/services/note_detail_image.js @@ -65,7 +65,7 @@ class NoteDetailImage { getFileUrl() { // electron needs absolute URL so we extract current host, port, protocol - return utils.getHost() + "/api/notes/" + noteDetailService.getActiveNoteId() + "/download"; + return utils.getHost() + `/api/notes/${this.ctx.note.noteId}/download`; } getContent() {} diff --git a/src/public/javascripts/services/note_type.js b/src/public/javascripts/services/note_type.js index 1a96dd393..a39d7b505 100644 --- a/src/public/javascripts/services/note_type.js +++ b/src/public/javascripts/services/note_type.js @@ -114,7 +114,7 @@ function NoteTypeContext(ctx) { } function confirmChangeIfContent() { - if (!noteDetailService.getActiveNoteContent()) { + if (!self.ctx.getComponent().getContent()) { return true; } diff --git a/src/public/javascripts/services/protected_session.js b/src/public/javascripts/services/protected_session.js index 919083241..e732d9355 100644 --- a/src/public/javascripts/services/protected_session.js +++ b/src/public/javascripts/services/protected_session.js @@ -70,7 +70,7 @@ async function enterProtectedSessionOnServer(password) { } async function protectNoteAndSendToServer() { - if (noteDetailService.getActiveNote().isProtected) { + if (!noteDetailService.getActiveNote() || noteDetailService.getActiveNote().isProtected) { return; } diff --git a/src/public/javascripts/services/tab_context.js b/src/public/javascripts/services/tab_context.js index f8bccee5e..c751e246c 100644 --- a/src/public/javascripts/services/tab_context.js +++ b/src/public/javascripts/services/tab_context.js @@ -192,13 +192,17 @@ class TabContext { return this.components[type]; } + async activate() { + await this.tabRow.activateTab(this.$tab[0]); + } + async saveNote() { if (this.note.isProtected && !protectedSessionHolder.isProtectedSessionAvailable()) { return; } this.note.title = this.$noteTitle.val(); - this.note.content = noteDetailService.getActiveNoteContent(); + this.note.content = this.getComponent().getContent(); // it's important to set the flag back to false immediatelly after retrieving title and content // otherwise we might overwrite another change (especially async code) diff --git a/src/public/javascripts/services/tab_row.js b/src/public/javascripts/services/tab_row.js index 10a2c3c84..660f8f6af 100644 --- a/src/public/javascripts/services/tab_row.js +++ b/src/public/javascripts/services/tab_row.js @@ -6,7 +6,6 @@ */ import optionsInit from './options_init.js'; -import utils from './utils.js'; !function(i,e){"function"==typeof define&&define.amd?define("jquery-bridget/jquery-bridget",["jquery"],function(t){return e(i,t)}):"object"==typeof module&&module.exports?module.exports=e(i,require("jquery")):i.jQueryBridget=e(i,i.jQuery)}(window,function(t,i){"use strict";var c=Array.prototype.slice,e=t.console,p=void 0===e?function(){}:function(t){e.error(t)};function n(d,o,u){(u=u||i||t.jQuery)&&(o.prototype.option||(o.prototype.option=function(t){u.isPlainObject(t)&&(this.options=u.extend(!0,this.options,t))}),u.fn[d]=function(t){if("string"==typeof t){var i=c.call(arguments,1);return s=i,a="$()."+d+'("'+(r=t)+'")',(e=this).each(function(t,i){var e=u.data(i,d);if(e){var n=e[r];if(n&&"_"!=r.charAt(0)){var o=n.apply(e,s);h=void 0===h?o:h}else p(a+" is not a valid method")}else p(d+" not initialized. Cannot call methods, i.e. "+a)}),void 0!==h?h:e}var e,r,s,h,a,n;return n=t,this.each(function(t,i){var e=u.data(i,d);e?(e.option(n),e._init()):(e=new o(i,n),u.data(i,d,e))}),this},r(u))}function r(t){!t||t&&t.bridget||(t.bridget=n)}return r(i||t.jQuery),n}),function(t,i){"use strict";"function"==typeof define&&define.amd?define("get-size/get-size",[],function(){return i()}):"object"==typeof module&&module.exports?module.exports=i():t.getSize=i()}(window,function(){"use strict";function m(t){var i=parseFloat(t);return-1==t.indexOf("%")&&!isNaN(i)&&i}var e="undefined"==typeof console?function(){}:function(t){console.error(t)},y=["paddingLeft","paddingRight","paddingTop","paddingBottom","marginLeft","marginRight","marginTop","marginBottom","borderLeftWidth","borderRightWidth","borderTopWidth","borderBottomWidth"],b=y.length;function E(t){var i=getComputedStyle(t);return i||e("Style returned "+i+". Are you running this code in a hidden iframe on Firefox? See http://bit.ly/getsizebug1"),i}var _,x=!1;function P(t){if(function(){if(!x){x=!0;var t=document.createElement("div");t.style.width="200px",t.style.padding="1px 2px 3px 4px",t.style.borderStyle="solid",t.style.borderWidth="1px 2px 3px 4px",t.style.boxSizing="border-box";var i=document.body||document.documentElement;i.appendChild(t);var e=E(t);P.isBoxSizeOuter=_=200==m(e.width),i.removeChild(t)}}(),"string"==typeof t&&(t=document.querySelector(t)),t&&"object"==typeof t&&t.nodeType){var i=E(t);if("none"==i.display)return function(){for(var t={width:0,height:0,innerWidth:0,innerHeight:0,outerWidth:0,outerHeight:0},i=0;i