-
-
Save jblanche/3004543 to your computer and use it in GitHub Desktop.
eventable.js
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
var Eventable = | |
{ | |
addEventListener: function(type, listener) | |
{ | |
if (this.eventMap == null) | |
this.eventMap = {}; | |
var list = this.eventMap[type]; | |
if (list == null) | |
{ | |
list = []; | |
this.eventMap[type] = list; | |
} | |
list.push(listener); | |
}, | |
dispatchEvent: function(event) | |
{ | |
if (this.eventMap == null) | |
return false; | |
if (event.target == null) | |
event.target = this; | |
var list = this.eventMap[event.type]; | |
if (list != null) | |
{ | |
var length = list.length; | |
for (var i=0; i<length; i++) | |
{ | |
var listener = list[i]; | |
if (listener != null) | |
{ | |
var res = listener(event); | |
if (res === false) return; | |
} | |
} | |
return true; | |
} | |
return false; | |
}, | |
hasEventListener: function(type) | |
{ | |
if (this.eventMap == null) | |
return false; | |
var h = this.eventMap[type]; | |
return (h == null) ? false : (h.length > 0); | |
}, | |
removeEventListener: function(type, listener) | |
{ | |
if (this.eventMap == null) | |
return; | |
var list = this.eventMap[type]; | |
if (isNullOrEmpty(list)) return; | |
var length = list.length; | |
for (var i=0; i<length; i++) | |
{ | |
if (list[i] === listener) | |
{ | |
list.splice(i, 1); | |
return; | |
} | |
} | |
}, | |
removeAllListeners: function() | |
{ | |
this.eventMap = null; | |
}, | |
willTrigger: function(type) | |
{ | |
if (this.eventMap == null) | |
return false; | |
return this.eventMap[type] != null; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment