Notes/src/services/script_context.js

66 lines
2.1 KiB
JavaScript
Raw Normal View History

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