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: Use:
$_.dom.removeClass(selector, className); $_.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);

140
kis.js
View File

@ -1,15 +1,15 @@
/** /**
Kis JS Keep It Simple JS Library Kis JS Keep It Simple JS Library
Copyright Timothy J. Warren Copyright Timothy J. Warren
License Public Domain License Public Domain
Version 0.0.1 Version 0.1.0
*/ */
(function(){ (function(){
"use strict"; "use strict";
var $_, $; var $_, $, _sel;
$_ = {}; $_ = {};
@ -26,6 +26,16 @@
return (x.length === 1) ? x[0] : x; return (x.length === 1) ? x[0] : x;
}; };
/**
* _sel
*
* Return the selector for the string
*/
_sel = function(s)
{
return (typeof s === "string") ? $(s) : s;
};
window.$ = $; window.$ = $;
/** /**
@ -49,7 +59,7 @@
url += (type === "GET") url += (type === "GET")
? "?" + this._serialize(data, true) ? "?" + this._serialize(data, true)
: ''; : '';
request.open(type, url); request.open(type, url);
@ -114,7 +124,7 @@
window.$_.hb = (history.pushState) ? false : true; window.$_.hb = (history.pushState) ? false : true;
var qs = { var qs = {
parse: function(hb) parse: function(hb)
{ {
hb = hb || $_.hb; hb = hb || $_.hb;
@ -290,10 +300,7 @@
} }
//Get the DOM object if you give me a selector string //Get the DOM object if you give me a selector string
if(typeof sel === "string") sel = _sel(sel);
{
sel = $(sel);
}
//Multiple events? Run recursively! //Multiple events? Run recursively!
event = event.split(" "); event = event.split(" ");
@ -345,71 +352,108 @@
}()); }());
/** /**
* Dom manipulation object * Dom manipulation object
* *
*/ */
(function(){ (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;
sel = _sel(sel);
ec = sel.className;
if(typeof c === "string") if(typeof ec === "string")
{ {
var cs = c.split(" "); cs = [];
var len = cs.length; cs = ec.split(" ");
len = cs.length;
classInd = false;
for(var i=0;i<len;i++) for(var i=0;i<len;i++)
{ {
if(cs[i] === c) 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 = { d = {
each: function(sel, callback)
{
sel = _sel(sel);
for(var x in sel)
{
callback(sel[x]);
}
},
addClass: function(sel, c) addClass: function(sel, c)
{ {
if(typeof sel === "string") sel = _sel(sel);
{
sel = $(sel);
}
var x, classInd, cs; this.each(sel, function(e){
x = _get(sel, c); _class(e, c, true);
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(" ");
}
}, },
removeClass: function(sel, c) removeClass: function(sel, c)
{ {
if(typeof sel === "string") sel = _sel(sel);
{
sel = $(sel);
}
var x, classInd, cs; this.each(sel, function(e){
x = _get(sel, c); _class(e, c, false);
classInd = (x[1]) ? x[1] : false; });
cs = (x[0]) ? x[0] : []; },
hide: function(sel)
{
sel = _sel(sel);
this.each(sel, function(e){
e.style.display = "none";
});
if(classInd !== false) },
show: function(sel, type)
{
sel = _sel(sel);
if(typeof type === "undefined")
{ {
cs.splice(classInd, 1); type="block";
sel.className = cs.join(" ");
} }
this.each(sel, function(e){
e.style.display = type;
});
} }
}; };