UNPKG

@travlrcom/uikit

Version:

TRAVLR UiKit

68 lines (58 loc) 1.8 kB
/** **************************************************** *** ******* ****** **** *** ****** ****** ***** *********** ************** ****** ****** *********** ************* ****** ******* *********** ************ ****** ******** *********** *********** ****** ********* *********** ********** ****** ********** *********** ********* ****** *********** *********** ******** ****** ************ *********** ******* ****** ************* *********** ****** ****** ************** **************************************************** * -------------------------------------------------- * Travlr UiKit: eventHandler.js * By Travlr Team * -------------------------------------------------- */ // const const NAME = 'eventHandler' class EventHandler { constructor (config) { this._config = config this._type = config.type this._eventName = config.eventName this._element = config.element this._action = config.action this.init() } init () { if (this._type === 'add') { this.addEvent() } else if (this._type === 'trigger') { this.triggerEvent() } else { return } } addEvent () { if (this._eventName && typeof this._action === 'function') { // Create the event new Event(this._eventName) // Add event listener this._element.addEventListener(this._eventName, this._action) } } triggerEvent () { if (this._eventName) { // Dispatch/Trigger/Fire the event this._element.dispatchEvent(new Event(this._eventName)) } } } window.$TravlrEvent = (config) => { new EventHandler(config) } export default EventHandler