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: [
|
|
|
|
{isOpen: false, message: '', action: null, timeout: null},
|
|
|
|
{isOpen: false, message: '', action: null, timeout: null},
|
|
|
|
{isOpen: false, message: '', action: null, timeout: null},
|
|
|
|
{isOpen: false, message: '', action: null, timeout: null},
|
|
|
|
{isOpen: false, message: '', action: null, timeout: null}
|
|
|
|
],
|
2020-06-15 19:08:42 +00:00
|
|
|
queue: [],
|
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-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) {
|
|
|
|
clearTimeout(toast.timeout);
|
|
|
|
toast.isOpen = false
|
2020-06-15 19:08:42 +00:00
|
|
|
if(this.queue.length !== 0) {
|
2020-06-27 15:06:42 +00:00
|
|
|
const nexToast = this.queue.shift()
|
|
|
|
this.open(nexToast.message, nexToast.action)
|
2020-06-15 19:08:42 +00:00
|
|
|
}
|
2020-06-14 21:13:35 +00:00
|
|
|
},
|
2020-06-15 19:08:42 +00:00
|
|
|
open: function (message, action) {
|
2020-06-27 10:41:34 +00:00
|
|
|
for(let i = this.toasts.length - 1; i >= 0 ; i--){
|
2020-06-27 15:06:42 +00:00
|
|
|
const toast = this.toasts[i]
|
|
|
|
if (!toast.isOpen) {
|
|
|
|
return this.showToast(message, action, toast)
|
2020-06-27 10:41:34 +00:00
|
|
|
}
|
2020-06-15 19:08:42 +00:00
|
|
|
}
|
2020-06-27 10:41:34 +00:00
|
|
|
this.queue.push({ message: message, action: action })
|
2020-06-14 21:13:35 +00:00
|
|
|
},
|
2020-06-27 15:06:42 +00:00
|
|
|
showToast: function(message, action, toast) {
|
|
|
|
toast.message = message
|
|
|
|
toast.action = action || (() => {})
|
|
|
|
toast.isOpen = true
|
|
|
|
toast.timeout = setTimeout(this.close, 2000, toast)
|
2020-06-27 10:41:34 +00:00
|
|
|
}
|
2020-06-14 21:13:35 +00:00
|
|
|
},
|
2020-06-15 19:08:42 +00:00
|
|
|
beforeDestroy: function () {
|
|
|
|
FtToastEvents.$off('toast.open', this.open)
|
|
|
|
},
|
2020-06-14 21:13:35 +00:00
|
|
|
})
|