Created
December 21, 2012 11:20
-
-
Save unsetbit/4352227 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
function getgo(container, events){ | |
var METHOD_NAME = "getgo", | |
ATTRIBUTE_NAME = "data-getgo", | |
BROWSER_DOESNT_SUCK = typeof container.addEventListener === "function"; | |
function createEventQueue(target){ | |
var pending = []; | |
function add(event){ | |
pending.push(event); | |
} | |
function dispatch(){ | |
var index = 0, | |
length = pending.length; | |
for(; index < length; index++){ | |
target.dispatchEvent(event); | |
} | |
pending = []; | |
delete target[GG_METHOD_NAME]; | |
} | |
} | |
var handler = function(event){ | |
event = event || window.event; | |
var target = event.target || event.srcElement, | |
directive, | |
pending; | |
// Find the first element on the way up with a getgo attribute | |
while (target) { | |
directive = target.getAttribute(ATTRIBUTE_NAME); | |
if(directive) break; | |
target = target.parentNode; | |
} | |
// If we found a target, cancel the event propogation here. | |
// Propogation will continue once the element's getgo method | |
// is called. | |
if(target){ | |
pending = target[METHOD_NAME] || target[METHOD_NAME] = createEventQueue(target); | |
pending.add(event); | |
if (typeof evt.stopPropagation === "function") { | |
event.stopPropagation(); | |
event.preventDefault(); | |
} else { | |
event.cancelBubble = true; | |
event.returnValue = false; | |
} | |
} | |
} | |
// Bind the listeners to the container | |
var index = 0; | |
length = eventNames.length; | |
if(BROWSER_DOESNT_SUCK) | |
for(; index < length; index++) | |
container.addEventListener(eventNames[index], handler, false); | |
else | |
for(; index < length; index++) | |
container.attachEvent('on' + eventNames[index], handler); | |
function destroy(){ | |
if(BROWSER_DOESNT_SUCK) | |
for(; index < length; index++) | |
container.removeEventListener(eventNames[index], handler, false); | |
else | |
for(; index < length; index++) | |
container.detachEvent("on" + eventNames[index], handler); | |
} | |
return { | |
destroy: destroy | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment