2020-07-29 01:19:18 +08:00
|
|
|
const listeners = {};
|
|
|
|
const stack = [];
|
|
|
|
|
2018-04-01 20:21:11 +08:00
|
|
|
/**
|
2020-07-29 01:19:18 +08:00
|
|
|
* Emit a custom event for any handlers to pick-up.
|
|
|
|
* @param {String} eventName
|
|
|
|
* @param {*} eventData
|
2018-04-01 20:21:11 +08:00
|
|
|
*/
|
2020-07-29 01:19:18 +08:00
|
|
|
function emit(eventName, eventData) {
|
|
|
|
stack.push({name: eventName, data: eventData});
|
|
|
|
if (typeof listeners[eventName] === 'undefined') return this;
|
|
|
|
let eventsToStart = listeners[eventName];
|
|
|
|
for (let i = 0; i < eventsToStart.length; i++) {
|
|
|
|
let event = eventsToStart[i];
|
|
|
|
event(eventData);
|
2018-04-01 20:21:11 +08:00
|
|
|
}
|
2020-07-29 01:19:18 +08:00
|
|
|
}
|
2018-04-01 20:21:11 +08:00
|
|
|
|
2020-07-29 01:19:18 +08:00
|
|
|
/**
|
|
|
|
* Listen to a custom event and run the given callback when that event occurs.
|
|
|
|
* @param {String} eventName
|
|
|
|
* @param {Function} callback
|
|
|
|
* @returns {Events}
|
|
|
|
*/
|
|
|
|
function listen(eventName, callback) {
|
|
|
|
if (typeof listeners[eventName] === 'undefined') listeners[eventName] = [];
|
|
|
|
listeners[eventName].push(callback);
|
|
|
|
}
|
2018-04-01 20:21:11 +08:00
|
|
|
|
2020-07-29 01:19:18 +08: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
|
|
|
|
*/
|
|
|
|
function emitPublic(targetElement, eventName, eventData) {
|
|
|
|
const event = new CustomEvent(eventName, {
|
|
|
|
detail: eventData,
|
|
|
|
bubbles: true
|
|
|
|
});
|
|
|
|
targetElement.dispatchEvent(event);
|
|
|
|
}
|
2019-10-17 01:01:35 +08:00
|
|
|
|
2020-07-29 01:19:18 +08:00
|
|
|
/**
|
|
|
|
* Notify of a http error.
|
|
|
|
* Check for standard scenarios such as validation errors and
|
|
|
|
* formats an error notification accordingly.
|
|
|
|
* @param {Error} error
|
|
|
|
*/
|
|
|
|
function showValidationErrors(error) {
|
|
|
|
if (!error.status) return;
|
|
|
|
if (error.status === 422 && error.data) {
|
|
|
|
const message = Object.values(error.data).flat().join('\n');
|
|
|
|
emit('error', message);
|
2019-10-17 01:01:35 +08:00
|
|
|
}
|
2018-04-01 20:21:11 +08:00
|
|
|
}
|
|
|
|
|
2020-07-29 01:19:18 +08:00
|
|
|
export default {
|
|
|
|
emit,
|
|
|
|
emitPublic,
|
|
|
|
listen,
|
|
|
|
success: (msg) => emit('success', msg),
|
|
|
|
error: (msg) => emit('error', msg),
|
|
|
|
showValidationErrors,
|
|
|
|
}
|