FreeTube/src/renderer/components/ft-toast/ft-toast.js

43 lines
1.1 KiB
JavaScript
Raw Normal View History

2020-06-14 23:13:35 +02:00
import Vue from 'vue'
import FtToastEvents from './ft-toast-events.js'
2020-06-14 23:13:35 +02:00
export default Vue.extend({
name: 'FtToast',
data: function () {
return {
toasts: []
2020-06-14 23:13:35 +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: {
performAction: function (index) {
this.toasts[index].action()
this.remove(index)
2020-06-14 23:13:35 +02:00
},
close: function (toast) {
// Wait for fade-out to finish
2020-08-02 16:52:48 +02:00
setTimeout(this.remove, 300, 0)
toast.isOpen = false
2020-06-14 23:13:35 +02:00
},
2020-07-04 17:44:35 +02:00
open: function (message, action, time) {
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)
setImmediate(() => { toast.isOpen = true })
if (this.toasts.length > 4) {
this.remove(0)
}
this.toasts.push(toast)
2020-06-14 23:13:35 +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-06-14 23:13:35 +02:00
})