Created
July 8, 2010 13:44
-
-
Save briancavalier/468015 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
// Extend NodeList with a rotateClass method that will rotate among a list of | |
// classArr, and optionally remove them as part of the rotation. | |
dojo.extend(dojo.NodeList, { | |
rotateClass: function(classArr, remove) { | |
var l = classArr.length; | |
return this.forEach(function(n) { | |
var rotated = false; | |
for(var i=0; i<l; ++i) { | |
if(remove && i == (l-1)) { | |
// Found last class, and remove == true, rotate to none | |
dojo.removeClass(n, classArr); | |
rotated = true; | |
break; | |
} else if(dojo.hasClass(n, classArr[i])) { | |
// Found one of the classes, rotate to next one in array | |
dojo.removeClass(n, classArr[i]); | |
dojo.addClass(n, classArr[(i+1) % l]); | |
rotated = true; | |
break; | |
} | |
} | |
// If we didn't find any rotatable class, and the remove == true | |
// add the first class. This allows rotating from a -> b -> none -> a, | |
// but also means that a node that initially starts with none will | |
// be rotated to a. | |
if(!rotated && remove) { | |
dojo.addClass(n, classArr[0]); | |
} | |
}); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment