From f0bea9cf71c9cc3f512805169c8bdf0cb8d73368 Mon Sep 17 00:00:00 2001 From: azivner Date: Sat, 24 Feb 2018 21:23:04 -0500 Subject: [PATCH] API changes necessary to port reddit plugin, closes #58 --- config-sample.ini | 4 ++++ src/public/javascripts/api.js | 4 ++-- src/public/javascripts/note_tree.js | 10 +++++++++- src/routes/api/tree.js | 2 ++ src/services/script.js | 2 +- src/services/script_context.js | 15 +++++++++------ 6 files changed, 27 insertions(+), 10 deletions(-) diff --git a/config-sample.ini b/config-sample.ini index b5b8e798d..634a704da 100644 --- a/config-sample.ini +++ b/config-sample.ini @@ -1,3 +1,7 @@ +[General] +# Instance name can be used to distinguish between different instances +instanceName= + [Network] port=8080 # true for TLS/SSL/HTTPS (secure), false for HTTP (unsecure). diff --git a/src/public/javascripts/api.js b/src/public/javascripts/api.js index 20f25c36b..c702d0de5 100644 --- a/src/public/javascripts/api.js +++ b/src/public/javascripts/api.js @@ -13,9 +13,9 @@ const api = (function() { $pluginButtons.append(button); } - return { addButtonToToolbar, - activateNote + activateNote, + getInstanceName: noteTree.getInstanceName } })(); \ No newline at end of file diff --git a/src/public/javascripts/note_tree.js b/src/public/javascripts/note_tree.js index b4a4c550f..2213d5d4f 100644 --- a/src/public/javascripts/note_tree.js +++ b/src/public/javascripts/note_tree.js @@ -5,6 +5,8 @@ const noteTree = (function() { const $parentList = $("#parent-list"); const $parentListList = $("#parent-list-inner"); + let instanceName = null; // should have better place + let startNotePath = null; let notesTreeMap = {}; @@ -648,6 +650,7 @@ const noteTree = (function() { async function loadTree() { const resp = await server.get('tree'); startNotePath = resp.start_note_path; + instanceName = resp.instanceName; if (document.location.hash) { startNotePath = getNotePathFromAddress(); @@ -823,6 +826,10 @@ const noteTree = (function() { return !!childToParents[noteId]; } + function getInstanceName() { + return instanceName; + } + $(document).bind('keydown', 'ctrl+o', e => { const node = getCurrentNode(); const parentNoteId = node.data.parentNoteId; @@ -897,6 +904,7 @@ const noteTree = (function() { setParentChildRelation, getSelectedNodes, sortAlphabetically, - noteExists + noteExists, + getInstanceName }; })(); \ No newline at end of file diff --git a/src/routes/api/tree.js b/src/routes/api/tree.js index e9db493ab..f8ea3b208 100644 --- a/src/routes/api/tree.js +++ b/src/routes/api/tree.js @@ -6,6 +6,7 @@ const sql = require('../../services/sql'); const options = require('../../services/options'); const utils = require('../../services/utils'); const auth = require('../../services/auth'); +const config = require('../../services/config'); const protected_session = require('../../services/protected_session'); const sync_table = require('../../services/sync_table'); const wrap = require('express-promise-wrap').wrap; @@ -41,6 +42,7 @@ router.get('/', auth.checkApiAuth, wrap(async (req, res, next) => { AND notes.isDeleted = 0`); res.send({ + instanceName: config.General ? config.General.instanceName : null, notes: notes, hiddenInAutocomplete: hiddenInAutocomplete, start_note_path: await options.getOption('start_note_path') diff --git a/src/services/script.js b/src/services/script.js index 00db4c1d0..d6244ed62 100644 --- a/src/services/script.js +++ b/src/services/script.js @@ -12,7 +12,7 @@ async function executeScript(dataKey, script, params) { let ret; await sql.doInTransaction(async () => { - ret = await (function() { return eval(`(${script})(${paramsStr})`); }.call(ctx)); + ret = await (function() { return eval(`const api = this; (${script})(${paramsStr})`); }.call(ctx)); }); return ret; diff --git a/src/services/script_context.js b/src/services/script_context.js index ce508e612..74571ee02 100644 --- a/src/services/script_context.js +++ b/src/services/script_context.js @@ -3,18 +3,21 @@ const protected_session = require('./protected_session'); const notes = require('./notes'); const attributes = require('./attributes'); const date_notes = require('./date_notes'); +const config = require('./config'); const Repository = require('./repository'); function ScriptContext(noteId, dataKey) { - this.dataKey = protected_session.getDataKey(dataKey); - this.repository = new Repository(dataKey); + dataKey = protected_session.getDataKey(dataKey); + const repository = new Repository(dataKey); + + this.getInstanceName = () => config.General ? config.General.instanceName : null; this.getNoteById = async function(noteId) { - return this.repository.getNote(noteId); + return repository.getNote(noteId); }; this.getNotesWithAttribute = async function (attrName, attrValue) { - return await attributes.getNotesWithAttribute(this.dataKey, attrName, attrValue); + return await attributes.getNotesWithAttribute(dataKey, attrName, attrValue); }; this.getNoteWithAttribute = async function (attrName, attrValue) { @@ -43,7 +46,7 @@ function ScriptContext(noteId, dataKey) { note.mime = "text/html"; } - const noteId = (await notes.createNewNote(parentNoteId, note, this.dataKey)).noteId; + const noteId = (await notes.createNewNote(parentNoteId, note, dataKey)).noteId; if (extraOptions.attributes) { for (const attrName in extraOptions.attributes) { @@ -56,7 +59,7 @@ function ScriptContext(noteId, dataKey) { this.createAttribute = attributes.createAttribute; - this.updateEntity = this.repository.updateEntity; + this.updateEntity = repository.updateEntity; this.log = message => log.info(`Script: ${message}`);