ddd-jquery
Version:
de-bowerified, de-amdified, de-gruntified... jQuery.
97 lines • 4.19 kB
JavaScript
var jQuery = require('../core'), rnotwhite = require('../var/rnotwhite'), strundefined = require('../var/strundefined'), data_priv = require('../data/var/data_priv'), __unnameddep__ = require('../core/init');
var rclass = /[\t\r\n\f]/g;
jQuery.fn.extend({
addClass: function (value) {
var classes, elem, cur, clazz, j, finalValue, proceed = typeof value === 'string' && value, i = 0, len = this.length;
if (jQuery.isFunction(value)) {
return this.each(function (j) {
jQuery(this).addClass(value.call(this, j, this.className));
});
}
if (proceed) {
classes = (value || '').match(rnotwhite) || [];
for (; i < len; i++) {
elem = this[i];
cur = elem.nodeType === 1 && (elem.className ? (' ' + elem.className + ' ').replace(rclass, ' ') : ' ');
if (cur) {
j = 0;
while (clazz = classes[j++]) {
if (cur.indexOf(' ' + clazz + ' ') < 0) {
cur += clazz + ' ';
}
}
finalValue = jQuery.trim(cur);
if (elem.className !== finalValue) {
elem.className = finalValue;
}
}
}
}
return this;
},
removeClass: function (value) {
var classes, elem, cur, clazz, j, finalValue, proceed = arguments.length === 0 || typeof value === 'string' && value, i = 0, len = this.length;
if (jQuery.isFunction(value)) {
return this.each(function (j) {
jQuery(this).removeClass(value.call(this, j, this.className));
});
}
if (proceed) {
classes = (value || '').match(rnotwhite) || [];
for (; i < len; i++) {
elem = this[i];
cur = elem.nodeType === 1 && (elem.className ? (' ' + elem.className + ' ').replace(rclass, ' ') : '');
if (cur) {
j = 0;
while (clazz = classes[j++]) {
while (cur.indexOf(' ' + clazz + ' ') >= 0) {
cur = cur.replace(' ' + clazz + ' ', ' ');
}
}
finalValue = value ? jQuery.trim(cur) : '';
if (elem.className !== finalValue) {
elem.className = finalValue;
}
}
}
}
return this;
},
toggleClass: function (value, stateVal) {
var type = typeof value;
if (typeof stateVal === 'boolean' && type === 'string') {
return stateVal ? this.addClass(value) : this.removeClass(value);
}
if (jQuery.isFunction(value)) {
return this.each(function (i) {
jQuery(this).toggleClass(value.call(this, i, this.className, stateVal), stateVal);
});
}
return this.each(function () {
if (type === 'string') {
var className, i = 0, self = jQuery(this), classNames = value.match(rnotwhite) || [];
while (className = classNames[i++]) {
if (self.hasClass(className)) {
self.removeClass(className);
} else {
self.addClass(className);
}
}
} else if (type === strundefined || type === 'boolean') {
if (this.className) {
data_priv.set(this, '__className__', this.className);
}
this.className = this.className || value === false ? '' : data_priv.get(this, '__className__') || '';
}
});
},
hasClass: function (selector) {
var className = ' ' + selector + ' ', i = 0, l = this.length;
for (; i < l; i++) {
if (this[i].nodeType === 1 && (' ' + this[i].className + ' ').replace(rclass, ' ').indexOf(className) >= 0) {
return true;
}
}
return false;
}
});