alert.vue
2.64 KB
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
<template>
<div :class="classObject" role="alert" v-if="localShow">
<button type="button"
class="close"
data-dismiss="alert"
aria-label="Close"
v-if="dismissible"
@click.stop.prevent="dismiss"
>
<span aria-hidden="true">×</span>
</button>
<slot></slot>
</div>
</template>
<script>
export default {
data() {
return {
countDownTimerId: null,
dismissed: false
};
},
computed: {
classObject() {
return ['alert', this.alertState, this.dismissible ? 'alert-dismissible' : ''];
},
alertState() {
return !this.state || this.state === `default` ? `alert-success` : `alert-${this.state}`;
},
localShow() {
return !this.dismissed && (this.countDownTimerId || this.show);
}
},
props: {
state: {
type: String,
default: 'info'
},
dismissible: {
type: Boolean,
default: false
},
show: {
type: [Boolean, Number],
default: false
}
},
watch: {
show() {
this.showChanged();
}
},
mounted() {
this.showChanged();
},
methods: {
dismiss() {
this.dismissed = true;
this.$emit('dismissed');
this.clearCounter();
},
clearCounter() {
if (this.countDownTimerId) {
clearInterval(this.countDownTimerId);
}
},
showChanged() {
// Reset dismiss status
this.dismissed = false;
// No timer for boolean values
if (this.show === true || this.show === false || this.show === null || this.show === 0) {
return;
}
let dismissCountDown = this.show;
this.$emit('dismiss-count-down', dismissCountDown);
// Start counter
this.clearCounter();
this.countDownTimerId = setInterval(() => {
if (dismissCountDown < 2) {
return this.dismiss();
}
dismissCountDown--;
this.$emit('dismiss-count-down', dismissCountDown);
}, 1000);
}
}
};
</script>