Updated to match local repo...DOM.children almost works

This commit is contained in:
Timothy Warren 2011-08-10 16:30:36 -04:00
parent 980410b901
commit 5529d98054
10 changed files with 237 additions and 106 deletions

View File

@ -33,11 +33,14 @@ Browser support: IE8+, Latest versions of Firefox, Chrome, Safari, Opera
}): }):
* ext: For extending the library, adds this.el to the object or function supplied * ext: For extending the library, adds this.el to the object or function supplied
Use: $_.ext("name", functionOrObject) Use: $_.ext("name", functionOrObject);
Example: $_.ext("zip", function(){ //function }); Example: $_.ext("zip", function(){ //function });
Adds 'zip' function to $_. Adds 'zip' function to $_.
* type: For getting the type of a variable
Use: $_.type(var);
### Ajax: simple, jQuery-like ajax functions ### ### Ajax: simple, jQuery-like ajax functions ###

View File

@ -77,5 +77,16 @@ $new_file .= "\n}());";
//Output the full file //Output the full file
file_put_contents("kis-custom.js", $new_file); file_put_contents("kis-custom.js", $new_file);
//Get a much-minified version from Google's closure compiler
$ch = curl_init('http://closure-compiler.appspot.com/compile');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'output_info=compiled_code&output_format=text&compilation_level=SIMPLE_OPTIMIZATIONS&js_code=' . urlencode($new_file));
$output = curl_exec($ch);
curl_close($ch);
file_put_contents("kis-min.js", $output);
//Display the output on-screen too //Display the output on-screen too
echo '<pre>'.htmlspecialchars($new_file).'</pre>'; echo '<pre>'.htmlspecialchars($new_file).'</pre>';

View File

@ -52,7 +52,7 @@
$_ = function(s) $_ = function(s)
{ {
//Have documentElement be default selector, just in case //Have documentElement be default selector, just in case
if(typeof s == "undefined") if(typeof s === "undefined")
{ {
sel = (typeof $_.el !== "undefined") sel = (typeof $_.el !== "undefined")
? $_.el ? $_.el
@ -60,7 +60,7 @@
} }
else else
{ {
sel = $(s); sel = (typeof s !== "object") ? $(s) : s;
} }
// Make a copy before adding properties // Make a copy before adding properties
@ -115,8 +115,8 @@
//Function to add to $_ object, and get sel //Function to add to $_ object, and get sel
$_.ext = function(name, obj) $_.ext = function(name, obj)
{ {
$_[name] = obj;
obj.el = sel; obj.el = sel;
$_[name] = obj;
}; };
//Selector iteration //Selector iteration
@ -144,6 +144,18 @@
} }
}); });
//Type retriever
$_.type = function(obj)
{
if((function() {return obj && (obj !== this)}).call(obj))
{
//fallback on 'typeof' for truthy primitive values
return (typeof obj).toLowerCase();
}
return ({}).toString.call(obj).match(/\s([a-z|A-Z]+)/)[1].toLowerCase();
}
//Set global variables //Set global variables
$_ = window.$_ = window.$_ || $_; $_ = window.$_ = window.$_ || $_;
$_.$ = $; $_.$ = $;
@ -341,10 +353,9 @@
* *
*/ */
(function (){ (function (){
var d, tag_reg, id_reg, class_reg; var d, tag_reg, class_reg;
tag_reg = /^([\w\-]+)$/; tag_reg = /^([\w\-]+)$/;
id_reg = /#([\w\-]+$)/;
class_reg = /\.([\w\-]+)$/; class_reg = /\.([\w\-]+)$/;
@ -436,7 +447,7 @@
outerHeight: "offsetHeight", outerHeight: "offsetHeight",
outerWidth: "offsetWidth", outerWidth: "offsetWidth",
top: "posTop" top: "posTop"
} };
//If you don't define a value, try returning the existing value //If you don't define a value, try returning the existing value
@ -473,17 +484,12 @@
len = curr_sel.length, len = curr_sel.length,
matches = []; matches = [];
if(typeof filter !== "string")
{
return filter;
}
//Filter by tag //Filter by tag
if(filter.match(tag_reg)) if(filter.match(tag_reg))
{ {
for(i=0;i<len;i++) for(i=0;i<len;i++)
{ {
if(curr_sell[i].tagName.toLowerCase() == filter.toLowerCase()) if(curr_sel[i].tagName.toLowerCase() == filter.toLowerCase())
{ {
matches.push(curr_sel[i]); matches.push(curr_sel[i]);
} }
@ -491,6 +497,9 @@
} }
else if(filter.match(class_reg)) else if(filter.match(class_reg))
{ {
//Remove the .
filter = filter.replace(".", "");
for(i=0;i<len;i++) for(i=0;i<len;i++)
{ {
if(curr_sel[i].classList.contains(filter)) if(curr_sel[i].classList.contains(filter))
@ -499,32 +508,15 @@
} }
} }
} }
else if(filter.match(id_reg))
{
return document.getElementById(filter);
}
else else
{ {
console.log(filter+" is not a valid filter"); console.log(filter+" is not a valid filter");
} }
return (matches.length === 1) ? matches[0] : matches; return (matches.length == 1) ? matches[0] : matches;
} }
function _set_sel(sel)
{
for(var i in $_)
{
if(typeof $_[i] === "object" || typeof $_[i] === "function")
{
$_[i].el = sel;
}
}
return $_;
}
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
d = { d = {
@ -615,22 +607,27 @@
}, },
children: function(filter) children: function(filter)
{ {
var sel; //Return the children directly, if there is no filter
if(typeof filter === "undefined")
if(typeof sel === "undefined")
{ {
sel = this.el.children; return $_(this.el.children);
}
var childs = (typeof this.el.children !== "undefined") ? this.el.children : this.el;
if($_.type(filter) !== "string")
{
return $_(filter);
}
else if(filter.match(/#([\w\-]+$)/))
{
return $_($_.$(filter));
} }
else else
{ {
sel = _sel_filter(filter, this.el.children); var filtered = _sel_filter(filter, childs);
return $_(filtered);
} }
//Update the $_ object to reflect the new selector
$_ = _set_sel(sel);
//Return the $_ object for chaining
return $_;
} }
}; };
@ -798,7 +795,7 @@
var type = (isPost) ? "POST" : "GET"; var type = (isPost) ? "POST" : "GET";
url += (type === "GET") ? "?"+this._serialize(data, true) : ''; url += (type === "GET") ? "?"+this._serialize(data) : '';
request.open(type, url); request.open(type, url);
@ -820,7 +817,7 @@
request.send(null); request.send(null);
} }
}, },
_serialize: function (data, encode) _serialize: function (data)
{ {
var pairs = []; var pairs = [];
@ -837,11 +834,8 @@
var value = data[name].toString(); var value = data[name].toString();
if (encode === true) name = encodeURIComponent(name);
{ value = encodeURIComponent(value);
name = encodeURIComponent(name.replace(" ", "+"));
value = encodeURIComponent(value.replace(" ", "+"));
}
pairs.push(name + "=" + value); pairs.push(name + "=" + value);
} }
@ -871,7 +865,7 @@
// Property name for expandos on DOM objects // Property name for expandos on DOM objects
var kis_expando = "KIS_0_3_0"; var kis_expando = "KIS_0_3_0";
var attach, remove, add_remove, e; var attach, remove, add_remove, e, attach_delegate, attach_live;
// Define the proper attach and remove functions // Define the proper attach and remove functions
// based on browser support // based on browser support
@ -987,6 +981,27 @@
} }
}; };
attach_delegate = function(sel, target, event, callback)
{
//Attach the listener to the parent object
add_remove(sel, event, function(e){
//Get the live version of the target selector
sel = $_.$(sel);
//todo: fire target callback when event bubbles from target
}, true);
};
attach_live = function(target, event, callback)
{
attach_delegate(document.documentElement, target, event, callback);
};
// --------------------------------------------------------------------------
e = { e = {
add: function (event, callback) add: function (event, callback)
{ {
@ -999,6 +1014,18 @@
$_.each(function(e){ $_.each(function(e){
add_remove(e, event, callback, false); add_remove(e, event, callback, false);
}); });
},
live: function (event, callback)
{
$_.each(function(e){
attach_live(e, event, callback);
});
},
delegate: function(target, event, callback)
{
$_.each(function(e){
attach_delegate(e, target, event, callback);
});
} }
}; };

17
kis-min.js vendored Normal file
View File

@ -0,0 +1,17 @@
(function(){if(document.querySelectorAll){var e,c,g,f;c=function(a){if(typeof a!=="string"||typeof a==="undefined")return a;if(a.match(/^#([\w\-]+$)/))return document.getElementById(a.split("#")[1]);else a=a.match(/^([\w\-]+)$/)?document.getElementsByTagName(a):document.querySelectorAll(a);return a.length===1?a[0]:a};e=function(a){f=typeof a==="undefined"?typeof e.el!=="undefined"?e.el:document.documentElement:typeof a!=="object"?c(a):a;var a=g(e),h;for(h in a)if(typeof a[h]==="object")a[h].el=f;
a.el=f;return a};g=function(a){var c;if(typeof a!=="undefined"){if(typeof Object.create!=="undefined")return Object.create(a);c=typeof a;if(!(c!=="object"&&c!=="function"))return c=function(){},c.prototype=a,new c}};e.ext=function(a,c){c.el=f;e[a]=c};e.ext("each",function(a){if(typeof f.length!=="undefined"&&f!==window){var c=f.length;if(c!==0)for(var d,b=0;b<c;b++)d=f.item(b)?f.item(b):f[b],a(d)}else a(f)});e.type=function(a){return function(){return a&&a!==this}.call(a)?(typeof a).toLowerCase():
{}.toString.call(a).match(/\s([a-z|A-Z]+)/)[1].toLowerCase()};e=window.$_=window.$_||e;e.$=c;if(typeof window.console==="undefined")window.console={log:function(){}};if(typeof String.prototype.trim==="undefined")String.prototype.trim=function(){return this.replace(/^\s+|\s+$/g,"")}}})();
(function(){var e=e||window.$_;typeof document!=="undefined"&&!("classList"in document.createElement("a"))&&function(c){var c=(c.HTMLElement||c.Element).prototype,g=Object,f=String.prototype.trim||function(){return this.replace(/^\s+|\s+$/g,"")},a=Array.prototype.indexOf||function(d){for(var b=0,a=this.length;b<a;b++)if(b in this&&this[b]===d)return b;return-1},e=function(d,b){this.name=d;this.code=DOMException[d];this.message=b},d=function(d,b){if(b==="")throw new e("SYNTAX_ERR","An invalid or illegal string was specified");
if(/\s/.test(b))throw new e("INVALID_CHARACTER_ERR","String contains an invalid character");return a.call(d,b)},b=function(d){for(var b=f.call(d.className),b=b?b.split(/\s+/):[],a=0,i=b.length;a<i;a++)this.push(b[a]);this._updateClassName=function(){d.className=this.toString()}},i=b.prototype=[],l=function(){return new b(this)};e.prototype=Error.prototype;i.item=function(d){return this[d]||null};i.contains=function(b){b+="";return d(this,b)!==-1};i.add=function(b){b+="";d(this,b)===-1&&(this.push(b),
this._updateClassName())};i.remove=function(b){b+="";b=d(this,b);b!==-1&&(this.splice(b,1),this._updateClassName())};i.toggle=function(b){b+="";d(this,b)===-1?this.add(b):this.remove(b)};i.toString=function(){return this.join(" ")};if(g.defineProperty){i={get:l,enumerable:!0,configurable:!0};try{g.defineProperty(c,"classList",i)}catch(j){if(j.number===-2146823252)i.enumerable=!1,g.defineProperty(c,"classList",i)}}else g.prototype.__defineGetter__&&c.__defineGetter__("classList",l)}(self);(function(){function c(d,
b,a){var c,f;if(typeof d.hasAttribute!=="undefined")d.hasAttribute(b)&&(c=d.getAttribute(b)),f=!0;else if(typeof d[b]!=="undefined")c=d[b],f=!1;else if(b==="class"&&typeof d.className!=="undefined")b="className",c=d.className,f=!1;if(typeof c==="undefined"&&(typeof a==="undefined"||a===null))console.log(a),console.log(d),console.log("Element does not have the selected attribute");else{if(typeof a==="undefined")return c;typeof a!=="undefined"&&a!==null?f===!0?d.setAttribute(b,a):d[b]=a:a===null&&(f===
!0?d.removeAttribute(b):delete d[b]);return typeof a!=="undefined"?a:c}}function g(d){return d.replace(/(\-[a-z])/g,function(b){return b.toUpperCase().replace("-","")})}function f(d,b,a){var c,b=g(b);c={outerHeight:"offsetHeight",outerWidth:"offsetWidth",top:"posTop"};if(typeof a==="undefined"&&d.style[b]!=="undefined")return d.style[b];else if(typeof a==="undefined"&&d.style[c[b]]!=="undefined")return d.style[c[b]];typeof d.style[b]!=="undefined"?d.style[b]=a:d.style[c[b]]?d.style[c[b]]=a:console.log("Property "+
b+" nor an equivalent seems to exist")}var a,h;a=/^([\w\-]+)$/;h=/\.([\w\-]+)$/;e.ext("dom",{addClass:function(d){e.each(function(b){b.classList.add(d)})},removeClass:function(d){e.each(function(b){b.classList.remove(d)})},hide:function(){this.css("display","none")},show:function(d){typeof d==="undefined"&&(d="block");this.css("display",d)},attr:function(d,b){var a=this.el;if(a.length>1&&typeof b==="undefined")console.log(a),console.log("Must be a singular element");else if(a.length>1&&typeof b!==
"undefined")e.each(function(a){return c(a,d,b)});else return c(a,d,b)},text:function(d){var b,a,c;c=this.el;a=typeof c.innerText!=="undefined"?"innerText":typeof c.textContent!=="undefined"?"textContent":"innerHTML";b=c[a];return typeof d!=="undefined"?c[a]=d:b},css:function(d,b){if(typeof b==="undefined")return f(this.el,d);e.each(function(a){f(a,d,b)})},children:function(d){if(typeof d==="undefined")return e(this.el.children);var b=typeof this.el.children!=="undefined"?this.el.children:this.el;
if(e.type(d)!=="string")return e(d);else if(d.match(/#([\w\-]+$)/))return e(e.$(d));else{var c,f=b.length,g=[];if(d.match(a))for(c=0;c<f;c++)b[c].tagName.toLowerCase()==d.toLowerCase()&&g.push(b[c]);else if(d.match(h)){d=d.replace(".","");for(c=0;c<f;c++)b[c].classList.contains(d)&&g.push(b[c])}else console.log(d+" is not a valid filter");return e(g.length==1?g[0]:g)}}})})();(function(){e.ext("store",{get:function(c){return JSON.parse(localStorage.getItem(c))},set:function(c,g){typeof g!=="string"&&
(g=JSON.stringify(g));localStorage.setItem(c,g)},getAll:function(){var c,g,f;g=localStorage.length;f={};for(c=0;c<g;c++){var a=localStorage.key(c),e=localStorage.getItem(a);f[a]=e}return f}})})();(function(){e.hb=history.pushState?!1:!0;e.ext("qs",{parse:function(c){var c=c||e.hb,g,f,a,h;a={};if(c===!0)c=location.hash.split("#!/"),c=c.length>1?c[1]:"";else if(c===!1||c===void 0)c=window.location.search.substring(1);else return!1;g=c.split("&");f=g.length;for(c=0;c<f;c++){h=g[c].split("=");if(h.length<
2)break;a[h[0]]=h[1]}return a},set:function(c,g,f){var f=f||e.hb,a=this.parse(f);c!==void 0&&g!==void 0&&(a[c]=g);var c=[],h;for(h in a)a.hasOwnProperty(h)&&c.push(h+"="+a[h]);a=c.join("&");if(f===!0)a="!/"+a,location.hash=a;return a},get:function(c,g){var g=g||e.hb,f=this.parse(g);return f[c]?f[c]:""}})})();(function(){var c={_do:function(c,f,a,e){typeof a==="undefined"&&(a=function(){});var d=typeof window.XMLHttpRequest!=="undefined"?new XMLHttpRequest:!1,e=e?"POST":"GET";c+=e==="GET"?"?"+this._serialize(f):
"";d.open(e,c);d.onreadystatechange=function(){d.readyState===4&&a(d.responseText)};e==="POST"?(d.setRequestHeader("Content-Type","application/x-www-form-urlencoded"),d.send(this._serialize(f))):d.send(null)},_serialize:function(c){var f=[],a;for(a in c)if(c.hasOwnProperty(a)&&typeof c[a]!=="function"){var e=c[a].toString();a=encodeURIComponent(a);e=encodeURIComponent(e);f.push(a+"="+e)}return f.join("&")}};e.ext("get",function(e,f,a){c._do(e,f,a,!1)});e.ext("post",function(e,f,a){c._do(e,f,a,!0)})})();
(function(){var c,g,f,a,h;typeof document.addEventListener!=="undefined"?(c=function(d,b,a){typeof d.addEventListener!=="undefined"&&d.addEventListener(b,a,!1)},g=function(d,b,a){typeof d.removeEventListener!=="undefined"&&d.removeEventListener(b,a,!1)}):typeof document.attachEvent!=="undefined"&&(c=function(d,b,a){var i;function c(){a.apply(arguments)}typeof d.attachEvent!=="undefined"?(g(b,a),d.attachEvent("on"+b,c),i=d.KIS_0_3_0=d.KIS_0_3_0||{},d=i,d.listeners=d.listeners||{},d.listeners[b]=d.listeners[b]||
[],d.listeners[b].push({callback:a,listener:c})):console.log("Failed to attach event:"+b+" on "+d)},g=function(a,b,c){if(typeof a.detachEvent!=="undefined"){var e=a.KIS_0_3_0;if(e&&e.listeners&&e.listeners[b])for(var f=e.listeners[b],g=f.length,h=0;h<g;h++)if(f[h].callback===c){a.detachEvent("on"+b,f[h].listener);f.splice(h,1);f.length===0&&delete e.listeners[b];break}}});f=function(a,b,e,h){var j,k;if(typeof a==="undefined")return console.log(arguments),console.log(b),!1;if(b.match(/^([\w\-]+)$/))h===
!0?c(a,b,e):g(a,b,e);else{b=b.split(" ");k=b.length;for(j=0;j<k;j++)f(a,b[j],e,h)}};a=function(a,b,c){f(a,c,function(){a=e.$(a)},!0)};h=function(c,b,e){a(document.documentElement,c,b,e)};e.ext("event",{add:function(a,b){e.each(function(c){f(c,a,b,!0)})},remove:function(a,b){e.each(function(c){f(c,a,b,!1)})},live:function(a,b){e.each(function(c){h(c,a,b)})},delegate:function(c,b,f){e.each(function(e){a(e,c,b,f)})}})})()})();

View File

@ -159,10 +159,9 @@ if (typeof document !== "undefined" && !("classList" in document.createElement("
* *
*/ */
(function (){ (function (){
var d, tag_reg, id_reg, class_reg; var d, tag_reg, class_reg;
tag_reg = /^([\w\-]+)$/; tag_reg = /^([\w\-]+)$/;
id_reg = /#([\w\-]+$)/;
class_reg = /\.([\w\-]+)$/; class_reg = /\.([\w\-]+)$/;
@ -254,7 +253,7 @@ if (typeof document !== "undefined" && !("classList" in document.createElement("
outerHeight: "offsetHeight", outerHeight: "offsetHeight",
outerWidth: "offsetWidth", outerWidth: "offsetWidth",
top: "posTop" top: "posTop"
} };
//If you don't define a value, try returning the existing value //If you don't define a value, try returning the existing value
@ -291,17 +290,12 @@ if (typeof document !== "undefined" && !("classList" in document.createElement("
len = curr_sel.length, len = curr_sel.length,
matches = []; matches = [];
if(typeof filter !== "string")
{
return filter;
}
//Filter by tag //Filter by tag
if(filter.match(tag_reg)) if(filter.match(tag_reg))
{ {
for(i=0;i<len;i++) for(i=0;i<len;i++)
{ {
if(curr_sell[i].tagName.toLowerCase() == filter.toLowerCase()) if(curr_sel[i].tagName.toLowerCase() == filter.toLowerCase())
{ {
matches.push(curr_sel[i]); matches.push(curr_sel[i]);
} }
@ -309,6 +303,9 @@ if (typeof document !== "undefined" && !("classList" in document.createElement("
} }
else if(filter.match(class_reg)) else if(filter.match(class_reg))
{ {
//Remove the .
filter = filter.replace(".", "");
for(i=0;i<len;i++) for(i=0;i<len;i++)
{ {
if(curr_sel[i].classList.contains(filter)) if(curr_sel[i].classList.contains(filter))
@ -317,32 +314,15 @@ if (typeof document !== "undefined" && !("classList" in document.createElement("
} }
} }
} }
else if(filter.match(id_reg))
{
return document.getElementById(filter);
}
else else
{ {
console.log(filter+" is not a valid filter"); console.log(filter+" is not a valid filter");
} }
return (matches.length === 1) ? matches[0] : matches; return (matches.length == 1) ? matches[0] : matches;
} }
function _set_sel(sel)
{
for(var i in $_)
{
if(typeof $_[i] === "object" || typeof $_[i] === "function")
{
$_[i].el = sel;
}
}
return $_;
}
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
d = { d = {
@ -433,22 +413,27 @@ if (typeof document !== "undefined" && !("classList" in document.createElement("
}, },
children: function(filter) children: function(filter)
{ {
var sel; //Return the children directly, if there is no filter
if(typeof filter === "undefined")
if(typeof sel === "undefined")
{ {
sel = this.el.children; return $_(this.el.children);
}
var childs = (typeof this.el.children !== "undefined") ? this.el.children : this.el;
if($_.type(filter) !== "string")
{
return $_(filter);
}
else if(filter.match(/#([\w\-]+$)/))
{
return $_($_.$(filter));
} }
else else
{ {
sel = _sel_filter(filter, this.el.children); var filtered = _sel_filter(filter, childs);
return $_(filtered);
} }
//Update the $_ object to reflect the new selector
$_ = _set_sel(sel);
//Return the $_ object for chaining
return $_;
} }
}; };

View File

@ -19,7 +19,7 @@
var type = (isPost) ? "POST" : "GET"; var type = (isPost) ? "POST" : "GET";
url += (type === "GET") ? "?"+this._serialize(data, true) : ''; url += (type === "GET") ? "?"+this._serialize(data) : '';
request.open(type, url); request.open(type, url);
@ -41,7 +41,7 @@
request.send(null); request.send(null);
} }
}, },
_serialize: function (data, encode) _serialize: function (data)
{ {
var pairs = []; var pairs = [];
@ -58,11 +58,8 @@
var value = data[name].toString(); var value = data[name].toString();
if (encode === true) name = encodeURIComponent(name);
{ value = encodeURIComponent(value);
name = encodeURIComponent(name.replace(" ", "+"));
value = encodeURIComponent(value.replace(" ", "+"));
}
pairs.push(name + "=" + value); pairs.push(name + "=" + value);
} }

View File

@ -52,7 +52,7 @@
$_ = function(s) $_ = function(s)
{ {
//Have documentElement be default selector, just in case //Have documentElement be default selector, just in case
if(typeof s == "undefined") if(typeof s === "undefined")
{ {
sel = (typeof $_.el !== "undefined") sel = (typeof $_.el !== "undefined")
? $_.el ? $_.el
@ -60,7 +60,7 @@
} }
else else
{ {
sel = $(s); sel = (typeof s !== "object") ? $(s) : s;
} }
// Make a copy before adding properties // Make a copy before adding properties
@ -115,8 +115,8 @@
//Function to add to $_ object, and get sel //Function to add to $_ object, and get sel
$_.ext = function(name, obj) $_.ext = function(name, obj)
{ {
$_[name] = obj;
obj.el = sel; obj.el = sel;
$_[name] = obj;
}; };
//Selector iteration //Selector iteration
@ -144,6 +144,18 @@
} }
}); });
//Type retriever
$_.type = function(obj)
{
if((function() {return obj && (obj !== this)}).call(obj))
{
//fallback on 'typeof' for truthy primitive values
return (typeof obj).toLowerCase();
}
return ({}).toString.call(obj).match(/\s([a-z|A-Z]+)/)[1].toLowerCase();
}
//Set global variables //Set global variables
$_ = window.$_ = window.$_ || $_; $_ = window.$_ = window.$_ || $_;
$_.$ = $; $_.$ = $;

View File

@ -8,7 +8,7 @@
// Property name for expandos on DOM objects // Property name for expandos on DOM objects
var kis_expando = "KIS_0_3_0"; var kis_expando = "KIS_0_3_0";
var attach, remove, add_remove, e; var attach, remove, add_remove, e, attach_delegate, attach_live;
// Define the proper attach and remove functions // Define the proper attach and remove functions
// based on browser support // based on browser support
@ -124,6 +124,27 @@
} }
}; };
attach_delegate = function(sel, target, event, callback)
{
//Attach the listener to the parent object
add_remove(sel, event, function(e){
//Get the live version of the target selector
sel = $_.$(sel);
//todo: fire target callback when event bubbles from target
}, true);
};
attach_live = function(target, event, callback)
{
attach_delegate(document.documentElement, target, event, callback);
};
// --------------------------------------------------------------------------
e = { e = {
add: function (event, callback) add: function (event, callback)
{ {
@ -136,6 +157,18 @@
$_.each(function(e){ $_.each(function(e){
add_remove(e, event, callback, false); add_remove(e, event, callback, false);
}); });
},
live: function (event, callback)
{
$_.each(function(e){
attach_live(e, event, callback);
});
},
delegate: function(target, event, callback)
{
$_.each(function(e){
attach_delegate(e, target, event, callback);
});
} }
}; };

32
tests/index.html Normal file
View File

@ -0,0 +1,32 @@
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<link rel="stylesheet" href="qunit/qunit.css" type="text/css" />
<title>Kis-js test app</title>
</head>
<body>
<h1 id="qunit-header">Kis-js Test Suite</h1>
<h2 id="qunit-banner"></h2>
<div id="qunit-testrunner-toolbar"></div>
<h2 id="qunit-userAgent"></h2>
<ol id="qunit-tests"></ol>
<div id="qunit-fixture">
test markup, will be hidden
<span id="testSpan"></span>
</div>
<section hidden="hidden">
<article id="r14">
This is important text!
</article>
<aside id="classChild">
<p class="child"></p>
<span class="child"></span>
<div class="nephew"></div>
</aside>
</section>
<script src="../kis-custom.js"></script>
<script src="qunit/qunit.js"></script>
<script src="./tests.js"></script>
</body>
</html>

View File

@ -1,5 +1,5 @@
(function(){ (function(){
"use strict"; //"use strict";
//Selector test function //Selector test function
function $(a) function $(a)
@ -22,6 +22,16 @@
strictEqual(typeof $_(), "object"); strictEqual(typeof $_(), "object");
}); });
test("Type Checking", function(){
equal($_.type(5), "number", "Number type");
equal($_.type("abc"), "string", "String type");
equal($_.type({}), "object", "Object type");
equal($_.type([0,1,2]), "array", "Array type");
equal($_.type(/x/), "regexp", "Regex type");
equal($_.type(function(){}), "function", "Function type");
equal($_.type(true), "boolean", "Boolean type");
});
test("Unique Selectors", function(){ test("Unique Selectors", function(){
var x = $_("ol"); var x = $_("ol");
var y = $_("aside"); var y = $_("aside");
@ -116,7 +126,7 @@
test("Show/Hide", function(){ test("Show/Hide", function(){
expect(3); expect(3);
var $test = $_("#classChild .child"); var $test = $_("#classChild .nephew");
var ele = $test.el; var ele = $test.el;
$test.dom.hide(); $test.dom.hide();
@ -132,14 +142,15 @@
}); });
test("Text", function(){ test("Text", function(){
expect(2); expect(3);
var $test = $_("article#r14"); var $test = $_("article#r14");
var ele = $test.el; var ele = $test.el;
var text = (typeof ele.innerText !== "undefined") ? ele.innerText : ele.textContent; var text = (typeof ele.innerText !== "undefined") ? ele.innerText : ele.textContent;
equal($test.el, $("article#r14"), "Selector property is correct"); equal($test.el, $("article#r14"), "Selector property is correct");
equal($test.dom.text(), text, "Getting test"); equal($test.dom.text(), text, "Getting text");
equal($test.dom.text(""), "", "Setting text");
}); });
test("Attr", function(){ test("Attr", function(){
@ -168,10 +179,13 @@
test("Children", function(){ test("Children", function(){
var $test = $_("section"); var $test = $_("section");
var ele = $test.el; var ele = $("section");
var ele2 = $_("section aside").el;
equal($test.dom.children().el, ele.children, "Returns children without parameters"); equal($_("section").dom.children().el, ele.children, "Returns children without parameters");
equal($test.dom.children('#r14').el, document.getElementById('r14'), "Finds id"); equal($_("section").dom.children('#r14').el, document.getElementById('r14'), "Finds id");
equal($_("section").dom.children("aside").el, ele2, "Finds children by tag name");
equal($_("section aside").dom.children(".child").el, $_("#classChild .child").el, "Finds children by class");
}); });
}()); }());