From 3ef9e89ab638905135c209a638b3c778d8329fee Mon Sep 17 00:00:00 2001 From: "Timothy J. Warren" Date: Fri, 12 Sep 2014 16:14:12 -0400 Subject: [PATCH] Simplify and cleanup event and dom modules, add context option to main constructor --- docs/$_.dom.html | 58 +++++++++++++++--------------- docs/$_.event.html | 30 ++++++++-------- docs/$_.html | 24 +++++++------ docs/$_.store.html | 16 +++++---- docs/DOM.js.html | 33 +++-------------- docs/ajax.js.html | 4 +-- docs/core.js.html | 25 ++++++------- docs/event.js.html | 16 +++------ docs/global.html | 2 +- docs/index.html | 6 ++-- docs/polyfill.js.html | 11 ++---- docs/store.js.html | 4 +-- docs/styles/jsdoc-default.css | 3 +- kis-all.js | 67 +++++++++-------------------------- kis-lite-dom-min.js | 18 +++++----- kis-lite-dom.js | 67 +++++++++-------------------------- kis-lite-min.js | 12 +++---- kis-lite.js | 38 +++++++------------- kis-min.js | 18 +++++----- src/core.js | 21 +++++------ src/modules/DOM.js | 29 ++------------- src/modules/event.js | 12 ++----- src/polyfill.js | 7 +--- tests/tests/dom.js | 15 ++++++-- 24 files changed, 202 insertions(+), 334 deletions(-) diff --git a/docs/$_.dom.html b/docs/$_.dom.html index 7f7dbf8..21888a3 100644 --- a/docs/$_.dom.html +++ b/docs/$_.dom.html @@ -68,7 +68,7 @@ Dom manipulation module
Source:
@@ -92,6 +92,8 @@ Dom manipulation module + + @@ -102,7 +104,7 @@ Dom manipulation module
-

<static> addClass(class)

+

(static) addClass(class)

@@ -192,7 +194,7 @@ selector
Source:
@@ -222,7 +224,7 @@ selector
-

<static> append(htm)

+

(static) append(htm)

@@ -311,7 +313,7 @@ selector
Source:
@@ -346,7 +348,7 @@ selector
-

<static> attr(name, value) → {string}

+

(static) attr(name, valueopt, nullable) → (nullable) {string}

@@ -379,7 +381,7 @@ current selector. Type - Argument + Attributes @@ -483,7 +485,7 @@ current selector.
Source:
@@ -531,7 +533,7 @@ current selector.
-

<static> css(property, value) → {string}

+

(static) css(property, valueopt, nullable) → (nullable) {string}

@@ -568,7 +570,7 @@ multiple key/value pairs. Type - Argument + Attributes @@ -675,7 +677,7 @@ multiple key/value pairs.
Source:
@@ -723,7 +725,7 @@ multiple key/value pairs.
-

<static> hide()

+

(static) hide()

@@ -764,7 +766,7 @@ multiple key/value pairs.
Source:
@@ -794,7 +796,7 @@ multiple key/value pairs.
-

<static> html(htm) → {string}

+

(static) html(htmopt, nullable) → (nullable) {string}

@@ -824,7 +826,7 @@ multiple key/value pairs. Type - Argument + Attributes @@ -897,7 +899,7 @@ multiple key/value pairs.
Source:
@@ -945,7 +947,7 @@ multiple key/value pairs.
-

<static> prepend(htm)

+

(static) prepend(htm)

@@ -1034,7 +1036,7 @@ multiple key/value pairs.
Source:
@@ -1064,7 +1066,7 @@ multiple key/value pairs.
-

<static> removeClass(class)

+

(static) removeClass(class)

@@ -1154,7 +1156,7 @@ selector
Source:
@@ -1184,7 +1186,7 @@ selector
-

<static> show(type)

+

(static) show(typeopt)

@@ -1217,7 +1219,7 @@ specified, the element is set to "display:block". Type - Argument + Attributes @@ -1288,7 +1290,7 @@ specified, the element is set to "display:block".
Source:
@@ -1318,7 +1320,7 @@ specified, the element is set to "display:block".
-

