Created
July 20, 2010 01:46
-
-
Save bga/482330 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
(function() | |
{ | |
var _fnMap = {}; | |
$.fn.addClass = function(classes) | |
{ | |
var vs = this; | |
if(!(classes > '') || vs.length == 0) | |
return this; | |
( | |
_fnMap[classes] || | |
(_fnMap[classes] = new Function( | |
'vs', | |
'var v,c,d,i=-1;while((v=vs[++i])){ if((d=v.className)){ c = " "+d+" "; v.className = d.concat(' + | |
classes.replace(/(\S+)/g, '(~c.indexOf(" $1 ") ? "" : " $1"),').slice(0, -1) + | |
') }else{ v.className="' + classes + '" } };' | |
) | |
) | |
)(vs); | |
return this; | |
}; | |
})(); | |
(function() | |
{ | |
reMap = {}; | |
$.fn.removeClass = function(classes) | |
{ | |
var vs = this; | |
if(!(classes > '') || vs.length == 0) | |
return this; | |
var re = reMap[classes] || | |
(reMap[classes] = | |
( | |
(classes.indexOf(' ') < 0) ? | |
' ' + classes + ' ' : | |
new RegExp(' (?:' + | |
classes. | |
replace(/[\[\]\(\)\{\}\?\!\:\|\+]/g, '\\$1'). | |
replace(/\s+/g, '|') + | |
')(?= )', 'g' | |
) | |
) | |
); | |
if(typeof(re) == 'string') | |
{ | |
var i = -1, v, c; while((v = vs[++i])) | |
{ | |
if((c = v.className)) | |
v.className = (' ' + c + ' ').replace(re, ' ').slice(1, -1); | |
} | |
} | |
else | |
{ | |
var i = -1, v, c; while((v = vs[++i])) | |
{ | |
if((c = v.className)) | |
v.className = (' ' + c + ' ').replace(re, '').slice(1, -1); | |
} | |
} | |
return this; | |
}; | |
})(); | |
(function() | |
{ | |
_fnMap = {}; | |
$.fn.toggleClass = function(classes, addOrRemove) | |
{ | |
var vs = this; | |
if(!(classes > '') || vs.length == 0) | |
return this; | |
if(addOrRemove != null) | |
{ | |
if(addOrRemove) | |
return this._addClass(classes); | |
else | |
return this._removeClass(classes); | |
} | |
if(_fnMap[classes]) | |
{ | |
_fnMap[classes](vs); | |
return this; | |
} | |
if(classes.indexOf(' ') < 0) | |
{ | |
/* | |
if((c = v.className)) | |
{ | |
if(~(c = " " + c + " ").indexOf(' $1 ')) | |
v.className = c.replace(" $1 ", " ").slice(1, -1); | |
else | |
v.className += ' $1'; | |
} | |
else | |
{ | |
v.className = '$1' | |
} | |
*/ | |
_fnMap[classes] = new Function( | |
'vs', | |
'var v,c,i=-1;while((v=vs[++i])){ ' + | |
'if((c=v.className)){if(~(c=" "+c+" ").indexOf(" ' + classes + ' "))v.className=c.replace(" ' + classes + ' ", " ").slice(1, -1);else v.className+=" ' + classes + '";}else{v.className="' + classes + '"}}' | |
); | |
} | |
else | |
{ | |
/* | |
if((c = v.className)) | |
c = ' ' + c + ' '; | |
else | |
c = ' '; | |
c = (c = v.className) ? ' ' + c + ' ' : ' '; | |
if(~c.indexOf(' $1 ')) | |
c = c.replace(' $1 ', ' '); | |
else | |
c += '$1 '; | |
*/ | |
_fnMap[classes] = new Function( | |
'vs', | |
'var v,c,i=-1;while((v=vs[++i])){ c=(c=v.className)?" "+c+" ":" "; ' + | |
classes.replace(/(\S+)/g, 'if(~c.indexOf(" $1 "))c=c.replace(" $1 ", " ");else c+="$1 ";') + | |
' v.className = c.slice(1, -1); };' | |
); | |
} | |
_fnMap[classes](vs); | |
return this; | |
}; | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment