From 3b9d1df05ccd215ce9b23c42e556466a404c87f2 Mon Sep 17 00:00:00 2001 From: azivner Date: Sun, 4 Mar 2018 14:21:11 -0500 Subject: [PATCH] fixed frontend script execution --- src/public/javascripts/api.js | 6 ++++-- src/public/javascripts/utils.js | 11 ++++++++++- src/routes/api/script.js | 11 ++++++----- src/services/attributes.js | 12 +++++------- src/services/script.js | 6 +----- src/services/script_context.js | 2 +- 6 files changed, 27 insertions(+), 21 deletions(-) diff --git a/src/public/javascripts/api.js b/src/public/javascripts/api.js index c702d0de5..47eee462c 100644 --- a/src/public/javascripts/api.js +++ b/src/public/javascripts/api.js @@ -1,4 +1,4 @@ -const api = (function() { +function Api() { const $pluginButtons = $("#plugin-buttons"); async function activateNote(notePath) { @@ -14,8 +14,10 @@ const api = (function() { } return { + __modules: {}, + __notes: {}, addButtonToToolbar, activateNote, getInstanceName: noteTree.getInstanceName } -})(); \ No newline at end of file +} \ No newline at end of file diff --git a/src/public/javascripts/utils.js b/src/public/javascripts/utils.js index 81abc2147..1c6d0e80b 100644 --- a/src/public/javascripts/utils.js +++ b/src/public/javascripts/utils.js @@ -116,7 +116,16 @@ async function stopWatch(what, func) { } function executeScript(script) { - eval(script); + const completeScript = ` +(async function() { + const api = Api(); + + ${script} +})();`; + + console.log(completeScript); + + eval(completeScript); } function formatValueWithWhitespace(val) { diff --git a/src/routes/api/script.js b/src/routes/api/script.js index 4306e3626..9519af69f 100644 --- a/src/routes/api/script.js +++ b/src/routes/api/script.js @@ -23,15 +23,15 @@ router.post('/job', auth.checkApiAuth, wrap(async (req, res, next) => { })); router.get('/startup', auth.checkApiAuth, wrap(async (req, res, next) => { - const noteIds = await attributes.getNoteIdsWithAttribute("run", "frontend_startup"); const repository = new Repository(req); + const notes = await attributes.getNotesWithAttribute(repository, "run", "frontend_startup"); const scripts = []; - for (const noteId of noteIds) { - const note = await repository.getNote(noteId); + for (const note of notes) { + const bundle = await script.getScriptBundle(note); - scripts.push(await script.getNoteScript(note)); + scripts.push(bundle.script); } res.send(scripts); @@ -40,8 +40,9 @@ router.get('/startup', auth.checkApiAuth, wrap(async (req, res, next) => { router.get('/subtree/:noteId', auth.checkApiAuth, wrap(async (req, res, next) => { const repository = new Repository(req); const note = await repository.getNote(req.params.noteId); + const bundle = await script.getScriptBundle(note); - res.send(await script.getNoteScript(note, repository)); + res.send(bundle.script); })); router.get('/render/:noteId', auth.checkApiAuth, wrap(async (req, res, next) => { diff --git a/src/services/attributes.js b/src/services/attributes.js index 37eb1fa69..fc7269ece 100644 --- a/src/services/attributes.js +++ b/src/services/attributes.js @@ -3,10 +3,10 @@ const sql = require('./sql'); const utils = require('./utils'); const sync_table = require('./sync_table'); -const Repository = require('./repository'); const BUILTIN_ATTRIBUTES = [ - 'run_on_startup', + 'frontend_startup', + 'backend_startup', 'disable_versioning', 'calendar_root', 'hide_in_autocomplete', @@ -27,9 +27,7 @@ async function getNoteIdWithAttribute(name, value) { AND attributes.value = ?`, [name, value]); } -async function getNotesWithAttribute(dataKey, name, value) { - const repository = new Repository(dataKey); - +async function getNotesWithAttribute(repository, name, value) { let notes; if (value !== undefined) { @@ -44,8 +42,8 @@ async function getNotesWithAttribute(dataKey, name, value) { return notes; } -async function getNoteWithAttribute(dataKey, name, value) { - const notes = getNotesWithAttribute(dataKey, name, value); +async function getNoteWithAttribute(repository, name, value) { + const notes = getNotesWithAttribute(repository, name, value); return notes.length > 0 ? notes[0] : null; } diff --git a/src/services/script.js b/src/services/script.js index 5463d705e..c78571eec 100644 --- a/src/services/script.js +++ b/src/services/script.js @@ -101,10 +101,6 @@ async function getRenderScript(note) { return "async function() {" + subTreeScripts + note.content + "\r\n}"; } -async function getNoteScript(note) { - -} - async function getScriptBundle(note, includedNoteIds = []) { if (!note.isJavaScript()) { return; @@ -150,6 +146,6 @@ module.exports = { executeNote, executeScript, setJob, - getNoteScript, + getScriptBundle, getRenderScript }; \ No newline at end of file diff --git a/src/services/script_context.js b/src/services/script_context.js index a521bb133..be94b3a45 100644 --- a/src/services/script_context.js +++ b/src/services/script_context.js @@ -31,7 +31,7 @@ function ScriptContext(dataKey, startNote, allNotes) { }; this.getNotesWithAttribute = async function (attrName, attrValue) { - return await attributes.getNotesWithAttribute(dataKey, attrName, attrValue); + return await attributes.getNotesWithAttribute(repository, attrName, attrValue); }; this.getNoteWithAttribute = async function (attrName, attrValue) {