Updated Events Module, and modularized tests

This commit is contained in:
Timothy Warren 2011-11-01 08:29:21 -04:00
parent d1f1af40ae
commit 8969cd31cb
11 changed files with 380 additions and 376 deletions

View File

@ -25,10 +25,10 @@
{ {
var x, c; var x, c;
if (typeof a !== "string" || typeof a === "undefined"){ return a;} if (typeof a != "string" || typeof a === "undefined"){ return a;}
//Check for a context of a specific element, otherwise, just run on the document //Check for a context of a specific element, otherwise, just run on the document
c = (typeof context === 'object' && context.nodeType === 1) c = (context != null && context.nodeType === 1)
? context ? context
: document; : document;
@ -1102,17 +1102,12 @@ if (typeof document !== "undefined" && !("classList" in document.createElement("
{ {
attach = function (sel, event, callback) attach = function (sel, event, callback)
{ {
if (typeof sel.addEventListener !== "undefined") sel.removeEventListener(event, callback, false);
{
sel.addEventListener(event, callback, false); sel.addEventListener(event, callback, false);
}
}; };
remove = function (sel, event, callback) remove = function (sel, event, callback)
{
if (typeof sel.removeEventListener !== "undefined")
{ {
sel.removeEventListener(event, callback, false); sel.removeEventListener(event, callback, false);
}
}; };
} }
//typeof function doesn't work in IE where attachEvent is available: brute force it //typeof function doesn't work in IE where attachEvent is available: brute force it
@ -1123,7 +1118,7 @@ if (typeof document !== "undefined" && !("classList" in document.createElement("
function listener () { function listener () {
// Internet Explorer fails to correctly set the 'this' object // Internet Explorer fails to correctly set the 'this' object
// for event listeners, so we need to set it ourselves. // for event listeners, so we need to set it ourselves.
callback.apply(arguments); callback.apply(arguments[0]);
} }
if (typeof sel.attachEvent !== "undefined") if (typeof sel.attachEvent !== "undefined")
@ -1243,7 +1238,7 @@ if (typeof document !== "undefined" && !("classList" in document.createElement("
$_.each(function(e){ $_.each(function(e){
add_remove(e, event, callback, false); add_remove(e, event, callback, false);
}); });
}, }/*,
live: function (event, callback) live: function (event, callback)
{ {
$_.each(function(e){ $_.each(function(e){
@ -1255,7 +1250,7 @@ if (typeof document !== "undefined" && !("classList" in document.createElement("
$_.each(function(e){ $_.each(function(e){
attach_delegate(e, target, event, callback); attach_delegate(e, target, event, callback);
}); });
} }*/
}; };
$_.ext('event', e); $_.ext('event', e);

42
kis-min.js vendored
View File

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

View File

@ -25,10 +25,10 @@
{ {
var x, c; var x, c;
if (typeof a !== "string" || typeof a === "undefined"){ return a;} if (typeof a != "string" || typeof a === "undefined"){ return a;}
//Check for a context of a specific element, otherwise, just run on the document //Check for a context of a specific element, otherwise, just run on the document
c = (typeof context === 'object' && context.nodeType === 1) c = (context != null && context.nodeType === 1)
? context ? context
: document; : document;

View File

@ -5,6 +5,8 @@
*/ */
(function (){ (function (){
"use strict";
// 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";
@ -16,17 +18,12 @@
{ {
attach = function (sel, event, callback) attach = function (sel, event, callback)
{ {
if (typeof sel.addEventListener !== "undefined") sel.removeEventListener(event, callback, false);
{
sel.addEventListener(event, callback, false); sel.addEventListener(event, callback, false);
}
}; };
remove = function (sel, event, callback) remove = function (sel, event, callback)
{
if (typeof sel.removeEventListener !== "undefined")
{ {
sel.removeEventListener(event, callback, false); sel.removeEventListener(event, callback, false);
}
}; };
} }
//typeof function doesn't work in IE where attachEvent is available: brute force it //typeof function doesn't work in IE where attachEvent is available: brute force it
@ -37,7 +34,7 @@
function listener () { function listener () {
// Internet Explorer fails to correctly set the 'this' object // Internet Explorer fails to correctly set the 'this' object
// for event listeners, so we need to set it ourselves. // for event listeners, so we need to set it ourselves.
callback.apply(arguments); callback.apply(arguments[0]);
} }
if (typeof sel.attachEvent !== "undefined") if (typeof sel.attachEvent !== "undefined")
@ -157,7 +154,7 @@
$_.each(function(e){ $_.each(function(e){
add_remove(e, event, callback, false); add_remove(e, event, callback, false);
}); });
}, }/*,
live: function (event, callback) live: function (event, callback)
{ {
$_.each(function(e){ $_.each(function(e){
@ -169,7 +166,7 @@
$_.each(function(e){ $_.each(function(e){
attach_delegate(e, target, event, callback); attach_delegate(e, target, event, callback);
}); });
} }*/
}; };
$_.ext('event', e); $_.ext('event', e);

View File

@ -27,6 +27,20 @@
</section> </section>
<script src="../kis-custom.js"></script> <script src="../kis-custom.js"></script>
<script src="qunit/qunit.js"></script> <script src="qunit/qunit.js"></script>
<script src="./tests.js"></script> <script type="text/javascript">
//Selector test function
function $(a)
{
var x = document.querySelectorAll(a);
//Return the single object if applicable
return (x.length === 1) ? x[0] : x;
}
</script>
<script src="tests/core.js"></script>
<script src="tests/ajax.js"></script>
<script src="tests/event.js"></script>
<script src="tests/dom.js"></script>
<script src="tests/util.js"></script>
</body> </body>
</html> </html>

View File

@ -1,328 +0,0 @@
(function(){
"use strict";
//Selector test function
function $(a)
{
var x = document.querySelectorAll(a);
//Return the single object if applicable
return (x.length === 1) ? x[0] : x;
}
module("core");
test("Basic requirements", function(){
expect(6);
ok(document.querySelectorAll, "querySelectorAll");
ok(document.getElementById, "getElementById");
ok(document.getElementsByTagName, "getElementsByTagName");
ok(String.prototype.trim, "String.trim()");
strictEqual(typeof $_, "function", "Global var");
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(){
var x = $_("ol");
var y = $_("aside");
expect(1);
notStrictEqual(x.el, y.el, "Unique Query Objects - see Issue #5");
});
test("Extend function", function(){
var o = $_("ol");
expect(4);
ok(o.ext, "Extend function exists");
$_.ext('test', {});
strictEqual(typeof o.test, "object", "Extend function adds to $_");
strictEqual(o.test.el, $_("ol").el, "Extend function adds selector to passed object");
strictEqual(o.test.el, o.el, "Selector is the same on parent and child object");
});
test("Selector tests", function(){
var i=0;
$_("div").each(function(e){
equal(e, $_("div").el[i], ".each function has current selector");
i++;
});
equal($_().el, window.document.documentElement, "Empty selector is set to documentElement");
});
test("Sub-modules", function(){
expect(4);
ok($_.qs, "Query String module");
ok($_().event, "Event module");
ok($_.store, "Local Storage module");
ok($_().dom, "Dom manipulation module");
});
// --------------------------------------------------------------------------
module("ajax");
test("Methods defined", function(){
expect(2);
ok($_.get, "AJAX get method");
ok($_.post, "AJAX post method");
});
// --------------------------------------------------------------------------
module("events");
test("Events defined", function(){
expect(2);
ok($_.event.add, "Add Method Exists");
ok($_.event.remove, "Remove Method Exists");
});
test("Browser expando support", function() {
expect(3);
// kis-js events uses expando properties to store event listeners for IE
// If this test fails, the event module will likely fail as well
var ele = document.createElement("div");
ele.expando = {a:5, b:"c", c: function cool(){return ele}};
equal(ele.expando.a, 5);
equal(ele.expando.b, "c");
equal(ele.expando.c(), ele,
"Closure isn't broken by being assigned to an expando property");
});
// --------------------------------------------------------------------------
module("dom");
test("Add/Remove Class", function() {
expect(4);
var $test = $_("#testSpan");
var ele = $test.el;
$test.dom.addClass("coolClass");
equal(ele.className, "coolClass");
$test.dom.addClass("anotherClass");
equal(ele.className, "coolClass anotherClass");
$test.dom.removeClass("coolClass");
equal(ele.className, "anotherClass");
$test.dom.removeClass("anotherClass");
ok(ele.className === undefined || ele.className === "", "testSpan.className is empty");
});
test("Show/Hide", function(){
expect(3);
var $test = $_("#classChild .nephew");
var ele = $test.el;
$test.dom.hide();
equal(ele.style.display, "none", "Element hidden with display:none");
$test.dom.show();
equal(ele.style.display, "block", "Element shown with display:block");
$test.dom.hide();
$test.dom.show('inline-block');
equal(ele.style.display, "inline-block", "Element shown with custom display type");
});
test("Text", function(){
expect(3);
var $test = $_("article#r14");
var ele = $test.el;
var text = (typeof ele.innerText !== "undefined") ? ele.innerText : ele.textContent;
equal($test.el, $("article#r14"), "Selector property is correct");
equal($test.dom.text(), text, "Getting text");
equal($test.dom.text(""), "", "Setting text");
});
test("Attr", function(){
expect(2);
var $test = $_("section");
var ele = $test.el;
$test.dom.attr("id", "testing");
equal($test.dom.attr('id'), "testing", "Getting attribute");
equal(ele.id, "testing", "Setting attribute");
});
test("CSS", function(){
expect(2);
var $test = $_("section[hidden='hidden']");
var ele = $test.el;
$test.dom.css("display", "block");
equal(ele.style.display, "block", "Setting CSS");
equal($test.dom.css("display"), "block", "Getting CSS");
});
// --------------------------------------------------------------------------
module("util");
test("Object keys", function(){
expect(1);
var test_o = {
"x": 2,
"a": 4,
"q": 3,
"r": 6
};
var test_keys = ["x", "a", "q", "r"];
deepEqual($_.util.object_keys(test_o), test_keys, "Retrieves object keys correctly");
});
test("Object values", function(){
expect(1);
var test_o = {
"x": 2,
"a": 4,
"q": 3,
"r": 6,
"p": "q"
};
var test_values = [2,4,3,6,"q"];
deepEqual($_.util.object_values(test_o), test_values, "Retrieves object values correctly");
});
test("Array combine", function(){
expect(3);
var keys_5 = ["a", "u", "i", "e", "o"];
var keys_obj = {
"x": 2,
"a": 4,
"q": 3,
"r": 1,
"p": "q"
};
var vals_5 = [1, 5, 3, 2, 4];
var vals_4 = [3, 6, 2, 7];
var obj_combined = {
2:1,
4:5,
3:3,
1:2,
"q":4
};
var combined = {
"a":1,
"u":5,
"i":3,
"e":2,
"o":4
};
equal($_.util.array_combine(keys_5, vals_4), false, "Can't combine arrays of different sizes");
deepEqual($_.util.array_combine(keys_obj, vals_5), obj_combined, "Combine with keys as object");
deepEqual($_.util.array_combine(keys_5, vals_5), combined, "Properly combines arrays");
});
/*test("Reverse Key Sort", function(){
expect(2);
var test_o = {
"x": 2,
"a": 4,
"q": 3,
"r": 6
};
var test_sorted = {
"x": 2,
"r": 6,
"q": 3,
"a": 4
};
var test_array = [7, 2, 6, 3];
var test_array_sorted = [3, 6, 2, 7];
deepEqual($_.util.reverse_key_sort(test_o), test_sorted, "Object sort");
deepEqual($_.util.object_values($_.util.reverse_key_sort(test_array)), test_array_sorted, "Array Sort");
});*/
test("Object Merge", function(){
expect(2);
var arr1 = {
"color": "red",
0: 2,
1: 4
},
arr2 = {
0: "a",
1: "b",
"color": "green",
"shape": "trapezoid",
2: 4
},
res1 = {
"color": "green",
0: 2,
1: 4,
2: "a",
3: "b",
"shape": "trapezoid",
4: 4
},
arr3 = [],
arr4 = {
1:'value',
},
res2 = {0:'value'};
deepEqual($_.util.object_merge(arr1, arr2), res1, "Merge objects with numeric and test keys");
deepEqual($_.util.object_merge(arr3, arr4), res2, "Merged object has reordered keys");
});
test("String translate", function(){
var test_str = "chotto",
test_replace = {
cho: 'ちょ',
to: 'と'
},
test_res = "ちょtと",
$trans = {'hello' : 'hi', 'hi' : 'hello'};
equal($_.util.str_trans(test_str, test_replace), test_res, "Correctly replaces substrings from replace pairs");
equal($_.util.str_trans("hi all, I said hello", $trans), 'hello all, I said hi', "Correctly replaces substrings from scalar pair");
});
}());

12
tests/tests/ajax.js Normal file
View File

@ -0,0 +1,12 @@
(function(){
"use strict";
module("ajax");
test("Methods defined", function(){
expect(2);
ok($_.get, "AJAX get method");
ok($_.post, "AJAX post method");
});
}());

61
tests/tests/core.js Normal file
View File

@ -0,0 +1,61 @@
(function(){
"use strict";
module("core");
test("Basic requirements", function(){
expect(6);
ok(document.querySelectorAll, "querySelectorAll");
ok(document.getElementById, "getElementById");
ok(document.getElementsByTagName, "getElementsByTagName");
ok(String.prototype.trim, "String.trim()");
strictEqual(typeof $_, "function", "Global var");
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(){
expect(1);
notStrictEqual($_("div").el, $_("aside").el, "Unique Query Objects - see Issue #5");
});
test("Extend function", function(){
var o = $_("ol");
expect(4);
ok(o.ext, "Extend function exists");
$_.ext('test', {});
strictEqual(typeof o.test, "object", "Extend function adds to $_");
strictEqual(o.test.el, $_("ol").el, "Extend function adds selector to passed object");
strictEqual(o.test.el, o.el, "Selector is the same on parent and child object");
o = null;
});
test("Selector tests", function(){
var i=0;
$_("div").each(function(e){
equal(e, $_("div").el[i], ".each function has current selector");
i++;
});
equal($_().el, window.document.documentElement, "Empty selector is set to documentElement");
});
test("Sub-modules", function(){
expect(4);
ok($_.qs, "Query String module");
ok($_().event, "Event module");
ok($_.store, "Local Storage module");
ok($_().dom, "Dom manipulation module");
});
}());

79
tests/tests/dom.js Normal file
View File

@ -0,0 +1,79 @@
(function(){
"use strict";
module("dom");
test("Add/Remove Class", function() {
expect(4);
var $test = $_("#testSpan");
var ele = $test.el;
$test.dom.addClass("coolClass");
equal(ele.className, "coolClass");
$test.dom.addClass("anotherClass");
equal(ele.className, "coolClass anotherClass");
$test.dom.removeClass("coolClass");
equal(ele.className, "anotherClass");
$test.dom.removeClass("anotherClass");
ok(ele.className === undefined || ele.className === "", "testSpan.className is empty");
});
test("Show/Hide", function(){
expect(3);
var $test = $_("#classChild .nephew");
var ele = $test.el;
$test.dom.hide();
equal(ele.style.display, "none", "Element hidden with display:none");
$test.dom.show();
equal(ele.style.display, "block", "Element shown with display:block");
$test.dom.hide();
$test.dom.show('inline-block');
equal(ele.style.display, "inline-block", "Element shown with custom display type");
});
test("Text", function(){
expect(3);
var $test = $_("article#r14");
var ele = $test.el;
var text = (typeof ele.innerText !== "undefined") ? ele.innerText : ele.textContent;
equal($test.el, $("article#r14"), "Selector property is correct");
equal($test.dom.text(), text, "Getting text");
equal($test.dom.text(""), "", "Setting text");
});
test("Attr", function(){
expect(2);
var $test = $_("section");
var ele = $test.el;
$test.dom.attr("id", "testing");
equal($test.dom.attr('id'), "testing", "Getting attribute");
equal(ele.id, "testing", "Setting attribute");
});
test("CSS", function(){
expect(2);
var $test = $_("section[hidden='hidden']");
var ele = $test.el;
$test.dom.css("display", "block");
equal(ele.style.display, "block", "Setting CSS");
equal($test.dom.css("display"), "block", "Getting CSS");
});
}());

24
tests/tests/event.js Normal file
View File

@ -0,0 +1,24 @@
(function(){
"use strict";
module("events");
test("Events defined", function(){
expect(2);
ok($_.event.add, "Add Method Exists");
ok($_.event.remove, "Remove Method Exists");
});
test("Browser expando support", function() {
expect(3);
// kis-js events uses expando properties to store event listeners for IE
// If this test fails, the event module will likely fail as well
var ele = document.createElement("div");
ele.expando = {a:5, b:"c", c: function cool(){return ele}};
equal(ele.expando.a, 5);
equal(ele.expando.b, "c");
equal(ele.expando.c(), ele,
"Closure isn't broken by being assigned to an expando property");
});
}());

150
tests/tests/util.js Normal file
View File

@ -0,0 +1,150 @@
(function(){
"use strict";
module("util");
test("Object keys", function(){
expect(1);
var test_o = {
"x": 2,
"a": 4,
"q": 3,
"r": 6
};
var test_keys = ["x", "a", "q", "r"];
deepEqual($_.util.object_keys(test_o), test_keys, "Retrieves object keys correctly");
});
test("Object values", function(){
expect(1);
var test_o = {
"x": 2,
"a": 4,
"q": 3,
"r": 6,
"p": "q"
};
var test_values = [2,4,3,6,"q"];
deepEqual($_.util.object_values(test_o), test_values, "Retrieves object values correctly");
});
test("Array combine", function(){
expect(3);
var keys_5 = ["a", "u", "i", "e", "o"];
var keys_obj = {
"x": 2,
"a": 4,
"q": 3,
"r": 1,
"p": "q"
};
var vals_5 = [1, 5, 3, 2, 4];
var vals_4 = [3, 6, 2, 7];
var obj_combined = {
2:1,
4:5,
3:3,
1:2,
"q":4
};
var combined = {
"a":1,
"u":5,
"i":3,
"e":2,
"o":4
};
equal($_.util.array_combine(keys_5, vals_4), false, "Can't combine arrays of different sizes");
deepEqual($_.util.array_combine(keys_obj, vals_5), obj_combined, "Combine with keys as object");
deepEqual($_.util.array_combine(keys_5, vals_5), combined, "Properly combines arrays");
});
/*test("Reverse Key Sort", function(){
expect(2);
var test_o = {
"x": 2,
"a": 4,
"q": 3,
"r": 6
};
var test_sorted = {
"x": 2,
"r": 6,
"q": 3,
"a": 4
};
var test_array = [7, 2, 6, 3];
var test_array_sorted = [3, 6, 2, 7];
deepEqual($_.util.reverse_key_sort(test_o), test_sorted, "Object sort");
deepEqual($_.util.object_values($_.util.reverse_key_sort(test_array)), test_array_sorted, "Array Sort");
});*/
test("Object Merge", function(){
expect(2);
var arr1 = {
"color": "red",
0: 2,
1: 4
},
arr2 = {
0: "a",
1: "b",
"color": "green",
"shape": "trapezoid",
2: 4
},
res1 = {
"color": "green",
0: 2,
1: 4,
2: "a",
3: "b",
"shape": "trapezoid",
4: 4
},
arr3 = [],
arr4 = {
1:'value',
},
res2 = {0:'value'};
deepEqual($_.util.object_merge(arr1, arr2), res1, "Merge objects with numeric and test keys");
deepEqual($_.util.object_merge(arr3, arr4), res2, "Merged object has reordered keys");
});
test("String translate", function(){
var test_str = "chotto",
test_replace = {
cho: 'ちょ',
to: 'と'
},
test_res = "ちょtと",
$trans = {'hello' : 'hi', 'hi' : 'hello'};
equal($_.util.str_trans(test_str, test_replace), test_res, "Correctly replaces substrings from replace pairs");
equal($_.util.str_trans("hi all, I said hello", $trans), 'hello all, I said hi', "Correctly replaces substrings from scalar pair");
});
}());