forked from openWB/openwb-ui-settings
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOpenwbBaseToast.vue
109 lines (107 loc) · 2.27 KB
/
OpenwbBaseToast.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<template>
<div class="toast" :class="'border-' + subtype" role="alert">
<div class="toast-header" :class="'bg-' + subtype">
<strong class="pr-2 mr-auto">OpenWB</strong>
<small v-if="timestamp" class="time-diff">{{ relativeTime }}</small>
<button
type="button"
class="ml-2 mb-1 close"
data-dismiss="toast"
aria-label="Close"
@click="dismiss"
>
<span aria-hidden="true">×</span>
</button>
</div>
<div class="toast-body">
<slot />
</div>
</div>
</template>
<script>
export default {
name: "OpenwbToast",
emits: ["dismiss", "hide"],
props: {
topic: { type: String, required: true },
subtype: {
validator: function (value) {
return (
[
"info",
"success",
"warning",
"danger",
"primary",
"secondary",
"light",
"dark",
].indexOf(value) !== -1
);
},
default: "secondary",
},
source: {
validator: function (value) {
return ["system", "client", "command"].indexOf(value) !== -1;
},
},
timestamp: { type: Number },
},
data() {
return {
handle: undefined,
relativeTime: undefined,
hidden: false,
};
},
methods: {
dismiss() {
this.$emit("dismiss", { topic: this.topic });
},
updateRelativeTime() {
// in milliseconds
const units = {
year: 24 * 60 * 60 * 1000 * 365,
month: (24 * 60 * 60 * 1000 * 365) / 12,
day: 24 * 60 * 60 * 1000,
hour: 60 * 60 * 1000,
minute: 60 * 1000,
second: 1000,
};
var rtf = new Intl.RelativeTimeFormat("de", { numeric: "auto" });
const now = new Date();
const elapsed = now - this.timestamp * 1000;
for (var unit in units) {
if (Math.abs(elapsed) > units[unit]) {
this.relativeTime = rtf.format(
Math.round(-elapsed / units[unit]), // negative time diff!
unit,
);
break;
}
}
// hide after 10 seconds
if (elapsed > 10 * 1000 && !this.hidden) {
this.hidden = true;
this.$emit("hide", { topic: this.topic });
}
},
},
mounted() {
this.handle = window.setInterval(this.updateRelativeTime, 1000);
},
unmounted() {
window.clearInterval(this.handle);
},
};
</script>
<style scoped>
.toast {
opacity: 1;
border: 1px solid rgba(0, 0, 0, 0.1);
}
.toast:not(:last-child) {
margin-bottom: 0.25rem !important;
}
</style>