Notes/src/public/javascripts/widgets/basic_widget.js

49 lines
927 B
JavaScript
Raw Normal View History

class BasicWidget {
/**
* @param {AppContext} appContext
*/
constructor(appContext) {
this.appContext = appContext;
this.widgetId = `widget-${this.constructor.name}`;
}
2020-01-14 20:27:40 +01:00
renderTo($parent) {
$parent.append(this.render());
}
render() {
// actual rendering is async
2020-01-14 20:27:40 +01:00
this.$widget = this.doRender();
2020-01-12 20:15:05 +01:00
// $widget.attr('id', this.widgetId);
2020-01-14 20:27:40 +01:00
return this.$widget;
}
/**
* for overriding
*/
2020-01-12 20:15:05 +01:00
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);
}
2020-01-14 20:27:40 +01:00
toggle(show) {
this.$widget.toggle(show);
}
cleanup() {}
}
export default BasicWidget;