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

118 lines
2.2 KiB
JavaScript
Raw Normal View History

2020-01-15 21:36:01 +01:00
import Component from "./component.js";
class BasicWidget extends Component {
constructor() {
super();
this.attrs = {
style: ''
};
this.classes = [];
}
id(id) {
this.attrs.id = id;
return this;
}
class(className) {
this.classes.push(className);
return this;
}
css(name, value) {
this.attrs.style += `${name}: ${value};`;
return this;
}
collapsible() {
this.css('min-height', '0');
return this;
}
2020-05-12 12:45:32 +02:00
filling() {
this.css('flex-grow', '1');
return this;
}
2020-03-01 19:16:30 +01:00
hideInZenMode() {
this.class('hide-in-zen-mode');
return this;
}
cssBlock(block) {
this.cssEl = block;
return this;
}
render() {
this.doRender();
2020-01-20 22:35:52 +01:00
this.$widget.addClass('component')
.prop('component', this);
2020-03-06 22:17:07 +01:00
this.toggleInt(this.isEnabled());
if (this.cssEl) {
const css = this.cssEl.trim().startsWith('<style>') ? this.cssEl : `<style>${this.cssEl}</style>`;
this.$widget.append(css);
}
for (const key in this.attrs) {
if (key === 'style') {
if (this.attrs[key]) {
let style = this.$widget.attr('style');
style = style ? `${style}; ${this.attrs[key]}` : this.attrs[key];
this.$widget.attr(key, style);
}
}
else {
this.$widget.attr(key, this.attrs[key]);
}
}
for (const className of this.classes) {
this.$widget.addClass(className);
}
return this.$widget;
}
isEnabled() {
return true;
}
/**
* for overriding
*/
doRender() {}
2020-03-06 22:17:07 +01:00
toggleInt(show) {
this.$widget.toggleClass('hidden-int', !show);
}
toggleExt(show) {
this.$widget.toggleClass('hidden-ext', !show);
2020-01-14 20:27:40 +01:00
}
isVisible() {
return this.$widget.is(":visible");
}
2020-03-16 21:16:09 +01:00
getPosition() {
return this.position;
}
2020-01-19 21:12:53 +01:00
remove() {
if (this.$widget) {
this.$widget.remove();
}
}
cleanup() {}
}
2020-05-12 12:45:32 +02:00
export default BasicWidget;