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

68 lines
1.5 KiB
JavaScript
Raw Normal View History

import BasicWidget from "./basic_widget.js";
export default class FlexContainer extends BasicWidget {
2020-02-27 00:58:10 +01:00
constructor(direction) {
super();
2020-02-27 10:03:14 +01:00
if (!direction || !['row', 'column'].includes(direction)) {
throw new Error(`Direction argument given as "${direction}", use either 'row' or 'column'`);
2020-02-27 00:58:10 +01:00
}
this.attrs = {
2020-02-27 10:03:14 +01:00
style: `display: flex; flex-direction: ${direction};`,
2020-02-27 00:58:10 +01:00
};
2020-02-27 10:08:21 +01:00
this.classes = [];
2020-02-27 00:58:10 +01:00
this.children = [];
}
id(id) {
this.attrs.id = id;
return this;
}
2020-02-27 10:08:21 +01:00
class(className) {
this.classes.push(className);
return this;
}
2020-02-27 00:58:10 +01:00
css(name, value) {
this.attrs.style += `${name}: ${value};`;
return this;
}
2020-02-27 10:03:14 +01:00
collapsible() {
this.css('min-height', '0');
2020-02-27 00:58:10 +01:00
return this;
}
cssBlock(block) {
this.cssEl = block;
return this;
}
doRender() {
2020-02-27 00:58:10 +01:00
this.$widget = $(`<div>`);
if (this.cssEl) {
2020-03-01 18:47:20 +01:00
const css = this.cssEl.trim().startsWith('<style>') ? this.cssEl : `<style>${this.cssEl}</style>`;
this.$widget.append(css);
2020-02-27 00:58:10 +01:00
}
for (const key in this.attrs) {
2020-02-27 00:58:10 +01:00
this.$widget.attr(key, this.attrs[key]);
}
2020-02-27 10:08:21 +01:00
for (const className of this.classes) {
this.$widget.addClass(className);
}
for (const widget of this.children) {
this.$widget.append(widget.render());
}
return this.$widget;
}
}