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

90 lines
1.6 KiB
JavaScript
Raw Normal View History

2020-01-15 21:36:01 +01:00
import Component from "./component.js";
2020-01-20 22:35:52 +01:00
import keyboardActionsService from "../services/keyboard_actions.js";
2020-01-15 21:36:01 +01:00
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;
}
cssBlock(block) {
this.cssEl = block;
return this;
}
render() {
2020-01-20 22:35:52 +01:00
const $widget = this.doRender();
$widget.addClass('component')
.prop('component', this);
this.toggle(this.isEnabled());
if (this.cssEl) {
const css = this.cssEl.trim().startsWith('<style>') ? this.cssEl : `<style>${this.cssEl}</style>`;
$widget.append(css);
}
for (const key in this.attrs) {
$widget.attr(key, this.attrs[key]);
}
for (const className of this.classes) {
$widget.addClass(className);
}
2020-01-20 22:35:52 +01:00
return $widget;
}
isEnabled() {
return true;
}
/**
* for overriding
*/
doRender() {}
2020-01-14 20:27:40 +01:00
toggle(show) {
this.$widget.toggle(show);
}
isVisible() {
return this.$widget.is(":visible");
}
2020-01-19 21:12:53 +01:00
remove() {
if (this.$widget) {
this.$widget.remove();
}
}
cleanup() {}
}
export default BasicWidget;