uptime-kuma/src/components/CountUp.vue

68 lines
1.3 KiB
Vue
Raw Normal View History

2021-06-30 13:04:58 +00:00
<template>
<span v-if="isNum" ref="output">{{ output }}</span> <span v-if="isNum">{{ unit }}</span>
<span v-else>{{ value }}</span>
</template>
<script>
2021-07-27 17:47:13 +00:00
import { sleep } from "../util-frontend"
2021-06-30 13:04:58 +00:00
export default {
props: {
value: [String, Number],
time: {
Number,
default: 0.3,
},
unit: {
String,
default: "ms",
2021-07-27 17:47:13 +00:00
},
2021-06-30 13:04:58 +00:00
},
data() {
return {
output: "",
frameDuration: 30,
}
},
computed: {
isNum() {
2021-07-27 17:47:13 +00:00
return typeof this.value === "number"
},
2021-06-30 13:04:58 +00:00
},
watch: {
async value(from, to) {
let diff = to - from;
let frames = 12;
let step = Math.floor(diff / frames);
2021-07-01 09:00:23 +00:00
if (isNaN(step) || ! this.isNum || (diff > 0 && step < 1) || (diff < 0 && step > 1) || diff === 0) {
2021-06-30 13:04:58 +00:00
// Lazy to NOT this condition, hahaha.
} else {
for (let i = 1; i < frames; i++) {
this.output += step;
await sleep(15)
}
}
this.output = this.value;
},
},
2021-07-27 17:47:13 +00:00
mounted() {
this.output = this.value;
},
methods: {},
2021-06-30 13:04:58 +00:00
}
</script>
<style scoped>
</style>