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