Better hash management

This commit is contained in:
2026-04-25 17:44:46 +02:00
parent c32998650f
commit dea14acd29
4 changed files with 31 additions and 26 deletions

View File

@@ -40,13 +40,13 @@ export default {
this.user.timezone = Intl.DateTimeFormat().resolvedOptions().timeZone || this.consts.default_timezone;
//Set initial page
let asInitHash = this.getHash();
if(!asInitHash.page) this.hash.page = this.spot.consts.default_page;
else this.hash = asInitHash;
let asInitHash = this.getBrowserHash();
if(!asInitHash.page) asInitHash.page = this.spot.consts.default_page;
this.setVarHash(asInitHash);
},
mounted() {
//Catch browser hash change
window.addEventListener('hashchange', this.onHashChange);
window.addEventListener('hashchange', this.onBrowserHashChange);
},
watch: {
hashSnapshot(jNewHash, jOldHash) {
@@ -55,8 +55,8 @@ export default {
this.spot.vars('page', this.hash.page); //FIXME remove
//Sync variable -> #hash
if(asNewHash != this.getHash()) {
this.setHash(asNewHash.page, asNewHash.items);
if(asNewHash != this.getBrowserHash()) {
this.setBrowserHash(asNewHash.page, asNewHash.items);
}
//Same Page change
@@ -66,25 +66,29 @@ export default {
}
},
methods: {
onHashChange() { //Sync #hash -> variable
let asHash = this.getHash();
if(asHash != this.hash) this.hash = asHash;
setVarHash(asHash) {
this.hash.page = asHash.page || '';
this.hash.items = Array.isArray(asHash.items) ? [...asHash.items.filter(n => n)] : [];
},
onBrowserHashChange() { //Sync #hash -> variable
let asHash = this.getBrowserHash();
if(asHash != this.hash) this.setVarHash(asHash);
this.spot.vars('page', this.hash.page); //FIXME remove
},
getHash() {
getBrowserHash() {
let sHash = window.location.hash.slice(1);
let asHash = sHash.split(this.spot.consts.hash_sep);
let asHash = sHash.split(this.spot.consts.hash_sep).filter(n => n);
let sPage = asHash.shift() || '';
return {page: sPage, items: asHash};
},
setHash(sPage = '', asItems = []) {
setBrowserHash(sPage = '', asItems = []) {
if(typeof asItems == 'string' && asItems != '') asItems = [asItems];
const sItems = (asItems.length > 0)?(this.spot.consts.hash_sep + asItems.join(this.spot.consts.hash_sep)):'';
window.location.hash = '#' + sPage + sItems;
}
},
beforeUnmount() {
window.removeEventListener('hashchange', this.onHashChange)
window.removeEventListener('hashchange', this.onBrowserHashChange)
}
}
</script>
@@ -93,4 +97,4 @@ export default {
<component :is="route" />
</div>
<div id="mobile"></div>
</template>
</template>