101 lines
2.6 KiB
Vue
101 lines
2.6 KiB
Vue
<script>
|
|
import Project from './components/project.vue';
|
|
import Admin from './components/admin.vue';
|
|
import Upload from './components/upload.vue';
|
|
|
|
const aoRoutes = {
|
|
'project': Project,
|
|
'admin': Admin,
|
|
'upload': Upload
|
|
};
|
|
|
|
export default {
|
|
data() {
|
|
return {
|
|
hash: {page: '', items: []},
|
|
consts: this.spot.consts,
|
|
user: this.spot.vars('user'),
|
|
routes: aoRoutes
|
|
};
|
|
},
|
|
provide() {
|
|
return {
|
|
hash: this.hash,
|
|
projects: this.spot.vars('projects'),
|
|
consts: this.consts,
|
|
user: this.user
|
|
};
|
|
},
|
|
computed: {
|
|
route() {
|
|
return this.routes[this.hash.page];
|
|
},
|
|
hashSnapshot() {
|
|
return JSON.stringify(this.hash);
|
|
}
|
|
},
|
|
inject: ['spot'],
|
|
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;
|
|
this.setVarHash(asInitHash);
|
|
},
|
|
mounted() {
|
|
//Catch browser hash change
|
|
window.addEventListener('hashchange', this.onBrowserHashChange);
|
|
},
|
|
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);
|
|
}
|
|
}
|
|
},
|
|
methods: {
|
|
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
|
|
},
|
|
getBrowserHash() {
|
|
let sHash = window.location.hash.slice(1);
|
|
let asHash = sHash.split(this.spot.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)):'';
|
|
window.location.hash = '#' + sPage + sItems;
|
|
}
|
|
},
|
|
beforeUnmount() {
|
|
window.removeEventListener('hashchange', this.onBrowserHashChange)
|
|
}
|
|
}
|
|
</script>
|
|
<template>
|
|
<div id="main">
|
|
<component :is="route" />
|
|
</div>
|
|
<div id="mobile"></div>
|
|
</template>
|