2018-03-25 21:29:35 -04:00
|
|
|
import ScriptContext from "./script_context.js";
|
2018-03-26 22:29:14 -04:00
|
|
|
import server from "./server.js";
|
2019-10-20 10:00:18 +02:00
|
|
|
import toastService from "./toast.js";
|
2021-04-16 23:01:56 +02:00
|
|
|
import froca from "./froca.js";
|
2021-03-18 15:09:08 -05:00
|
|
|
import utils from "./utils.js";
|
2018-03-25 21:16:57 -04:00
|
|
|
|
2018-08-10 13:30:20 +02:00
|
|
|
async function getAndExecuteBundle(noteId, originEntity = null) {
|
2022-12-21 15:19:05 +01:00
|
|
|
const bundle = await server.get(`script/bundle/${noteId}`);
|
2018-07-29 16:06:13 +02:00
|
|
|
|
2019-08-17 11:28:36 +02:00
|
|
|
return await executeBundle(bundle, originEntity);
|
2018-07-29 16:06:13 +02:00
|
|
|
}
|
|
|
|
|
2020-02-25 19:19:10 +01:00
|
|
|
async function executeBundle(bundle, originEntity, $container) {
|
|
|
|
const apiContext = await ScriptContext(bundle.noteId, bundle.allNoteIds, originEntity, $container);
|
2018-03-25 21:16:57 -04:00
|
|
|
|
2018-08-17 11:31:42 +02:00
|
|
|
try {
|
|
|
|
return await (function () {
|
|
|
|
return eval(`const apiContext = this; (async function() { ${bundle.script}\r\n})()`);
|
|
|
|
}.call(apiContext));
|
|
|
|
}
|
|
|
|
catch (e) {
|
2021-04-16 22:57:37 +02:00
|
|
|
const note = await froca.getNote(bundle.noteId);
|
2020-08-15 00:06:26 +02:00
|
|
|
|
|
|
|
toastService.showAndLogError(`Execution of JS note "${note.title}" with ID ${bundle.noteId} failed with error: ${e.message}`);
|
2018-08-17 11:31:42 +02:00
|
|
|
}
|
2018-03-25 21:16:57 -04:00
|
|
|
}
|
|
|
|
|
2018-03-26 22:29:14 -04:00
|
|
|
async function executeStartupBundles() {
|
2021-03-18 15:09:08 -05:00
|
|
|
const isMobile = utils.isMobile();
|
|
|
|
const scriptBundles = await server.get("script/startup" + (isMobile ? "?mobile=true" : ""));
|
2018-03-26 22:29:14 -04:00
|
|
|
|
|
|
|
for (const bundle of scriptBundles) {
|
|
|
|
await executeBundle(bundle);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-16 23:25:52 +01:00
|
|
|
class WidgetsByParent {
|
|
|
|
constructor() {
|
|
|
|
this.byParent = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
add(widget) {
|
|
|
|
if (!widget.parentWidget) {
|
2023-05-05 23:17:23 +02:00
|
|
|
console.log(`Custom widget does not have mandatory 'parentWidget' property defined`);
|
2020-03-16 23:25:52 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.byParent[widget.parentWidget] = this.byParent[widget.parentWidget] || [];
|
|
|
|
this.byParent[widget.parentWidget].push(widget);
|
|
|
|
}
|
|
|
|
|
|
|
|
get(parentName) {
|
|
|
|
return this.byParent[parentName] || [];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-16 21:16:09 +01:00
|
|
|
async function getWidgetBundlesByParent() {
|
|
|
|
const scriptBundles = await server.get("script/widgets");
|
|
|
|
|
2020-03-16 23:25:52 +01:00
|
|
|
const widgetsByParent = new WidgetsByParent();
|
2020-03-16 21:16:09 +01:00
|
|
|
|
|
|
|
for (const bundle of scriptBundles) {
|
|
|
|
let widget;
|
|
|
|
|
|
|
|
try {
|
|
|
|
widget = await executeBundle(bundle);
|
2021-04-21 21:25:52 +01:00
|
|
|
widgetsByParent.add(widget);
|
2020-03-16 21:16:09 +01:00
|
|
|
}
|
|
|
|
catch (e) {
|
2020-10-12 21:05:34 +02:00
|
|
|
logError("Widget initialization failed: ", e);
|
2020-03-16 21:16:09 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-16 23:25:52 +01:00
|
|
|
return widgetsByParent;
|
2020-03-16 21:16:09 +01:00
|
|
|
}
|
|
|
|
|
2018-03-25 21:16:57 -04:00
|
|
|
export default {
|
2018-03-26 22:29:14 -04:00
|
|
|
executeBundle,
|
2018-07-29 16:06:13 +02:00
|
|
|
getAndExecuteBundle,
|
2020-03-16 21:16:09 +01:00
|
|
|
executeStartupBundles,
|
|
|
|
getWidgetBundlesByParent
|
2020-06-14 14:30:57 +02:00
|
|
|
}
|