mirror of
https://github.com/flarum/framework.git
synced 2025-05-23 23:29:57 +08:00
Massive JavaScript cleanup
- Use JSX for templates - Docblock/comment everything - Mostly passes ESLint (still some work to do) - Lots of renaming, refactoring, etc. CSS hasn't been updated yet.
This commit is contained in:
@ -1,45 +1,79 @@
|
||||
/**
|
||||
* The `evented` mixin provides methods allowing an object to trigger events,
|
||||
* running externally registered event handlers.
|
||||
*/
|
||||
export default {
|
||||
/**
|
||||
* Arrays of registered event handlers, grouped by the event name.
|
||||
*
|
||||
* @type {Object}
|
||||
* @protected
|
||||
*/
|
||||
handlers: null,
|
||||
|
||||
/**
|
||||
|
||||
* Get all of the registered handlers for an event.
|
||||
*
|
||||
* @param {String} event The name of the event.
|
||||
* @return {Array}
|
||||
* @protected
|
||||
*/
|
||||
getHandlers(event) {
|
||||
this.handlers = this.handlers || {};
|
||||
return this.handlers[event] = this.handlers[event] || [];
|
||||
|
||||
this.handlers[event] = this.handlers[event] || [];
|
||||
|
||||
return this.handlers[event];
|
||||
},
|
||||
|
||||
/**
|
||||
|
||||
* Trigger an event.
|
||||
*
|
||||
* @param {String} event The name of the event.
|
||||
* @param {...*} args Arguments to pass to event handlers.
|
||||
* @public
|
||||
*/
|
||||
trigger(event, ...args) {
|
||||
this.getHandlers(event).forEach((handler) => handler.apply(this, args));
|
||||
this.getHandlers(event).forEach(handler => handler.apply(this, args));
|
||||
},
|
||||
|
||||
/**
|
||||
|
||||
* Register an event handler.
|
||||
*
|
||||
* @param {String} event The name of the event.
|
||||
* @param {function} handler The function to handle the event.
|
||||
*/
|
||||
on(event, handler) {
|
||||
this.getHandlers(event).push(handler);
|
||||
},
|
||||
|
||||
/**
|
||||
|
||||
* Register an event handler so that it will run only once, and then
|
||||
* unregister itself.
|
||||
*
|
||||
* @param {String} event The name of the event.
|
||||
* @param {function} handler The function to handle the event.
|
||||
*/
|
||||
one(event, handler) {
|
||||
var wrapper = function() {
|
||||
const wrapper = function() {
|
||||
handler.apply(this, arguments);
|
||||
|
||||
this.off(event, wrapper);
|
||||
};
|
||||
|
||||
this.getHandlers(event).push(wrapper);
|
||||
},
|
||||
|
||||
/**
|
||||
|
||||
* Unregister an event handler.
|
||||
*
|
||||
* @param {String} event The name of the event.
|
||||
* @param {function} handler The function that handles the event.
|
||||
*/
|
||||
off(event, handler) {
|
||||
var handlers = this.getHandlers(event);
|
||||
var index = handlers.indexOf(handler);
|
||||
const handlers = this.getHandlers(event);
|
||||
const index = handlers.indexOf(handler);
|
||||
|
||||
if (index !== -1) {
|
||||
handlers.splice(index, 1);
|
||||
}
|
||||
|
Reference in New Issue
Block a user