2020-01-12 19:48:17 +08:00
|
|
|
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";
|
2020-02-06 05:08:45 +08:00
|
|
|
import options from "./options.js";
|
2020-02-03 05:32:44 +08:00
|
|
|
import utils from "./utils.js";
|
2020-02-06 05:08:45 +08:00
|
|
|
import ZoomService from "./zoom.js";
|
2020-02-07 04:47:31 +08:00
|
|
|
import Layout from "../widgets/layout.js";
|
2020-02-08 04:08:55 +08:00
|
|
|
import TabManager from "./tab_manager.js";
|
2020-02-10 04:13:05 +08:00
|
|
|
import treeService from "./tree.js";
|
2020-02-17 02:21:17 +08:00
|
|
|
import Component from "../widgets/component.js";
|
2020-02-17 03:09:59 +08:00
|
|
|
import keyboardActionsService from "./keyboard_actions.js";
|
2020-01-12 04:19:56 +08:00
|
|
|
|
2020-02-17 02:21:17 +08:00
|
|
|
class AppContext extends Component {
|
2020-02-07 04:47:31 +08:00
|
|
|
constructor(layout) {
|
2020-02-17 02:21:17 +08:00
|
|
|
super(null);
|
|
|
|
|
2020-02-07 04:47:31 +08:00
|
|
|
this.layout = layout;
|
2020-02-15 17:41:21 +08:00
|
|
|
this.executors = [];
|
2020-01-13 02:05:09 +08:00
|
|
|
}
|
|
|
|
|
2020-02-06 05:08:45 +08:00
|
|
|
async start() {
|
|
|
|
options.load(await server.get('options'));
|
|
|
|
|
2020-02-03 05:04:28 +08:00
|
|
|
this.showWidgets();
|
|
|
|
|
2020-02-08 04:08:55 +08:00
|
|
|
this.tabManager.loadTabs();
|
2020-02-03 05:32:44 +08:00
|
|
|
|
2020-02-25 18:02:59 +08:00
|
|
|
setTimeout(() => bundleService.executeStartupBundles(), 2000);
|
2020-02-03 05:04:28 +08:00
|
|
|
}
|
2020-01-13 02:05:09 +08:00
|
|
|
|
2020-02-03 05:04:28 +08:00
|
|
|
showWidgets() {
|
2020-02-17 02:21:17 +08:00
|
|
|
const rootWidget = this.layout.getRootWidget(this);
|
|
|
|
const $renderedWidget = rootWidget.render();
|
2020-01-15 04:23:32 +08:00
|
|
|
|
2020-02-17 03:09:59 +08:00
|
|
|
keyboardActionsService.updateDisplayedShortcuts($renderedWidget);
|
|
|
|
|
2020-02-10 05:31:52 +08:00
|
|
|
$("body").append($renderedWidget);
|
|
|
|
|
2020-02-29 21:32:26 +08:00
|
|
|
$renderedWidget.on('click', "[data-trigger-command]", e => {
|
|
|
|
const commandName = $(e.target).attr('data-trigger-command');
|
2020-02-10 05:31:52 +08:00
|
|
|
|
2020-02-29 21:32:26 +08:00
|
|
|
this.triggerCommand(commandName);
|
2020-02-10 05:31:52 +08:00
|
|
|
});
|
2020-02-06 05:08:45 +08:00
|
|
|
|
2020-02-27 17:03:14 +08:00
|
|
|
this.tabManager = new TabManager();
|
|
|
|
|
2020-02-15 17:41:21 +08:00
|
|
|
this.executors = [
|
2020-02-17 02:54:11 +08:00
|
|
|
this.tabManager,
|
2020-02-27 17:03:14 +08:00
|
|
|
new DialogCommandExecutor(),
|
|
|
|
new Entrypoints()
|
2020-01-14 04:48:44 +08:00
|
|
|
];
|
2020-02-06 05:08:45 +08:00
|
|
|
|
2020-02-27 17:03:14 +08:00
|
|
|
this.child(rootWidget);
|
|
|
|
|
|
|
|
for (const executor of this.executors) {
|
|
|
|
this.child(executor);
|
|
|
|
}
|
2020-02-18 05:38:46 +08:00
|
|
|
|
2020-02-06 05:08:45 +08:00
|
|
|
if (utils.isElectron()) {
|
2020-02-27 17:03:14 +08:00
|
|
|
this.child(new ZoomService());
|
2020-02-06 05:08:45 +08:00
|
|
|
}
|
2020-02-07 04:16:02 +08:00
|
|
|
|
2020-02-17 02:21:17 +08:00
|
|
|
this.triggerEvent('initialRenderComplete');
|
2020-01-12 04:19:56 +08:00
|
|
|
}
|
|
|
|
|
2020-02-17 02:21:17 +08:00
|
|
|
async triggerEvent(name, data) {
|
|
|
|
await this.handleEvent(name, data);
|
2020-02-02 05:29:32 +08:00
|
|
|
}
|
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) {
|
2020-02-17 02:54:11 +08:00
|
|
|
const called = await executor.handleCommand(name, data);
|
2020-02-15 17:41:21 +08:00
|
|
|
|
|
|
|
if (called) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-18 05:38:46 +08:00
|
|
|
console.debug(`Unhandled command ${name}, converting to event.`);
|
|
|
|
|
|
|
|
await this.triggerEvent(name, data);
|
2020-02-15 17:41:21 +08:00
|
|
|
}
|
|
|
|
|
2020-02-17 02:21:17 +08:00
|
|
|
getComponentByEl(el) {
|
|
|
|
return $(el).closest(".component").prop('component');
|
|
|
|
}
|
2020-02-15 17:41:21 +08:00
|
|
|
|
2020-02-17 02:23:49 +08:00
|
|
|
async protectedSessionStartedEvent() {
|
2020-02-17 02:21:17 +08:00
|
|
|
await treeCache.loadInitialTree();
|
2020-02-15 17:41:21 +08:00
|
|
|
|
2020-02-17 02:21:17 +08:00
|
|
|
this.triggerEvent('treeCacheReloaded');
|
2020-02-15 17:41:21 +08:00
|
|
|
}
|
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-01-12 19:48:17 +08:00
|
|
|
|
2020-02-02 17:41:43 +08:00
|
|
|
// we should save all outstanding changes before the page/app is closed
|
|
|
|
$(window).on('beforeunload', () => {
|
2020-02-17 02:21:17 +08:00
|
|
|
appContext.triggerEvent('beforeUnload');
|
2020-02-02 17:41:43 +08:00
|
|
|
});
|
|
|
|
|
2020-02-04 03:07:34 +08:00
|
|
|
function isNotePathInAddress() {
|
2020-02-29 20:03:05 +08:00
|
|
|
const [notePath, tabId] = treeService.getHashValueFromAddress();
|
2020-02-04 03:07:34 +08:00
|
|
|
|
|
|
|
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-04 03:07:34 +08:00
|
|
|
|
2020-02-09 03:53:07 +08:00
|
|
|
appContext.tabManager.switchToTab(tabId, notePath);
|
2020-02-04 03:07:34 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-01-12 16:57:28 +08:00
|
|
|
export default appContext;
|