Skip to content
This repository was archived by the owner on Jan 8, 2025. It is now read-only.

Commit 66b5cf1

Browse files
committed
feat(site): add nprogress
1 parent 97d6bf1 commit 66b5cf1

File tree

2 files changed

+86
-1
lines changed

2 files changed

+86
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import Progress from '../../components/progress'
2+
import config from '@config'
3+
import { getBrowserThemes, watchThemes } from '../../utils'
4+
import { get } from 'lodash-es'
5+
6+
export default {
7+
name: 'NProgress',
8+
data() {
9+
return {
10+
trackColor: this.getTrackColor(),
11+
color: this.getColor(),
12+
value: 0,
13+
currentThemes: '',
14+
timer: null
15+
}
16+
},
17+
methods: {
18+
start() {
19+
this.value = 0
20+
window.setTimeout(() => (this.color = this.getColor(this.currentThemes)), 200)
21+
this.changeValue()
22+
},
23+
end() {
24+
this.value = 100
25+
setTimeout(() => (this.color = this.getTrackColor(this.currentThemes)), 300)
26+
window.clearTimeout(this.timer)
27+
},
28+
getColor(themes) {
29+
const themesKey = get(config, 'themesKey')
30+
31+
return get(config, `${themes ?? getBrowserThemes(themesKey)}.color-progress`)
32+
},
33+
getTrackColor(themes) {
34+
const themesKey = get(config, 'themesKey')
35+
36+
return get(config, `${themes ?? getBrowserThemes(themesKey)}.color-progress-track`)
37+
},
38+
changeValue() {
39+
this.timer = window.setTimeout(() => {
40+
if (this.value >= 95) return
41+
let num = Math.random()
42+
43+
if (this.value < 70) num = Math.round(5 * Math.random())
44+
45+
this.value += num
46+
this.changeValue()
47+
}, 200)
48+
}
49+
},
50+
created() {
51+
watchThemes(this, (themes) => {
52+
this.currentThemes = themes
53+
this.trackColor = this.getTrackColor(themes)
54+
this.color = this.value === 100 ? this.getTrackColor(themes) : this.getColor(themes)
55+
}, false)
56+
},
57+
render() {
58+
return (
59+
<Progress
60+
style={{
61+
position: 'fixed',
62+
width: '100%',
63+
left: 0,
64+
top: 0,
65+
zIndex: 10086
66+
}}
67+
lineWidth={3}
68+
trackColor={this.trackColor}
69+
color={this.color}
70+
value={this.value}
71+
/>
72+
)
73+
}
74+
}

packages/varlet-vue2-cli/site/pc/router.js

+12-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import VueRouter from 'vue-router'
33
import config from '@config'
44
import routes from '@pc-routes'
55
import { get } from 'lodash-es'
6+
import { mountComponent } from '../utils'
7+
import NProgress from './components/NProgress'
68

79
const originalReplace = VueRouter.prototype.replace
810
VueRouter.prototype.replace = function replace(location, onResolve, onReject) {
@@ -14,6 +16,9 @@ VueRouter.prototype.replace = function replace(location, onResolve, onReject) {
1416

1517
Vue.use(VueRouter)
1618

19+
let isEnd = true
20+
const { instance } = mountComponent(NProgress)
21+
1722
const defaultLanguage = get(config, 'defaultLanguage')
1823
const redirect = get(config, 'pc.redirect')
1924
const mobileRedirect = get(config, 'mobile.redirect')
@@ -35,7 +40,8 @@ router.beforeEach((to, from, next) => {
3540
return
3641
}
3742

38-
// TODO: progress
43+
isEnd = false
44+
setTimeout(() => !isEnd && instance.start(), 200)
3945

4046
if (window._hmt) {
4147
if (to.path) {
@@ -46,6 +52,11 @@ router.beforeEach((to, from, next) => {
4652
next()
4753
})
4854

55+
router.afterEach(() => {
56+
isEnd = true
57+
instance.end()
58+
})
59+
4960
Object.defineProperty(window, 'onMobileRouteChange', {
5061
value: (path, language, replace) => {
5162
if (path === mobileRedirect) {

0 commit comments

Comments
 (0)