fix: open_tab 用 opts.items 判断已存在,content 失败也记录避免重复新增

This commit is contained in:
p 2026-08-14 16:18:02 +08:00
parent a268f2f523
commit 6a94b96a6c

View File

@ -5,24 +5,35 @@
if (bricks.TabPanel.prototype._dyn_patched) return; if (bricks.TabPanel.prototype._dyn_patched) return;
bricks.TabPanel.prototype._dyn_patched = true; bricks.TabPanel.prototype._dyn_patched = true;
// 动态打开 tab:已存在则切换,不存在则新增 // 动态打开 tab:已存在(opts.items 同名)则切换,不存在则新增
bricks.TabPanel.prototype.open_tab = async function(desc){ bricks.TabPanel.prototype.open_tab = async function(desc){
if (this.content_buffer.hasOwnProperty(desc.name)){ // 1. 已存在判断:opts.items 里找同名(不用 content_buffer,因 content 可能构建失败)
var existing = null;
for (var i=0;i<this.opts.items.length;i++){
if (this.opts.items[i].name === desc.name){ existing = this.opts.items[i]; break; }
}
if (existing){
if (desc.name !== this.cur_tab_name){ if (desc.name !== this.cur_tab_name){
this.cur_tab_name = desc.name; this.cur_tab_name = desc.name;
this.switch_content(this.content_buffer[desc.name]); var w = this.content_buffer[desc.name];
if (!w){
var c = existing.content || {"widgettype":"urlwidget","options":{"url":existing.url}};
w = await bricks.widgetBuild(c, this, {});
if (w) this.content_buffer[desc.name] = w;
}
if (w) this.switch_content(w);
if (this.toolbar && this.toolbar.click) this.toolbar.click(desc.name); if (this.toolbar && this.toolbar.click) this.toolbar.click(desc.name);
} }
return; return;
} }
// 2. 新增
desc.content = desc.content || {"widgettype":"urlwidget","options":{"url":desc.url}}; desc.content = desc.content || {"widgettype":"urlwidget","options":{"url":desc.url}};
this.opts.items.push(desc); this.opts.items.push(desc);
if (this.add_tab) this.add_tab(desc); if (this.add_tab) this.add_tab(desc);
var w = await bricks.widgetBuild(desc.content, this, {}); var w = await bricks.widgetBuild(desc.content, this, {});
if (!w) return; this.content_buffer[desc.name] = w; // 即使 null 也记录(标记已存在,避免重复新增)
this.content_buffer[desc.name] = w;
this.cur_tab_name = desc.name; this.cur_tab_name = desc.name;
this.switch_content(w); if (w) this.switch_content(w);
}; };
// 修复 show_tabcontent 的 bug(cur_tab_name 赋值用了未定义变量 name) // 修复 show_tabcontent 的 bug(cur_tab_name 赋值用了未定义变量 name)