import utils from "../services/utils.js"; import TabAwareWidget from "./tab_aware_widget.js"; const WIDGET_TPL = `
`; class StandardWidget extends TabAwareWidget { getWidgetTitle() { return "Untitled widget"; } getHeaderActions() { return []; } getHelp() { return {}; } getMaxHeight() { return null; } //getPosition() { return this.widgetOptions.position; } render() { const widgetInstanceId = this.widgetId + "-" + utils.randomString(10); this.$widget = $(WIDGET_TPL); this.$widget.find('[data-target]').attr('data-target', "#" + widgetInstanceId); this.$bodyWrapper = this.$widget.find('.body-wrapper'); this.$bodyWrapper.attr('id', widgetInstanceId); // if (this.state.expanded) { this.$bodyWrapper.collapse("show"); // } this.$body = this.$bodyWrapper.find('.card-body'); const maxHeight = this.getMaxHeight(); if (maxHeight) { this.$body.css("max-height", maxHeight); this.$body.css("overflow", "auto"); } // 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'); this.$title.text(this.getWidgetTitle()); this.$help = this.$widget.find('.widget-help'); const help = this.getHelp(); if (help.title) { this.$help.attr("title", help.title); this.$help.attr("href", help.url || "javascript:"); if (!help.url) { this.$help.addClass('no-link'); } } else { this.$help.hide(); } this.$headerActions = this.$widget.find('.widget-header-actions'); this.$headerActions.append(...this.getHeaderActions()); // actual rendering is async this.renderBody(); return this.$widget; } async renderBody() { // if (!this.isExpanded() || this.rendered) { // 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; } } isExpanded() { return this.$bodyWrapper.hasClass("show"); } getWidgetState() { return { name: this.widgetName, expanded: this.isExpanded() }; } cleanup() {} } export default StandardWidget;