ui: memorize open tab

This commit is contained in:
undefined committed 2025-09-12 04:25:33 +08:00
1 parent 9f6d318db7
commit 065c48b5eb
1 file changed
+25 -2
+25 -2
View File
@@ -24,6 +24,7 @@ export default class Tab extends DOMAttachedObject {
constructor($dom) { constructor($dom) {
super($dom); super($dom);
this.attached = false; this.attached = false;
this.hasMemo = false;
this.attach(); this.attach();
} }
@@ -96,6 +97,7 @@ export default class Tab extends DOMAttachedObject {
// 4. Finalize // 4. Finalize
this.currentIndex = idx; this.currentIndex = idx;
if (this.hasMemo) location.hash = `#tab-${idx}`;
this.isAnimating = false; this.isAnimating = false;
} }
@@ -131,13 +133,34 @@ export default class Tab extends DOMAttachedObject {
this.$dom = $container; this.$dom = $container;
this.currentIndex = 0; this.currentIndex = 0;
this.hasMemo = this.$dom.hasClass('memo');
if (this.hasMemo) {
const getIdx = () => {
const hash = location.hash.substring(1);
const match = /^tab-(\d+)$/.exec(hash);
if (match) {
const idx = +match[1];
if (!Number.isNaN(idx) && idx < this.$header.children().length) {
return idx;
}
}
return null;
};
const handleIdChange = () => {
const idx = getIdx();
if (typeof idx === 'number') this.switchToTab(idx);
};
$(window).on('hashchange', handleIdChange);
handleIdChange();
} else this.currentIndex = 0;
this.$content this.$content
.children() .children()
.eq(0) .eq(this.currentIndex)
.addClass('active'); .addClass('active');
this.$header this.$header
.children() .children()
.eq(0) .eq(this.currentIndex)
.addClass('selected'); .addClass('selected');
return true; return true;