uptime-kuma/src/components/HeartbeatBar.vue

255 lines
6.7 KiB
Vue
Raw Normal View History

2021-06-25 19:03:06 +00:00
<template>
2021-07-27 17:47:13 +00:00
<div ref="wrap" class="wrap" :style="wrapStyle">
2022-01-21 16:13:38 +00:00
<div class="hp-bar-big d-flex" :style="barStyle">
2021-06-29 08:06:20 +00:00
<div
2021-07-27 17:47:13 +00:00
v-for="(beat, index) in shortBeatList"
:key="index"
2021-06-29 08:06:20 +00:00
class="beat"
:class="{ 'empty' : (beat === 0), 'down' : (beat.status === 0), 'pending' : (beat.status === 2) }"
2021-06-29 08:06:20 +00:00
:style="beatStyle"
:title="getBeatTitle(beat)"
2022-01-21 16:13:38 +00:00
@mouseenter="toggleActivateSibling"
@mouseleave="toggleActivateSibling"
>
<div class="beat-inner" />
</div>
2021-06-25 19:03:06 +00:00
</div>
</div>
</template>
<script>
export default {
2021-06-27 08:10:55 +00:00
props: {
size: {
type: String,
2021-07-27 17:47:13 +00:00
default: "big",
2021-06-27 08:10:55 +00:00
},
monitorId: {
type: Number,
required: true,
},
2021-09-11 15:43:07 +00:00
heartbeatList: {
type: Array,
default: null,
}
2021-06-27 08:10:55 +00:00
},
2021-06-25 19:03:06 +00:00
data() {
return {
beatWidth: 10,
beatHeight: 30,
hoverScale: 1.5,
beatMargin: 4,
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
},
computed: {
2021-06-26 06:32:12 +00:00
2021-09-11 15:43:07 +00:00
/**
* If heartbeatList is null, get it from $root.heartbeatList
*/
2021-06-27 08:10:55 +00:00
beatList() {
2021-09-11 15:43:07 +00:00
if (this.heartbeatList === null) {
return this.$root.heartbeatList[this.monitorId];
} else {
return this.heartbeatList;
}
2021-06-27 08:10:55 +00:00
},
2021-06-26 06:32:12 +00:00
shortBeatList() {
if (! this.beatList) {
return [];
}
let placeholders = [];
2021-06-27 08:10:55 +00:00
2021-06-26 06:32:12 +00:00
let start = this.beatList.length - this.maxBeat;
if (this.move) {
start = start - 1;
}
if (start < 0) {
2021-06-27 08:10:55 +00:00
// Add empty placeholder
for (let i = start; i < 0; i++) {
placeholders.push(0)
2021-06-27 08:10:55 +00:00
}
2021-06-26 06:32:12 +00:00
start = 0;
}
return placeholders.concat(this.beatList.slice(start))
2021-06-26 06:32:12 +00:00
},
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);
return {
padding: `${topBottom}px ${leftRight}px`,
2021-07-28 15:03:37 +00:00
width: "100%",
}
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
}
2021-07-27 17:47:13 +00:00
return {
transform: "translateX(0)",
}
2021-07-27 17:47:13 +00:00
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-07-27 17:47:13 +00:00
},
2021-06-26 06:32:12 +00:00
},
watch: {
beatList: {
handler(val, oldVal) {
this.move = true;
setTimeout(() => {
this.move = false;
}, 300)
2021-06-26 06:32:12 +00:00
},
deep: true,
2021-07-27 17:47:13 +00:00
},
},
unmounted() {
window.removeEventListener("resize", this.resize);
},
beforeMount() {
2021-09-11 15:43:07 +00:00
if (this.heartbeatList === null) {
if (! (this.monitorId in this.$root.heartbeatList)) {
this.$root.heartbeatList[this.monitorId] = [];
}
}
},
2021-07-27 17:47:13 +00:00
mounted() {
if (this.size === "small") {
this.beatWidth = 5;
this.beatHeight = 16;
this.beatMargin = 2;
}
// Suddenly, have an idea how to handle it universally.
// If the pixel * ratio != Integer, then it causes render issue, round it to solve it!!
const actualWidth = this.beatWidth * window.devicePixelRatio;
const actualMargin = this.beatMargin * window.devicePixelRatio;
if (! Number.isInteger(actualWidth)) {
this.beatWidth = Math.round(actualWidth) / window.devicePixelRatio;
}
if (! Number.isInteger(actualMargin)) {
this.beatMargin = Math.round(actualMargin) / window.devicePixelRatio;
2021-06-26 06:32:12 +00:00
}
2021-07-27 17:47:13 +00:00
window.addEventListener("resize", this.resize);
this.resize();
},
methods: {
resize() {
if (this.$refs.wrap) {
this.maxBeat = Math.floor(this.$refs.wrap.clientWidth / (this.beatWidth + this.beatMargin * 2))
2021-07-27 17:47:13 +00:00
}
},
getBeatTitle(beat) {
2021-10-22 09:59:53 +00:00
return `${this.$root.datetime(beat.time)}` + ((beat.msg) ? ` - ${beat.msg}` : ``);
2022-01-21 16:13:38 +00:00
},
// Toggling the activeSibling class on hover over the current hover item
toggleActivateSibling(e) {
// Variable definition
const element = e.target;
const previous = element.previousSibling;
const next = element.nextSibling;
// Return if the hovered element has empty class
if (element.classList.contains("empty")) {
return;
}
// Check if Previous Sibling is heartbar element and doesn't have the empty class
if (previous.children && !previous.classList.contains("empty")) {
previous.classList.toggle("active-sibling");
}
// Check if Next Sibling is heartbar element and doesn't have the empty class
if (next.children && !next.classList.contains("empty")) {
2022-01-21 16:13:38 +00:00
next.classList.toggle("active-sibling");
}
}
2021-07-27 17:47:13 +00:00
},
2022-01-21 16:13:38 +00:00
};
2021-06-25 19:03:06 +00:00
</script>
<style lang="scss" scoped>
2021-06-25 19:03:06 +00:00
@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 {
background-color: $primary;
border-radius: $border-radius;
2022-01-21 16:13:38 +00:00
display: inline-block;
transition: all ease 0.6s;
2021-06-25 19:03:06 +00:00
2021-06-27 08:10:55 +00:00
&.empty {
2021-07-13 04:16:11 +00:00
background-color: aliceblue;
2021-06-26 06:32:12 +00:00
}
2021-06-27 08:10:55 +00:00
&.down {
background-color: $danger;
}
2021-06-25 19:03:06 +00:00
&.pending {
background-color: $warning;
}
2022-01-21 16:13:38 +00:00
.beat-inner {
border-radius: $border-radius;
display: inline-block;
height: 100%;
width: 5px;
}
2021-06-27 08:10:55 +00:00
&:not(.empty):hover {
2022-01-21 16:13:38 +00:00
transition: all ease 0.15s;
2021-06-25 19:03:06 +00:00
opacity: 0.8;
transform: scale(var(--hover-scale));
}
2022-01-21 16:13:38 +00:00
&.active-sibling {
transform: scale(1.3);
transition: all ease 0.15s;
}
2021-06-25 19:03:06 +00:00
}
}
.dark {
2021-08-24 15:38:25 +00:00
.hp-bar-big .beat.empty {
background-color: #848484;
}
}
2021-06-25 19:03:06 +00:00
</style>