-
Notifications
You must be signed in to change notification settings - Fork 17
/
Layout.vue
208 lines (201 loc) · 5.99 KB
/
Layout.vue
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
<template>
<div>
<div class="wrap">
<div class="theme-container container"
:class="pageClasses"
@touchstart="onTouchStart"
@touchend="onTouchEnd">
<Navbar v-if="shouldShowNavbar" @toggle-sidebar="toggleSidebar"/>
<div class="sidebar-mask" @click="toggleSidebar(false)"></div>
<Sidebar :items="sidebarItems" @toggle-sidebar="toggleSidebar"/>
<div class="page-container" :style="pageContainerClasses">
<!-- custom layout -->
<div class="custom-layout" v-if="$page.frontmatter.layout">
<component :is="$page.frontmatter.layout"/>
</div>
<!-- Activity layout -->
<Activity v-else-if="$page.frontmatter.activity"/>
<!-- article list -->
<ArticleGroup v-else-if="isRoot" :page-items="pageItems" />
<!-- nav with layout list -->
<ArticleGroup v-else-if="isNavLayout" :page-items="pageItems" />
<!-- tags -->
<Tags v-else-if="isTag" />
<!-- article page -->
<Page v-else :sidebar-items="sidebarItems"/>
<!-- pagation selector -->
<Pagation v-if="isRoot || isNavLayout"
:page-items="pages"
@change="page => currentPage = page" />
</div>
<ToolGroup v-if="!isNoToolGroup" />
</div>
<SWUpdatePopup :updateEvent="swUpdateEvent" />
</div>
<div class="background-mask" :style="wrapClasses"></div>
</div>
</template>
<script>
import Vue from "vue";
import nprogress from "nprogress";
import Activity from "./layout/Activity.vue";
import Navbar from "./components/Navbar.vue";
import Page from "./Page.vue";
import Sidebar from "./components/Sidebar.vue";
import ToolGroup from "./components/ToolGroup.vue";
import ArticleGroup from './components/ArticleGroup.vue'
import LeetCodeGroup from './components/LeetCodeGroup.vue'
import Pagation from './components/Pagation.vue'
import SWUpdatePopup from './components/SWUpdatePopup.vue'
import Tags from './components/Tags.vue'
import navLayoutMixin from './lib/navLayout.mixin'
import { resolveSidebarItems, getTitle } from "./lib/util";
export default {
mixins: [navLayoutMixin],
components: {
Activity,
Page,
Sidebar,
Navbar,
ToolGroup,
ArticleGroup,
LeetCodeGroup,
Pagation,
Tags,
SWUpdatePopup
},
data() {
return {
isSidebarOpen: false,
currentPage: 1,
swUpdateEvent: null
};
},
computed: {
isRoot() {
return this.$route.meta.root || this.$route.path === this.$rootOptions.path;
},
isTag() {
return this.$route.meta.tag;
},
isNoToolGroup() {
return this.$page.frontmatter.layout || this.$page.frontmatter.activity
},
pageItems() {
const start = (this.currentPage - 1) * this.perPage
const end = this.currentPage * this.perPage
return this.pages.filter((page, i) => (i >= start && i < end))
},
shouldShowNavbar() {
const { themeConfig } = this.$site;
return (
this.$site.title ||
themeConfig.logo ||
themeConfig.repo ||
themeConfig.nav
);
},
shouldShowSidebar() {
const { themeConfig } = this.$site;
const { frontmatter } = this.$page;
return (
!frontmatter.layout &&
!frontmatter.activity &&
frontmatter.sidebar !== false &&
this.sidebarItems.length
);
},
sidebarItems() {
return resolveSidebarItems(
this.$page,
this.$route,
this.$site,
this.$localePath
);
},
// when Activity Component, dont show tool group and expand the page container's width to 100%
pageContainerClasses() {
return !this.isNoToolGroup ? {} : { width: '100%' }
},
pageClasses() {
const userPageClass = this.$page.frontmatter.pageClass;
return [
{
"no-navbar": !this.shouldShowNavbar,
"sidebar-open": this.isSidebarOpen,
"no-sidebar": !this.shouldShowSidebar,
},
userPageClass
];
},
wrapClasses() {
const { themeConfig } = this.$site;
return themeConfig.background
? {
background:
'url("' +
`${this.$withBase(themeConfig.background)}` +
'") no-repeat fixed'
}
: { background: "#f6f6f6" };
}
},
created() {
if (this.$ssrContext) {
this.$ssrContext.title = getTitle(this.$title, this.$page);
this.$ssrContext.lang = this.$lang;
this.$ssrContext.description =
this.$page.description || this.$description;
}
},
mounted() {
// when swtich tab, change the current page
const updateCurPage = () => {
this.currentPage = 1
}
this.$watch('pages', updateCurPage)
// configure progress bar
nprogress.configure({ showSpinner: false });
this.$router.beforeEach((to, from, next) => {
if (to.path !== from.path && !Vue.component(to.name)) {
nprogress.start();
}
next();
});
this.$router.afterEach(() => {
nprogress.done();
this.isSidebarOpen = false;
});
this.$on('sw-updated', this.onSWUpdated);
},
methods: {
toggleSidebar(to) {
this.isSidebarOpen = typeof to === "boolean" ? to : !this.isSidebarOpen;
},
// side swipe
onTouchStart(e) {
this.touchStart = {
x: e.changedTouches[0].clientX,
y: e.changedTouches[0].clientY
};
},
onTouchEnd(e) {
const dx = e.changedTouches[0].clientX - this.touchStart.x;
const dy = e.changedTouches[0].clientY - this.touchStart.y;
if (Math.abs(dx) > Math.abs(dy) && Math.abs(dx) > 40) {
if (dx > 0 && this.touchStart.x <= 80) {
this.toggleSidebar(true);
} else {
this.toggleSidebar(false);
}
}
},
onSWUpdated(e) {
console.log('pwa update: ', e);
this.swUpdateEvent = e;
}
}
};
</script>
<style src="prismjs/themes/prism-tomorrow.css"></style>
<style src="./styles/theme.styl" lang="stylus"></style>