2018-03-25 13:41:29 -04:00
|
|
|
import treeService from './tree.js';
|
2018-03-25 19:49:33 -04:00
|
|
|
import server from './server.js';
|
2018-04-04 23:51:47 -04:00
|
|
|
import utils from './utils.js';
|
2018-04-08 07:48:47 -04:00
|
|
|
import infoService from './info.js';
|
2018-07-29 16:06:13 +02:00
|
|
|
import linkService from './link.js';
|
2018-03-25 11:09:17 -04:00
|
|
|
|
2018-08-10 13:30:20 +02:00
|
|
|
function ScriptApi(startNote, currentNote, originEntity = null) {
|
2018-02-14 23:31:20 -05:00
|
|
|
const $pluginButtons = $("#plugin-buttons");
|
2018-02-03 10:37:57 -05:00
|
|
|
|
|
|
|
|
async function activateNote(notePath) {
|
2018-03-24 21:39:15 -04:00
|
|
|
await treeService.activateNode(notePath);
|
2018-02-03 10:37:57 -05:00
|
|
|
}
|
|
|
|
|
|
2018-08-14 13:50:04 +02:00
|
|
|
async function activateNewNote(notePath) {
|
|
|
|
|
await treeService.reload();
|
|
|
|
|
|
|
|
|
|
await treeService.activateNode(notePath, true);
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-03 10:37:57 -05:00
|
|
|
function addButtonToToolbar(buttonId, button) {
|
|
|
|
|
$("#" + buttonId).remove();
|
|
|
|
|
|
|
|
|
|
button.attr('id', buttonId);
|
|
|
|
|
|
2018-02-14 23:31:20 -05:00
|
|
|
$pluginButtons.append(button);
|
2018-02-03 10:37:57 -05:00
|
|
|
}
|
|
|
|
|
|
2018-03-04 21:43:14 -05:00
|
|
|
function prepareParams(params) {
|
|
|
|
|
if (!params) {
|
|
|
|
|
return params;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return params.map(p => {
|
|
|
|
|
if (typeof p === "function") {
|
|
|
|
|
return "!@#Function: " + p.toString();
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
return p;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-06 23:04:35 -05:00
|
|
|
async function runOnServer(script, params = []) {
|
2018-03-04 21:43:14 -05:00
|
|
|
if (typeof script === "function") {
|
|
|
|
|
script = script.toString();
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-04 23:28:26 -05:00
|
|
|
const ret = await server.post('script/exec', {
|
|
|
|
|
script: script,
|
|
|
|
|
params: prepareParams(params),
|
2018-03-06 23:04:35 -05:00
|
|
|
startNoteId: startNote.noteId,
|
2018-07-29 16:06:13 +02:00
|
|
|
currentNoteId: currentNote.noteId,
|
2018-08-10 14:31:57 +02:00
|
|
|
originEntityName: originEntity ? originEntity.constructor.tableName : null,
|
2018-08-10 13:30:20 +02:00
|
|
|
originEntityId: originEntity ? originEntity.noteId : null
|
2018-03-04 23:28:26 -05:00
|
|
|
});
|
2018-03-04 21:43:14 -05:00
|
|
|
|
|
|
|
|
return ret.executionResult;
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-03 10:37:57 -05:00
|
|
|
return {
|
2018-03-06 23:04:35 -05:00
|
|
|
startNote: startNote,
|
|
|
|
|
currentNote: currentNote,
|
2018-08-10 13:30:20 +02:00
|
|
|
originEntity: originEntity,
|
2018-02-03 10:37:57 -05:00
|
|
|
addButtonToToolbar,
|
2018-02-24 21:23:04 -05:00
|
|
|
activateNote,
|
2018-08-14 13:50:04 +02:00
|
|
|
activateNewNote,
|
2018-03-25 22:56:23 -04:00
|
|
|
getInstanceName: () => window.glob.instanceName,
|
2018-04-04 23:51:47 -04:00
|
|
|
runOnServer,
|
2018-04-06 19:08:42 -04:00
|
|
|
formatDateISO: utils.formatDateISO,
|
2018-04-07 13:14:01 -04:00
|
|
|
parseDate: utils.parseDate,
|
2018-04-08 07:48:47 -04:00
|
|
|
showMessage: infoService.showMessage,
|
|
|
|
|
showError: infoService.showError,
|
2018-08-14 12:54:58 +02:00
|
|
|
reloadTree: treeService.reload, // deprecated
|
|
|
|
|
refreshTree: treeService.reload,
|
2018-07-29 16:06:13 +02:00
|
|
|
createNoteLink: linkService.createNoteLink
|
2018-02-03 10:37:57 -05:00
|
|
|
}
|
2018-03-25 11:09:17 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default ScriptApi;
|