uptime-kuma/src/components/HeartbeatBar.vue

149 lines
3.4 KiB
Vue
Raw Normal View History

2021-06-25 19:03:06 +00:00
<template>
2021-06-26 06:32:12 +00:00
<div class="wrap" :style="wrapStyle" ref="wrap">
2021-06-25 19:03:06 +00:00
<div class="hp-bar-big" :style="barStyle">
2021-06-26 06:32:12 +00:00
<div class="beat" :style="beatStyle" v-for="(beat, index) in shortBeatList" :key="index">
</div>
2021-06-25 19:03:06 +00:00
</div>
</div>
</template>
<script>
export default {
data() {
return {
2021-06-26 06:32:12 +00:00
i: 1,
2021-06-25 19:03:06 +00:00
beatList: [
2021-06-26 06:32:12 +00:00
2021-06-25 19:03:06 +00:00
],
beatWidth: 10,
beatHeight: 30,
hoverScale: 1.5,
2021-06-26 06:32:12 +00:00
beatMargin: 3, // Odd number only, even = blurry
2021-06-25 19:03:06 +00:00
move: false,
2021-06-26 06:32:12 +00:00
maxBeat: -1,
2021-06-25 19:03:06 +00:00
}
},
2021-06-26 06:32:12 +00:00
destroyed() {
window.removeEventListener("resize", this.resize);
},
2021-06-25 19:03:06 +00:00
mounted() {
2021-06-26 06:32:12 +00:00
window.addEventListener("resize", this.resize);
this.resize();
2021-06-25 19:03:06 +00:00
2021-06-26 06:32:12 +00:00
setInterval(() => {
this.beatList.push(this.i++)
2021-06-25 19:03:06 +00:00
}, 3000)
},
methods: {
2021-06-26 06:32:12 +00:00
resize() {
this.maxBeat = Math.floor(this.$refs.wrap.clientWidth / (this.beatWidth + this.beatMargin * 2))
2021-06-25 19:03:06 +00:00
}
},
computed: {
2021-06-26 06:32:12 +00:00
shortBeatList() {
let start = this.beatList.length - this.maxBeat;
if (this.move) {
start = start - 1;
}
if (start < 0) {
start = 0;
}
return this.beatList.slice(start)
},
2021-06-25 19:03:06 +00:00
wrapStyle() {
let topBottom = (((this.beatHeight * this.hoverScale) - this.beatHeight) / 2);
let leftRight = (((this.beatWidth * this.hoverScale) - this.beatWidth) / 2);
2021-06-26 06:32:12 +00:00
let width
if (this.maxBeat > 0) {
width = (this.beatWidth + this.beatMargin * 2) * this.maxBeat + (leftRight * 2) + "px"
} {
width = "100%"
}
2021-06-25 19:03:06 +00:00
return {
padding: `${topBottom}px ${leftRight}px`,
2021-06-26 06:32:12 +00:00
width: width
2021-06-25 19:03:06 +00:00
}
},
2021-06-26 06:32:12 +00:00
2021-06-25 19:03:06 +00:00
barStyle() {
2021-06-26 06:32:12 +00:00
if (this.move && this.shortBeatList.length > this.maxBeat) {
2021-06-25 19:03:06 +00:00
let width = -(this.beatWidth + this.beatMargin * 2);
2021-06-26 06:32:12 +00:00
return {
2021-06-25 19:03:06 +00:00
transition: "all ease-in-out 0.25s",
transform: `translateX(${width}px)`,
}
2021-06-26 06:32:12 +00:00
2021-06-25 19:03:06 +00:00
} else {
return {
2021-06-26 06:32:12 +00:00
transform: `translateX(0)`,
2021-06-25 19:03:06 +00:00
}
}
},
2021-06-26 06:32:12 +00:00
2021-06-25 19:03:06 +00:00
beatStyle() {
return {
width: this.beatWidth + "px",
height: this.beatHeight + "px",
margin: this.beatMargin + "px",
"--hover-scale": this.hoverScale,
}
}
2021-06-26 06:32:12 +00:00
},
watch: {
beatList: {
handler(val, oldVal) {
console.log("add beat2")
this.move = true;
setTimeout(() => {
this.move = false;
}, 300)
},
deep: true,
}
2021-06-25 19:03:06 +00:00
}
}
</script>
<style scoped lang="scss">
@import "../assets/vars.scss";
.wrap {
overflow: hidden;
2021-06-26 06:32:12 +00:00
width: 100%;
2021-06-25 19:03:06 +00:00
white-space: nowrap;
}
.hp-bar-big {
.beat {
display: inline-block;
background-color: $primary;
border-radius: 50rem;
transition: all ease-in-out 0.15s;
2021-06-26 06:32:12 +00:00
&.new-beat {
background-color: aliceblue;
}
2021-06-25 19:03:06 +00:00
&:hover {
opacity: 0.8;
transform: scale(var(--hover-scale));
}
}
}
</style>