2018-01-28 06:18:19 +08:00
|
|
|
const log = require('./log');
|
2018-04-02 09:27:46 +08:00
|
|
|
const noteService = 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-04-03 08:46:46 +08:00
|
|
|
const dateUtils = require('./date_utils');
|
2018-08-07 18:48:11 +08:00
|
|
|
const attributeService = require('./attributes');
|
2018-04-02 09:27:46 +08:00
|
|
|
const dateNoteService = require('./date_notes');
|
2018-08-01 15:26:02 +08:00
|
|
|
const treeService = require('./tree');
|
2018-02-25 10:23:04 +08:00
|
|
|
const config = require('./config');
|
2018-03-31 21:07:58 +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-08-10 02:08:00 +08:00
|
|
|
function ScriptContext(startNote, allNotes, workEntity = null) {
|
2018-03-07 12:04:35 +08:00
|
|
|
this.modules = {};
|
|
|
|
this.notes = utils.toObject(allNotes, note => [note.noteId, note]);
|
2018-08-10 02:08:00 +08:00
|
|
|
this.apis = utils.toObject(allNotes, note => [note.noteId, new ScriptApi(startNote, note, workEntity)]);
|
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
|
|
|
}
|
|
|
|
|
2018-08-10 02:08:00 +08:00
|
|
|
function ScriptApi(startNote, currentNote, workEntity) {
|
2018-03-07 12:04:35 +08:00
|
|
|
this.startNote = startNote;
|
|
|
|
this.currentNote = currentNote;
|
2018-08-10 02:08:00 +08:00
|
|
|
this.workEntity = workEntity;
|
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-04-03 08:46:46 +08:00
|
|
|
isoDateTimeStr: dateUtils.dateStr,
|
|
|
|
isoDateStr: date => dateUtils.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
|
|
|
|
2018-04-02 00:45:35 +08:00
|
|
|
this.getNote = repository.getNote;
|
|
|
|
this.getBranch = repository.getBranch;
|
2018-08-07 18:48:11 +08:00
|
|
|
this.getAttribute = repository.getAttribute;
|
2018-04-02 00:45:35 +08:00
|
|
|
this.getImage = repository.getImage;
|
|
|
|
this.getEntity = repository.getEntity;
|
|
|
|
this.getEntities = repository.getEntities;
|
2018-01-28 06:18:19 +08:00
|
|
|
|
2018-08-07 18:48:11 +08:00
|
|
|
this.createAttribute = attributeService.createAttribute;
|
|
|
|
this.getNotesWithLabel = attributeService.getNotesWithLabel;
|
|
|
|
this.getNoteWithLabel = attributeService.getNoteWithLabel;
|
2018-04-03 10:53:01 +08:00
|
|
|
|
|
|
|
this.createNote = noteService.createNote;
|
2018-02-14 11:46:45 +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-04-08 01:03:16 +08:00
|
|
|
this.getRootCalendarNote = dateNoteService.getRootCalendarNote;
|
|
|
|
this.getDateNote = dateNoteService.getDateNote;
|
2018-02-25 11:44:45 +08:00
|
|
|
|
2018-08-01 15:26:02 +08:00
|
|
|
this.sortNotesAlphabetically = treeService.sortNotesAlphabetically;
|
|
|
|
|
2018-04-08 01:03:16 +08:00
|
|
|
this.transactional = sql.transactional;
|
2018-01-28 06:18:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = ScriptContext;
|