Various constructor fixes, removed some redundancies

This commit is contained in:
Timothy Warren 2011-07-08 13:03:38 -04:00
parent badaf25867
commit a3f270ba95
2 changed files with 47 additions and 53 deletions

View File

@ -13,11 +13,11 @@ Browser support: IE8+, Latest versions of Firefox, Chrome, Safari, Opera
## Modules: ## ## Modules: ##
**Global**: Core functions **Global**: Core functions
properties:
* el: The html object returned by the selector function.
functions: functions:
* $:
Use:
$(selector);
*each: For applying changes to every item matched by a selector *each: For applying changes to every item matched by a selector
Use: Use:
@ -115,6 +115,8 @@ Browser support: IE8+, Latest versions of Firefox, Chrome, Safari, Opera
Use: Use:
Set: $_(selector).dom.text(text); Set: $_(selector).dom.text(text);
Get: $_(selector).dom.text(); Get: $_(selector).dom.text();

90
kis.js
View File

@ -27,12 +27,18 @@
*/ */
$_ = function(sel) $_ = function(sel)
{ {
//Get the DOM objects from the selector
sel = $(sel);
//Have window be default selector, just in case
if(typeof sel === "undefined") if(typeof sel === "undefined")
{ {
sel = window; sel = (typeof $_.el !== "undefined")
? $_.el
: window;
} }
$_.sel = $(sel); $_.el = sel;
return $_; return $_;
}; };
@ -71,6 +77,7 @@
x = document.querySelectorAll(a); x = document.querySelectorAll(a);
} }
//Return the single object if applicable
return (x.length === 1) ? x[0] : x; return (x.length === 1) ? x[0] : x;
}; };
@ -78,14 +85,15 @@
$_.each = function (callback) $_.each = function (callback)
{ {
sel = $_.sel; var sel = $_.el
if(!sel.length) //Don't try to iterate over the window object
if(sel === window || typeof sel === "undefined")
{ {
// sel is a DOM Element return;
callback(sel);
} }
else
if(typeof sel.length !== "undefined")
{ {
var len = sel.length; var len = sel.length;
@ -106,6 +114,10 @@
callback(selx); callback(selx);
} }
} }
else
{
callback(sel);
}
}; };
/** /**
@ -416,7 +428,7 @@
var i, len, selx; var i, len, selx;
//Get the DOM object //Get the DOM object
sel = $_.sel; var sel = $_.el
if(arguments.length === 4) if(arguments.length === 4)
{ {
@ -531,9 +543,14 @@
} }
else if (value === null) else if (value === null)
{ {
(doAttr === true) if(doAttr === true)
? sel.removeAttribute(name) {
: sel[name] = null; sel.removeAttribute(name);
}
else
{
delete sel[name];
}
} }
return (typeof value !== "undefined") ? value : oldVal; return (typeof value !== "undefined") ? value : oldVal;
@ -621,7 +638,7 @@
} }
function _css(prop, val) function _css(sel, prop, val)
{ {
var equi; var equi;
@ -638,11 +655,12 @@
//Let have an object with equivalent properties //Let have an object with equivalent properties
//for `special` browsers, and other quirks //for `special` browsers, and other quirks
//Todo: get more common properties
equi = { equi = {
top: "", outerHeight: "offsetHeight",
right: "", outerWidth: "offsetWidth",
bottom: "", top: "posTop"
left: ""
}; };
if(sel.style[equi[prop]]) if(sel.style[equi[prop]])
@ -658,7 +676,7 @@
d = { d = {
addClass: function (c) addClass: function (c)
{ {
var sel = $_.sel; var sel = $_.el
$_.each(function (e){ $_.each(function (e){
_class(e, c, true); _class(e, c, true);
@ -666,7 +684,7 @@
}, },
removeClass: function (c) removeClass: function (c)
{ {
var sel = $_.sel; var sel = $_.el
$_.each(function (e){ $_.each(function (e){
_class(e, c, false); _class(e, c, false);
@ -674,46 +692,20 @@
}, },
hide: function () hide: function ()
{ {
var sel = $_.sel; this.css('display', 'none');
if (sel.length > 1)
{
$_.each(function (e){
e.style.display = "none";
});
}
else
{
if (sel.style)
{
sel.style.display = "none";
}
}
}, },
show: function (type) show: function (type)
{ {
var sel = $_.sel;
if (typeof type === "undefined") if (typeof type === "undefined")
{ {
type = "block"; type = "block";
} }
if (sel.length > 1) this.css("display", type);
{
$_.each(function (e){
e.style.display = type;
});
}
else
{
sel.style.display = type;
}
}, },
attr: function (name, value) attr: function (name, value)
{ {
var sel = $_.sel; var sel = $_.el
//Make sure you don't try to get a bunch of elements //Make sure you don't try to get a bunch of elements
if (sel.length > 1 && typeof value === "undefined") if (sel.length > 1 && typeof value === "undefined")
@ -737,7 +729,7 @@
{ {
var oldValue, set, type, sel; var oldValue, set, type, sel;
sel = $_.sel; var sel = $_.el
set = (typeof value !== "undefined") ? true : false; set = (typeof value !== "undefined") ? true : false;