2022-12-01 16:22:04 +01:00
|
|
|
import CalendarWidget from "../buttons/calendar.js";
|
|
|
|
import SpacerWidget from "../spacer.js";
|
|
|
|
import BookmarkButtons from "../bookmark_buttons.js";
|
|
|
|
import ProtectedSessionStatusWidget from "../buttons/protected_session_status.js";
|
|
|
|
import SyncStatusWidget from "../sync_status.js";
|
|
|
|
import BasicWidget from "../basic_widget.js";
|
2022-12-02 16:46:14 +01:00
|
|
|
import NoteLauncher from "../buttons/launcher/note_launcher.js";
|
|
|
|
import ScriptLauncher from "../buttons/launcher/script_launcher.js";
|
|
|
|
import CommandButtonWidget from "../buttons/command_button.js";
|
2022-12-12 23:48:34 +01:00
|
|
|
import utils from "../../services/utils.js";
|
2022-12-18 23:25:35 +01:00
|
|
|
import TodayLauncher from "../buttons/launcher/today_launcher.js";
|
2022-12-23 20:17:04 +01:00
|
|
|
import HistoryNavigationButton from "../buttons/history_navigation.js";
|
2024-11-22 21:57:03 +02:00
|
|
|
import QuickSearchWidget from "../quick_search.js";
|
2022-12-01 16:22:04 +01:00
|
|
|
|
|
|
|
export default class LauncherWidget extends BasicWidget {
|
2022-12-02 16:46:14 +01:00
|
|
|
constructor() {
|
2022-12-01 16:22:04 +01:00
|
|
|
super();
|
2022-12-07 12:46:41 +01:00
|
|
|
|
2022-12-01 16:22:04 +01:00
|
|
|
this.innerWidget = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
isEnabled() {
|
|
|
|
return this.innerWidget.isEnabled();
|
|
|
|
}
|
|
|
|
|
|
|
|
doRender() {
|
|
|
|
this.$widget = this.innerWidget.render();
|
|
|
|
}
|
|
|
|
|
2022-12-02 16:46:14 +01:00
|
|
|
async initLauncher(note) {
|
|
|
|
if (note.type !== 'launcher') {
|
|
|
|
throw new Error(`Note '${note.noteId}' '${note.title}' is not a launcher even though it's in the launcher subtree`);
|
|
|
|
}
|
2022-12-01 16:22:04 +01:00
|
|
|
|
2023-06-29 00:14:12 +02:00
|
|
|
if (!utils.isDesktop() && note.isLabelTruthy('desktopOnly')) {
|
2022-12-12 23:48:34 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-12-02 16:46:14 +01:00
|
|
|
const launcherType = note.getLabelValue("launcherType");
|
2022-12-01 16:22:04 +01:00
|
|
|
|
2022-12-24 13:57:42 +01:00
|
|
|
if (glob.TRILIUM_SAFE_MODE && launcherType === 'customWidget') {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-12-02 16:46:14 +01:00
|
|
|
if (launcherType === 'command') {
|
2022-12-11 22:26:18 +01:00
|
|
|
this.innerWidget = this.initCommandLauncherWidget(note)
|
2022-12-11 13:20:37 +01:00
|
|
|
.class("launcher-button");
|
2022-12-01 16:22:04 +01:00
|
|
|
} else if (launcherType === 'note') {
|
2022-12-11 13:20:37 +01:00
|
|
|
this.innerWidget = new NoteLauncher(note)
|
|
|
|
.class("launcher-button");
|
2022-12-01 16:22:04 +01:00
|
|
|
} else if (launcherType === 'script') {
|
2022-12-11 13:20:37 +01:00
|
|
|
this.innerWidget = new ScriptLauncher(note)
|
|
|
|
.class("launcher-button");
|
2022-12-01 16:22:04 +01:00
|
|
|
} else if (launcherType === 'customWidget') {
|
2022-12-02 16:46:14 +01:00
|
|
|
this.innerWidget = await this.initCustomWidget(note);
|
2022-12-01 16:22:04 +01:00
|
|
|
} else if (launcherType === 'builtinWidget') {
|
2022-12-02 16:46:14 +01:00
|
|
|
this.innerWidget = this.initBuiltinWidget(note);
|
2022-12-01 16:22:04 +01:00
|
|
|
} else {
|
2022-12-07 12:46:41 +01:00
|
|
|
throw new Error(`Unrecognized launcher type '${launcherType}' for launcher '${note.noteId}' title '${note.title}'`);
|
2022-12-01 16:22:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!this.innerWidget) {
|
2022-12-02 16:46:14 +01:00
|
|
|
throw new Error(`Unknown initialization error for note '${note.noteId}', title '${note.title}'`);
|
2022-12-01 16:22:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
this.child(this.innerWidget);
|
2022-12-12 23:48:34 +01:00
|
|
|
|
|
|
|
return true;
|
2022-12-01 16:22:04 +01:00
|
|
|
}
|
|
|
|
|
2022-12-11 22:26:18 +01:00
|
|
|
initCommandLauncherWidget(note) {
|
2022-12-02 16:46:14 +01:00
|
|
|
return new CommandButtonWidget()
|
2022-12-23 20:17:04 +01:00
|
|
|
.title(() => note.title)
|
|
|
|
.icon(() => note.getIcon())
|
2022-12-02 16:46:14 +01:00
|
|
|
.command(() => note.getLabelValue("command"));
|
|
|
|
}
|
2022-12-01 16:22:04 +01:00
|
|
|
|
2022-12-02 16:46:14 +01:00
|
|
|
async initCustomWidget(note) {
|
|
|
|
const widget = await note.getRelationTarget('widget');
|
2022-12-01 16:22:04 +01:00
|
|
|
|
2022-12-02 16:46:14 +01:00
|
|
|
if (widget) {
|
|
|
|
return await widget.executeScript();
|
2022-12-01 16:22:04 +01:00
|
|
|
} else {
|
2022-12-02 16:46:14 +01:00
|
|
|
throw new Error(`Custom widget of launcher '${note.noteId}' '${note.title}' is not defined.`);
|
2022-12-01 16:22:04 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-02 16:46:14 +01:00
|
|
|
initBuiltinWidget(note) {
|
|
|
|
const builtinWidget = note.getLabelValue("builtinWidget");
|
2024-11-22 21:56:34 +02:00
|
|
|
switch (builtinWidget) {
|
|
|
|
case "calendar":
|
2024-11-22 21:57:03 +02:00
|
|
|
return new CalendarWidget(note.title, note.getIcon());
|
2024-11-22 21:56:34 +02:00
|
|
|
case "spacer":
|
2024-11-22 21:57:03 +02:00
|
|
|
// || has to be inside since 0 is a valid value
|
|
|
|
const baseSize = parseInt(note.getLabelValue("baseSize") || "40");
|
|
|
|
const growthFactor = parseInt(note.getLabelValue("growthFactor") || "100");
|
|
|
|
|
|
|
|
return new SpacerWidget(baseSize, growthFactor);
|
2024-11-22 21:56:34 +02:00
|
|
|
case "bookmarks":
|
2024-11-22 21:57:03 +02:00
|
|
|
return new BookmarkButtons();
|
2024-11-22 21:56:34 +02:00
|
|
|
case "protectedSession":
|
2024-11-22 21:57:03 +02:00
|
|
|
return new ProtectedSessionStatusWidget();
|
2024-11-22 21:56:34 +02:00
|
|
|
case "syncStatus":
|
2024-11-22 21:57:03 +02:00
|
|
|
return new SyncStatusWidget();
|
2024-11-22 21:56:34 +02:00
|
|
|
case "backInHistoryButton":
|
2024-11-22 21:57:03 +02:00
|
|
|
return new HistoryNavigationButton(note, "backInNoteHistory");
|
2024-11-22 21:56:34 +02:00
|
|
|
case "forwardInHistoryButton":
|
2024-11-22 21:57:03 +02:00
|
|
|
return new HistoryNavigationButton(note, "forwardInNoteHistory");
|
2024-11-22 21:56:34 +02:00
|
|
|
case "todayInJournal":
|
2024-11-22 21:57:03 +02:00
|
|
|
return new TodayLauncher(note);
|
|
|
|
case "quickSearch":
|
|
|
|
return new QuickSearchWidget();
|
2024-11-22 21:56:34 +02:00
|
|
|
default:
|
2024-11-22 21:57:03 +02:00
|
|
|
throw new Error(`Unrecognized builtin widget ${builtinWidget} for launcher ${note.noteId} "${note.title}"`);
|
2022-12-01 16:22:04 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|