Simplified closing and replacement functionality
This commit is contained in:
parent
a26690b5be
commit
8c510b7c3c
|
@ -5,8 +5,7 @@ export default Vue.extend({
|
||||||
name: 'FtToast',
|
name: 'FtToast',
|
||||||
data: function () {
|
data: function () {
|
||||||
return {
|
return {
|
||||||
toasts: [
|
toasts: [],
|
||||||
],
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
mounted: function () {
|
mounted: function () {
|
||||||
|
@ -16,39 +15,28 @@ export default Vue.extend({
|
||||||
FtToastEvents.$off('toast.open', this.open)
|
FtToastEvents.$off('toast.open', this.open)
|
||||||
},
|
},
|
||||||
methods: {
|
methods: {
|
||||||
performAction: function (toast) {
|
performAction: function (index) {
|
||||||
toast.action()
|
this.toasts[index].action()
|
||||||
this.close(toast)
|
this.remove(index)
|
||||||
},
|
},
|
||||||
close: function (toast) {
|
close: function (toast) {
|
||||||
clearTimeout(toast.timeout)
|
// Wait for fade-out to finish
|
||||||
// Remove toasts when most recent toast has finished to avoid re-render
|
setTimeout(this.remove, 300, this.toasts.length)
|
||||||
if (this.toasts.filter(toast => toast.isOpen).length === 1) {
|
|
||||||
// Wait for fade-out to finish
|
|
||||||
setTimeout(this.clear, 300)
|
|
||||||
}
|
|
||||||
toast.isOpen = false
|
toast.isOpen = false
|
||||||
},
|
},
|
||||||
open: function (message, action, time) {
|
open: function (message, action, time) {
|
||||||
const toast = { message: message, action: action || (() => { }), isOpen: false, timeout: null }
|
const toast = { message: message, action: action || (() => { }), isOpen: false, timeout: null }
|
||||||
toast.timeout = setTimeout(this.close, time || 3000, toast)
|
toast.timeout = setTimeout(this.close, time || 6000, toast)
|
||||||
setImmediate(() => { toast.isOpen = true })
|
setImmediate(() => { toast.isOpen = true })
|
||||||
if (this.toasts.length > 4) {
|
if (this.toasts.length > 4) {
|
||||||
for (let i = this.toasts.length - 1; i >= 0; i--) {
|
this.remove(0)
|
||||||
if (!this.toasts[i].isOpen) {
|
|
||||||
// Replace the first hidden toast starting from the bottom
|
|
||||||
return this.toasts.splice(i, 1, toast)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// Else replace the most recent
|
|
||||||
return this.toasts.splice(4, 1, toast)
|
|
||||||
}
|
}
|
||||||
this.toasts.push(toast)
|
this.toasts.push(toast)
|
||||||
},
|
},
|
||||||
clear: function () {
|
remove: function(index) {
|
||||||
if (this.toasts.every(toast => !toast.isOpen)) {
|
const removed = this.toasts.splice(index, 1)
|
||||||
this.toasts = []
|
clearTimeout(removed.timeout)
|
||||||
}
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
|
@ -5,7 +5,7 @@
|
||||||
:key="'toast-' + index"
|
:key="'toast-' + index"
|
||||||
class="toast"
|
class="toast"
|
||||||
:class="{ closed: !toast.isOpen, open: toast.isOpen }"
|
:class="{ closed: !toast.isOpen, open: toast.isOpen }"
|
||||||
@click="performAction(toast)"
|
@click="performAction(index)"
|
||||||
>
|
>
|
||||||
<p class="message">
|
<p class="message">
|
||||||
{{ toast.message }}
|
{{ toast.message }}
|
||||||
|
|
Loading…
Reference in New Issue