forked from pterodactyl/panel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFlash.vue
More file actions
103 lines (93 loc) · 3.03 KB
/
Flash.vue
File metadata and controls
103 lines (93 loc) · 3.03 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
93
94
95
96
97
98
99
100
101
102
103
<template>
<div v-if="notifications.length > 0" :class="this.container">
<transition-group tag="div" name="fade">
<div v-for="(item, index) in notifications" :key="item.title">
<MessageBox
:class="[item.class, {'mb-2': index < notifications.length - 1}]"
:title="item.title"
:message="item.message"
/>
</div>
</transition-group>
</div>
</template>
<script lang="ts">
import Vue from 'vue';
import MessageBox from './MessageBox.vue';
type DataStructure = {
notifications: Array<{
message: string,
severity: string,
title: string,
class: string,
}>,
}
export default Vue.extend({
name: 'Flash',
components: {
MessageBox
},
props: {
container: {type: String, default: ''},
timeout: {type: Number, default: 0},
types: {
type: Object,
default: function () {
return {
base: 'alert',
success: 'alert success',
info: 'alert info',
warning: 'alert warning',
error: 'alert error',
}
}
}
},
data: function (): DataStructure {
return {
notifications: [],
};
},
/**
* Listen for flash events.
*/
created: function () {
const self = this;
window.events.$on('flash', function (data: any) {
self.flash(data.message, data.title, data.severity);
});
window.events.$on('clear-flashes', function () {
self.clear();
});
},
methods: {
/**
* Flash a message to the screen when a flash event is emitted over
* the global event stream.
*/
flash: function (message: string, title: string, severity: string) {
this.notifications.push({
message, severity, title, class: this.$props.types[severity] || this.$props.types.base,
});
if (this.$props.timeout > 0) {
setTimeout(this.hide, this.$props.timeout);
}
},
/**
* Clear all of the flash messages from the screen.
*/
clear: function () {
this.notifications = [];
window.events.$emit('flashes-cleared');
},
/**
* Hide a notification after a given amount of time.
*/
hide: function (item?: number) {
// @ts-ignore
let key = this.notifications.indexOf(item || this.notifications[0]);
this.notifications.splice(key, 1);
},
},
});
</script>