trilium/src/public/javascripts/widgets/standard_widget.js

84 lines
2.2 KiB
JavaScript
Raw Normal View History

2019-08-16 03:08:41 +08:00
const WIDGET_TPL = `
<div class="card widget">
<div class="card-header">
<button class="btn btn-sm widget-title" data-toggle="collapse" data-target="#[to be set]">
Collapsible Group Item
</button>
<div class="widget-header-actions"></div>
</div>
<div id="[to be set]" class="collapse body-wrapper" style="transition: none;">
2019-08-16 03:08:41 +08:00
<div class="card-body"></div>
</div>
</div>
`;
class StandardWidget {
/**
* @param {TabContext} ctx
* @param {object} state
*/
constructor(ctx, state) {
2019-08-16 03:08:41 +08:00
this.ctx = ctx;
this.widgetName = this.constructor.name;
const widgetId = `tab-${ctx.tabId}-widget-${this.widgetName}`;
2019-08-16 03:08:41 +08:00
this.$widget = $(WIDGET_TPL);
this.$widget.find('[data-target]').attr('data-target', "#" + widgetId);
2019-08-16 03:08:41 +08:00
this.$bodyWrapper = this.$widget.find('.body-wrapper');
this.$bodyWrapper.attr('id', widgetId);
2019-08-16 03:08:41 +08:00
if (state && state.visible) {
this.$bodyWrapper.collapse("show");
2019-08-16 03:08:41 +08:00
}
this.$body = this.$bodyWrapper.find('.card-body');
this.$widget.on('shown.bs.collapse', () => this.renderBody());
this.$widget.on('shown.bs.collapse', () => this.ctx.stateChanged());
this.$widget.on('hidden.bs.collapse', () => this.ctx.stateChanged());
this.$title = this.$widget.find('.widget-title');
2019-08-17 16:45:20 +08:00
this.$title.text(this.getWidgetTitle());
2019-08-16 03:08:41 +08:00
this.$headerActions = this.$widget.find('.widget-header-actions');
2019-08-17 16:45:20 +08:00
this.$headerActions.append(...this.getHeaderActions());
2019-08-16 03:08:41 +08:00
}
2019-08-17 16:45:20 +08:00
getWidgetTitle() { return "Untitled widget"; }
getHeaderActions() { return []; }
2019-08-16 03:08:41 +08:00
async renderBody() {
if (!this.isVisible() || this.rendered) {
return;
}
this.rendered = true;
await this.doRenderBody();
}
/** for overriding */
async doRenderBody() {}
isVisible() {
2019-08-17 03:52:36 +08:00
return this.$bodyWrapper.hasClass("show");
2019-08-16 03:08:41 +08:00
}
getWidgetState() {
return {
name: this.widgetName,
2019-08-16 03:08:41 +08:00
visible: this.isVisible()
};
}
getWidgetElement() {
return this.$widget;
}
syncDataReceived(syncData) {}
}
export default StandardWidget;