2018-01-27 17:18:19 -05:00
|
|
|
const log = require('./log');
|
|
|
|
const protected_session = require('./protected_session');
|
|
|
|
const notes = require('./notes');
|
2018-02-24 22:44:45 -05:00
|
|
|
const sql = require('./sql');
|
2018-02-26 20:47:34 -05:00
|
|
|
const utils = require('./utils');
|
2018-03-24 22:02:26 -04:00
|
|
|
const labels = require('./labels');
|
2018-01-27 17:18:19 -05:00
|
|
|
const date_notes = require('./date_notes');
|
2018-02-24 21:23:04 -05:00
|
|
|
const config = require('./config');
|
2018-01-29 23:17:44 -05:00
|
|
|
const Repository = require('./repository');
|
2018-02-26 20:47:34 -05:00
|
|
|
const axios = require('axios');
|
2018-01-27 17:18:19 -05:00
|
|
|
|
2018-03-31 08:53:52 -04:00
|
|
|
function ScriptContext(startNote, allNotes) {
|
2018-03-06 23:04:35 -05:00
|
|
|
this.modules = {};
|
|
|
|
this.notes = utils.toObject(allNotes, note => [note.noteId, note]);
|
2018-03-31 08:53:52 -04:00
|
|
|
this.apis = utils.toObject(allNotes, note => [note.noteId, new ScriptApi(startNote, note)]);
|
2018-03-10 11:53:51 -05:00
|
|
|
this.require = moduleNoteIds => {
|
|
|
|
return moduleName => {
|
|
|
|
const candidates = allNotes.filter(note => moduleNoteIds.includes(note.noteId));
|
|
|
|
const note = candidates.find(c => c.title === moduleName);
|
|
|
|
|
|
|
|
if (!note) {
|
|
|
|
throw new Error("Could not find module note " + moduleName);
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.modules[note.noteId].exports;
|
|
|
|
}
|
|
|
|
};
|
2018-03-06 23:04:35 -05:00
|
|
|
}
|
|
|
|
|
2018-03-31 08:53:52 -04:00
|
|
|
function ScriptApi(startNote, currentNote) {
|
|
|
|
const repository = new Repository();
|
2018-03-06 23:04:35 -05:00
|
|
|
this.startNote = startNote;
|
|
|
|
this.currentNote = currentNote;
|
2018-03-04 10:32:53 -05:00
|
|
|
|
2018-03-04 12:06:35 -05:00
|
|
|
this.axios = axios;
|
|
|
|
|
2018-02-26 20:47:34 -05:00
|
|
|
this.utils = {
|
|
|
|
unescapeHtml: utils.unescapeHtml,
|
2018-03-10 11:53:51 -05:00
|
|
|
isoDateTimeStr: utils.dateStr,
|
|
|
|
isoDateStr: date => utils.dateStr(date).substr(0, 10)
|
2018-02-26 20:47:34 -05:00
|
|
|
};
|
|
|
|
|
2018-02-24 21:23:04 -05:00
|
|
|
this.getInstanceName = () => config.General ? config.General.instanceName : null;
|
2018-01-27 17:18:19 -05:00
|
|
|
|
|
|
|
this.getNoteById = async function(noteId) {
|
2018-02-24 21:23:04 -05:00
|
|
|
return repository.getNote(noteId);
|
2018-01-27 17:18:19 -05:00
|
|
|
};
|
|
|
|
|
2018-03-24 22:02:26 -04:00
|
|
|
this.getNotesWithLabel = async function (attrName, attrValue) {
|
|
|
|
return await labels.getNotesWithLabel(repository, attrName, attrValue);
|
2018-01-28 12:08:57 -05:00
|
|
|
};
|
|
|
|
|
2018-03-24 22:02:26 -04:00
|
|
|
this.getNoteWithLabel = async function (attrName, attrValue) {
|
|
|
|
const notes = await this.getNotesWithLabel(attrName, attrValue);
|
2018-01-28 12:08:57 -05:00
|
|
|
|
|
|
|
return notes.length > 0 ? notes[0] : null;
|
2018-01-27 17:18:19 -05:00
|
|
|
};
|
|
|
|
|
2018-02-26 00:07:43 -05:00
|
|
|
this.createNote = async function(parentNoteId, title, content = "", extraOptions = {}) {
|
2018-02-26 20:47:34 -05:00
|
|
|
return await notes.createNote(parentNoteId, title, content, extraOptions);
|
2018-01-27 17:18:19 -05:00
|
|
|
};
|
|
|
|
|
2018-03-24 22:02:26 -04:00
|
|
|
this.createLabel = labels.createLabel;
|
2018-02-13 22:46:45 -05:00
|
|
|
|
2018-02-24 21:23:04 -05:00
|
|
|
this.updateEntity = repository.updateEntity;
|
2018-01-27 17:18:19 -05:00
|
|
|
|
2018-03-06 23:04:35 -05:00
|
|
|
this.log = message => log.info(`Script ${currentNote.noteId}: ${message}`);
|
2018-01-27 17:18:19 -05:00
|
|
|
|
2018-02-24 14:42:52 -05:00
|
|
|
this.getRootCalendarNoteId = date_notes.getRootCalendarNoteId;
|
2018-01-27 17:18:19 -05:00
|
|
|
this.getDateNoteId = date_notes.getDateNoteId;
|
2018-02-24 22:44:45 -05:00
|
|
|
|
|
|
|
this.transaction = sql.doInTransaction;
|
2018-01-27 17:18:19 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = ScriptContext;
|