Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes CSS classes functions #2

Merged
merged 1 commit into from
Nov 28, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 74 additions & 40 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -42,82 +42,116 @@
[parseFloat(coord[0]), parseFloat(coord[1])], 'EPSG:3857', 'EPSG:4326'
);
},
isNumeric: function(str){
return /^\d+$/.test(str);
},
classRegex: function(classname) {
return new RegExp('(^|\\s+)' + classname + '(\\s+|$)');
},
_addClass: function(el, c){
if (el.classList)
el.classList.add(c);
else
el.className = (el.className + ' ' + c).trim();
},
addClass: function(el, classname){
if(Array.isArray(el)){
el.forEach(function(each){
/**
* @param {Element|Array<Element>} element DOM node or array of nodes.
* @param {String|Array<String>} classname Class or array of classes.
* For example: 'class1 class2' or ['class1', 'class2']
* @param {Number|undefined} timeout Timeout to remove a class.
*/
addClass: function(element, classname, timeout){
if(Array.isArray(element)){
element.forEach(function(each){
utils.addClass(each, classname);
});
return;
}

//classname can be ['class1', 'class2'] or 'class1 class2'
var
array = (Array.isArray(classname)) ?
classname : classname.split(utils.whiteSpaceRegex),
array = (Array.isArray(classname)) ? classname : classname.split(/\s+/),
i = array.length
;
while(i--){
if(!utils.hasClass(el, array[i])) {
utils._addClass(el, array[i]);
if(!utils.hasClass(element, array[i])) {
utils._addClass(element, array[i], timeout);
}
}
},
_removeClass: function(el, c){
if (el.classList){
el.classList.remove(c);
_addClass: function(el, c, timeout){
// use native if available
if (el.classList) {
el.classList.add(c);
} else {
el.className =
(el.className.replace(utils.classReg(c), ' ')).trim();
el.className = (el.className + ' ' + c).trim();
}

if(timeout && utils.isNumeric(timeout)){
win.setTimeout(function(){
utils._removeClass(el, c);
}, timeout);
}
},
removeClass: function(el, classname){
if(Array.isArray(el)){
el.forEach(function(each){
utils.removeClass(each, classname);
/**
* @param {Element|Array<Element>} element DOM node or array of nodes.
* @param {String|Array<String>} classname Class or array of classes.
* For example: 'class1 class2' or ['class1', 'class2']
* @param {Number|undefined} timeout Timeout to add a class.
*/
removeClass: function(element, classname, timeout){
if(Array.isArray(element)){
element.forEach(function(each){
utils.removeClass(each, classname, timeout);
});
return;
}

//classname can be ['class1', 'class2'] or 'class1 class2'
var
array = (Array.isArray(classname)) ?
classname : classname.split(utils.whiteSpaceRegex),
array = (Array.isArray(classname)) ? classname : classname.split(/\s+/),
i = array.length
;
while(i--){
if(utils.hasClass(el, array[i])) {
utils._removeClass(el, array[i]);
if(utils.hasClass(element, array[i])) {
utils._removeClass(element, array[i], timeout);
}
}
},
hasClass: function(el, c){
return (el.classList) ?
el.classList.contains(c) : utils.classReg(c).test(el.className);
_removeClass: function(el, c, timeout){
if (el.classList){
el.classList.remove(c);
} else {
el.className = (el.className.replace(utils.classRegex(c), ' ')).trim();
}
if(timeout && utils.isNumeric(timeout)){
win.setTimeout(function() {
utils._addClass(el, c);
}, timeout);
}
},
/**
* @param {Element} element DOM node.
* @param {String} classname Classname.
* @return {Boolean}
*/
hasClass: function(element, c) {
// use native if available
return (element.classList) ?
element.classList.contains(c) : utils.classRegex(c).test(element.className);
},
toggleClass: function(el, c){
if(Array.isArray(el)){
el.forEach(function(each){
utils.toggleClass(each, c);
/**
* @param {Element|Array<Element>} element DOM node or array of nodes.
* @param {String} classname Classe.
*/
toggleClass: function(element, classname){
if(Array.isArray(element)) {
element.forEach(function(each) {
utils.toggleClass(each, classname);
});
return;
}

if(el.classList) {
el.classList.toggle(c);
// use native if available
if(element.classList) {
element.classList.toggle(classname);
} else {
if(utils.hasClass(el, c)){
utils._removeClass(el, c);
if(utils.hasClass(element, classname)){
utils._removeClass(element, classname);
} else {
utils._addClass(el, c);
utils._addClass(element, classname);
}
}
},