2024-02-01 17:54:04 +08:00
|
|
|
var bricks = window.bricks || {};
|
2024-02-01 16:40:31 +08:00
|
|
|
bricks.TabPanel = class extends bricks.Layout {
|
2023-09-14 10:28:07 +08:00
|
|
|
/*
|
|
|
|
options
|
|
|
|
{
|
|
|
|
css:
|
|
|
|
tab_long: 100%
|
|
|
|
tab_pos:"top"
|
|
|
|
items:[
|
|
|
|
{
|
|
|
|
name:
|
|
|
|
label:"tab1",
|
|
|
|
icon:
|
|
|
|
removable:
|
|
|
|
refresh:false,
|
|
|
|
content:{
|
|
|
|
"widgettype":...
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
css:
|
|
|
|
tab
|
|
|
|
tab-button
|
|
|
|
tab-button-active
|
|
|
|
tab-button-hover
|
|
|
|
tab-content
|
|
|
|
*/
|
|
|
|
constructor(options){
|
|
|
|
super(options);
|
|
|
|
this.content_buffer = {};
|
|
|
|
this.cur_tab_name = '';
|
2024-03-20 13:14:56 +08:00
|
|
|
this.content_container = new bricks.Filler({});
|
2023-09-14 10:28:07 +08:00
|
|
|
if (this.opts.tab_pos == 'top' || this.opts.tab_pos == 'bottom'){
|
|
|
|
this.set_css('vbox');
|
2024-03-06 16:36:20 +08:00
|
|
|
this.tab_container = new bricks.VBox({
|
|
|
|
height:'auto',
|
|
|
|
width:'auto'
|
|
|
|
});
|
2023-09-14 10:28:07 +08:00
|
|
|
} else {
|
|
|
|
this.set_css('hbox');
|
2024-03-06 16:36:20 +08:00
|
|
|
this.tab_container = new bricks.VBox({
|
|
|
|
width:'auto',
|
|
|
|
height:'auto'
|
|
|
|
});
|
2023-09-14 10:28:07 +08:00
|
|
|
}
|
|
|
|
if (this.opts.tab_pos == 'top' || this.opts.tab_pos == 'left'){
|
|
|
|
this.add_widget(this.tab_container);
|
|
|
|
this.add_widget(this.content_container);
|
|
|
|
} else {
|
|
|
|
this.add_widget(this.content_container);
|
|
|
|
this.add_widget(this.tab_container);
|
|
|
|
}
|
|
|
|
this.createToolbar();
|
|
|
|
this.set_css('tabpanel');
|
|
|
|
this.content_container.set_css('tabpanel-content');
|
2024-06-20 17:35:25 +08:00
|
|
|
this.show_first_tab();
|
2023-09-14 10:28:07 +08:00
|
|
|
}
|
|
|
|
show_first_tab(){
|
2024-06-20 17:35:25 +08:00
|
|
|
this.toolbar.dispatch('command', this.opts.items[0]);
|
2023-09-14 10:28:07 +08:00
|
|
|
}
|
|
|
|
createToolbar(){
|
|
|
|
var desc = {
|
|
|
|
tools:this.opts.items
|
|
|
|
};
|
|
|
|
var orient;
|
|
|
|
if (this.opts.tab_pos == 'top' || this.opts.tab_pos == 'bottom'){
|
|
|
|
orient = 'horizontal';
|
|
|
|
} else {
|
|
|
|
orient = 'vertical';
|
|
|
|
}
|
|
|
|
desc.orientation = orient;
|
2024-02-01 17:54:04 +08:00
|
|
|
this.toolbar = new bricks.Toolbar(desc);
|
2023-09-14 10:28:07 +08:00
|
|
|
this.toolbar.bind('command', this.show_tabcontent.bind(this));
|
|
|
|
this.toolbar.bind('remove', this.tab_removed.bind(this));
|
|
|
|
this.toolbar.bind('ready', this.show_first_tab.bind(this));
|
|
|
|
this.tab_container.add_widget(this.toolbar);
|
|
|
|
}
|
2024-06-29 20:17:22 +08:00
|
|
|
async show_tabcontent(event){
|
2023-09-14 10:28:07 +08:00
|
|
|
var tdesc = event.params;
|
|
|
|
var items = this.opts.items;
|
|
|
|
if (tdesc.name == this.cur_tab_name){
|
2024-03-08 10:54:16 +08:00
|
|
|
bricks.debug('TabPanel(): click duplication click on same tab', tdesc)
|
2023-09-14 10:28:07 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
for (var i=0;i<items.length;i++){
|
|
|
|
if (tdesc.name == items[i].name){
|
|
|
|
var w = objget(this.content_buffer, tdesc.name);
|
|
|
|
if (w && ! items[i].refresh){
|
|
|
|
this.content_container.clear_widgets();
|
|
|
|
this.content_container.add_widget(w);
|
|
|
|
this.cur_tab_name = name;
|
|
|
|
return;
|
|
|
|
}
|
2024-04-12 14:05:13 +08:00
|
|
|
try {
|
|
|
|
w = await bricks.widgetBuild(items[i].content, this, {});
|
|
|
|
if (! w){
|
|
|
|
bricks.debug('TabPanel():create content error', items[i].content);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.content_buffer[tdesc.name] = w;
|
|
|
|
this.content_container.clear_widgets();
|
|
|
|
this.content_container.add_widget(w);
|
|
|
|
this.cur_tab_name = tdesc.name;
|
2023-09-14 10:28:07 +08:00
|
|
|
return;
|
|
|
|
}
|
2024-04-12 14:05:13 +08:00
|
|
|
catch (e) {
|
|
|
|
console.log('except ', e)
|
|
|
|
}
|
2023-09-14 10:28:07 +08:00
|
|
|
}
|
|
|
|
}
|
2024-03-08 10:54:16 +08:00
|
|
|
bricks.debug('TabPanel(): click event handled but noting to do', tdesc)
|
2023-09-14 10:28:07 +08:00
|
|
|
}
|
|
|
|
add_tab(desc){
|
|
|
|
var item = this.toolbar.createTool(desc);
|
|
|
|
if (desc.removable){
|
|
|
|
this.add_removeable(item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
tab_removed(event){
|
|
|
|
var desc = event.params;
|
|
|
|
if (this.content_buffer.hasOwnProperty(desc.name))
|
|
|
|
delete this.content_buffer[desc.name];
|
|
|
|
|
|
|
|
if (desc.name == this.cur_tab_name){
|
|
|
|
this.show_first_tab();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-01 16:40:31 +08:00
|
|
|
bricks.Factory.register('TabPanel', bricks.TabPanel);
|