Created
March 2, 2009 11:35
-
-
Save weepy/72714 to your computer and use it in GitHub Desktop.
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 HookManager = function() { | |
this.list = {}; | |
this.hookTable = {} | |
} | |
HookManager.prototype = { | |
add: function(n, type, fn, priority, off) { | |
if(typeof type != "string") | |
log("Cannot add type as hook: " + n) | |
this.list[n] = { name: n, type: type, fn: fn, priority: priority == null ? 10 : priority, off: off }; | |
}, | |
fire: function(s, param) { | |
events = s.split(",") | |
for(var i in events ) | |
this.fireSingle(events[i], param) | |
}, | |
fireSingle: function(event, param) { | |
this.hookTable[event] = this.hookTable[event] || this.calcHooks(event); | |
var hooks = this.hookTable[event]; | |
for(var i in hooks) { | |
if(!hooks[i].off) | |
hooks[i].fn(param); | |
} | |
}, | |
calcHooks: function(event) { | |
var hooks = [] | |
for(var i in this.list) { | |
var hook = this.list[i]; | |
var types = hook.type.split(","); | |
for(var i in types) { | |
if(types[i] == event) | |
hooks.push(hook) | |
} | |
} | |
if(hooks.length == 0) | |
log("No hooks found for event: " + event) | |
hooks.sort(function(a,b) {return a.priority - b.priority}) | |
return hooks; | |
} | |
}; | |
var Hooks = new HookManager(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment