2020-07-28 19:19:18 +02:00
|
|
|
const listeners = {};
|
|
|
|
const stack = [];
|
|
|
|
|
2018-04-01 14:21:11 +02:00
|
|
|
/**
|
2020-07-28 19:19:18 +02:00
|
|
|
* Emit a custom event for any handlers to pick-up.
|
|
|
|
* @param {String} eventName
|
|
|
|
* @param {*} eventData
|
2018-04-01 14:21:11 +02:00
|
|
|
*/
|
2023-04-19 16:20:04 +02:00
|
|
|
export function emit(eventName, eventData) {
|
2020-07-28 19:19:18 +02:00
|
|
|
stack.push({name: eventName, data: eventData});
|
2023-04-19 16:20:04 +02:00
|
|
|
|
|
|
|
const listenersToRun = listeners[eventName] || [];
|
|
|
|
for (const listener of listenersToRun) {
|
|
|
|
listener(eventData);
|
2018-04-01 14:21:11 +02:00
|
|
|
}
|
2020-07-28 19:19:18 +02:00
|
|
|
}
|
2018-04-01 14:21:11 +02:00
|
|
|
|
2020-07-28 19:19:18 +02:00
|
|
|
/**
|
|
|
|
* Listen to a custom event and run the given callback when that event occurs.
|
|
|
|
* @param {String} eventName
|
|
|
|
* @param {Function} callback
|
|
|
|
* @returns {Events}
|
|
|
|
*/
|
2023-04-19 16:20:04 +02:00
|
|
|
export function listen(eventName, callback) {
|
2020-07-28 19:19:18 +02:00
|
|
|
if (typeof listeners[eventName] === 'undefined') listeners[eventName] = [];
|
|
|
|
listeners[eventName].push(callback);
|
|
|
|
}
|
2018-04-01 14:21:11 +02:00
|
|
|
|
2020-07-28 19:19:18 +02:00
|
|
|
/**
|
|
|
|
* Emit an event for public use.
|
|
|
|
* Sends the event via the native DOM event handling system.
|
|
|
|
* @param {Element} targetElement
|
|
|
|
* @param {String} eventName
|
|
|
|
* @param {Object} eventData
|
|
|
|
*/
|
2023-04-19 16:20:04 +02:00
|
|
|
export function emitPublic(targetElement, eventName, eventData) {
|
2020-07-28 19:19:18 +02:00
|
|
|
const event = new CustomEvent(eventName, {
|
|
|
|
detail: eventData,
|
2023-04-18 23:20:02 +02:00
|
|
|
bubbles: true,
|
2020-07-28 19:19:18 +02:00
|
|
|
});
|
|
|
|
targetElement.dispatchEvent(event);
|
|
|
|
}
|
2019-10-16 19:01:35 +02:00
|
|
|
|
2023-04-19 16:20:04 +02:00
|
|
|
/**
|
|
|
|
* Emit a success event with the provided message.
|
|
|
|
* @param {String} message
|
|
|
|
*/
|
|
|
|
export function success(message) {
|
|
|
|
emit('success', message);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Emit an error event with the provided message.
|
|
|
|
* @param {String} message
|
|
|
|
*/
|
|
|
|
export function error(message) {
|
|
|
|
emit('error', message);
|
|
|
|
}
|
|
|
|
|
2020-07-28 19:19:18 +02:00
|
|
|
/**
|
2023-01-26 13:16:23 +01:00
|
|
|
* Notify of standard server-provided validation errors.
|
2023-04-19 16:20:04 +02:00
|
|
|
* @param {Object} responseErr
|
2020-07-28 19:19:18 +02:00
|
|
|
*/
|
2023-04-19 16:20:04 +02:00
|
|
|
export function showValidationErrors(responseErr) {
|
|
|
|
if (!responseErr.status) return;
|
|
|
|
if (responseErr.status === 422 && responseErr.data) {
|
|
|
|
const message = Object.values(responseErr.data).flat().join('\n');
|
|
|
|
error(message);
|
2019-10-16 19:01:35 +02:00
|
|
|
}
|
2018-04-01 14:21:11 +02:00
|
|
|
}
|
|
|
|
|
2023-01-26 13:16:23 +01:00
|
|
|
/**
|
|
|
|
* Notify standard server-provided error messages.
|
2023-04-19 16:20:04 +02:00
|
|
|
* @param {Object} responseErr
|
2023-01-26 13:16:23 +01:00
|
|
|
*/
|
2023-04-19 16:20:04 +02:00
|
|
|
export function showResponseError(responseErr) {
|
|
|
|
if (!responseErr.status) return;
|
|
|
|
if (responseErr.status >= 400 && responseErr.data && responseErr.data.message) {
|
|
|
|
error(responseErr.data.message);
|
2023-01-26 13:16:23 +01:00
|
|
|
}
|
|
|
|
}
|