uptime-kuma/src/components/CountUp.vue

80 lines
1.7 KiB
Vue
Raw Normal View History

2021-06-30 13:04:58 +00:00
<template>
<span v-if="isNum" ref="output">{{ outputFixed }}</span> <span v-if="isNum">{{ unit }}</span>
2021-06-30 13:04:58 +00:00
<span v-else>{{ value }}</span>
</template>
<script lang="ts">
2021-06-30 13:04:58 +00:00
2022-04-13 16:30:32 +00:00
import { sleep } from "../util.ts";
2021-06-30 13:04:58 +00:00
export default {
props: {
/** Value to count */
2022-04-30 01:51:14 +00:00
value: {
type: [ String, Number ],
default: 0,
},
2021-06-30 13:04:58 +00:00
time: {
2021-07-27 17:53:59 +00:00
type: Number,
2021-06-30 13:04:58 +00:00
default: 0.3,
},
/** Unit of the value */
2021-06-30 13:04:58 +00:00
unit: {
2021-07-27 17:53:59 +00:00
type: String,
2021-06-30 13:04:58 +00:00
default: "ms",
2021-07-27 17:47:13 +00:00
},
2021-06-30 13:04:58 +00:00
},
data() {
return {
output: "",
frameDuration: 30,
2022-04-13 16:30:32 +00:00
};
2021-06-30 13:04:58 +00:00
},
computed: {
isNum() {
2022-04-13 16:30:32 +00:00
return typeof this.value === "number";
2021-07-27 17:47:13 +00:00
},
outputFixed() {
if (typeof this.output === "number") {
if (this.output < 1) {
return "<1";
} else if (Number.isInteger(this.output)) {
return this.output;
} else {
return this.output.toFixed(2);
}
} else {
return this.output;
}
}
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);
if (! (isNaN(step) || ! this.isNum || (diff > 0 && step < 1) || (diff < 0 && step > 1) || diff === 0)) {
2021-06-30 13:04:58 +00:00
for (let i = 1; i < frames; i++) {
this.output += step;
2022-04-13 16:30:32 +00:00
await sleep(15);
2021-06-30 13:04:58 +00:00
}
}
this.output = this.value;
},
},
2021-07-27 17:47:13 +00:00
mounted() {
this.output = this.value;
},
methods: {},
2022-04-13 16:30:32 +00:00
};
2021-06-30 13:04:58 +00:00
</script>