From 9268f88bc33a6386b9296e9965ef1059d16f2c24 Mon Sep 17 00:00:00 2001 From: azivner Date: Sun, 13 Jan 2019 12:16:05 +0100 Subject: [PATCH] frontend scripts now have startNote, currentNote and targetNote as NoteShort entities which e.g. provides easy access to relations/labels --- src/public/javascripts/services/bundle.js | 2 +- src/public/javascripts/services/script_context.js | 6 +++++- src/routes/api/script.js | 6 +++--- src/services/repository.js | 1 - src/services/script.js | 15 ++++++++++++++- 5 files changed, 23 insertions(+), 7 deletions(-) diff --git a/src/public/javascripts/services/bundle.js b/src/public/javascripts/services/bundle.js index 60a9fe992..2f6f6bd87 100644 --- a/src/public/javascripts/services/bundle.js +++ b/src/public/javascripts/services/bundle.js @@ -9,7 +9,7 @@ async function getAndExecuteBundle(noteId, originEntity = null) { } async function executeBundle(bundle, originEntity) { - const apiContext = ScriptContext(bundle.note, bundle.allNotes, originEntity); + const apiContext = await ScriptContext(bundle.noteId, bundle.allNoteIds, originEntity); try { return await (function () { diff --git a/src/public/javascripts/services/script_context.js b/src/public/javascripts/services/script_context.js index 2cea8c9cf..e6ebbd811 100644 --- a/src/public/javascripts/services/script_context.js +++ b/src/public/javascripts/services/script_context.js @@ -1,9 +1,13 @@ import FrontendScriptApi from './frontend_script_api.js'; import utils from './utils.js'; +import treeCache from './tree_cache.js'; -function ScriptContext(startNote, allNotes, originEntity = null) { +async function ScriptContext(startNoteId, allNoteIds, originEntity = null) { const modules = {}; + const startNote = await treeCache.getNote(startNoteId); + const allNotes = await treeCache.getNotes(allNoteIds); + return { modules: modules, notes: utils.toObject(allNotes, note => [note.noteId, note]), diff --git a/src/routes/api/script.js b/src/routes/api/script.js index 1479b24b9..87927e612 100644 --- a/src/routes/api/script.js +++ b/src/routes/api/script.js @@ -30,7 +30,7 @@ async function getStartupBundles() { const bundles = []; for (const note of notes) { - const bundle = await scriptService.getScriptBundle(note); + const bundle = await scriptService.getScriptBundleForFrontend(note); if (bundle) { bundles.push(bundle); @@ -54,7 +54,7 @@ async function getRelationBundles(req) { for (const noteId of uniqueNoteIds) { const note = await repository.getNote(noteId); - const bundle = await scriptService.getScriptBundle(note); + const bundle = await scriptService.getScriptBundleForFrontend(note); if (bundle) { bundles.push(bundle); @@ -67,7 +67,7 @@ async function getRelationBundles(req) { async function getBundle(req) { const note = await repository.getNote(req.params.noteId); - return await scriptService.getScriptBundle(note); + return await scriptService.getScriptBundleForFrontend(note); } module.exports = { diff --git a/src/services/repository.js b/src/services/repository.js index 44f8dbe06..2bb660c61 100644 --- a/src/services/repository.js +++ b/src/services/repository.js @@ -117,7 +117,6 @@ module.exports = { getEntity, getNote, getBranch, - getImage, getAttribute, getOption, updateEntity, diff --git a/src/services/script.js b/src/services/script.js index 7e2209d90..2c3a7855e 100644 --- a/src/services/script.js +++ b/src/services/script.js @@ -79,6 +79,19 @@ function getParams(params) { }).join(","); } +async function getScriptBundleForFrontend(note) { + const bundle = await getScriptBundle(note); + + // for frontend we return just noteIds because frontend needs to use its own entity instances + bundle.noteId = bundle.note.noteId; + delete bundle.note; + + bundle.allNoteIds = bundle.allNotes.map(note => note.noteId); + delete bundle.allNotes; + + return bundle; +} + async function getScriptBundle(note, root = true, scriptEnv = null, includedNoteIds = []) { if (!note.isContentAvailable) { return; @@ -156,5 +169,5 @@ function sanitizeVariableName(str) { module.exports = { executeNote, executeScript, - getScriptBundle + getScriptBundleForFrontend }; \ No newline at end of file