class PageNotifier {
static swalConfirm;
static swalConfirmInfo;
static swalConfirmWarning;
static swalConfirmDanger;
static sessionNotify(message, type) {
PageNotifier.notify({
type: type,
message: message,
htmlMode: true
})
}
static sessionNotifyWithTitle(message, title, type) {
PageNotifier.notify({
type: type,
title: title,
message: message,
})
}
static notify(data) {
if (data.errors ?? false) {
for (const [key, value] of Object.entries(data.errors)) {
document.querySelector(`input[name=${key}]`)?.classList?.add('is-invalid');
}
data.type = 'danger';
}
if (data.title ?? false) {
data.htmlMode = true;
data.message = `${data.title}
${data.message}`;
}
if (data.message) {
// https://github.com/dominique-mueller/notification-js
notification.notify(data.type, data.message, {
notification: {
position: data.position ?? ['right', 'top'],
},
behaviour: {
htmlMode: data.htmlMode ?? false,
autoHide: 3,
visible: false,
onMouseover: false
},
callbacks: {
onClosed: () => {
if (data.reloadAfter ?? false) {
document.location.reload()
}
}
}
})
}
}
/**
* @param {string} message
* @param {string} title
* @param {string|null} confirmHTML
* @param {string|null} cancelHTML
* @return {Promise}
*/
static confirmDangerousAction(title, message, confirmHTML = null, cancelHTML = null) {
if (confirmHTML === null) {
confirmHTML = ' ' + __('buttons.action.confirm');
}
if (cancelHTML === null) {
cancelHTML = ' ' + __('buttons.action.cancel')
}
return PageNotifier.swalConfirmDanger.fire({
title: `${title}`,
html: message,
showCancelButton: true,
confirmButtonText: confirmHTML,
cancelButtonText: cancelHTML,
}).then(result => result.isConfirmed);
}
/**
* @param {string} message
* @param {string} title
* @param {string|null} confirmHTML
* @param {string|null} cancelHTML
* @return {Promise}
*/
static confirm(title, message, confirmHTML = null, cancelHTML = null) {
if (confirmHTML === null) {
confirmHTML = ' ' + __('buttons.action.confirm');
}
if (cancelHTML === null) {
cancelHTML = ' ' + __('buttons.action.cancel')
}
return PageNotifier.swalConfirm.fire({
title: `${title}`,
html: message,
showCancelButton: true,
confirmButtonText: confirmHTML,
cancelButtonText: cancelHTML,
}).then(result => result.isConfirmed);
}
/**
* @param {string} message
* @param {string} title
* @return {Promise}
*/
static throwInfo(title, message) {
return PageNotifier.swalConfirmInfo.fire({
title: title,
html: message,
width: 600,
})
}
/**
* @param {string} title
* @param {string} message
* @return {Promise}
*/
static throwWarning(title, message) {
return PageNotifier.swalConfirmWarning.fire({
title: `${title}`,
html: message,
})
}
/**
* @param {string} title
* @param {string} message
* @return {Promise}
*/
static throwError(title, message) {
return PageNotifier.swalConfirmDanger.fire({
title: `${title}`,
html: message,
})
}
}
PageNotifier.swalConfirm = Swal.mixin({
customClass: {
confirmButton: 'btn btn-sm btn-success',
cancelButton: 'btn btn-sm btn-secondary'
},
buttonsStyling: false,
reverseButtons: true,
});
PageNotifier.swalConfirmInfo = Swal.mixin({
customClass: {
confirmButton: 'btn btn-sm btn-warning',
cancelButton: 'btn btn-sm btn-primary'
},
buttonsStyling: false,
reverseButtons: true,
});
PageNotifier.swalConfirmWarning = Swal.mixin({
customClass: {
confirmButton: 'btn btn-sm btn-warning',
cancelButton: 'btn btn-sm btn-primary'
},
buttonsStyling: false,
reverseButtons: true,
});
PageNotifier.swalConfirmDanger = Swal.mixin({
customClass: {
confirmButton: 'btn btn-sm btn-danger',
cancelButton: 'btn btn-sm btn-secondary'
},
buttonsStyling: false,
reverseButtons: true,
});
['success', 'warning', 'error', 'default', 'info'].forEach(
profile => {
notification.configProfile(profile, {
notification: {
position: ['right', 'top'],
roundCorners: [5, 5, 5, 5],
},
behaviour: {
autoHide: 3
},
dismiss: {
visible: false
},
})
}
)
notification.configProfile('info', {
symbol: {
visible: true,
resource: ''
}
})
notification.configProfile('warning', {
symbol: {
visible: true,
resource: ''
}
})
notification.addProfile('danger', {
notification: {
position: ['right', 'top'],
color: '#B9493E'
},
dismiss: {
visible: false
},
symbol: {
visible: true,
resource: ''
}
})