2018-01-27 17:18:19 -05:00
|
|
|
const log = require('./log');
|
2018-04-01 21:27:46 -04:00
|
|
|
const noteService = 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-04-02 20:46:46 -04:00
|
|
|
const dateUtils = require('./date_utils');
|
2018-04-01 21:27:46 -04:00
|
|
|
const labelService = require('./labels');
|
|
|
|
const dateNoteService = require('./date_notes');
|
2018-08-01 09:26:02 +02:00
|
|
|
const treeService = require('./tree');
|
2018-02-24 21:23:04 -05:00
|
|
|
const config = require('./config');
|
2018-03-31 09:07:58 -04: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-07-29 16:06:13 +02:00
|
|
|
function ScriptContext(startNote, allNotes, targetNote = null) {
|
2018-03-06 23:04:35 -05:00
|
|
|
this.modules = {};
|
|
|
|
this.notes = utils.toObject(allNotes, note => [note.noteId, note]);
|
2018-07-29 16:06:13 +02:00
|
|
|
this.apis = utils.toObject(allNotes, note => [note.noteId, new ScriptApi(startNote, note, targetNote)]);
|
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-07-29 16:06:13 +02:00
|
|
|
function ScriptApi(startNote, currentNote, targetNote) {
|
2018-03-06 23:04:35 -05:00
|
|
|
this.startNote = startNote;
|
|
|
|
this.currentNote = currentNote;
|
2018-07-29 16:06:13 +02:00
|
|
|
this.targetNote = targetNote;
|
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-04-02 20:46:46 -04:00
|
|
|
isoDateTimeStr: dateUtils.dateStr,
|
|
|
|
isoDateStr: date => dateUtils.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
|
|
|
|
2018-04-01 12:45:35 -04:00
|
|
|
this.getNote = repository.getNote;
|
|
|
|
this.getBranch = repository.getBranch;
|
|
|
|
this.getLabel = repository.getLabel;
|
2018-07-29 16:06:13 +02:00
|
|
|
this.getRelation = repository.getRelation;
|
2018-04-01 12:45:35 -04:00
|
|
|
this.getImage = repository.getImage;
|
|
|
|
this.getEntity = repository.getEntity;
|
|
|
|
this.getEntities = repository.getEntities;
|
2018-01-27 17:18:19 -05:00
|
|
|
|
2018-04-01 21:27:46 -04:00
|
|
|
this.createLabel = labelService.createLabel;
|
2018-04-02 22:53:01 -04:00
|
|
|
this.getNotesWithLabel = labelService.getNotesWithLabel;
|
|
|
|
this.getNoteWithLabel = labelService.getNoteWithLabel;
|
|
|
|
|
|
|
|
this.createNote = noteService.createNote;
|
2018-02-13 22:46:45 -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-04-07 13:03:16 -04:00
|
|
|
this.getRootCalendarNote = dateNoteService.getRootCalendarNote;
|
|
|
|
this.getDateNote = dateNoteService.getDateNote;
|
2018-02-24 22:44:45 -05:00
|
|
|
|
2018-08-01 09:26:02 +02:00
|
|
|
this.sortNotesAlphabetically = treeService.sortNotesAlphabetically;
|
|
|
|
|
2018-04-07 13:03:16 -04:00
|
|
|
this.transactional = sql.transactional;
|
2018-01-27 17:18:19 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = ScriptContext;
|