trilium/src/public/javascripts/services/app_context.js

151 lines
3.8 KiB
JavaScript
Raw Normal View History

import server from "./server.js";
2020-01-16 04:36:01 +08:00
import treeCache from "./tree_cache.js";
2020-01-20 04:12:53 +08:00
import bundleService from "./bundle.js";
2020-02-15 17:41:21 +08:00
import DialogCommandExecutor from "./dialog_command_executor.js";
2020-01-22 05:54:16 +08:00
import Entrypoints from "./entrypoints.js";
import options from "./options.js";
2020-02-03 05:32:44 +08:00
import utils from "./utils.js";
import ZoomService from "./zoom.js";
2020-02-07 04:47:31 +08:00
import Layout from "../widgets/layout.js";
import TabManager from "./tab_manager.js";
2020-02-10 04:13:05 +08:00
import treeService from "./tree.js";
2020-01-12 16:57:28 +08:00
class AppContext {
2020-02-07 04:47:31 +08:00
constructor(layout) {
this.layout = layout;
this.tabManager = new TabManager(this);
2020-02-09 03:53:07 +08:00
this.components = [];
2020-02-15 17:41:21 +08:00
this.executors = [];
this.idToComponent = {};
2020-01-13 02:05:09 +08:00
}
async start() {
options.load(await server.get('options'));
2020-02-03 05:04:28 +08:00
this.showWidgets();
this.tabManager.loadTabs();
2020-02-03 05:32:44 +08:00
2020-02-03 05:04:28 +08:00
bundleService.executeStartupBundles();
}
2020-01-13 02:05:09 +08:00
2020-02-03 05:04:28 +08:00
showWidgets() {
2020-02-07 04:47:31 +08:00
const rootContainer = this.layout.getRootWidget(this);
2020-02-10 05:31:52 +08:00
const $renderedWidget = rootContainer.render();
2020-02-10 05:31:52 +08:00
$("body").append($renderedWidget);
$renderedWidget.on('click', "[data-trigger-event]", e => {
const eventName = $(e.target).attr('data-trigger-event');
this.trigger(eventName);
});
2020-01-21 05:35:52 +08:00
this.components = [
2020-02-09 03:53:07 +08:00
this.tabManager,
rootContainer,
2020-02-15 17:41:21 +08:00
new Entrypoints(this)
];
this.executors = [
new DialogCommandExecutor(this, this)
2020-01-14 04:48:44 +08:00
];
if (utils.isElectron()) {
2020-02-15 16:43:47 +08:00
this.components.push(new ZoomService(this, this));
import("./spell_check.js").then(spellCheckService => spellCheckService.initSpellCheck());
}
this.trigger('initialRenderComplete');
}
registerComponent(componentId, component) {
this.idToComponent[componentId] = component;
}
findComponentById(componentId) {
return this.idToComponent[componentId];
}
getComponentByEl(el) {
return $(el).closest(".component").prop('component');
}
2020-02-13 03:31:31 +08:00
async trigger(name, data) {
2020-01-13 06:03:55 +08:00
this.eventReceived(name, data);
2020-02-13 03:31:31 +08:00
const promises = [];
2020-01-22 05:54:16 +08:00
for (const component of this.components) {
2020-02-13 03:31:31 +08:00
promises.push(component.eventReceived(name, data));
2020-01-16 04:36:01 +08:00
}
2020-02-13 03:31:31 +08:00
await Promise.all(promises);
}
2020-02-13 03:31:31 +08:00
async eventReceived(name, data) {
2020-01-13 06:03:55 +08:00
const fun = this[name + 'Listener'];
if (typeof fun === 'function') {
2020-02-13 03:31:31 +08:00
await fun.call(this, data);
2020-01-13 06:03:55 +08:00
}
}
async protectedSessionStartedListener() {
await treeCache.loadInitialTree();
this.trigger('treeCacheReloaded');
}
2020-02-15 17:41:21 +08:00
2020-02-16 18:22:37 +08:00
async triggerCommand(name, data = {}) {
2020-02-15 17:41:21 +08:00
for (const executor of this.executors) {
const fun = executor[name + 'Command'];
const called = await this.callMethod(executor, fun, data);
if (called) {
return;
}
}
console.error(`Unhandled command ${name}`);
}
async callMethod(thiz, fun, data) {
if (typeof fun !== 'function') {
return false;
}
await fun.call(thiz, data);
return true;
}
2020-01-13 02:05:09 +08:00
}
2020-02-07 04:47:31 +08:00
const layout = new Layout();
const appContext = new AppContext(layout);
2020-02-02 17:41:43 +08:00
// we should save all outstanding changes before the page/app is closed
$(window).on('beforeunload', () => {
appContext.trigger('beforeUnload');
});
function isNotePathInAddress() {
const [notePath, tabId] = getHashValueFromAddress();
return notePath.startsWith("root")
// empty string is for empty/uninitialized tab
|| (notePath === '' && !!tabId);
}
$(window).on('hashchange', function() {
if (isNotePathInAddress()) {
2020-02-10 04:13:05 +08:00
const [notePath, tabId] = treeService.getHashValueFromAddress();
2020-02-09 03:53:07 +08:00
appContext.tabManager.switchToTab(tabId, notePath);
}
});
2020-01-12 16:57:28 +08:00
export default appContext;