continuing refactoring

This commit is contained in:
zadam
2020-01-15 21:36:01 +01:00
parent f98a20928c
commit 7963de0abc
12 changed files with 138 additions and 118 deletions

View File

@@ -1,9 +1,8 @@
class BasicWidget {
/**
* @param {AppContext} appContext
*/
import Component from "./component.js";
class BasicWidget extends Component {
constructor(appContext) {
this.appContext = appContext;
super(appContext);
this.widgetId = `widget-${this.constructor.name}`;
}
@@ -25,20 +24,6 @@ class BasicWidget {
*/
doRender() {}
eventReceived(name, data) {
console.log("received", name, "to", this.widgetId);
const fun = this[name + 'Listener'];
if (typeof fun === 'function') {
fun.call(this, data);
}
}
trigger(name, data) {
this.appContext.trigger(name, data);
}
toggle(show) {
this.$widget.toggle(show);
}

View File

@@ -0,0 +1,24 @@
export default class Component {
/** @param {AppContext} appContext */
constructor(appContext) {
this.appContext = appContext;
/** @type Component[] */
this.children = [];
}
eventReceived(name, data) {
const fun = this[name + 'Listener'];
if (typeof fun === 'function') {
fun.call(this, data);
}
for (const child of this.children) {
child.eventReceived(name, data);
}
}
trigger(name, data) {
this.appContext.trigger(name, data);
}
}

View File

@@ -58,6 +58,24 @@ export default class NoteDetailWidget extends TabAwareWidget {
this.getComponent().show();
await this.getComponent().render();
this.setupClasses();
}
setupClasses() {
const note = this.tabContext.note;
for (const clazz of Array.from(this.$widget[0].classList)) { // create copy to safely iterate over while removing classes
if (clazz !== 'note-detail') {
this.$widget.removeClass(clazz);
}
}
this.$widget.addClass(note.cssClass);
this.$widget.addClass(utils.getNoteTypeClass(note.type));
this.$widget.addClass(utils.getMimeTypeClass(note.mime));
this.$widget.toggleClass("protected", note.isProtected);
}
getComponent() {

View File

@@ -101,7 +101,7 @@ export default class NoteTreeWidget extends BasicWidget {
const notePath = await treeUtils.getNotePath(data.node);
noteDetailService.switchToNote(notePath);
this.appContext.activateNote(notePath);
},
expand: (event, data) => treeService.setExpandedToServer(data.node.data.branchId, true),
collapse: (event, data) => treeService.setExpandedToServer(data.node.data.branchId, false),

View File

@@ -1,6 +1,6 @@
import BasicWidget from "./basic_widget.js";
export default class HorizontalFlexContainer extends BasicWidget {
export default class RowFlexContainer extends BasicWidget {
constructor(appContext, widgets) {
super(appContext);