Created
February 9, 2015 23:49
-
-
Save monolithed/2d1bc3306dd3a58359f5 to your computer and use it in GitHub Desktop.
DOMEvent
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
/** | |
* @class DOMEvent | |
* @param {Object} element | |
*/ | |
export default class DOMEvent { | |
constructor (element) { | |
this.element = element; | |
this.events = {}; | |
} | |
/** | |
* Special handleEvent function | |
* | |
* @param {Object} event | |
*/ | |
handleEvent (event) { | |
var events = this.events[event.type]; | |
events.callback.call(this, event, events.data); | |
} | |
/** | |
* Set events | |
* | |
* @param {string} event | |
* @param {Object|null} data | |
* @param {Function} callback | |
* @param {boolean} phase | |
*/ | |
on (event, data, callback, phase) { | |
if (this.element) { | |
this.events[event] = { callback, data, phase }; | |
this.element.addEventListener(event, this, phase); | |
} | |
} | |
/** | |
* Remove event | |
* | |
* @param {string} event | |
*/ | |
off (event) { | |
if (this.element) { | |
this.element.removeEventListener(event, this, | |
this.events[event].phase); | |
} | |
} | |
/** | |
* Trigger event | |
* | |
* @param {string} event | |
* @param {Object} [data] | |
* | |
* TODO: придумать как допилить | |
*/ | |
/* | |
trigger (event, data) { | |
var register = new CustomEvent(event, { | |
detail: data | |
}); | |
document.dispatchEvent(register); | |
} | |
*/ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment