From a3f270ba956ca4678905aaf69c735e1b7f629f72 Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Fri, 8 Jul 2011 13:03:38 -0400 Subject: [PATCH] Various constructor fixes, removed some redundancies --- README.md | 10 ++++--- kis.js | 90 +++++++++++++++++++++++++------------------------------ 2 files changed, 47 insertions(+), 53 deletions(-) diff --git a/README.md b/README.md index e900a0a..0ddeac7 100644 --- a/README.md +++ b/README.md @@ -13,11 +13,11 @@ Browser support: IE8+, Latest versions of Firefox, Chrome, Safari, Opera ## Modules: ## **Global**: Core functions + properties: + + * el: The html object returned by the selector function. + functions: - - * $: - Use: - $(selector); *each: For applying changes to every item matched by a selector Use: @@ -115,6 +115,8 @@ Browser support: IE8+, Latest versions of Firefox, Chrome, Safari, Opera Use: Set: $_(selector).dom.text(text); Get: $_(selector).dom.text(); + + diff --git a/kis.js b/kis.js index 6ef0a68..b28c001 100644 --- a/kis.js +++ b/kis.js @@ -27,12 +27,18 @@ */ $_ = function(sel) { + //Get the DOM objects from the selector + sel = $(sel); + + //Have window be default selector, just in case if(typeof sel === "undefined") { - sel = window; + sel = (typeof $_.el !== "undefined") + ? $_.el + : window; } - - $_.sel = $(sel); + + $_.el = sel; return $_; }; @@ -71,6 +77,7 @@ x = document.querySelectorAll(a); } + //Return the single object if applicable return (x.length === 1) ? x[0] : x; }; @@ -78,14 +85,15 @@ $_.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 - callback(sel); + return; } - else + + if(typeof sel.length !== "undefined") { var len = sel.length; @@ -106,6 +114,10 @@ callback(selx); } } + else + { + callback(sel); + } }; /** @@ -416,7 +428,7 @@ var i, len, selx; //Get the DOM object - sel = $_.sel; + var sel = $_.el if(arguments.length === 4) { @@ -531,9 +543,14 @@ } else if (value === null) { - (doAttr === true) - ? sel.removeAttribute(name) - : sel[name] = null; + if(doAttr === true) + { + sel.removeAttribute(name); + } + else + { + delete sel[name]; + } } return (typeof value !== "undefined") ? value : oldVal; @@ -621,7 +638,7 @@ } - function _css(prop, val) + function _css(sel, prop, val) { var equi; @@ -638,11 +655,12 @@ //Let have an object with equivalent properties //for `special` browsers, and other quirks + + //Todo: get more common properties equi = { - top: "", - right: "", - bottom: "", - left: "" + outerHeight: "offsetHeight", + outerWidth: "offsetWidth", + top: "posTop" }; if(sel.style[equi[prop]]) @@ -658,7 +676,7 @@ d = { addClass: function (c) { - var sel = $_.sel; + var sel = $_.el $_.each(function (e){ _class(e, c, true); @@ -666,7 +684,7 @@ }, removeClass: function (c) { - var sel = $_.sel; + var sel = $_.el $_.each(function (e){ _class(e, c, false); @@ -674,46 +692,20 @@ }, hide: function () { - var sel = $_.sel; - - if (sel.length > 1) - { - $_.each(function (e){ - e.style.display = "none"; - }); - } - else - { - if (sel.style) - { - sel.style.display = "none"; - } - } - + this.css('display', 'none'); }, show: function (type) { - var sel = $_.sel; - if (typeof type === "undefined") { type = "block"; } - if (sel.length > 1) - { - $_.each(function (e){ - e.style.display = type; - }); - } - else - { - sel.style.display = type; - } + this.css("display", type); }, attr: function (name, value) { - var sel = $_.sel; + var sel = $_.el //Make sure you don't try to get a bunch of elements if (sel.length > 1 && typeof value === "undefined") @@ -737,7 +729,7 @@ { var oldValue, set, type, sel; - sel = $_.sel; + var sel = $_.el set = (typeof value !== "undefined") ? true : false;