2016-11-07 01:34:58 +00:00
|
|
|
<script>
|
|
|
|
export default {
|
2018-11-24 18:49:19 +00:00
|
|
|
name: 'NuxtLoading',
|
2019-09-10 09:51:14 +00:00
|
|
|
data () {
|
2016-11-07 01:34:58 +00:00
|
|
|
return {
|
2016-11-07 18:21:32 +00:00
|
|
|
percent: 0,
|
|
|
|
show: false,
|
2018-10-29 15:48:40 +00:00
|
|
|
canSucceed: true,
|
|
|
|
reversed: false,
|
|
|
|
skipTimerCount: 0,
|
2018-11-24 18:30:28 +00:00
|
|
|
rtl: <%= Boolean(loading.rtl) %>,
|
2019-01-03 13:31:00 +00:00
|
|
|
throttle: <%= typeof loading.throttle === 'number' ? loading.throttle : 200 %>,
|
2019-01-03 13:33:29 +00:00
|
|
|
duration: <%= typeof loading.duration === 'number' ? loading.duration : 3000 %>,
|
2018-11-24 18:30:28 +00:00
|
|
|
continuous: <%= Boolean(loading.continuous) %>
|
2018-10-29 15:48:40 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
computed: {
|
2019-09-10 09:51:14 +00:00
|
|
|
left () {
|
2018-10-29 15:48:40 +00:00
|
|
|
if (!this.continuous && !this.rtl) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return this.rtl
|
|
|
|
? (this.reversed ? '0px' : 'auto')
|
|
|
|
: (!this.reversed ? '0px' : 'auto')
|
2016-11-07 01:34:58 +00:00
|
|
|
}
|
|
|
|
},
|
2019-09-10 09:51:14 +00:00
|
|
|
beforeDestroy () {
|
2018-10-29 15:48:40 +00:00
|
|
|
this.clear()
|
|
|
|
},
|
2016-11-07 01:34:58 +00:00
|
|
|
methods: {
|
2019-09-10 09:51:14 +00:00
|
|
|
clear () {
|
2018-10-29 15:48:40 +00:00
|
|
|
clearInterval(this._timer)
|
|
|
|
clearTimeout(this._throttle)
|
2023-01-19 18:51:41 +00:00
|
|
|
clearTimeout(this._hide)
|
2018-10-29 15:48:40 +00:00
|
|
|
this._timer = null
|
|
|
|
},
|
2019-09-10 09:51:14 +00:00
|
|
|
start () {
|
2018-10-29 15:48:40 +00:00
|
|
|
this.clear()
|
|
|
|
this.percent = 0
|
|
|
|
this.reversed = false
|
|
|
|
this.skipTimerCount = 0
|
|
|
|
this.canSucceed = true
|
|
|
|
|
|
|
|
if (this.throttle) {
|
|
|
|
this._throttle = setTimeout(() => this.startTimer(), this.throttle)
|
|
|
|
} else {
|
|
|
|
this.startTimer()
|
2016-11-07 18:21:32 +00:00
|
|
|
}
|
|
|
|
return this
|
|
|
|
},
|
2019-09-10 09:51:14 +00:00
|
|
|
set (num) {
|
2016-11-07 18:21:32 +00:00
|
|
|
this.show = true
|
2018-10-29 15:48:40 +00:00
|
|
|
this.canSucceed = true
|
|
|
|
this.percent = Math.min(100, Math.max(0, Math.floor(num)))
|
2016-11-07 18:21:32 +00:00
|
|
|
return this
|
|
|
|
},
|
2019-09-10 09:51:14 +00:00
|
|
|
get () {
|
2018-10-29 15:48:40 +00:00
|
|
|
return this.percent
|
2016-11-07 18:21:32 +00:00
|
|
|
},
|
2019-09-10 09:51:14 +00:00
|
|
|
increase (num) {
|
2018-10-29 15:48:40 +00:00
|
|
|
this.percent = Math.min(100, Math.floor(this.percent + num))
|
2016-11-07 18:21:32 +00:00
|
|
|
return this
|
|
|
|
},
|
2019-09-10 09:51:14 +00:00
|
|
|
decrease (num) {
|
2018-10-29 15:48:40 +00:00
|
|
|
this.percent = Math.max(0, Math.floor(this.percent - num))
|
2016-11-07 18:21:32 +00:00
|
|
|
return this
|
|
|
|
},
|
2019-09-10 09:51:14 +00:00
|
|
|
pause () {
|
2016-11-07 18:21:32 +00:00
|
|
|
clearInterval(this._timer)
|
|
|
|
return this
|
|
|
|
},
|
2019-09-10 09:51:14 +00:00
|
|
|
resume () {
|
2018-10-29 15:48:40 +00:00
|
|
|
this.startTimer()
|
|
|
|
return this
|
|
|
|
},
|
2019-09-10 09:51:14 +00:00
|
|
|
finish () {
|
2018-10-29 15:48:40 +00:00
|
|
|
this.percent = this.reversed ? 0 : 100
|
|
|
|
this.hide()
|
|
|
|
return this
|
|
|
|
},
|
2019-09-10 09:51:14 +00:00
|
|
|
hide () {
|
2018-10-29 15:48:40 +00:00
|
|
|
this.clear()
|
2023-01-19 18:51:41 +00:00
|
|
|
this._hide = setTimeout(() => {
|
2016-11-07 18:21:32 +00:00
|
|
|
this.show = false
|
2018-10-29 15:48:40 +00:00
|
|
|
this.$nextTick(() => {
|
|
|
|
this.percent = 0
|
|
|
|
this.reversed = false
|
2016-11-07 18:21:32 +00:00
|
|
|
})
|
2016-11-07 20:58:56 +00:00
|
|
|
}, 500)
|
2016-11-07 18:21:32 +00:00
|
|
|
return this
|
|
|
|
},
|
2020-02-03 15:41:37 +00:00
|
|
|
fail (error) {
|
2018-10-29 15:48:40 +00:00
|
|
|
this.canSucceed = false
|
2016-11-07 18:21:32 +00:00
|
|
|
return this
|
2018-10-29 15:48:40 +00:00
|
|
|
},
|
2019-09-10 09:51:14 +00:00
|
|
|
startTimer () {
|
2018-10-29 15:48:40 +00:00
|
|
|
if (!this.show) {
|
|
|
|
this.show = true
|
|
|
|
}
|
|
|
|
if (typeof this._cut === 'undefined') {
|
|
|
|
this._cut = 10000 / Math.floor(this.duration)
|
|
|
|
}
|
|
|
|
|
|
|
|
this._timer = setInterval(() => {
|
|
|
|
/**
|
|
|
|
* When reversing direction skip one timers
|
|
|
|
* so 0, 100 are displayed for two iterations
|
|
|
|
* also disable css width transitioning
|
|
|
|
* which otherwise interferes and shows
|
|
|
|
* a jojo effect
|
|
|
|
*/
|
|
|
|
if (this.skipTimerCount > 0) {
|
|
|
|
this.skipTimerCount--
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.reversed) {
|
|
|
|
this.decrease(this._cut)
|
|
|
|
} else {
|
|
|
|
this.increase(this._cut)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.continuous) {
|
|
|
|
if (this.percent >= 100) {
|
|
|
|
this.skipTimerCount = 1
|
|
|
|
|
|
|
|
this.reversed = !this.reversed
|
|
|
|
} else if (this.percent <= 0) {
|
|
|
|
this.skipTimerCount = 1
|
|
|
|
|
|
|
|
this.reversed = !this.reversed
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}, 100)
|
|
|
|
}
|
|
|
|
},
|
2019-09-10 09:51:14 +00:00
|
|
|
render (h) {
|
2018-10-29 15:48:40 +00:00
|
|
|
let el = h(false)
|
|
|
|
if (this.show) {
|
|
|
|
el = h('div', {
|
|
|
|
staticClass: 'nuxt-progress',
|
|
|
|
class: {
|
|
|
|
'nuxt-progress-notransition': this.skipTimerCount > 0,
|
|
|
|
'nuxt-progress-failed': !this.canSucceed
|
|
|
|
},
|
|
|
|
style: {
|
2019-11-26 22:42:39 +00:00
|
|
|
width: this.percent + '%',
|
|
|
|
left: this.left
|
2018-10-29 15:48:40 +00:00
|
|
|
}
|
|
|
|
})
|
2016-11-07 18:21:32 +00:00
|
|
|
}
|
2018-10-29 15:48:40 +00:00
|
|
|
return el
|
2016-11-07 01:34:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
</script>
|
2018-10-29 15:48:40 +00:00
|
|
|
<% if (loading && loading.css) { %>
|
2017-08-27 17:52:27 +00:00
|
|
|
<style>
|
2017-08-27 01:16:30 +00:00
|
|
|
.nuxt-progress {
|
2016-11-07 18:21:32 +00:00
|
|
|
position: fixed;
|
|
|
|
top: 0px;
|
2017-08-29 11:58:45 +00:00
|
|
|
left: <%= loading.rtl === true ? 'auto' : '0px' %>;
|
2016-11-07 18:21:32 +00:00
|
|
|
right: 0px;
|
2018-10-29 15:48:40 +00:00
|
|
|
height: <%= loading.height %>;
|
2016-11-07 18:21:32 +00:00
|
|
|
width: 0%;
|
|
|
|
opacity: 1;
|
2018-10-29 15:48:40 +00:00
|
|
|
transition: width 0.1s, opacity 0.4s;
|
|
|
|
background-color: <%= loading.color %>;
|
2016-11-07 18:21:32 +00:00
|
|
|
z-index: 999999;
|
|
|
|
}
|
2018-10-29 15:48:40 +00:00
|
|
|
|
|
|
|
.nuxt-progress.nuxt-progress-notransition {
|
|
|
|
transition: none;
|
|
|
|
}
|
|
|
|
|
|
|
|
.nuxt-progress-failed {
|
|
|
|
background-color: <%= loading.failedColor %>;
|
|
|
|
}
|
|
|
|
</style><% } %>
|