2020-06-14 21:13:35 +00:00
|
|
|
import Vue from 'vue'
|
2020-06-15 19:08:42 +00:00
|
|
|
import FtToastEvents from './ft-toast-events.js'
|
2020-06-14 21:13:35 +00:00
|
|
|
|
|
|
|
export default Vue.extend({
|
|
|
|
name: 'FtToast',
|
|
|
|
data: function () {
|
|
|
|
return {
|
2020-06-27 10:41:34 +00:00
|
|
|
toasts: [
|
|
|
|
],
|
2020-06-14 21:13:35 +00:00
|
|
|
}
|
|
|
|
},
|
2020-06-15 19:08:42 +00:00
|
|
|
mounted: function () {
|
|
|
|
FtToastEvents.$on('toast.open', this.open)
|
|
|
|
},
|
2020-06-27 15:27:03 +00:00
|
|
|
beforeDestroy: function () {
|
|
|
|
FtToastEvents.$off('toast.open', this.open)
|
|
|
|
},
|
2020-06-14 21:13:35 +00:00
|
|
|
methods: {
|
2020-06-27 15:06:42 +00:00
|
|
|
performAction: function (toast) {
|
|
|
|
toast.action()
|
|
|
|
this.close(toast)
|
2020-06-14 21:13:35 +00:00
|
|
|
},
|
2020-06-27 15:06:42 +00:00
|
|
|
close: function (toast) {
|
2020-06-27 15:27:03 +00:00
|
|
|
clearTimeout(toast.timeout)
|
2020-07-04 15:13:47 +00:00
|
|
|
// Remove toasts when most recent toast has finished to avoid re-render
|
|
|
|
if (this.toasts.filter(toast => toast.isOpen).length === 1) {
|
|
|
|
// Wait for fade-out to finish
|
|
|
|
setTimeout(this.clear, 300)
|
2020-06-15 19:08:42 +00:00
|
|
|
}
|
2020-07-04 15:13:47 +00:00
|
|
|
toast.isOpen = false
|
|
|
|
|
2020-06-14 21:13:35 +00:00
|
|
|
},
|
2020-07-04 15:44:35 +00:00
|
|
|
open: function (message, action, time) {
|
2020-07-04 15:13:47 +00:00
|
|
|
const toast = { message: message, action: action || (() => { }), isOpen: false, timeout: null }
|
2020-07-04 15:44:35 +00:00
|
|
|
toast.timeout = setTimeout(this.close, time || 3000, toast)
|
2020-07-04 15:13:47 +00:00
|
|
|
setImmediate(() => toast.isOpen = true)
|
|
|
|
if (this.toasts.length > 4) {
|
|
|
|
for (let i = this.toasts.length - 1; i >= 0; i--) {
|
|
|
|
if (!this.toasts[i].isOpen) {
|
|
|
|
// Replace the first hidden toast starting from the bottom
|
|
|
|
return this.toasts.splice(i, 1, toast)
|
|
|
|
}
|
2020-06-27 10:41:34 +00:00
|
|
|
}
|
2020-07-04 15:13:47 +00:00
|
|
|
// Else replace the most recent
|
|
|
|
return this.toasts.splice(4, 1, toast)
|
2020-06-15 19:08:42 +00:00
|
|
|
}
|
2020-07-04 15:13:47 +00:00
|
|
|
this.toasts.push(toast)
|
2020-06-14 21:13:35 +00:00
|
|
|
},
|
2020-07-04 15:13:47 +00:00
|
|
|
clear: function () {
|
|
|
|
if (this.toasts.every(toast => !toast.isOpen)) {
|
|
|
|
this.toasts = []
|
|
|
|
}
|
2020-06-27 10:41:34 +00:00
|
|
|
}
|
2020-06-14 21:13:35 +00:00
|
|
|
},
|
|
|
|
})
|