2018-01-28 06:18:19 +08:00
|
|
|
const log = require('./log');
|
|
|
|
const protected_session = require('./protected_session');
|
|
|
|
const notes = require('./notes');
|
2018-02-25 11:44:45 +08:00
|
|
|
const sql = require('./sql');
|
2018-02-27 09:47:34 +08:00
|
|
|
const utils = require('./utils');
|
2018-01-28 06:18:19 +08:00
|
|
|
const attributes = require('./attributes');
|
|
|
|
const date_notes = require('./date_notes');
|
2018-02-25 10:23:04 +08:00
|
|
|
const config = require('./config');
|
2018-01-30 12:17:44 +08:00
|
|
|
const Repository = require('./repository');
|
2018-02-27 09:47:34 +08:00
|
|
|
const axios = require('axios');
|
2018-01-28 06:18:19 +08:00
|
|
|
|
2018-03-05 01:06:35 +08:00
|
|
|
function ScriptContext(dataKey, startNote, allNotes) {
|
2018-02-25 10:23:04 +08:00
|
|
|
dataKey = protected_session.getDataKey(dataKey);
|
|
|
|
|
2018-03-07 12:04:35 +08:00
|
|
|
this.modules = {};
|
|
|
|
this.notes = utils.toObject(allNotes, note => [note.noteId, note]);
|
|
|
|
this.apis = utils.toObject(allNotes, note => [note.noteId, new ScriptApi(dataKey, startNote, note)]);
|
2018-03-11 00:53:51 +08: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-07 12:04:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
function ScriptApi(dataKey, startNote, currentNote) {
|
|
|
|
const repository = new Repository(dataKey);
|
|
|
|
this.startNote = startNote;
|
|
|
|
this.currentNote = currentNote;
|
2018-03-04 23:32:53 +08:00
|
|
|
|
2018-03-05 01:06:35 +08:00
|
|
|
this.axios = axios;
|
|
|
|
|
2018-02-27 09:47:34 +08:00
|
|
|
this.utils = {
|
|
|
|
unescapeHtml: utils.unescapeHtml,
|
2018-03-11 00:53:51 +08:00
|
|
|
isoDateTimeStr: utils.dateStr,
|
|
|
|
isoDateStr: date => utils.dateStr(date).substr(0, 10)
|
2018-02-27 09:47:34 +08:00
|
|
|
};
|
|
|
|
|
2018-02-25 10:23:04 +08:00
|
|
|
this.getInstanceName = () => config.General ? config.General.instanceName : null;
|
2018-01-28 06:18:19 +08:00
|
|
|
|
|
|
|
this.getNoteById = async function(noteId) {
|
2018-02-25 10:23:04 +08:00
|
|
|
return repository.getNote(noteId);
|
2018-01-28 06:18:19 +08:00
|
|
|
};
|
|
|
|
|
2018-01-29 01:08:57 +08:00
|
|
|
this.getNotesWithAttribute = async function (attrName, attrValue) {
|
2018-03-05 03:21:11 +08:00
|
|
|
return await attributes.getNotesWithAttribute(repository, attrName, attrValue);
|
2018-01-29 01:08:57 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
this.getNoteWithAttribute = async function (attrName, attrValue) {
|
2018-01-31 10:25:47 +08:00
|
|
|
const notes = await this.getNotesWithAttribute(attrName, attrValue);
|
2018-01-29 01:08:57 +08:00
|
|
|
|
|
|
|
return notes.length > 0 ? notes[0] : null;
|
2018-01-28 06:18:19 +08:00
|
|
|
};
|
|
|
|
|
2018-02-26 13:07:43 +08:00
|
|
|
this.createNote = async function(parentNoteId, title, content = "", extraOptions = {}) {
|
|
|
|
extraOptions.dataKey = dataKey;
|
2018-01-28 06:18:19 +08:00
|
|
|
|
2018-02-27 09:47:34 +08:00
|
|
|
return await notes.createNote(parentNoteId, title, content, extraOptions);
|
2018-01-28 06:18:19 +08:00
|
|
|
};
|
|
|
|
|
2018-02-14 11:46:45 +08:00
|
|
|
this.createAttribute = attributes.createAttribute;
|
|
|
|
|
2018-02-25 10:23:04 +08:00
|
|
|
this.updateEntity = repository.updateEntity;
|
2018-01-28 06:18:19 +08:00
|
|
|
|
2018-03-07 12:04:35 +08:00
|
|
|
this.log = message => log.info(`Script ${currentNote.noteId}: ${message}`);
|
2018-01-28 06:18:19 +08:00
|
|
|
|
2018-02-25 03:42:52 +08:00
|
|
|
this.getRootCalendarNoteId = date_notes.getRootCalendarNoteId;
|
2018-01-28 06:18:19 +08:00
|
|
|
this.getDateNoteId = date_notes.getDateNoteId;
|
2018-02-25 11:44:45 +08:00
|
|
|
|
|
|
|
this.transaction = sql.doInTransaction;
|
2018-01-28 06:18:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = ScriptContext;
|