2022-07-07 17:59:55 +00:00
|
|
|
import { computed, defineComponent, h, onBeforeUnmount, ref } from 'vue'
|
2023-10-30 21:05:02 +00:00
|
|
|
import { useNuxtApp } from '../nuxt'
|
|
|
|
import { useRouter } from '../composables/router'
|
2023-10-27 14:32:09 +00:00
|
|
|
import { isChangingPage } from './utils'
|
2023-06-16 14:47:20 +00:00
|
|
|
|
|
|
|
// @ts-expect-error virtual file
|
|
|
|
import { globalMiddleware } from '#build/middleware'
|
2022-07-07 17:59:55 +00:00
|
|
|
|
|
|
|
export default defineComponent({
|
|
|
|
name: 'NuxtLoadingIndicator',
|
|
|
|
props: {
|
|
|
|
throttle: {
|
|
|
|
type: Number,
|
|
|
|
default: 200
|
|
|
|
},
|
|
|
|
duration: {
|
|
|
|
type: Number,
|
|
|
|
default: 2000
|
|
|
|
},
|
|
|
|
height: {
|
|
|
|
type: Number,
|
|
|
|
default: 3
|
|
|
|
},
|
|
|
|
color: {
|
2023-01-21 23:15:32 +00:00
|
|
|
type: [String, Boolean],
|
2022-07-07 17:59:55 +00:00
|
|
|
default: 'repeating-linear-gradient(to right,#00dc82 0%,#34cdfe 50%,#0047e1 100%)'
|
|
|
|
}
|
|
|
|
},
|
2022-10-10 10:12:36 +00:00
|
|
|
setup (props, { slots }) {
|
2023-08-07 13:03:41 +00:00
|
|
|
// TODO: use computed values in useLoadingIndicator
|
2023-10-31 16:36:44 +00:00
|
|
|
const { progress, isLoading, start, finish, clear } = useLoadingIndicator({
|
2022-07-07 17:59:55 +00:00
|
|
|
duration: props.duration,
|
|
|
|
throttle: props.throttle
|
|
|
|
})
|
|
|
|
|
2023-10-31 16:36:44 +00:00
|
|
|
if (import.meta.client) {
|
|
|
|
// Hook to app lifecycle
|
|
|
|
// TODO: Use unified loading API
|
|
|
|
const nuxtApp = useNuxtApp()
|
|
|
|
const router = useRouter()
|
2023-06-16 14:47:20 +00:00
|
|
|
|
2023-10-31 16:36:44 +00:00
|
|
|
globalMiddleware.unshift(start)
|
|
|
|
router.onError(() => {
|
|
|
|
finish()
|
|
|
|
})
|
|
|
|
router.beforeResolve((to, from) => {
|
|
|
|
if (!isChangingPage(to, from)) {
|
|
|
|
finish()
|
|
|
|
}
|
|
|
|
})
|
2023-07-03 11:14:17 +00:00
|
|
|
|
2023-10-31 16:36:44 +00:00
|
|
|
router.afterEach((_to, _from, failure) => {
|
|
|
|
if (failure) {
|
|
|
|
finish()
|
|
|
|
}
|
|
|
|
})
|
2023-07-03 11:14:17 +00:00
|
|
|
|
2023-10-31 16:36:44 +00:00
|
|
|
const unsubPage = nuxtApp.hook('page:finish', finish)
|
|
|
|
const unsubError = nuxtApp.hook('vue:error', finish)
|
2023-07-03 11:14:17 +00:00
|
|
|
|
2023-10-31 16:36:44 +00:00
|
|
|
onBeforeUnmount(() => {
|
|
|
|
const index = globalMiddleware.indexOf(start)
|
|
|
|
if (index >= 0) {
|
|
|
|
globalMiddleware.splice(index, 1)
|
|
|
|
}
|
|
|
|
unsubPage()
|
|
|
|
unsubError()
|
|
|
|
clear()
|
|
|
|
})
|
|
|
|
}
|
2022-07-07 17:59:55 +00:00
|
|
|
|
|
|
|
return () => h('div', {
|
|
|
|
class: 'nuxt-loading-indicator',
|
|
|
|
style: {
|
|
|
|
position: 'fixed',
|
|
|
|
top: 0,
|
|
|
|
right: 0,
|
|
|
|
left: 0,
|
|
|
|
pointerEvents: 'none',
|
2023-02-16 13:04:09 +00:00
|
|
|
width: 'auto',
|
2022-07-07 17:59:55 +00:00
|
|
|
height: `${props.height}px`,
|
2023-10-31 16:36:44 +00:00
|
|
|
opacity: isLoading.value ? 1 : 0,
|
2023-01-21 23:15:32 +00:00
|
|
|
background: props.color || undefined,
|
2023-10-31 16:36:44 +00:00
|
|
|
backgroundSize: `${(100 / progress.value) * 100}% auto`,
|
|
|
|
transform: `scaleX(${progress.value}%)`,
|
2023-02-16 13:04:09 +00:00
|
|
|
transformOrigin: 'left',
|
|
|
|
transition: 'transform 0.1s, height 0.4s, opacity 0.4s',
|
2022-07-07 17:59:55 +00:00
|
|
|
zIndex: 999999
|
|
|
|
}
|
2022-10-10 10:12:36 +00:00
|
|
|
}, slots)
|
2022-07-07 17:59:55 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
function useLoadingIndicator (opts: {
|
|
|
|
duration: number,
|
|
|
|
throttle: number
|
|
|
|
}) {
|
|
|
|
const progress = ref(0)
|
|
|
|
const isLoading = ref(false)
|
|
|
|
const step = computed(() => 10000 / opts.duration)
|
|
|
|
|
|
|
|
let _timer: any = null
|
|
|
|
let _throttle: any = null
|
|
|
|
|
|
|
|
function start () {
|
|
|
|
clear()
|
|
|
|
progress.value = 0
|
2023-08-07 22:03:40 +00:00
|
|
|
if (opts.throttle && import.meta.client) {
|
2023-01-02 18:20:53 +00:00
|
|
|
_throttle = setTimeout(() => {
|
|
|
|
isLoading.value = true
|
|
|
|
_startTimer()
|
|
|
|
}, opts.throttle)
|
2022-07-07 17:59:55 +00:00
|
|
|
} else {
|
2023-01-02 18:20:53 +00:00
|
|
|
isLoading.value = true
|
2022-07-07 17:59:55 +00:00
|
|
|
_startTimer()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
function finish () {
|
|
|
|
progress.value = 100
|
|
|
|
_hide()
|
|
|
|
}
|
|
|
|
|
|
|
|
function clear () {
|
|
|
|
clearInterval(_timer)
|
|
|
|
clearTimeout(_throttle)
|
|
|
|
_timer = null
|
|
|
|
_throttle = null
|
|
|
|
}
|
|
|
|
|
|
|
|
function _increase (num: number) {
|
|
|
|
progress.value = Math.min(100, progress.value + num)
|
|
|
|
}
|
|
|
|
|
|
|
|
function _hide () {
|
|
|
|
clear()
|
2023-08-07 22:03:40 +00:00
|
|
|
if (import.meta.client) {
|
2022-07-07 17:59:55 +00:00
|
|
|
setTimeout(() => {
|
|
|
|
isLoading.value = false
|
|
|
|
setTimeout(() => { progress.value = 0 }, 400)
|
|
|
|
}, 500)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function _startTimer () {
|
2023-08-07 22:03:40 +00:00
|
|
|
if (import.meta.client) {
|
2022-07-07 17:59:55 +00:00
|
|
|
_timer = setInterval(() => { _increase(step.value) }, 100)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
progress,
|
|
|
|
isLoading,
|
|
|
|
start,
|
|
|
|
finish,
|
|
|
|
clear
|
|
|
|
}
|
|
|
|
}
|