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

120 lines
3.2 KiB
JavaScript
Raw Normal View History

import optionsService from "../services/options.js";
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
2019-08-25 23:41:08 +08:00
* @param {Options} options
2019-08-29 02:29:10 +08:00
* @param {object} sidebarState
2019-08-16 03:08:41 +08:00
*/
2019-08-29 02:29:10 +08:00
constructor(ctx, options, sidebarState) {
2019-08-16 03:08:41 +08:00
this.ctx = ctx;
// construct in camelCase
this.widgetName = this.constructor.name.substr(0, 1).toLowerCase() + this.constructor.name.substr(1);
2019-08-26 00:46:32 +08:00
this.widgetOptions = options.getJson(this.widgetName) || {};
2019-08-29 02:29:10 +08:00
this.state = sidebarState.widgets.find(s => s.name === this.widgetName) || {
expanded: this.widgetOptions.expanded
};
}
getWidgetTitle() { return "Untitled widget"; }
getHeaderActions() { return []; }
getMaxHeight() { return null; }
2019-08-27 02:19:14 +08:00
getPosition() { return this.widgetOptions.position; }
async render() {
const widgetId = `tab-${this.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
2019-08-29 02:29:10 +08:00
if (this.state.expanded) {
this.$bodyWrapper.collapse("show");
2019-08-16 03:08:41 +08:00
}
this.$body = this.$bodyWrapper.find('.card-body');
2019-08-20 02:59:40 +08:00
const maxHeight = this.getMaxHeight();
if (maxHeight) {
this.$body.css("max-height", maxHeight);
this.$body.css("overflow", "auto");
}
2019-08-16 03:08:41 +08:00
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());
await this.renderBody();
2019-08-17 16:45:20 +08:00
return this.$widget;
}
2019-08-20 02:59:40 +08:00
2019-08-16 03:08:41 +08:00
async renderBody() {
2019-08-23 02:58:43 +08:00
if (!this.isExpanded() || this.rendered) {
2019-08-16 03:08:41 +08:00
return;
}
this.rendered = true;
await this.doRenderBody();
}
/** for overriding */
async doRenderBody() {}
async isEnabled() {
const label = await this.ctx.note.getLabelValue(this.widgetName);
if (label === 'enabled') {
return true;
} else if (label === 'disabled') {
return false;
}
else {
return this.widgetOptions.enabled;
}
}
2019-08-23 02:58:43 +08:00
isExpanded() {
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-23 02:58:43 +08:00
expanded: this.isExpanded()
2019-08-16 03:08:41 +08:00
};
}
syncDataReceived(syncData) {}
2019-08-29 03:15:16 +08:00
cleanup() {}
2019-08-16 03:08:41 +08:00
}
export default StandardWidget;