bricks/bricks/layout.js
2024-03-20 13:14:56 +08:00

101 lines
2.4 KiB
JavaScript
Executable File

var bricks = window.bricks || {};
bricks.Layout = class extends bricks.JsWidget {
constructor(options){
super(options);
this._container = true;
this.children = [];
}
add_widget(w, index){
if (index !== null && index >=0 && index < this.children.length){
var pos_w = this.children[index];
this.dom_element.insertBefore(w.dom_element, pos_w.dom_element);
this.children.insert(index, w);
} else {
// append child at end
w.parent = this;
this.children.push(w);
this.dom_element.appendChild(w.dom_element);
}
w.dispatch('on_parent', this);
}
remove_widgets_at_begin(cnt){
return this._remove_widgets(cnt, false);
}
remove_widgets_at_end(cnt){
return this._remove_widgets(cnt, true);
}
_remove_widgets(cnt, from_end){
var children = objcopy(this.children);
var len = this.children.length;
for (var i=0; i<len; i++){
if (i >= cnt) break;
var k = i;
if (from_end) k = len - 1 - i;
var w = children[k]
this.children.remove(w);
this.remove_widget(w);
}
}
remove_widget(w){
w.parent = null;
this.children = this.children.filter(function(item){
return item != w;
});
this.dom_element.removeChild(w.dom_element);
w.dispatch('on_parent', null);
}
clear_widgets(w){
this.dom_element.replaceChildren();
for (var i=0;i<this.children.length;i++){
var w = this.children[i];
w.parent = null;
w.dispatch('on_parent', null);
}
this.children = [];
}
}
bricks.VBox = class extends bricks.Layout {
constructor(options){
super(options);
this.set_css('vcontainer');
}
}
bricks.Filler = class extends bricks.Layout {
constructor(options){
super(options);
this.set_style('flexGrow', 1);
this.set_style('overflow', 'auto');
}
}
bricks.HBox = class extends bricks.Layout {
constructor(options){
super(options);
this.set_css('hcontainer');
}
}
bricks.FHBox = class extends bricks.HBox {
constructor(options){
super(options);
this.set_style('flexWrap', 'nowrap');
}
}
bricks.FVBox = class extends bricks.VBox {
constructor(options){
super(options);
this.set_style('flexWrap', 'nowrap');
}
}
bricks.Factory.register('HBox', bricks.HBox);
bricks.Factory.register('FHBox', bricks.FHBox);
bricks.Factory.register('VBox', bricks.VBox);
bricks.Factory.register('FVBox', bricks.FVBox);
bricks.Factory.register('Filler', bricks.Filler);
bricks.Factory.register('HFiller', bricks.Filler);
bricks.Factory.register('VFiller', bricks.Filler);