-
Notifications
You must be signed in to change notification settings - Fork 37
/
Toast.vue
154 lines (153 loc) · 3.7 KB
/
Toast.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<template>
<div
v-bind:style="data.style"
v-bind:class="classNames"
style="display: block;"
@click="clicked()"
v-on:mouseover="onMouseOver"
v-on:mouseout="onMouseOut"
>
<toast-progress
v-if="progressbar"
:percent="progressBarPercent"
ref="progressBar"
></toast-progress>
<div v-if="data.title" class="toast-title" v-html="data.title"></div>
<div
v-if="data.msg && !$slots.default"
class="toast-message"
v-html="data.msg"
></div>
<div v-if="$slots.default" class="toast-message">
<slot></slot>
</div>
</div>
</template>
<script>
import ToastProgress from "./ToastProgress.vue"
import IntervalTimeManager from "./IntervalTimeManager.js"
export default {
components: {
ToastProgress
},
props: ["data"],
data() {
return {
progressbar: false,
progressBarTimer: null,
timeoutTimer: null
}
},
mounted() {
// console.log("ready", this.data);
if (this.progressBarTimer != null) {
this.progressBarTimer.start()
}
if (this.timeoutTimer != null) {
this.timeoutTimer.start()
}
},
created() {
if (typeof this.data.timeout !== "undefined" && this.data.timeout !== 0) {
// SetUP timeout Manager
this.timeoutTimer = IntervalTimeManager({
totalTime: this.data.timeout,
callbackFunctions: {
"after:finish": () => {
this.close()
// console.log("Timeout Fired");
}
}
})
// SetUP progressbar Time Manager
if (this.data.progressbar !== false) {
this.progressbar = true
this.progressBarTimer = IntervalTimeManager({
totalTime: this.data.timeout
})
}
} else if (
this.data.progressBarValue !== null &&
this.data.progressbar !== false
) {
this.progressbar = true
}
},
computed: {
classNames() {
return ["toast", "toast-" + this.data.type].concat(this.data.classNames)
},
progressBarPercent() {
if (this.data.progressBarValue != null) {
return this.data.progressBarValue
}
return this.progressBarTimer.getPercent()
}
},
beforeDestroy() {
if (this.progressBarTimer != null) {
this.progressBarTimer.stop()
}
if (this.timeoutTimer != null) {
this.timeoutTimer.stop()
}
},
methods: {
// Enter Hover
onMouseOver() {
// console.log("onMouseOver")
if (typeof this.data.onMouseOver !== "undefined") {
this.data.onMouseOver()
}
if (this.data.closeOnHover) {
if (this.progressBarTimer != null) {
this.progressBarTimer.pause()
}
if (this.timeoutTimer != null) {
this.timeoutTimer.pause()
}
}
},
// Leave Hover
onMouseOut() {
// console.log("onMouseOut")
if (typeof this.data.onMouseOut !== "undefined") {
this.data.onMouseOut()
}
if (this.data.closeOnHover) {
if (this.progressBarTimer != null) {
this.progressBarTimer.resume()
}
if (this.timeoutTimer != null) {
this.timeoutTimer.resume()
}
}
},
// Clicked Toast
clicked() {
if (typeof this.data.onClicked !== "undefined") {
this.data.onClicked()
}
this.clickClose()
},
// Click Close?
clickClose() {
if (
typeof this.data.clickClose !== "undefined" &&
this.data.clickClose === false
) {
return
}
this.close()
},
// Close Toast
close() {
// console.log(typeof this.$parent, this);
// if toast not manuel closed.
if (this.$parent != null) {
this.$parent.Close(this.data)
}
}
}
}
</script>