From 5bffba4e2ff6e2e79a8bc396ee55f887d8ac6734 Mon Sep 17 00:00:00 2001 From: azivner Date: Sat, 3 Feb 2018 10:37:57 -0500 Subject: [PATCH] add API to add plugin buttons, fixes --- src/public/javascripts/api.js | 21 +++++++++++++++++++++ src/public/javascripts/utils.js | 3 ++- src/public/stylesheets/style.css | 6 +----- src/routes/api/script.js | 9 +++++---- src/scripts/today.js | 11 +++++++++++ src/scripts/weight.html | 2 ++ src/services/notes.js | 26 -------------------------- src/views/index.ejs | 10 ++++++++-- 8 files changed, 50 insertions(+), 38 deletions(-) create mode 100644 src/public/javascripts/api.js create mode 100644 src/scripts/today.js diff --git a/src/public/javascripts/api.js b/src/public/javascripts/api.js new file mode 100644 index 000000000..941187d2b --- /dev/null +++ b/src/public/javascripts/api.js @@ -0,0 +1,21 @@ +const api = (function() { + const pluginButtonsEl = $("#plugin-buttons"); + + async function activateNote(notePath) { + await noteTree.activateNode(notePath); + } + + function addButtonToToolbar(buttonId, button) { + $("#" + buttonId).remove(); + + button.attr('id', buttonId); + + pluginButtonsEl.append(button); + } + + + return { + addButtonToToolbar, + activateNote + } +})(); \ No newline at end of file diff --git a/src/public/javascripts/utils.js b/src/public/javascripts/utils.js index e5ff83f88..9e1ae2e5b 100644 --- a/src/public/javascripts/utils.js +++ b/src/public/javascripts/utils.js @@ -116,5 +116,6 @@ async function stopWatch(what, func) { } function executeScript(script) { - eval("(async function() {" + script + "})()"); + // last \r\n is necessary if script contains line comment on its last line + eval("(async function() {" + script + "\r\n})()"); } \ No newline at end of file diff --git a/src/public/stylesheets/style.css b/src/public/stylesheets/style.css index db8901b7e..876cfca95 100644 --- a/src/public/stylesheets/style.css +++ b/src/public/stylesheets/style.css @@ -190,11 +190,6 @@ div.ui-tooltip { float: right; } -#note-id-display { - color: lightgrey; - margin-left: 10px; -} - #note-source { height: 98%; width: 100%; @@ -245,6 +240,7 @@ div.ui-tooltip { right: 10px; bottom: 5px; z-index: 1000; + color: lightgrey; } #note-type-dropdown { diff --git a/src/routes/api/script.js b/src/routes/api/script.js index 376aaaa97..85042a30e 100644 --- a/src/routes/api/script.js +++ b/src/routes/api/script.js @@ -19,11 +19,12 @@ router.post('/exec', auth.checkApiAuth, wrap(async (req, res, next) => { router.get('/startup', auth.checkApiAuth, wrap(async (req, res, next) => { const noteIds = await attributes.getNoteIdsWithAttribute("run_on_startup"); + const repository = new Repository(req); const scripts = []; for (const noteId of noteIds) { - scripts.push(await getNoteWithSubtreeScript(noteId, req)); + scripts.push(await getNoteWithSubtreeScript(noteId, repository)); } res.send(scripts); @@ -41,10 +42,10 @@ router.get('/subtree/:noteId', auth.checkApiAuth, wrap(async (req, res, next) => res.send(subTreeScripts + noteScript); })); -async function getNoteWithSubtreeScript(noteId, req) { - const noteScript = (await notes.getNoteById(noteId, req)).content; +async function getNoteWithSubtreeScript(noteId, repository) { + const noteScript = (await repository.getNote(noteId)).content; - const subTreeScripts = await getSubTreeScripts(noteId, [noteId], req); + const subTreeScripts = await getSubTreeScripts(noteId, [noteId], repository); return subTreeScripts + noteScript; } diff --git a/src/scripts/today.js b/src/scripts/today.js new file mode 100644 index 000000000..f348cc70d --- /dev/null +++ b/src/scripts/today.js @@ -0,0 +1,11 @@ +api.addButtonToToolbar('go-today', $('')); + +window.goToday = async function() { + const todayDateStr = formatDateISO(new Date()); + + const todayNoteId = await server.exec([todayDateStr], async todayDateStr => { + return await this.getDateNoteId(todayDateStr); + }); + + api.activateNote(todayNoteId); +}; \ No newline at end of file diff --git a/src/scripts/weight.html b/src/scripts/weight.html index 59ec0e79f..57fcbd9ad 100644 --- a/src/scripts/weight.html +++ b/src/scripts/weight.html @@ -65,6 +65,8 @@ }); } + data.sort((a, b) => a.date < b.date ? -1 : +1); + return data; }); diff --git a/src/services/notes.js b/src/services/notes.js index 38b1cf797..c8e695dab 100644 --- a/src/services/notes.js +++ b/src/services/notes.js @@ -5,32 +5,6 @@ const sync_table = require('./sync_table'); const attributes = require('./attributes'); const protected_session = require('./protected_session'); -async function updateJsonNote(noteId, data) { - const ret = await createNewNote(noteId, { - title: name, - content: JSON.stringify(data), - target: 'into', - isProtected: false, - type: 'code', - mime: 'application/json' - }); - - return ret.noteId; -} - -async function createNewJsonNote(parentNoteId, name, payload) { - const ret = await createNewNote(parentNoteId, { - title: name, - content: JSON.stringify(payload), - target: 'into', - isProtected: false, - type: 'code', - mime: 'application/json' - }); - - return ret.noteId; -} - async function createNewNote(parentNoteId, noteOpts, dataKey, sourceId) { const noteId = utils.newNoteId(); const noteTreeId = utils.newNoteTreeId(); diff --git a/src/views/index.ejs b/src/views/index.ejs index 18db3ac05..9ccd021e3 100644 --- a/src/views/index.ejs +++ b/src/views/index.ejs @@ -20,12 +20,18 @@ +
+
+
- +
@@ -492,7 +498,7 @@ - +