Added each, show, and hide functions to dom module

This commit is contained in:
Timothy Warren 2011-06-21 13:33:03 -04:00
parent 1dc9a96cba
commit a21c00f43a
2 changed files with 103 additions and 49 deletions

View File

@ -83,7 +83,17 @@ Browser support: IE8+, Latest versions of Firefox, Chrome, Safari, Opera
Use:
$_.dom.removeClass(selector, className);
*each: For applying changes to every item matched by a selector
Use:
$.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]);
*hide: Hides the elements matching the selector
Use:
$.dom.hide(selector);

124
kis.js
View File

@ -2,14 +2,14 @@
Kis JS Keep It Simple JS Library
Copyright Timothy J. Warren
License Public Domain
Version 0.0.1
Version 0.1.0
*/
(function(){
"use strict";
var $_, $;
var $_, $, _sel;
$_ = {};
@ -26,6 +26,16 @@
return (x.length === 1) ? x[0] : x;
};
/**
* _sel
*
* Return the selector for the string
*/
_sel = function(s)
{
return (typeof s === "string") ? $(s) : s;
};
window.$ = $;
/**
@ -290,10 +300,7 @@
}
//Get the DOM object if you give me a selector string
if(typeof sel === "string")
{
sel = $(sel);
}
sel = _sel(sel);
//Multiple events? Run recursively!
event = event.split(" ");
@ -350,66 +357,103 @@
*
*/
(function(){
var d, c, cs, len;
var d, len;
function _get(sel, c)
function _class(sel, c, add)
{
c = sel.className;
var ec, cs, len, i, classInd;
if(typeof c === "string")
sel = _sel(sel);
ec = sel.className;
if(typeof ec === "string")
{
var cs = c.split(" ");
var len = cs.length;
cs = [];
cs = ec.split(" ");
len = cs.length;
classInd = false;
for(var i=0;i<len;i++)
{
if(cs[i] === c)
{
return [cs, i];
classInd = i;
break;
}
}
}
return [[], false];
if(add === true)
{
if(classInd === false)
{
cs.push(c);
sel.className = (cs.length > 1) ? cs.join(" ") : cs[0];
}
}
if(add === false)
{
if(classInd !== false)
{
cs.splice(classInd, 1);
sel.className = (cs.length > 1) ? cs.join(" ") : cs[0];
}
}
return sel.className;
}
d = {
each: function(sel, callback)
{
sel = _sel(sel);
for(var x in sel)
{
callback(sel[x]);
}
},
addClass: function(sel, c)
{
if(typeof sel === "string")
{
sel = $(sel);
}
sel = _sel(sel);
var x, classInd, cs;
x = _get(sel, c);
classInd = (x[1] != false || x[1] === 0) ? x[1] : false;
cs = (x[0]) ? x[0] : [];
if(classInd !== false)
{
cs.push(c);
sel.className = cs.join(" ");
}
this.each(sel, function(e){
_class(e, c, true);
});
},
removeClass: function(sel, c)
{
if(typeof sel === "string")
sel = _sel(sel);
this.each(sel, function(e){
_class(e, c, false);
});
},
hide: function(sel)
{
sel = $(sel);
sel = _sel(sel);
this.each(sel, function(e){
e.style.display = "none";
});
},
show: function(sel, type)
{
sel = _sel(sel);
if(typeof type === "undefined")
{
type="block";
}
var x, classInd, cs;
x = _get(sel, c);
classInd = (x[1]) ? x[1] : false;
cs = (x[0]) ? x[0] : [];
if(classInd !== false)
{
cs.splice(classInd, 1);
sel.className = cs.join(" ");
}
this.each(sel, function(e){
e.style.display = type;
});
}
};