2020-05-07 20:10:13 +00:00
|
|
|
import Toastify from 'toastify-js'
|
|
|
|
import $ from 'jquery'
|
|
|
|
|
2020-07-16 22:11:28 +00:00
|
|
|
import { socket } from '@/utils/socket'
|
2020-04-22 20:06:59 +00:00
|
|
|
|
2020-04-21 20:20:19 +00:00
|
|
|
let toastsWithId = {}
|
|
|
|
|
2020-07-16 22:11:28 +00:00
|
|
|
export const toast = function(msg, icon = null, dismiss = true, id = null) {
|
2020-04-21 20:20:19 +00:00
|
|
|
if (toastsWithId[id]) {
|
|
|
|
let toastObj = toastsWithId[id]
|
|
|
|
let toastDOM = $(`div.toastify[toast_id=${id}]`)
|
|
|
|
if (msg) {
|
|
|
|
toastDOM.find('.toast-message').html(msg)
|
|
|
|
}
|
|
|
|
if (icon) {
|
|
|
|
if (icon == 'loading') icon = `<div class="circle-loader"></div>`
|
|
|
|
else icon = `<i class="material-icons">${icon}</i>`
|
|
|
|
toastDOM.find('.toast-icon').html(icon)
|
|
|
|
}
|
|
|
|
if (dismiss !== null && dismiss) {
|
2020-07-25 15:22:31 +00:00
|
|
|
toastDOM.addClass('dismissable')
|
2020-07-16 22:11:28 +00:00
|
|
|
setTimeout(function() {
|
2020-04-21 20:20:19 +00:00
|
|
|
toastObj.hideToast()
|
|
|
|
delete toastsWithId[id]
|
|
|
|
}, 3000)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (icon == null) icon = ''
|
|
|
|
else if (icon == 'loading') icon = `<div class="circle-loader"></div>`
|
|
|
|
else icon = `<i class="material-icons">${icon}</i>`
|
|
|
|
let toastObj = Toastify({
|
|
|
|
text: `<span class="toast-icon">${icon}</span><span class="toast-message">${msg}</toast>`,
|
|
|
|
duration: dismiss ? 3000 : 0,
|
|
|
|
gravity: 'bottom',
|
2020-07-25 13:54:13 +00:00
|
|
|
position: 'left',
|
2020-07-25 15:22:31 +00:00
|
|
|
className: dismiss ? 'dismissable' : '',
|
2020-07-25 13:54:13 +00:00
|
|
|
onClick: function(){
|
2020-07-25 15:22:31 +00:00
|
|
|
let dismissable = true
|
|
|
|
if (id){
|
|
|
|
let toastClasses = document.querySelector(`div.toastify[toast_id=${id}]`).classList
|
|
|
|
if (toastClasses){
|
2020-07-27 21:37:50 +00:00
|
|
|
dismissable = Array.from(toastClasses).indexOf('dismissable') != -1
|
2020-07-25 15:22:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (toastObj && dismissable) {
|
2020-07-25 13:54:13 +00:00
|
|
|
toastObj.hideToast()
|
2020-07-31 12:10:36 +00:00
|
|
|
if (id) delete toastsWithId[id]
|
2020-07-25 13:54:13 +00:00
|
|
|
}
|
|
|
|
}
|
2020-04-21 20:20:19 +00:00
|
|
|
}).showToast()
|
|
|
|
if (id) {
|
|
|
|
toastsWithId[id] = toastObj
|
|
|
|
$(toastObj.toastElement).attr('toast_id', id)
|
|
|
|
}
|
|
|
|
}
|
2020-04-22 20:06:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
socket.on('toast', data => {
|
2020-05-22 22:15:29 +00:00
|
|
|
const { msg, icon, dismiss, id } = data
|
|
|
|
|
|
|
|
toast(msg, icon || null, dismiss !== undefined ? dismiss : true, id || null)
|
2020-04-22 20:06:59 +00:00
|
|
|
})
|