2020-07-28 13:19:18 -04:00
|
|
|
const listeners = {};
|
|
|
|
const stack = [];
|
|
|
|
|
2018-04-01 08:21:11 -04:00
|
|
|
/**
|
2020-07-28 13:19:18 -04:00
|
|
|
* Emit a custom event for any handlers to pick-up.
|
|
|
|
* @param {String} eventName
|
|
|
|
* @param {*} eventData
|
2018-04-01 08:21:11 -04:00
|
|
|
*/
|
2023-04-19 10:20:04 -04:00
|
|
|
export function emit(eventName, eventData) {
|
2020-07-28 13:19:18 -04:00
|
|
|
stack.push({name: eventName, data: eventData});
|
2023-04-19 10:20:04 -04:00
|
|
|
|
|
|
|
const listenersToRun = listeners[eventName] || [];
|
|
|
|
for (const listener of listenersToRun) {
|
|
|
|
listener(eventData);
|
2018-04-01 08:21:11 -04:00
|
|
|
}
|
2020-07-28 13:19:18 -04:00
|
|
|
}
|
2018-04-01 08:21:11 -04:00
|
|
|
|
2020-07-28 13:19:18 -04: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 10:20:04 -04:00
|
|
|
export function listen(eventName, callback) {
|
2020-07-28 13:19:18 -04:00
|
|
|
if (typeof listeners[eventName] === 'undefined') listeners[eventName] = [];
|
|
|
|
listeners[eventName].push(callback);
|
|
|
|
}
|
2018-04-01 08:21:11 -04:00
|
|
|
|
2020-07-28 13:19:18 -04: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 10:20:04 -04:00
|
|
|
export function emitPublic(targetElement, eventName, eventData) {
|
2020-07-28 13:19:18 -04:00
|
|
|
const event = new CustomEvent(eventName, {
|
|
|
|
detail: eventData,
|
2023-04-18 17:20:02 -04:00
|
|
|
bubbles: true,
|
2020-07-28 13:19:18 -04:00
|
|
|
});
|
|
|
|
targetElement.dispatchEvent(event);
|
|
|
|
}
|
2019-10-16 13:01:35 -04:00
|
|
|
|
2023-04-19 10:20:04 -04: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 13:19:18 -04:00
|
|
|
/**
|
2023-01-26 07:16:23 -05:00
|
|
|
* Notify of standard server-provided validation errors.
|
2023-04-19 10:20:04 -04:00
|
|
|
* @param {Object} responseErr
|
2020-07-28 13:19:18 -04:00
|
|
|
*/
|
2023-04-19 10:20:04 -04: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 13:01:35 -04:00
|
|
|
}
|
2018-04-01 08:21:11 -04:00
|
|
|
}
|
|
|
|
|
2023-01-26 07:16:23 -05:00
|
|
|
/**
|
|
|
|
* Notify standard server-provided error messages.
|
2023-04-19 10:20:04 -04:00
|
|
|
* @param {Object} responseErr
|
2023-01-26 07:16:23 -05:00
|
|
|
*/
|
2023-04-19 10:20:04 -04: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 07:16:23 -05:00
|
|
|
}
|
|
|
|
}
|