<static> text(value) → {string}

+

(static) text(valueopt, nullable) → (nullable) {string}

@@ -1351,7 +1353,7 @@ otherwise it will return the value of the current element Type - Argument + Attributes @@ -1424,7 +1426,7 @@ otherwise it will return the value of the current element
Source:
@@ -1485,13 +1487,13 @@ otherwise it will return the value of the current element
- Documentation generated by JSDoc 3.3.0-alpha5 on Fri Sep 05 2014 11:56:00 GMT-0400 (EDT) + Documentation generated by JSDoc 3.3.0-alpha9 on Fri Sep 12 2014 16:11:50 GMT-0400 (EDT)
diff --git a/docs/$_.event.html b/docs/$_.event.html index 8726161..5f298e9 100644 --- a/docs/$_.event.html +++ b/docs/$_.event.html @@ -90,6 +90,8 @@ + + @@ -100,7 +102,7 @@
-

<static> add(event, callback)

+

(static) add(event, callback)

@@ -213,7 +215,7 @@ event and selector
Source:
@@ -248,7 +250,7 @@ event and selector
-

<static> create(name, data) → {Object}

+

(static) create(name, dataopt) → {Object}

@@ -278,7 +280,7 @@ event and selector Type - Argument + Attributes @@ -433,7 +435,7 @@ event and selector
-

<static> delegate(target, event, callback)

+

(static) delegate(target, event, callback)

@@ -568,7 +570,7 @@ event and selector
Source:
@@ -603,7 +605,7 @@ event and selector
-

<static> live(target, event, callback)

+

(static) live(target, event, callback)

@@ -738,7 +740,7 @@ event and selector
Source:
@@ -773,7 +775,7 @@ event and selector
-

<static> remove(event, callback)

+

(static) remove(event, callback)

@@ -885,7 +887,7 @@ event and selector
Source:
@@ -920,7 +922,7 @@ event and selector
-

<static> trigger(event) → {boolean}

+

(static) trigger(event) → {boolean}

@@ -1009,7 +1011,7 @@ event and selector
Source:
@@ -1075,13 +1077,13 @@ event and selector
- Documentation generated by JSDoc 3.3.0-alpha5 on Fri Sep 05 2014 11:56:00 GMT-0400 (EDT) + Documentation generated by JSDoc 3.3.0-alpha9 on Fri Sep 12 2014 16:11:50 GMT-0400 (EDT)
diff --git a/docs/$_.html b/docs/$_.html index 25c36ae..7a2fd1d 100644 --- a/docs/$_.html +++ b/docs/$_.html @@ -90,6 +90,8 @@ Constructor function + +

Namespaces

@@ -113,7 +115,7 @@ Constructor function
-

<static> $(selector, context) → {Object}

+

(static) $(selector, contextopt) → {Object}

@@ -143,7 +145,7 @@ Constructor function Type - Argument + Attributes @@ -245,7 +247,7 @@ Constructor function
Source:
@@ -293,7 +295,7 @@ Constructor function
-

<static> each(callback)

+

(static) each(callback)

@@ -382,7 +384,7 @@ Constructor function
Source:
@@ -412,7 +414,7 @@ Constructor function
-

<static> get(url, data, success_callback, error_callback)

+

(static) get(url, data, success_callback, error_callbackopt)

@@ -442,7 +444,7 @@ Constructor function Type - Argument + Attributes @@ -636,7 +638,7 @@ Constructor function
-

<static> post(url, data, success_callback, error_callback)

+

(static) post(url, data, success_callback, error_callbackopt)

@@ -666,7 +668,7 @@ Constructor function Type - Argument + Attributes @@ -873,13 +875,13 @@ Constructor function
- Documentation generated by JSDoc 3.3.0-alpha5 on Fri Sep 05 2014 11:56:00 GMT-0400 (EDT) + Documentation generated by JSDoc 3.3.0-alpha9 on Fri Sep 12 2014 16:11:50 GMT-0400 (EDT)
diff --git a/docs/$_.store.html b/docs/$_.store.html index 0efe944..f31f2a5 100644 --- a/docs/$_.store.html +++ b/docs/$_.store.html @@ -92,6 +92,8 @@ to use sessionStorage rather than the default localStorage. + + @@ -102,7 +104,7 @@ to use sessionStorage rather than the default localStorage.
-

<static> clear(session)

+

(static) clear(session)

@@ -221,7 +223,7 @@ to use sessionStorage rather than the default localStorage.
-

<static> get(key, session) → {Object}

+

(static) get(key, session) → {Object}

@@ -382,7 +384,7 @@ based on the specified key
-

<static> getAll(session) → {Object}

+

(static) getAll(session) → {Object}

@@ -519,7 +521,7 @@ based on the specified key
-

<static> remove(key, session)

+

(static) remove(key, session)

@@ -661,7 +663,7 @@ based on the specified key
-

<static> set(key, value, session)

+

(static) set(key, value, session)

@@ -840,13 +842,13 @@ and JSON-encodes the value if not a string
- Documentation generated by JSDoc 3.3.0-alpha5 on Fri Sep 05 2014 11:56:00 GMT-0400 (EDT) + Documentation generated by JSDoc 3.3.0-alpha9 on Fri Sep 12 2014 16:11:50 GMT-0400 (EDT)
diff --git a/docs/DOM.js.html b/docs/DOM.js.html index c3e5996..6c1548d 100644 --- a/docs/DOM.js.html +++ b/docs/DOM.js.html @@ -85,40 +85,20 @@ function _css(sel, prop, val) { - var equi; - //Camel-case prop = _toCamel(prop); - //Equivalent properties for 'special' browsers - equi = { - outerHeight: "offsetHeight", - outerWidth: "offsetWidth", - top: "posTop" - }; - - //If you don't define a value, try returning the existing value if(val === undefined && sel.style[prop] !== undefined) { return sel.style[prop]; } - else if(val === undefined && sel.style[equi[prop]] !== undefined) - { - return sel.style[equi[prop]]; - } - //Let's try the easy way first + // Let's set a value instead if(sel.style[prop] !== undefined) { sel.style[prop] = val; - //Short circuit - return null; - } - else if(sel.style[equi[prop]]) - { - sel.style[equi[prop]] = val; return null; } } @@ -284,14 +264,11 @@ // If passed an object, recurse! if($_.type(prop) === 'object') { - for (prop_key in prop) - { - if ( ! prop.hasOwnProperty(prop_key)) continue; - + Object.keys(prop).forEach(function(prop_key) { $_.each(function (e){ _css(e, prop_key, prop[prop_key]); }); - } + }); } //Return the current value if a value is not set else if(val === undefined && $_.type(prop) !== 'object') @@ -362,13 +339,13 @@
- Documentation generated by JSDoc 3.3.0-alpha5 on Fri Sep 05 2014 11:56:00 GMT-0400 (EDT) + Documentation generated by JSDoc 3.3.0-alpha9 on Fri Sep 12 2014 16:11:50 GMT-0400 (EDT)
diff --git a/docs/ajax.js.html b/docs/ajax.js.html index ccaefbb..0a305b3 100644 --- a/docs/ajax.js.html +++ b/docs/ajax.js.html @@ -160,13 +160,13 @@
- Documentation generated by JSDoc 3.3.0-alpha5 on Fri Sep 05 2014 11:56:00 GMT-0400 (EDT) + Documentation generated by JSDoc 3.3.0-alpha9 on Fri Sep 12 2014 16:11:50 GMT-0400 (EDT)
diff --git a/docs/core.js.html b/docs/core.js.html index 30ec522..6b45804 100644 --- a/docs/core.js.html +++ b/docs/core.js.html @@ -46,9 +46,10 @@ * @constructor * @namespace $_ * @param {string} selector - The dom selector string + * @param {Object} [context] - Context of the dom selector string * @return {Object} */ - $_ = function(s) + $_ = function(s, context) { // Have documentElement be default selector, just in case if (s === undefined) @@ -60,7 +61,7 @@ } else { - sel = $(s); + sel = $(s, context); } // Add the selector to the prototype @@ -91,29 +92,29 @@ * @param {Object} [context] * @return {Object} */ - $ = function (a, context) + $ = function (selector, context) { - var x, c; + var elements; - if (typeof a != "string" || a === undefined){ return a;} + if (typeof selector != "string" || selector === undefined){ return selector;} //Check for a context of a specific element, otherwise, just run on the document - c = (context != null && context.nodeType === 1) + context = (context != null && context.nodeType === 1) ? context : document; //Pick the quickest method for each kind of selector - if (a.match(/^#([\w\-]+$)/)) + if (selector.match(/^#([\w\-]+$)/)) { - return document.getElementById(a.split('#')[1]); + return document.getElementById(selector.split('#')[1]); } else { - x = c.querySelectorAll(a); + elements = context.querySelectorAll(selector); } //Return the single object if applicable - return (x.length === 1) ? x[0] : x; + return (elements.length === 1) ? elements[0] : elements; }; /** @@ -179,13 +180,13 @@
- Documentation generated by JSDoc 3.3.0-alpha5 on Fri Sep 05 2014 11:56:00 GMT-0400 (EDT) + Documentation generated by JSDoc 3.3.0-alpha9 on Fri Sep 12 2014 16:11:50 GMT-0400 (EDT)
diff --git a/docs/event.js.html b/docs/event.js.html index 596493c..30c5d82 100644 --- a/docs/event.js.html +++ b/docs/event.js.html @@ -112,21 +112,13 @@ */ create: function(name, data) { - // Do a terrible browser-sniffic hack because I don't know of a good - // feature test - if (/MSIE|Trident/i.test(navigator.userAgent)) - { + data = data || {}; + // Okay, I guess we have to do this the hard way... :( - // Microsoft, your browser still sucks var e = document.createEvent('CustomEvent'); e.initCustomEvent(name, true, true, data); return e; - } - else - { - return new CustomEvent(name, data); - } }, /** * Adds an event that returns a callback when triggered on the selected @@ -222,13 +214,13 @@
- Documentation generated by JSDoc 3.3.0-alpha5 on Fri Sep 05 2014 11:56:00 GMT-0400 (EDT) + Documentation generated by JSDoc 3.3.0-alpha9 on Fri Sep 12 2014 16:11:50 GMT-0400 (EDT)
diff --git a/docs/global.html b/docs/global.html index 475f6eb..54090dc 100644 --- a/docs/global.html +++ b/docs/global.html @@ -168,7 +168,7 @@ Module for making ajax requests
- Documentation generated by JSDoc 3.3.0-alpha5 on Fri Sep 05 2014 11:56:00 GMT-0400 (EDT) + Documentation generated by JSDoc 3.3.0-alpha5 on Wed Sep 10 2014 00:23:43 GMT-0400 (EDT)
diff --git a/docs/index.html b/docs/index.html index b0abbb1..69f35f9 100644 --- a/docs/index.html +++ b/docs/index.html @@ -112,6 +112,8 @@ + + @@ -131,13 +133,13 @@
- Documentation generated by JSDoc 3.3.0-alpha5 on Fri Sep 05 2014 11:56:00 GMT-0400 (EDT) + Documentation generated by JSDoc 3.3.0-alpha9 on Fri Sep 12 2014 16:11:50 GMT-0400 (EDT)
diff --git a/docs/polyfill.js.html b/docs/polyfill.js.html index 3780d75..e89f141 100644 --- a/docs/polyfill.js.html +++ b/docs/polyfill.js.html @@ -28,12 +28,7 @@
/**
  * A module of various browser polyfills
  * @file polyfill.js
- */
-
-/**
- * Promise.prototype.done polyfill
- */
-if (!Promise.prototype.done) { Promise.prototype.done = function (cb, eb) { this.then(cb, eb).then(null, function (err) { setTimeout(function () { throw err; }, 0); }); }; }
+ */ @@ -43,13 +38,13 @@ if (!Promise.prototype.done) { Promise.prototype.done = function (cb, eb) { this
- Documentation generated by JSDoc 3.3.0-alpha5 on Fri Sep 05 2014 11:56:00 GMT-0400 (EDT) + Documentation generated by JSDoc 3.3.0-alpha9 on Fri Sep 12 2014 16:11:50 GMT-0400 (EDT)
diff --git a/docs/store.js.html b/docs/store.js.html index 60dadb4..d2186e8 100644 --- a/docs/store.js.html +++ b/docs/store.js.html @@ -151,13 +151,13 @@
- Documentation generated by JSDoc 3.3.0-alpha5 on Fri Sep 05 2014 11:56:00 GMT-0400 (EDT) + Documentation generated by JSDoc 3.3.0-alpha9 on Fri Sep 12 2014 16:11:50 GMT-0400 (EDT)
diff --git a/docs/styles/jsdoc-default.css b/docs/styles/jsdoc-default.css index bf6b51c..124ef6c 100644 --- a/docs/styles/jsdoc-default.css +++ b/docs/styles/jsdoc-default.css @@ -58,8 +58,7 @@ section display: none; } -.optional:after { - content: "opt"; +.signature-attributes { font-size: 60%; color: #aaa; font-style: italic; diff --git a/kis-all.js b/kis-all.js index 0650526..a97e1ec 100755 --- a/kis-all.js +++ b/kis-all.js @@ -19,9 +19,10 @@ * @constructor * @namespace $_ * @param {string} selector - The dom selector string + * @param {Object} [context] - Context of the dom selector string * @return {Object} */ - $_ = function(s) + $_ = function(s, context) { // Have documentElement be default selector, just in case if (s === undefined) @@ -33,7 +34,7 @@ } else { - sel = $(s); + sel = $(s, context); } // Add the selector to the prototype @@ -64,29 +65,29 @@ * @param {Object} [context] * @return {Object} */ - $ = function (a, context) + $ = function (selector, context) { - var x, c; + var elements; - if (typeof a != "string" || a === undefined){ return a;} + if (typeof selector != "string" || selector === undefined){ return selector;} //Check for a context of a specific element, otherwise, just run on the document - c = (context != null && context.nodeType === 1) + context = (context != null && context.nodeType === 1) ? context : document; //Pick the quickest method for each kind of selector - if (a.match(/^#([\w\-]+$)/)) + if (selector.match(/^#([\w\-]+$)/)) { - return document.getElementById(a.split('#')[1]); + return document.getElementById(selector.split('#')[1]); } else { - x = c.querySelectorAll(a); + elements = context.querySelectorAll(selector); } //Return the single object if applicable - return (x.length === 1) ? x[0] : x; + return (elements.length === 1) ? elements[0] : elements; }; /** @@ -151,11 +152,6 @@ * @file polyfill.js */ -/** - * Promise.prototype.done polyfill - */ -if (!Promise.prototype.done) { Promise.prototype.done = function (cb, eb) { this.then(cb, eb).then(null, function (err) { setTimeout(function () { throw err; }, 0); }); }; } - // -------------------------------------------------------------------------- @@ -224,40 +220,20 @@ if (!Promise.prototype.done) { Promise.prototype.done = function (cb, eb) { this function _css(sel, prop, val) { - var equi; - //Camel-case prop = _toCamel(prop); - //Equivalent properties for 'special' browsers - equi = { - outerHeight: "offsetHeight", - outerWidth: "offsetWidth", - top: "posTop" - }; - - //If you don't define a value, try returning the existing value if(val === undefined && sel.style[prop] !== undefined) { return sel.style[prop]; } - else if(val === undefined && sel.style[equi[prop]] !== undefined) - { - return sel.style[equi[prop]]; - } - //Let's try the easy way first + // Let's set a value instead if(sel.style[prop] !== undefined) { sel.style[prop] = val; - //Short circuit - return null; - } - else if(sel.style[equi[prop]]) - { - sel.style[equi[prop]] = val; return null; } } @@ -423,14 +399,11 @@ if (!Promise.prototype.done) { Promise.prototype.done = function (cb, eb) { this // If passed an object, recurse! if($_.type(prop) === 'object') { - for (prop_key in prop) - { - if ( ! prop.hasOwnProperty(prop_key)) continue; - + Object.keys(prop).forEach(function(prop_key) { $_.each(function (e){ _css(e, prop_key, prop[prop_key]); }); - } + }); } //Return the current value if a value is not set else if(val === undefined && $_.type(prop) !== 'object') @@ -706,21 +679,13 @@ if (!Promise.prototype.done) { Promise.prototype.done = function (cb, eb) { this */ create: function(name, data) { - // Do a terrible browser-sniffic hack because I don't know of a good - // feature test - if (/MSIE|Trident/i.test(navigator.userAgent)) - { + data = data || {}; + // Okay, I guess we have to do this the hard way... :( - // Microsoft, your browser still sucks var e = document.createEvent('CustomEvent'); e.initCustomEvent(name, true, true, data); return e; - } - else - { - return new CustomEvent(name, data); - } }, /** * Adds an event that returns a callback when triggered on the selected diff --git a/kis-lite-dom-min.js b/kis-lite-dom-min.js index 9204be1..aafdbec 100755 --- a/kis-lite-dom-min.js +++ b/kis-lite-dom-min.js @@ -1,9 +1,9 @@ -(function(e){var c,h,b;c=function(a){b=a===e?c.el!==e?c.el:document.documentElement:h(a);c.prototype.el=b;a=Object.create(c);for(var d in a)"object"===typeof a[d]&&(a[d].el=b);a.el=b;return a};h=function(a,d){var b;if("string"!=typeof a||a===e)return a;b=null!=d&&1===d.nodeType?d:document;if(a.match(/^#([\w\-]+$)/))return document.getElementById(a.split("#")[1]);b=b.querySelectorAll(a);return 1===b.length?b[0]:b};c.ext=function(a,d){d.el=b;c[a]=d};c.ext("each",function(a){b.length!==e&&b!==window? -[].forEach.call(b,a):a.call(b,b)});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()};c=window.$_=window.$_||c;c.$=h})();Promise.prototype.done||(Promise.prototype.done=function(e,c){this.then(e,c).then(null,function(e){setTimeout(function(){throw e;},0)})}); -(function(e){var c={_do:function(c,b,a,d,g){var f=new XMLHttpRequest;a===e&&(a=function(){});g=g?"POST":"GET";"GET"===g&&(c+=c.match(/\?/)?this._serialize(b):"?"+this._serialize(b));f.open(g,c);f.onreadystatechange=function(){4===f.readyState&&(200===f.status?a.call(f.responseText,f.responseText):d!==e&&d.call(f.status,f.status))};"POST"===g?(f.setRequestHeader("Content-Type","application/x-www-form-urlencoded"),f.send(this._serialize(b))):f.send(null)},_serialize:function(c){var b,a,d=[];for(b in c)c.hasOwnProperty(b)&& -"function"!==$_.type(c[b])&&(a=c[b].toString(),b=encodeURIComponent(b),a=encodeURIComponent(a),d.push(b+"="+a));return d.join("&")}};$_.ext("get",function(e,b,a,d){c._do(e,b,a,d,!1)});$_.ext("post",function(e,b,a,d){c._do(e,b,a,d,!0)})})(); -(function(e){var c,h;c=function(b,a,d,g){var f,e;if(a.match(/^([\w\-]+)$/))!0===g?b.addEventListener(a,d,!1):b.removeEventListener(a,d,!1);else for(a=a.split(" "),e=a.length,f=0;f