Skip to content

Instantly share code, notes, and snippets.

@briancavalier
Created July 8, 2010 13:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save briancavalier/468015 to your computer and use it in GitHub Desktop.
Save briancavalier/468015 to your computer and use it in GitHub Desktop.
// 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