remove appContext as copied dependency across components

This commit is contained in:
zadam 2020-02-16 18:11:32 +01:00
parent 2c3f07521d
commit 22c042e21f
14 changed files with 71 additions and 73 deletions

View File

@ -35,7 +35,7 @@ async function setupActionsForElement(scope, $el, component) {
getActionsForScope("window").then(actions => { getActionsForScope("window").then(actions => {
for (const action of actions) { for (const action of actions) {
for (const shortcut of action.effectiveShortcuts) {console.log(`Binding ${shortcut} for ${action.actionName}`); for (const shortcut of action.effectiveShortcuts) {
utils.bindGlobalShortcut(shortcut, () => appContext.triggerCommand(action.actionName)); utils.bindGlobalShortcut(shortcut, () => appContext.triggerCommand(action.actionName));
} }
} }

View File

@ -1,8 +1,6 @@
import protectedSessionHolder from "./protected_session_holder.js"; import protectedSessionHolder from "./protected_session_holder.js";
import server from "./server.js"; import server from "./server.js";
import bundleService from "./bundle.js";
import utils from "./utils.js"; import utils from "./utils.js";
import optionsService from "./options.js";
import appContext from "./app_context.js"; import appContext from "./app_context.js";
import treeService from "./tree.js"; import treeService from "./tree.js";
import Component from "../widgets/component.js"; import Component from "../widgets/component.js";
@ -11,11 +9,11 @@ import hoistedNoteService from "./hoisted_note.js";
class TabContext extends Component { class TabContext extends Component {
/** /**
* @param {AppContext} appContext * @param {Component} parent
* @param {string|null} tabId * @param {string|null} tabId
*/ */
constructor(appContext, tabId = null) { constructor(parent, tabId = null) {
super(appContext, parent); super(parent);
this.tabId = tabId || utils.randomString(4); this.tabId = tabId || utils.randomString(4);
@ -85,7 +83,7 @@ class TabContext extends Component {
} }
isActive() { isActive() {
return this.tabManager.activeTabId === this.tabId; return appContext.tabManager.activeTabId === this.tabId;
} }
getTabState() { getTabState() {

View File

@ -1,19 +1,15 @@
import utils from '../services/utils.js'; import utils from '../services/utils.js';
import Mutex from "../services/mutex.js"; import Mutex from "../services/mutex.js";
import appContext from "../services/app_context.js";
export default class Component { export default class Component {
/** /**
* @param {AppContext} appContext
* @param {Component} parent * @param {Component} parent
*/ */
constructor(appContext, parent) { constructor(parent) {
this.componentId = `comp-${this.constructor.name}-` + utils.randomString(6); this.componentId = `comp-${this.constructor.name}-` + utils.randomString(6);
/** @type AppContext */
this.appContext = appContext;
/** @type Component */ /** @type Component */
this.parent = parent; this.parent = parent;
/** @type TabManager */
this.tabManager = appContext.tabManager;
/** @type Component[] */ /** @type Component[] */
this.children = []; this.children = [];
this.initialized = Promise.resolve(); this.initialized = Promise.resolve();
@ -39,7 +35,7 @@ export default class Component {
} }
async trigger(name, data) { async trigger(name, data) {
await this.appContext.trigger(name, data); await appContext.trigger(name, data);
} }
async triggerChildren(name, data) { async triggerChildren(name, data) {

View File

@ -1,8 +1,8 @@
import BasicWidget from "./basic_widget.js"; import BasicWidget from "./basic_widget.js";
export default class FlexContainer extends BasicWidget { export default class FlexContainer extends BasicWidget {
constructor(appContext, parent, attrs, widgetFactories) { constructor(parent, attrs, widgetFactories) {
super(appContext, parent); super(parent);
this.attrs = attrs; this.attrs = attrs;
this.children = widgetFactories.map(wf => wf(this)); this.children = widgetFactories.map(wf => wf(this));

View File

@ -28,42 +28,42 @@ import SidePaneToggles from "./side_pane_toggles.js";
export default class Layout { export default class Layout {
getRootWidget(appContext) { getRootWidget(appContext) {
return new FlexContainer(appContext, appContext, { 'flex-direction': 'column', 'height': '100vh' }, [ return new FlexContainer(appContext, { 'flex-direction': 'column', 'height': '100vh' }, [
parent => new FlexContainer(appContext, parent, { 'flex-direction': 'row' }, [ parent => new FlexContainer(parent, { 'flex-direction': 'row' }, [
parent => new GlobalMenuWidget(appContext, parent), parent => new GlobalMenuWidget(parent),
parent => new TabRowWidget(appContext, parent), parent => new TabRowWidget(parent),
parent => new TitleBarButtonsWidget(appContext, parent) parent => new TitleBarButtonsWidget(parent)
]), ]),
parent => new StandardTopWidget(appContext, parent), parent => new StandardTopWidget(parent),
parent => new FlexContainer(appContext, parent, { 'flex-direction': 'row', 'overflow': 'hidden' }, [ parent => new FlexContainer(parent, { 'flex-direction': 'row', 'overflow': 'hidden' }, [
parent => new SidePaneContainer(appContext, parent, 'left', [ parent => new SidePaneContainer(parent, 'left', [
parent => new GlobalButtonsWidget(appContext, parent), parent => new GlobalButtonsWidget(parent),
parent => new SearchBoxWidget(appContext, parent), parent => new SearchBoxWidget(parent),
parent => new SearchResultsWidget(appContext, parent), parent => new SearchResultsWidget(parent),
parent => new NoteTreeWidget(appContext, parent) parent => new NoteTreeWidget(parent)
]), ]),
parent => new FlexContainer(appContext, parent, { id: 'center-pane', 'flex-direction': 'column' }, [ parent => new FlexContainer(parent, { id: 'center-pane', 'flex-direction': 'column' }, [
parent => new FlexContainer(appContext, parent, { 'flex-direction': 'row' }, [ parent => new FlexContainer(parent, { 'flex-direction': 'row' }, [
parent => new TabCachingWidget(appContext, parent, parent => new NotePathsWidget(appContext, parent)), parent => new TabCachingWidget(parent, parent => new NotePathsWidget(parent)),
parent => new NoteTitleWidget(appContext, parent), parent => new NoteTitleWidget(parent),
parent => new RunScriptButtonsWidget(appContext, parent), parent => new RunScriptButtonsWidget(parent),
parent => new ProtectedNoteSwitchWidget(appContext, parent), parent => new ProtectedNoteSwitchWidget(parent),
parent => new NoteTypeWidget(appContext, parent), parent => new NoteTypeWidget(parent),
parent => new NoteActionsWidget(appContext, parent) parent => new NoteActionsWidget(parent)
]), ]),
parent => new TabCachingWidget(appContext, parent, parent => new PromotedAttributesWidget(appContext, parent)), parent => new TabCachingWidget(parent, parent => new PromotedAttributesWidget(parent)),
parent => new TabCachingWidget(appContext, parent, parent => new NoteDetailWidget(appContext, parent)) parent => new TabCachingWidget(parent, parent => new NoteDetailWidget(parent))
]), ]),
parent => new SidePaneContainer(appContext, parent, 'right', [ parent => new SidePaneContainer(parent, 'right', [
parent => new NoteInfoWidget(appContext, parent), parent => new NoteInfoWidget(parent),
parent => new TabCachingWidget(appContext, parent, parent => new CalendarWidget(appContext, parent)), parent => new TabCachingWidget(parent, parent => new CalendarWidget(parent)),
parent => new TabCachingWidget(appContext, parent, parent => new AttributesWidget(appContext, parent)), parent => new TabCachingWidget(parent, parent => new AttributesWidget(parent)),
parent => new TabCachingWidget(appContext, parent, parent => new LinkMapWidget(appContext, parent)), parent => new TabCachingWidget(parent, parent => new LinkMapWidget(parent)),
parent => new TabCachingWidget(appContext, parent, parent => new NoteRevisionsWidget(appContext, parent)), parent => new TabCachingWidget(parent, parent => new NoteRevisionsWidget(parent)),
parent => new TabCachingWidget(appContext, parent, parent => new SimilarNotesWidget(appContext, parent)), parent => new TabCachingWidget(parent, parent => new SimilarNotesWidget(parent)),
parent => new TabCachingWidget(appContext, parent, parent => new WhatLinksHereWidget(appContext, parent)) parent => new TabCachingWidget(parent, parent => new WhatLinksHereWidget(parent))
]), ]),
parent => new SidePaneToggles(appContext, parent) parent => new SidePaneToggles(parent)
]) ])
]) ])
} }

View File

@ -14,6 +14,7 @@ import RenderTypeWidget from "./type_widgets/render.js";
import RelationMapTypeWidget from "./type_widgets/relation_map.js"; import RelationMapTypeWidget from "./type_widgets/relation_map.js";
import ProtectedSessionTypeWidget from "./type_widgets/protected_session.js"; import ProtectedSessionTypeWidget from "./type_widgets/protected_session.js";
import BookTypeWidget from "./type_widgets/book.js"; import BookTypeWidget from "./type_widgets/book.js";
import appContext from "../services/app_context.js";
const TPL = ` const TPL = `
<div class="note-detail"> <div class="note-detail">
@ -39,8 +40,8 @@ const typeWidgetClasses = {
}; };
export default class NoteDetailWidget extends TabAwareWidget { export default class NoteDetailWidget extends TabAwareWidget {
constructor(appContext, parent) { constructor(parent) {
super(appContext, parent); super(parent);
this.typeWidgets = {}; this.typeWidgets = {};
@ -63,7 +64,7 @@ export default class NoteDetailWidget extends TabAwareWidget {
this.$widget.on("dragleave", e => e.preventDefault()); this.$widget.on("dragleave", e => e.preventDefault());
this.$widget.on("drop", async e => { this.$widget.on("drop", async e => {
const activeNote = this.tabManager.getActiveTabNote(); const activeNote = appContext.tabManager.getActiveTabNote();
if (!activeNote) { if (!activeNote) {
return; return;
@ -98,7 +99,7 @@ export default class NoteDetailWidget extends TabAwareWidget {
if (!(this.type in this.typeWidgets)) { if (!(this.type in this.typeWidgets)) {
const clazz = typeWidgetClasses[this.type]; const clazz = typeWidgetClasses[this.type];
const typeWidget = this.typeWidgets[this.type] = new clazz(this.appContext, this); const typeWidget = this.typeWidgets[this.type] = new clazz(this);
typeWidget.spacedUpdate = this.spacedUpdate; typeWidget.spacedUpdate = this.spacedUpdate;
this.children.push(typeWidget); this.children.push(typeWidget);

View File

@ -12,6 +12,7 @@ import TabAwareWidget from "./tab_aware_widget.js";
import server from "../services/server.js"; import server from "../services/server.js";
import noteCreateService from "../services/note_create.js"; import noteCreateService from "../services/note_create.js";
import toastService from "../services/toast.js"; import toastService from "../services/toast.js";
import appContext from "../services/app_context.js";
const TPL = ` const TPL = `
<div class="tree"> <div class="tree">
@ -51,7 +52,7 @@ export default class NoteTreeWidget extends TabAwareWidget {
const notePath = treeService.getNotePath(node); const notePath = treeService.getNotePath(node);
if (notePath) { if (notePath) {
const tabContext = this.tabManager.openEmptyTab(); const tabContext = appContext.tabManager.openEmptyTab();
tabContext.setNote(notePath); tabContext.setNote(notePath);
} }
@ -85,10 +86,10 @@ export default class NoteTreeWidget extends TabAwareWidget {
node.setFocus(true); node.setFocus(true);
} }
else if (event.ctrlKey) { else if (event.ctrlKey) {
const tabContext = this.tabManager.openEmptyTab(); const tabContext = appContext.tabManager.openEmptyTab();
const notePath = treeService.getNotePath(node); const notePath = treeService.getNotePath(node);
tabContext.setNote(notePath); tabContext.setNote(notePath);
this.tabManager.activateTab(tabContext.tabId); appContext.tabManager.activateTab(tabContext.tabId);
} }
else { else {
node.setActive(); node.setActive();
@ -105,7 +106,7 @@ export default class NoteTreeWidget extends TabAwareWidget {
const notePath = treeService.getNotePath(data.node); const notePath = treeService.getNotePath(data.node);
const activeTabContext = this.tabManager.getActiveTabContext(); const activeTabContext = appContext.tabManager.getActiveTabContext();
await activeTabContext.setNote(notePath); await activeTabContext.setNote(notePath);
}, },
expand: (event, data) => this.setExpandedToServer(data.node.data.branchId, true), expand: (event, data) => this.setExpandedToServer(data.node.data.branchId, true),
@ -285,7 +286,7 @@ export default class NoteTreeWidget extends TabAwareWidget {
} }
async scrollToActiveNoteListener() { async scrollToActiveNoteListener() {
const activeContext = this.tabManager.getActiveTabContext(); const activeContext = appContext.tabManager.getActiveTabContext();
if (activeContext && activeContext.notePath) { if (activeContext && activeContext.notePath) {
this.tree.setFocus(); this.tree.setFocus();
@ -542,7 +543,7 @@ export default class NoteTreeWidget extends TabAwareWidget {
} }
if (activeNotePath) { if (activeNotePath) {
this.tabManager.getActiveTabContext().setNote(activeNotePath); appContext.tabManager.getActiveTabContext().setNote(activeNotePath);
} }
} }

View File

@ -2,8 +2,8 @@ import options from "../services/options.js";
import FlexContainer from "./flex_container.js"; import FlexContainer from "./flex_container.js";
export default class SidePaneContainer extends FlexContainer { export default class SidePaneContainer extends FlexContainer {
constructor(appContext, parent, side, widgetFactories) { constructor(parent, side, widgetFactories) {
super(appContext, parent, {id: side + '-pane', 'flex-direction': 'column', 'height': '100%'}, widgetFactories); super(parent, {id: side + '-pane', 'flex-direction': 'column', 'height': '100%'}, widgetFactories);
this.side = side; this.side = side;
} }

View File

@ -66,7 +66,7 @@ export default class StandardTopWidget extends BasicWidget {
doRender() { doRender() {
this.$widget = $(TPL); this.$widget = $(TPL);
const historyNavigationWidget = new HistoryNavigationWidget(this.appContext); const historyNavigationWidget = new HistoryNavigationWidget(this);
this.$widget.prepend(historyNavigationWidget.render()); this.$widget.prepend(historyNavigationWidget.render());

View File

@ -1,4 +1,5 @@
import BasicWidget from "./basic_widget.js"; import BasicWidget from "./basic_widget.js";
import appContext from "../services/app_context.js";
export default class TabAwareWidget extends BasicWidget { export default class TabAwareWidget extends BasicWidget {
setTabContextListener({tabContext}) { setTabContextListener({tabContext}) {
@ -68,7 +69,7 @@ export default class TabAwareWidget extends BasicWidget {
refreshWithNote(note, notePath) {} refreshWithNote(note, notePath) {}
activeTabChangedListener() { activeTabChangedListener() {
this.tabContext = this.tabManager.getActiveTabContext(); this.tabContext = appContext.tabManager.getActiveTabContext();
this.activeTabChanged(); this.activeTabChanged();
} }
@ -79,7 +80,7 @@ export default class TabAwareWidget extends BasicWidget {
lazyLoadedListener() { lazyLoadedListener() {
if (!this.tabContext) { // has not been loaded yet if (!this.tabContext) { // has not been loaded yet
this.tabContext = this.tabManager.getActiveTabContext(); this.tabContext = appContext.tabManager.getActiveTabContext();
} }
this.refresh(); this.refresh();

View File

@ -1,8 +1,8 @@
import TabAwareWidget from "./tab_aware_widget.js"; import TabAwareWidget from "./tab_aware_widget.js";
export default class TabCachingWidget extends TabAwareWidget { export default class TabCachingWidget extends TabAwareWidget {
constructor(appContext, parent, widgetFactory) { constructor(parent, widgetFactory) {
super(appContext, parent); super(parent);
this.widgetFactory = widgetFactory; this.widgetFactory = widgetFactory;
this.widgets = {}; this.widgets = {};

File diff suppressed because one or more lines are too long

View File

@ -1,5 +1,4 @@
import noteAutocompleteService from '../../services/note_autocomplete.js'; import noteAutocompleteService from '../../services/note_autocomplete.js';
import treeService from "../../services/tree.js";
import TypeWidget from "./type_widget.js"; import TypeWidget from "./type_widget.js";
import appContext from "../../services/app_context.js"; import appContext from "../../services/app_context.js";

View File

@ -6,6 +6,7 @@ import contextMenuWidget from "../../services/context_menu.js";
import toastService from "../../services/toast.js"; import toastService from "../../services/toast.js";
import attributeAutocompleteService from "../../services/attribute_autocomplete.js"; import attributeAutocompleteService from "../../services/attribute_autocomplete.js";
import TypeWidget from "./type_widget.js"; import TypeWidget from "./type_widget.js";
import appContext from "../../services/app_context.js";
const uniDirectionalOverlays = [ const uniDirectionalOverlays = [
[ "Arrow", { [ "Arrow", {
@ -195,7 +196,7 @@ export default class RelationMapTypeWidget extends TypeWidget {
const noteId = this.idToNoteId($noteBox.prop("id")); const noteId = this.idToNoteId($noteBox.prop("id"));
if (cmd === "open-in-new-tab") { if (cmd === "open-in-new-tab") {
const tabContext = this.tabManager.openEmptyTab(); const tabContext = appContext.tabManager.openEmptyTab();
tabContext.setNote(noteId); tabContext.setNote(noteId);
} }
else if (cmd === "remove") { else if (cmd === "remove") {