-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathapp.js
53 lines (47 loc) · 1.95 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import homepage from './pages/home.js'
import * as pages from './pages/index.js'
import store from './store.js'
export default {
name: 'App',
components: Object.assign({homepage}, pages),
setup() {
const {watchEffect, onMounted, ref} = Vue;
const page = ref(null);
//store management: save $variables to localstorage
onMounted(() => {
window.addEventListener('beforeunload', () => {
Object.keys(store).forEach(function (key){
if (key.charAt(0) == "$") {localStorage.setItem(key, store[key]); } else {localStorage.removeItem("$" + key);}
});
});
Object.keys(store).forEach(function (key){
if (key.charAt(0) == "$") {
if (localStorage.getItem(key)) store[key] = localStorage.getItem(key);
}}
)
})
//url management
watchEffect(() => {
const urlpage = window.location.pathname.split("/").pop();
if (page.value == null) {page.value = urlpage}
if (page.value != urlpage) {const url = page.value ? page.value : './'; window.history.pushState({url: url}, '', url); }
window.onpopstate = function() {page.value = window.location.pathname.split("/").pop()};
})
return {page, pages}
},
template: `
<div id="sidebar">
<nav>
<button v-on:click="page = ''">Home</button>
<template v-for="item, index in pages" key="item.name">
<button v-on:click="page = index">
{{ item.name }}
</button>
</template>
</nav><hr>
</div>
<div id="content">
<component :is="page || 'homepage'"></component>
</div>
`,
};