2020-06-14 23:13:35 +02:00
|
|
|
import Vue from 'vue'
|
2020-06-15 21:08:42 +02:00
|
|
|
import FtToastEvents from './ft-toast-events.js'
|
2020-06-14 23:13:35 +02:00
|
|
|
|
|
|
|
export default Vue.extend({
|
|
|
|
name: 'FtToast',
|
|
|
|
data: function () {
|
|
|
|
return {
|
2020-08-05 04:18:39 +02:00
|
|
|
toasts: []
|
2020-06-14 23:13:35 +02:00
|
|
|
}
|
|
|
|
},
|
2020-06-15 21:08:42 +02:00
|
|
|
mounted: function () {
|
|
|
|
FtToastEvents.$on('toast.open', this.open)
|
|
|
|
},
|
2020-06-27 17:27:03 +02:00
|
|
|
beforeDestroy: function () {
|
|
|
|
FtToastEvents.$off('toast.open', this.open)
|
|
|
|
},
|
2020-06-14 23:13:35 +02:00
|
|
|
methods: {
|
2020-08-02 13:28:10 +02:00
|
|
|
performAction: function (index) {
|
|
|
|
this.toasts[index].action()
|
|
|
|
this.remove(index)
|
2020-06-14 23:13:35 +02:00
|
|
|
},
|
2020-06-27 17:06:42 +02:00
|
|
|
close: function (toast) {
|
2020-08-02 13:28:10 +02:00
|
|
|
// Wait for fade-out to finish
|
2020-08-02 16:52:48 +02:00
|
|
|
setTimeout(this.remove, 300, 0)
|
2020-08-02 13:28:10 +02:00
|
|
|
|
2020-07-04 17:13:47 +02:00
|
|
|
toast.isOpen = false
|
2020-06-14 23:13:35 +02:00
|
|
|
},
|
2020-07-04 17:44:35 +02:00
|
|
|
open: function (message, action, time) {
|
2020-07-04 17:13:47 +02:00
|
|
|
const toast = { message: message, action: action || (() => { }), isOpen: false, timeout: null }
|
2020-08-02 13:34:51 +02:00
|
|
|
toast.timeout = setTimeout(this.close, time || 3000, toast)
|
2020-07-04 17:54:49 +02:00
|
|
|
setImmediate(() => { toast.isOpen = true })
|
2020-07-04 17:13:47 +02:00
|
|
|
if (this.toasts.length > 4) {
|
2020-08-02 13:28:10 +02:00
|
|
|
this.remove(0)
|
2020-06-15 21:08:42 +02:00
|
|
|
}
|
2020-07-04 17:13:47 +02:00
|
|
|
this.toasts.push(toast)
|
2020-06-14 23:13:35 +02:00
|
|
|
},
|
2020-08-02 13:28:10 +02:00
|
|
|
remove: function(index) {
|
|
|
|
const removed = this.toasts.splice(index, 1)
|
2020-08-02 16:52:48 +02:00
|
|
|
clearTimeout(removed[0].timeout)
|
2020-06-27 12:41:34 +02:00
|
|
|
}
|
2020-08-05 04:18:39 +02:00
|
|
|
}
|
2020-06-14 23:13:35 +02:00
|
|
|
})
|