2020-05-07 20:10:13 +00:00
|
|
|
import Toastify from 'toastify-js'
|
2020-10-17 15:52:31 +00:00
|
|
|
import 'toastify-js/src/toastify.css'
|
|
|
|
import '@/styles/css/toasts.css'
|
2020-05-07 20:10:13 +00:00
|
|
|
|
2020-07-16 22:11:28 +00:00
|
|
|
import { socket } from '@/utils/socket'
|
2020-04-22 20:06:59 +00:00
|
|
|
|
2020-10-17 15:52:31 +00:00
|
|
|
const sharedOptions = {
|
|
|
|
gravity: 'bottom',
|
2020-10-30 09:54:46 +00:00
|
|
|
position: 'left'
|
2020-10-17 15:52:31 +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]
|
2020-09-22 19:59:18 +00:00
|
|
|
|
|
|
|
let toastElement = document.querySelectorAll(`div.toastify[toast_id=${id}]`)
|
|
|
|
|
2020-04-21 20:20:19 +00:00
|
|
|
if (msg) {
|
2020-09-22 19:59:18 +00:00
|
|
|
toastElement.forEach(toast => {
|
|
|
|
const messages = toast.querySelectorAll('.toast-message')
|
|
|
|
|
|
|
|
messages.forEach(message => {
|
|
|
|
message.innerHTML = msg
|
|
|
|
})
|
|
|
|
})
|
2020-04-21 20:20:19 +00:00
|
|
|
}
|
2020-09-22 19:59:18 +00:00
|
|
|
|
2020-04-21 20:20:19 +00:00
|
|
|
if (icon) {
|
2020-09-22 19:59:18 +00:00
|
|
|
if (icon == 'loading') {
|
|
|
|
icon = `<div class="circle-loader"></div>`
|
|
|
|
} else {
|
|
|
|
icon = `<i class="material-icons">${icon}</i>`
|
|
|
|
}
|
|
|
|
|
|
|
|
toastElement.forEach(toast => {
|
|
|
|
const icons = toast.querySelectorAll('.toast-icon')
|
|
|
|
|
|
|
|
icons.forEach(toastIcon => {
|
|
|
|
toastIcon.innerHTML = icon
|
|
|
|
})
|
|
|
|
})
|
2020-04-21 20:20:19 +00:00
|
|
|
}
|
|
|
|
if (dismiss !== null && dismiss) {
|
2020-09-22 19:59:18 +00:00
|
|
|
toastElement.forEach(toast => {
|
|
|
|
toast.classList.add('dismissable')
|
|
|
|
})
|
|
|
|
|
|
|
|
setTimeout(() => {
|
2020-04-21 20:20:19 +00:00
|
|
|
toastObj.hideToast()
|
2020-09-22 19:59:18 +00:00
|
|
|
|
2020-04-21 20:20:19 +00:00
|
|
|
delete toastsWithId[id]
|
|
|
|
}, 3000)
|
|
|
|
}
|
|
|
|
} else {
|
2020-09-22 19:59:18 +00:00
|
|
|
if (icon == null) {
|
|
|
|
icon = ''
|
|
|
|
} else if (icon == 'loading') {
|
|
|
|
icon = `<div class="circle-loader"></div>`
|
|
|
|
} else {
|
|
|
|
icon = `<i class="material-icons">${icon}</i>`
|
|
|
|
}
|
|
|
|
|
2020-04-21 20:20:19 +00:00
|
|
|
let toastObj = Toastify({
|
2020-10-17 15:52:31 +00:00
|
|
|
...sharedOptions,
|
2020-04-21 20:20:19 +00:00
|
|
|
text: `<span class="toast-icon">${icon}</span><span class="toast-message">${msg}</toast>`,
|
|
|
|
duration: dismiss ? 3000 : 0,
|
2020-07-25 15:22:31 +00:00
|
|
|
className: dismiss ? 'dismissable' : '',
|
2020-09-22 19:59:18 +00:00
|
|
|
onClick: function() {
|
2020-07-25 15:22:31 +00:00
|
|
|
let dismissable = true
|
2020-09-22 19:59:18 +00:00
|
|
|
|
|
|
|
if (id) {
|
2020-07-25 15:22:31 +00:00
|
|
|
let toastClasses = document.querySelector(`div.toastify[toast_id=${id}]`).classList
|
2020-09-22 19:59:18 +00:00
|
|
|
|
|
|
|
if (toastClasses) {
|
|
|
|
dismissable = Array.prototype.slice.call(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-09-22 19:59:18 +00:00
|
|
|
|
|
|
|
if (id) {
|
|
|
|
delete toastsWithId[id]
|
|
|
|
}
|
2020-07-25 13:54:13 +00:00
|
|
|
}
|
2020-10-30 09:54:46 +00:00
|
|
|
},
|
|
|
|
offset: {
|
|
|
|
x: 'true' === localStorage.getItem('slimSidebar') ? '3rem': '14rem'
|
2020-07-25 13:54:13 +00:00
|
|
|
}
|
2020-04-21 20:20:19 +00:00
|
|
|
}).showToast()
|
|
|
|
if (id) {
|
|
|
|
toastsWithId[id] = toastObj
|
2020-09-22 19:59:18 +00:00
|
|
|
|
|
|
|
toastObj.toastElement.setAttribute('toast_id', id)
|
2020-04-21 20:20:19 +00:00
|
|
|
}
|
|
|
|
}
|
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
|
|
|
})
|