2020-01-15 04:23:32 +08:00
|
|
|
import utils from "../services/utils.js";
|
|
|
|
import TabAwareWidget from "./tab_aware_widget.js";
|
2019-08-23 05:31:02 +08:00
|
|
|
|
2019-08-16 03:08:41 +08:00
|
|
|
const WIDGET_TPL = `
|
|
|
|
<div class="card widget">
|
2019-09-10 03:23:04 +08:00
|
|
|
<div class="card-header">
|
|
|
|
<div>
|
|
|
|
<button class="btn btn-sm widget-title" data-toggle="collapse" data-target="#[to be set]">
|
|
|
|
Collapsible Group Item
|
|
|
|
</button>
|
|
|
|
|
2019-11-02 19:17:00 +08:00
|
|
|
<a class="widget-help external no-arrow bx bx-info-circle"></a>
|
2019-09-10 03:23:04 +08:00
|
|
|
</div>
|
2019-08-16 03:08:41 +08:00
|
|
|
|
|
|
|
<div class="widget-header-actions"></div>
|
|
|
|
</div>
|
|
|
|
|
2019-08-16 03:18:33 +08:00
|
|
|
<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>
|
|
|
|
`;
|
|
|
|
|
2020-01-15 04:23:32 +08:00
|
|
|
class StandardWidget extends TabAwareWidget {
|
2019-08-23 05:31:02 +08:00
|
|
|
getWidgetTitle() { return "Untitled widget"; }
|
|
|
|
|
|
|
|
getHeaderActions() { return []; }
|
|
|
|
|
2019-09-10 03:23:04 +08:00
|
|
|
getHelp() { return {}; }
|
|
|
|
|
2019-08-23 05:31:02 +08:00
|
|
|
getMaxHeight() { return null; }
|
|
|
|
|
2020-01-15 04:23:32 +08:00
|
|
|
//getPosition() { return this.widgetOptions.position; }
|
2019-08-27 02:19:14 +08:00
|
|
|
|
2019-09-08 15:40:29 +08:00
|
|
|
render() {
|
2020-01-15 04:23:32 +08:00
|
|
|
const widgetInstanceId = this.widgetId + "-" + utils.randomString(10);
|
2019-08-16 03:08:41 +08:00
|
|
|
|
|
|
|
this.$widget = $(WIDGET_TPL);
|
2020-01-15 04:23:32 +08:00
|
|
|
this.$widget.find('[data-target]').attr('data-target', "#" + widgetInstanceId);
|
2019-08-16 03:08:41 +08:00
|
|
|
|
|
|
|
this.$bodyWrapper = this.$widget.find('.body-wrapper');
|
2020-01-15 04:23:32 +08:00
|
|
|
this.$bodyWrapper.attr('id', widgetInstanceId);
|
2019-08-16 03:08:41 +08:00
|
|
|
|
2020-01-15 04:23:32 +08:00
|
|
|
// if (this.state.expanded) {
|
2019-08-17 03:29:44 +08:00
|
|
|
this.$bodyWrapper.collapse("show");
|
2020-01-15 04:23:32 +08:00
|
|
|
// }
|
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");
|
|
|
|
}
|
|
|
|
|
2020-01-15 04:23:32 +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());
|
2019-09-10 03:23:04 +08:00
|
|
|
|
2019-08-16 03:08:41 +08:00
|
|
|
this.$title = this.$widget.find('.widget-title');
|
2019-08-17 16:45:20 +08:00
|
|
|
this.$title.text(this.getWidgetTitle());
|
2019-09-10 03:23:04 +08:00
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
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-09-08 15:40:29 +08:00
|
|
|
// actual rendering is async
|
|
|
|
this.renderBody();
|
2019-08-17 16:45:20 +08:00
|
|
|
|
2019-08-23 05:31:02 +08:00
|
|
|
return this.$widget;
|
|
|
|
}
|
2019-08-20 02:59:40 +08:00
|
|
|
|
2019-08-16 03:08:41 +08:00
|
|
|
async renderBody() {
|
2020-01-15 04:23:32 +08:00
|
|
|
// if (!this.isExpanded() || this.rendered) {
|
|
|
|
// return;
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// this.rendered = true;
|
2019-08-16 03:08:41 +08:00
|
|
|
|
|
|
|
await this.doRenderBody();
|
|
|
|
}
|
|
|
|
|
|
|
|
/** for overriding */
|
|
|
|
async doRenderBody() {}
|
|
|
|
|
2019-08-23 05:31:02 +08:00
|
|
|
async isEnabled() {
|
2019-09-01 15:52:07 +08:00
|
|
|
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 05:31:02 +08:00
|
|
|
}
|
|
|
|
|
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 {
|
2019-08-16 03:18:33 +08:00
|
|
|
name: this.widgetName,
|
2019-08-23 02:58:43 +08:00
|
|
|
expanded: this.isExpanded()
|
2019-08-16 03:08:41 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-08-29 03:15:16 +08:00
|
|
|
cleanup() {}
|
2019-08-16 03:08:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
export default StandardWidget;
|