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