Notes/src/public/app/widgets/containers/launcher_container.js

157 lines
6.7 KiB
JavaScript
Raw Normal View History

2022-08-04 23:00:32 +02:00
import FlexContainer from "./flex_container.js";
import froca from "../../services/froca.js";
import ButtonWidget from "../buttons/button_widget.js";
2022-08-05 16:44:26 +02:00
import CalendarWidget from "../buttons/calendar.js";
import appContext from "../../services/app_context.js";
2022-08-05 19:15:28 +02:00
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";
2022-11-25 15:29:57 +01:00
import BackInHistoryButtonWidget from "../buttons/history/history_back.js";
import ForwardInHistoryButtonWidget from "../buttons/history/history_forward.js";
2022-11-29 16:16:57 +01:00
import dialogService from "../../services/dialog.js";
2022-08-04 23:00:32 +02:00
2022-12-01 10:16:57 +01:00
export default class LauncherContainer extends FlexContainer {
2022-08-04 23:00:32 +02:00
constructor() {
super('column');
2022-12-01 10:16:57 +01:00
this.id('launcher-container');
2022-08-04 23:00:32 +02:00
this.css('height', '100%');
this.filling();
this.load();
}
async load() {
this.children = [];
2022-12-01 10:16:57 +01:00
const visibleLaunchersRoot = await froca.getNote('lb_visiblelaunchers', true);
2022-08-06 15:00:56 +02:00
2022-12-01 10:16:57 +01:00
if (!visibleLaunchersRoot) {
console.log("Visible launchers root note doesn't exist.");
2022-08-06 15:00:56 +02:00
return;
}
2022-08-04 23:00:32 +02:00
2022-11-28 23:39:23 +01:00
await Promise.allSettled(
2022-12-01 10:16:57 +01:00
(await visibleLaunchersRoot.getChildNotes())
.map(launcher => this.initLauncher(launcher))
2022-11-28 23:39:23 +01:00
);
2022-08-04 23:00:32 +02:00
this.$widget.empty();
this.renderChildren();
2022-11-27 23:43:25 +01:00
await this.handleEventInChildren('initialRenderComplete');
const activeContext = appContext.tabManager.getActiveContext();
2022-11-28 23:39:23 +01:00
if (activeContext) {
await this.handleEvent('setNoteContext', {
noteContext: activeContext
});
if (activeContext.notePath) {
await this.handleEvent('noteSwitched', {
noteContext: activeContext,
notePath: activeContext.notePath
});
}
}
2022-11-27 23:43:25 +01:00
}
2022-12-01 10:16:57 +01:00
async initLauncher(launcher) {
2022-11-28 23:39:23 +01:00
try {
2022-12-01 10:16:57 +01:00
if (launcher.type !== 'launcher') {
console.warn(`Note ${launcher.noteId} is not a launcher even though it's in launcher subtree`);
2022-11-28 23:39:23 +01:00
return;
}
2022-11-27 23:43:25 +01:00
2022-12-01 10:16:57 +01:00
const launcherType = launcher.getLabelValue("launcherType");
2022-11-29 16:16:57 +01:00
2022-12-01 10:16:57 +01:00
if (launcherType === 'command') {
2022-11-28 23:39:23 +01:00
this.child(new ButtonWidget()
2022-12-01 10:16:57 +01:00
.title(launcher.title)
.icon(launcher.getIcon())
.command(launcher.getLabelValue("command")));
} else if (launcherType === 'note') {
// we're intentionally displaying the launcher title and icon instead of the target
// e.g. you want to make launchers to 2 mermaid diagrams which both have mermaid icon (ok),
2022-11-30 16:57:51 +01:00
// but on the launchpad you want them distinguishable.
// for titles, the note titles may follow a different scheme than maybe desirable on the launchpad
// another reason is the discrepancy between what user sees on the launchpad and in the config (esp. icons).
// The only (but major) downside is more work in setting up the typical case where you actually want to have both title and icon in sync.
this.child(new ButtonWidget()
2022-12-01 10:16:57 +01:00
.title(launcher.title)
.icon(launcher.getIcon())
2022-11-29 16:16:57 +01:00
.onClick(() => {
2022-12-01 10:16:57 +01:00
const targetNoteId = launcher.getRelationValue('targetNote');
2022-11-29 16:16:57 +01:00
if (!targetNoteId) {
2022-12-01 10:16:57 +01:00
dialogService.info("This launcher doesn't define target note.");
2022-11-29 16:16:57 +01:00
return;
}
appContext.tabManager.openTabWithNoteWithHoisting(targetNoteId, true)
}));
2022-12-01 10:16:57 +01:00
} else if (launcherType === 'script') {
2022-11-28 23:39:23 +01:00
this.child(new ButtonWidget()
2022-12-01 10:16:57 +01:00
.title(launcher.title)
.icon(launcher.getIcon())
2022-11-28 23:39:23 +01:00
.onClick(async () => {
2022-12-01 10:16:57 +01:00
const script = await launcher.getRelationTarget('script');
2022-11-28 23:39:23 +01:00
await script.executeScript();
}));
2022-12-01 10:16:57 +01:00
} else if (launcherType === 'customWidget') {
const widget = await launcher.getRelationTarget('widget');
2022-11-28 23:39:23 +01:00
2022-11-30 16:57:51 +01:00
if (widget) {
const res = await widget.executeScript();
2022-11-28 23:39:23 +01:00
2022-11-30 16:57:51 +01:00
this.child(res);
}
2022-12-01 10:16:57 +01:00
} else if (launcherType === 'builtinWidget') {
const builtinWidget = launcher.getLabelValue("builtinWidget");
2022-11-28 23:39:23 +01:00
if (builtinWidget) {
if (builtinWidget === 'calendar') {
2022-12-01 10:16:57 +01:00
this.child(new CalendarWidget(launcher.title, launcher.getIcon()));
2022-11-28 23:39:23 +01:00
} else if (builtinWidget === 'spacer') {
// || has to be inside since 0 is a valid value
2022-12-01 10:16:57 +01:00
const baseSize = parseInt(launcher.getLabelValue("baseSize") || "40");
const growthFactor = parseInt(launcher.getLabelValue("growthFactor") || "100");
2022-11-28 23:39:23 +01:00
this.child(new SpacerWidget(baseSize, growthFactor));
} else if (builtinWidget === 'bookmarks') {
this.child(new BookmarkButtons());
} else if (builtinWidget === 'protectedSession') {
this.child(new ProtectedSessionStatusWidget());
} else if (builtinWidget === 'syncStatus') {
this.child(new SyncStatusWidget());
} else if (builtinWidget === 'backInHistoryButton') {
this.child(new BackInHistoryButtonWidget());
} else if (builtinWidget === 'forwardInHistoryButton') {
this.child(new ForwardInHistoryButtonWidget());
} else {
2022-12-01 10:16:57 +01:00
console.warn(`Unrecognized builtin widget ${builtinWidget} for launcher ${launcher.noteId} "${launcher.title}"`);
2022-11-28 23:39:23 +01:00
}
2022-11-27 23:43:25 +01:00
}
2022-11-29 16:16:57 +01:00
} else {
2022-12-01 10:16:57 +01:00
console.warn(`Unrecognized launcher type ${launcherType} for launcher '${launcher.noteId}' title ${launcher.title}`);
2022-11-27 23:43:25 +01:00
}
}
2022-11-28 23:39:23 +01:00
catch (e) {
2022-12-01 10:16:57 +01:00
console.error(`Initialization of launcher '${launcher.noteId}' with title '${launcher.title}' failed with error: ${e.message} ${e.stack}`);
2022-11-28 23:39:23 +01:00
}
2022-08-04 23:00:32 +02:00
}
entitiesReloadedEvent({loadResults}) {
2022-08-07 15:34:59 +02:00
if (loadResults.getNoteIds().find(noteId => froca.notes[noteId]?.isLaunchBarConfig())
|| loadResults.getBranches().find(branch => branch.parentNoteId.startsWith("lb_"))
|| loadResults.getAttributes().find(attr => froca.notes[attr.noteId]?.isLaunchBarConfig())) {
2022-08-04 23:00:32 +02:00
this.load();
}
}
}