Added attribute method to dom module, tweaked dom.each, dom.addClass, dom.removeClass functions

This commit is contained in:
Timothy Warren 2011-06-23 13:16:02 -04:00
parent e3cf259a93
commit 5055bce0fc
2 changed files with 192 additions and 48 deletions

View File

@ -85,15 +85,21 @@ Browser support: IE8+, Latest versions of Firefox, Chrome, Safari, Opera
*each: For applying changes to every item matched by a selector
Use:
$.dom.each(selector, callback);
$_.dom.each(selector, callback);
*show: For setting dom elements as visible. Type defaults as "block", can be set with optional second parameter.
Use:
$.dom.show(selector, [type]);
$_.dom.show(selector, [type]);
*hide: Hides the elements matching the selector
Use:
$.dom.hide(selector);
$_.dom.hide(selector);
#attr: Gets, sets, or removes an attribute from a selector.
Use:
Set: $_.dom.attr(selector, attributeName, attributeValue);
Get: $_.dom.attr(selector, attributeName);
Remove: $_.dom.attr(selector, attributeName, null);

168
kis.js
View File

@ -38,6 +38,15 @@
window.$ = $;
//Console.log will contain error messages: if it doesn't exist, create a dummy method
if(typeof window.console === "undefined")
{
window.console = {
log: function(){}
};
}
/**
* Ajax
*
@ -359,22 +368,41 @@
(function(){
var d, len;
// Private function for class manipulation
function _class(sel, c, add)
{
var ec, cs, len, i, classInd;
sel = _sel(sel);
//We can do this the easy way
if(sel.classList)
{
if(add === true)
{
sel.classList.add(c);
return;
}
else if(add === false)
{
sel.classList.remove(c);
return;
}
}
else //Or the hard way
{
//No class attribute? Set an empty string
ec = _attr(sel, "class");
ec = (typeof ec === "string") ? ec : '';
ec = sel.className;
//Convert class attribute string into array
if(typeof ec === "string")
{
cs = [];
cs = ec.split(" ");
cs = (ec !== '') ? ec.split(" ") : [];
len = cs.length;
classInd = false;
//Check for the class in the array
for(var i=0;i<len;i++)
{
if(cs[i] === c)
@ -385,40 +413,123 @@
}
}
//Add or remove from class array
if(add === true)
{
//Only add the class if isn't already there
if(classInd === false)
{
cs.push(c);
sel.className = (cs.length > 1) ? cs.join(" ") : cs[0];
}
}
if(add === false)
else if(add === false)
{
//Make sure the class you want to remove exists
if(classInd !== false)
{
cs.splice(classInd, 1);
sel.className = (cs.length > 1) ? cs.join(" ") : cs[0];
}
}
return sel.className;
var cName = (cs.length > 1) ? cs.join(" ") : cs[0];
//Check if trim method exists
if(cName.trim)
{
cName = cName.trim();
}
if(typeof sel.className !== "undefined")
{
sel.className = cName;
}
else if(typeof sel.setAttribute !== "undefined")
{
sel.setAttribute('class', cName)
}
else
{
console.log(sel);
}
return cName;
}
}
//Private function for getting/setting attributes
function _attr(sel, name, value)
{
var oldVal, doAttr;
//Get the value of the attribute, if it exists
if(typeof sel.hasAttribute !== "undefined")
{
if(sel.hasAttribute(name))
{
oldVal = sel.getAttribute(name);
}
doAttr = true;
}
else if(typeof sel[name] !== "undefined")
{
oldVal = sel[name];
doAttr = false;
}
else if(name === "class" && typeof sel.className !== "undefined") //className attribute
{
name = "className";
oldVal = sel.className;
doAttr = false;
}
//Well, I guess that attribute doesn't exist
if(typeof oldVal === "undefined" && (typeof value === "undefined" || value === null))
{
console.log(sel);
console.log("Element does not have the selected attribute");
return;
}
//No value to set? Return the current value
if(typeof value === "undefined")
{
return oldVal;
}
//Determine what to do with the attribute
if(typeof value !== "undefined" && value !== null)
{
(doAttr === true)
? sel.setAttribute(name, value)
: sel[name] = value;
}
else if(value === null)
{
(doAttr === true)
? sel.removeAttribute(name)
: sel[name] = null;
}
return (typeof value !== "undefined") ? value : oldValue;
}
d = {
each: function(sel, callback)
{
sel = _sel(sel);
if(sel.length < 2)
if(typeof sel === "string")
{
callback(sel);
return;
sel = $(sel);
}
for(var x in sel)
var len = sel.length;
if(len === 0){ return }
if(len === 1){ return callback(sel); }
for(var x=0;x<sel.length;x++)
{
callback(sel[x]);
}
@ -450,9 +561,12 @@
});
}
else
{
if(sel.style)
{
sel.style.display = "none";
}
}
},
show: function(sel, type)
@ -474,6 +588,30 @@
{
sel.style.display = type;
}
},
attr: function(sel, name, value)
{
var oldVal;
sel = _sel(sel);
//Make sure you don't try to get a bunch of elements
if(sel.length > 1 && typeof value === "undefined")
{
console.log(sel);
console.log("Must be a singular element");
return;
}
else if(sel.length > 1 && typeof value !== "undefined") //You can set a bunch, though
{
this.each(sel, function(e){
_attr(e, name, value);
});
}
else //Normal behavior
{
return _attr(sel, name, value);
}
}
};