mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-20 01:38:19 +08:00
ui: memorize open tab
This commit is contained in:
1 file changed
+25
-2
@@ -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;
|
||||||
|
|||||||
Reference in new issue
Block a user