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

46 lines
1.1 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.style = `display: flex; flex-direction: ${direction};`;
2020-02-27 10:08:21 +01:00
2020-02-27 00:58:10 +01:00
this.children = [];
2020-03-16 21:16:09 +01:00
this.positionCounter = 10;
}
2020-03-16 22:14:18 +01:00
child(...components) {
2020-03-16 23:25:52 +01:00
if (!components) {
return this;
}
2020-03-16 22:14:18 +01:00
super.child(...components);
for (const component of components) {
if (!component.position) {
component.position = this.positionCounter;
this.positionCounter += 10;
}
}
2020-03-16 21:16:09 +01:00
2020-03-16 22:14:18 +01:00
this.children.sort((a, b) => a.position - b.position < 0 ? -1 : 1);
2020-03-16 21:16:09 +01:00
return this;
2020-02-27 00:58:10 +01:00
}
doRender() {
2020-02-27 00:58:10 +01:00
this.$widget = $(`<div>`);
for (const widget of this.children) {
this.$widget.append(widget.render());
}
return this.$widget;
}
}