Bye bye spot.js
This commit is contained in:
46
src/Spot.vue
46
src/Spot.vue
@@ -13,59 +13,59 @@ export default {
|
||||
data() {
|
||||
return {
|
||||
hash: {page: '', items: []},
|
||||
consts: this.spot.consts,
|
||||
user: this.spot.vars('user'),
|
||||
routes: aoRoutes
|
||||
consts: this.appConfig.consts,
|
||||
mobile: false
|
||||
};
|
||||
},
|
||||
provide() {
|
||||
return {
|
||||
hash: this.hash,
|
||||
projects: this.spot.vars('projects'),
|
||||
consts: this.consts,
|
||||
user: this.user
|
||||
isMobile: () => this.isMobile()
|
||||
};
|
||||
},
|
||||
computed: {
|
||||
route() {
|
||||
return this.routes[this.hash.page];
|
||||
return aoRoutes[this.hash.page];
|
||||
},
|
||||
hashSnapshot() {
|
||||
return JSON.stringify(this.hash);
|
||||
}
|
||||
},
|
||||
inject: ['spot'],
|
||||
inject: ['appConfig'],
|
||||
created() {
|
||||
//User
|
||||
this.user.timezone = Intl.DateTimeFormat().resolvedOptions().timeZone || this.consts.default_timezone;
|
||||
|
||||
//Set initial page
|
||||
let asInitHash = this.getBrowserHash();
|
||||
if(!asInitHash.page) asInitHash.page = this.spot.consts.default_page;
|
||||
if(!asInitHash.page) asInitHash.page = this.consts.default_page;
|
||||
this.setVarHash(asInitHash);
|
||||
},
|
||||
mounted() {
|
||||
//Catch browser hash change
|
||||
window.addEventListener('hashchange', this.onBrowserHashChange);
|
||||
window.addEventListener('resize', this.updateMobile);
|
||||
this.updateMobile();
|
||||
},
|
||||
watch: {
|
||||
hashSnapshot(jNewHash, jOldHash) {
|
||||
const asNewHash = JSON.parse(jNewHash);
|
||||
const asOldHash = JSON.parse(jOldHash);
|
||||
this.spot.vars('page', this.hash.page); //FIXME remove
|
||||
|
||||
//Sync variable -> #hash
|
||||
if(asNewHash != this.getBrowserHash()) {
|
||||
this.setBrowserHash(asNewHash.page, asNewHash.items);
|
||||
}
|
||||
|
||||
//Same Page change
|
||||
if(asNewHash != asOldHash && asNewHash.page == asOldHash.page) {
|
||||
this.spot.onSamePageMove(asNewHash, asOldHash);
|
||||
}
|
||||
this.setPageTitle(asNewHash.page);
|
||||
}
|
||||
},
|
||||
methods: {
|
||||
isMobile() {
|
||||
return this.mobile;
|
||||
},
|
||||
updateMobile() {
|
||||
this.mobile = getComputedStyle(this.$refs.mobile).display !== 'none';
|
||||
},
|
||||
setPageTitle(sTitle) {
|
||||
document.title = this.consts.title + ' - ' + sTitle.trim();
|
||||
},
|
||||
setVarHash(asHash) {
|
||||
this.hash.page = asHash.page || '';
|
||||
this.hash.items = Array.isArray(asHash.items) ? [...asHash.items.filter(n => n)] : [];
|
||||
@@ -73,22 +73,22 @@ export default {
|
||||
onBrowserHashChange() { //Sync #hash -> variable
|
||||
let asHash = this.getBrowserHash();
|
||||
if(asHash != this.hash) this.setVarHash(asHash);
|
||||
this.spot.vars('page', this.hash.page); //FIXME remove
|
||||
},
|
||||
getBrowserHash() {
|
||||
let sHash = window.location.hash.slice(1);
|
||||
let asHash = sHash.split(this.spot.consts.hash_sep).filter(n => n);
|
||||
let asHash = sHash.split(this.consts.hash_sep).filter(n => n);
|
||||
let sPage = asHash.shift() || '';
|
||||
return {page: sPage, items: asHash};
|
||||
},
|
||||
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)):'';
|
||||
const sItems = (asItems.length > 0)?(this.consts.hash_sep + asItems.join(this.consts.hash_sep)):'';
|
||||
window.location.hash = '#' + sPage + sItems;
|
||||
}
|
||||
},
|
||||
beforeUnmount() {
|
||||
window.removeEventListener('hashchange', this.onBrowserHashChange)
|
||||
window.removeEventListener('hashchange', this.onBrowserHashChange);
|
||||
window.removeEventListener('resize', this.updateMobile);
|
||||
}
|
||||
}
|
||||
</script>
|
||||
@@ -96,5 +96,5 @@ export default {
|
||||
<div id="main">
|
||||
<component :is="route" />
|
||||
</div>
|
||||
<div id="mobile"></div>
|
||||
<div id="mobile" ref="mobile"></div>
|
||||
</template>
|
||||
|
||||
Reference in New Issue
Block a user