2016-11-07 01:34:58 +00:00
|
|
|
<script>
|
|
|
|
export default {
|
2018-11-24 18:49:19 +00:00
|
|
|
name: 'NuxtLoading',
|
2018-10-24 13:46:06 +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) %>,
|
|
|
|
throttle: <%= Number(loading.throttle) ? loading.throttle : 200 %>,
|
|
|
|
duration: <%= Number(loading.duration) ? loading.duration : 3000 %>,
|
|
|
|
continuous: <%= Boolean(loading.continuous) %>
|
2018-10-29 15:48:40 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
computed: {
|
|
|
|
left() {
|
|
|
|
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
|
|
|
}
|
|
|
|
},
|
2018-10-29 15:48:40 +00:00
|
|
|
beforeDestroy() {
|
|
|
|
this.clear()
|
|
|
|
},
|
2016-11-07 01:34:58 +00:00
|
|
|
methods: {
|
2018-10-29 15:48:40 +00:00
|
|
|
clear() {
|
|
|
|
clearInterval(this._timer)
|
|
|
|
clearTimeout(this._throttle)
|
|
|
|
this._timer = null
|
|
|
|
},
|
2018-10-24 13:46:06 +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
|
|
|
|
},
|
2018-10-24 13:46:06 +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
|
|
|
|
},
|
2018-10-24 13:46:06 +00:00
|
|
|
get() {
|
2018-10-29 15:48:40 +00:00
|
|
|
return this.percent
|
2016-11-07 18:21:32 +00:00
|
|
|
},
|
2018-10-24 13:46:06 +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
|
|
|
|
},
|
2018-10-24 13:46:06 +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
|
|
|
|
},
|
2018-10-24 13:46:06 +00:00
|
|
|
pause() {
|
2016-11-07 18:21:32 +00:00
|
|
|
clearInterval(this._timer)
|
|
|
|
return this
|
|
|
|
},
|
2018-10-29 15:48:40 +00:00
|
|
|
resume() {
|
|
|
|
this.startTimer()
|
|
|
|
return this
|
|
|
|
},
|
|
|
|
finish() {
|
|
|
|
this.percent = this.reversed ? 0 : 100
|
|
|
|
this.hide()
|
|
|
|
return this
|
|
|
|
},
|
2018-10-24 13:46:06 +00:00
|
|
|
hide() {
|
2018-10-29 15:48:40 +00:00
|
|
|
this.clear()
|
2016-11-07 18:21:32 +00:00
|
|
|
setTimeout(() => {
|
|
|
|
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
|
|
|
|
},
|
2018-10-24 13:46:06 +00:00
|
|
|
fail() {
|
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
|
|
|
},
|
|
|
|
startTimer() {
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
render(h) {
|
|
|
|
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: {
|
|
|
|
'width': this.percent + '%',
|
|
|
|
'left': this.left
|
|
|
|
}
|
|
|
|
})
|
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><% } %>
|