From 02947a11922a11fe4f8bb3d1a7ccc79e5c367e4f Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Thu, 7 Jul 2011 14:38:16 -0700 Subject: [PATCH 1/2] Converted to constructor selector function. Made 'each' function global --- kis.js | 209 +++++++++++++++++++++++++++++---------------------------- 1 file changed, 107 insertions(+), 102 deletions(-) diff --git a/kis.js b/kis.js index f536042..6ef0a68 100644 --- a/kis.js +++ b/kis.js @@ -2,7 +2,7 @@ Kis JS Keep It Simple JS Library Copyright Timothy J. Warren License Public Domain - Version 0.1.1 + Version 0.2.0 */ (function (){ @@ -10,7 +10,7 @@ "use strict"; // Property name for expandos on DOM objects - var kis_expando = "KIS_0_1_0"; + var kis_expando = "KIS_0_2_0"; //Browser requirements check if (!document.querySelectorAll) @@ -18,17 +18,33 @@ return; } - var $_, $; + var $_, $, sel; - $_ = {}; + /** + * $_ + * + * Constructor function + */ + $_ = function(sel) + { + if(typeof sel === "undefined") + { + sel = window; + } + + $_.sel = $(sel); + + return $_; + }; $_ = window.$_ = window.$_ || $_; + //console.log polyfill if(typeof window.console === "undefined") { window.console = { log:function(){} - } + }; } /** @@ -59,6 +75,38 @@ }; window.$ = $; + + $_.each = function (callback) + { + sel = $_.sel; + + if(!sel.length) + { + // sel is a DOM Element + callback(sel); + } + else + { + var len = sel.length; + + if (len === 0) + { + return; + } + + if (len === 1) + { + return callback(sel); + } + + var selx; + for (var x = 0; x < sel.length; x++) + { + selx = (sel.item(x)) ? sel.item(x) : sel[x]; + callback(selx); + } + } + }; /** * Ajax @@ -313,14 +361,12 @@ function listener () { // Internet Explorer fails to correctly set the 'this' object // for event listeners, so we need to set it ourselves. - callback.apply(sel, arguments); + callback.apply(arguments); } - var i, len; - if (typeof sel.attachEvent !== "undefined") { - remove(sel, event, callback); // Make sure we don't have duplicate listeners + remove(event, callback); // Make sure we don't have duplicate listeners sel.attachEvent("on" + event, listener); // Store our listener so we can remove it later @@ -346,7 +392,7 @@ && expando.listeners[event]) { var listeners = expando.listeners[event]; - var len = listeners.length + var len = listeners.length; for (var i=0; i 1) - { - len = sel.length; - for (i = 0; i < len; i++) - { - selx = (sel.item(i)) ? sel.item(i) : sel[i]; - - //Call recursively - add_remove(selx, event, callback, add); - } - } - else - { + //Go over additonal DOM objects as needed + $_.each(function(e){ + (add === true) - ? attach(sel, event, callback) - : remove(sel, event, callback); - } + ? attach(e, event, callback) + : remove(e, event, callback); + + }); }; e = { - add: function (sel, event, callback) + add: function (event, callback) { - add_remove(sel, event, callback, true); + add_remove(event, callback, true); }, - remove: function (sel, event, callback) + remove: function (event, callback) { - add_remove(sel, event, callback, false); + add_remove(event, callback, false); } }; @@ -438,16 +484,6 @@ function _attr(sel, name, value) { var oldVal, doAttr, i; - - if(sel.length > 1) - { - var len = sel.length; - for(i=0;i 1) { - this.each(sel, function (e){ + $_.each(function (e){ e.style.display = "none"; }); } @@ -686,9 +691,9 @@ } }, - show: function (sel, type) + show: function (type) { - sel = $(sel); + var sel = $_.sel; if (typeof type === "undefined") { @@ -697,7 +702,7 @@ if (sel.length > 1) { - this.each(sel, function (e){ + $_.each(function (e){ e.style.display = type; }); } @@ -706,9 +711,9 @@ sel.style.display = type; } }, - attr: function (sel, name, value) + attr: function (name, value) { - sel = $(sel); + var sel = $_.sel; //Make sure you don't try to get a bunch of elements if (sel.length > 1 && typeof value === "undefined") @@ -719,7 +724,7 @@ } else if (sel.length > 1 && typeof value !== "undefined") //You can set a bunch, though { - this.each(sel, function (e){ + $_.each(function (e){ _attr(e, name, value); }); } @@ -728,11 +733,11 @@ return _attr(sel, name, value); } }, - text: function(sel, value) + text: function (value) { - var oldValue, set, type; + var oldValue, set, type, sel; - sel = $(sel); + sel = $_.sel; set = (typeof value !== "undefined") ? true : false; @@ -754,9 +759,9 @@ return oldValue; } }, - css: function(sel, prop, val) + css: function (prop, val) { - this.each(sel, function (e){ + $_.each(function (e){ _css(e, prop, val); }); } From 167f5fafdf19551c43a302a73b6c52de1ee2de64 Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Thu, 7 Jul 2011 14:39:43 -0700 Subject: [PATCH 2/2] Converted to constructor selector function. Made 'each' function global --- README.md | 50 ++++++++++++++++++++++++++++---------------------- 1 file changed, 28 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index ba3169c..e900a0a 100644 --- a/README.md +++ b/README.md @@ -8,11 +8,25 @@ Browser support: IE8+, Latest versions of Firefox, Chrome, Safari, Opera ## Basic Use: ## -* Selector: `var x = $(selector);` - -* Function: `$_.module.function(params);` +* Function: `$_(selector).module.function(params);` ## Modules: ## +**Global**: Core functions + + functions: + + * $: + Use: + $(selector); + + *each: For applying changes to every item matched by a selector + Use: + $_(selector).dom.each(callback); + + Example : $_(".foo").dom.each(function(e){ + $_(e).dom.text(value); + }): + **Ajax**: simple, jQuery-like ajax functions @@ -65,11 +79,11 @@ Browser support: IE8+, Latest versions of Firefox, Chrome, Safari, Opera *Add: Use: - $_.event.add(selector, event, callback); + $_(selector).event.add(event, callback); *Remove Use: - $_.event.remove(selector, event, callback); + $_(selector).event.remove(event, callback); **DOM**: Dom manipulation module @@ -77,38 +91,30 @@ Browser support: IE8+, Latest versions of Firefox, Chrome, Safari, Opera *addClass: Use: - $_.dom.addClass(selector, className); + $_(selector).dom.addClass(className); *RemoveClass: Use: - $_.dom.removeClass(selector, className); - - *each: For applying changes to every item matched by a selector - Use: - $_.dom.each(selector, callback); - - Example : $_.dom.each(".foo", function(e){ - $_.dom.text(e, value); - }): + $_(selector).dom.removeClass(className); *show: For setting dom elements as visible. Type defaults as "block", can be set with optional second parameter. Use: - $_.dom.show(selector, [type]); + $_(selector).dom.show([type]); *hide: Hides the elements matching the selector Use: - $_.dom.hide(selector); + $_(selector).dom.hide(); *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); + Set: $_(selector).dom.attr(attributeName, attributeValue); + Get: $_(selector).dom.attr(attributeName); + Remove: $_(selector).dom.attr(attributeName, null); *text: Gets or sets the text in between an element's tags Use: - Set: $_.dom.text(selector, text); - Get: $_.dom.text(selector); + Set: $_(selector).dom.text(text); + Get: $_(selector).dom.text();