Created
March 24, 2010 12:00
-
-
Save 3rd-Eden/342226 to your computer and use it in GitHub Desktop.
Event Delegation for Spry using CSS3 selectors
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
Spry.Utils.liveListener = function( eventType, root, capture ){ | |
var that = this; | |
Spry.Utils.Notifier.call( that ); | |
that.root = root || document.documentElement; | |
that.eventType = eventType || "click"; | |
Spry.Utils.addEventListener( that.root, eventType, function( evt ){ | |
var target = evt.target || evt.srcElement, | |
tmp, selector, j, i = that.observers.length; | |
while( i-- ){ | |
if( that.observers[ i ] ){ | |
for( selector in that.observers[ i ] ){ | |
tmp = Spry.$$( selector, that.root ); | |
j = tmp.length; | |
while( j-- ){ | |
if( tmp[j] === target ){ | |
that.notifyObservers( selector, { element: target, event: evt, selector: tmp }) | |
break; | |
} | |
} | |
} | |
} | |
} | |
}, capture); | |
}; | |
Spry.Utils.liveListener.prototype = new Spry.Utils.Notifier(); | |
Spry.Utils.liveListener.prototype.constructor = Spry.Utils.liveListener; | |
/* | |
var listener = new Spry.Utils.liveListener( "click" ); | |
// now you can easily register multiple event listeners based on CSS3 selectors | |
listeners.addObserver({ | |
"a[href='#selector']" : function( observer, event ){ | |
alert("You have clicked the #selector, painting element red!"); | |
event.element.style.border = "1px solid red"; | |
}, | |
"[class='shakeTrigger']": function( observer, event ){ | |
Spry.Effect.DoShare( event.element, { duration: 500 } ); | |
} | |
}); | |
// and you can just remove it just as easily using the .removeObserver syntax :)! | |
*/ |
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
Spry.Utils.Notifier = function() | |
{ | |
this.observers = []; | |
this.suppressNotifications = 0; | |
}; | |
Spry.Utils.Notifier.prototype.addObserver = function(observer) | |
{ | |
if (!observer) | |
return; | |
var len = this.observers.length; | |
for (var i = 0; i < len; i++) | |
{ | |
if (this.observers[i] == observer) | |
return; | |
} | |
this.observers[len] = observer; | |
}; | |
Spry.Utils.Notifier.prototype.removeObserver = function(observer) | |
{ | |
if (!observer) | |
return; | |
for (var i = 0; i < this.observers.length; i++) | |
{ | |
if (this.observers[i] == observer) | |
{ | |
this.observers.splice(i, 1); | |
break; | |
} | |
} | |
}; | |
Spry.Utils.Notifier.prototype.notifyObservers = function(methodName, data) | |
{ | |
if (!methodName) | |
return; | |
if (!this.suppressNotifications) | |
{ | |
var len = this.observers.length; | |
for (var i = 0; i < len; i++) | |
{ | |
var obs = this.observers[i]; | |
if (obs) | |
{ | |
if (typeof obs == "function") | |
obs(methodName, this, data); | |
else if (obs[methodName]) | |
obs[methodName](this, data); | |
} | |
} | |
} | |
}; | |
Spry.Utils.Notifier.prototype.enableNotifications = function() | |
{ | |
if (--this.suppressNotifications < 0) | |
{ | |
this.suppressNotifications = 0; | |
Spry.Debug.reportError("Unbalanced enableNotifications() call!\n"); | |
} | |
}; | |
Spry.Utils.Notifier.prototype.disableNotifications = function() | |
{ | |
++this.suppressNotifications; | |
}; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment