diff --git a/js/tinyDOM.js b/js/tinyDOM.js index efc42c71d3d401f432560c87befd3d83dd78e3d6..bfbf53b64d9f72dd768e79844e2691ce432d70c9 100644 --- a/js/tinyDOM.js +++ b/js/tinyDOM.js @@ -1,33 +1,35 @@ -(function() { +/*global Element */ +/*jslint plusplus: true */ +/*jslint nomen: true*/ + +(function () { 'use strict'; + var proto, TinyDOMFunction, tinyDOM; + /* * Polyfill from https://gist.github.com/elijahmanor/6452535 */ if (Element && !Element.prototype.matches) { - var proto = Element.prototype; + proto = Element.prototype; proto.matches = proto.matchesSelector || - proto.mozMatchesSelector || proto.msMatchesSelector || - proto.oMatchesSelector || proto.webkitMatchesSelector; + proto.mozMatchesSelector || proto.msMatchesSelector || + proto.oMatchesSelector || proto.webkitMatchesSelector; } /* * End Polyfill */ - var tinyDOM = function(selector){ - return new tinyDOMFunction(selector); - }; - - var tinyDOMFunction = function(selector) { + TinyDOMFunction = function (selector) { if (selector === null || typeof (selector) === 'undefined') { this.length = 0; - } else if(typeof(selector) === 'string') { - var elements = document.querySelectorAll(selector); + } else if (typeof (selector) === 'string') { + var elements = document.querySelectorAll(selector), i, e; this.length = elements.length; - for(var i = 0; i < elements.length; i++) { - var e = elements.item(i); - if(typeof(e.td_prop) === 'undefined') { + for (i = 0; i < elements.length; i += 1) { + e = elements.item(i); + if (typeof (e.td_prop) === 'undefined') { e.td_prop = { isHidden: false }; @@ -40,19 +42,23 @@ } return this; - } + }; + + tinyDOM = function (selector) { + return new TinyDOMFunction(selector); + }; - tinyDOM.fn = tinyDOMFunction.prototype = { - each: function(fn) { + tinyDOM.fn = TinyDOMFunction.prototype = { + each: function (fn) { var l = this.length; - while(l--) { + while (l--) { fn(l, this[l], this); } return this; }, - hide: function(){ - this.each(function(i, e) { - if(!e.td_prop.isHidden) { + hide: function () { + this.each(function (i, e) { + if (!e.td_prop.isHidden) { e.style.td_previousDisplay = e.style.display; e.style.display = 'none'; e.td_prop.isHidden = true; @@ -60,10 +66,10 @@ }); return this; }, - show: function() { - this.each(function(i, e) { - if(e.td_prop.isHidden === true) { - if(typeof(e.style.td_previousDisplay) !== 'undefined') { + show: function () { + this.each(function (i, e) { + if (e.td_prop.isHidden === true) { + if (typeof (e.style.td_previousDisplay) !== 'undefined') { e.style.display = e.style.td_previousDisplay; } else { e.style.display = 'block'; @@ -73,96 +79,98 @@ }); return this; }, - on: function(ev, del, fn) { - if(typeof(del) === 'string') { - this.each(function(i, e) { - e.addEventListener(ev, function(firedevent) { - var target = firedevent.target; - var matched = false; + on: function (ev, del, fn) { + if (typeof (del) === 'string') { + this.each(function (i, e) { + e.addEventListener(ev, function (firedevent) { + var target = firedevent.target, + matched = false; do { - if(target && target.matches(del)) { + if (target && target.matches(del)) { fn.call(target, firedevent); matched = true; } else { target = target.parentNode; - if(!target || !target.matches || target === e) { + if (!target || !target.matches || target === e) { matched = true; } } - } while(matched !== true); + } while (matched !== true); }); }); } else { - var fn = del; - this.each(function(i, e) { + fn = del; + this.each(function (i, e) { e.addEventListener(ev, fn); }); } return this; }, - first: function() { - if(typeof(this[0]) !== 'undefined') { + first: function () { + if (typeof (this[0]) !== 'undefined') { return this[0]; } else { return null; } }, - data: function(key, value) { + data: function (key, value) { var e = this[0]; - if(typeof(value) !== 'undefined') { - e.setAttribute('data-'+key, value); + if (typeof (value) !== 'undefined') { + e.setAttribute('data-' + key, value); return this; } else { - return e.getAttribute('data-'+key); + return e.getAttribute('data-' + key); } } - } + }; - tinyDOM.exists = function(obj) { - return obj !== null && typeof(obj) !== 'undefined'; - } + tinyDOM.exists = function (obj) { + return obj !== null && typeof (obj) !== 'undefined'; + }; - tinyDOM.merge = function(json1, json2) { - if(!this.exists(json1) || !this.exists(json2)) { + tinyDOM.merge = function (json1, json2) { + if (!this.exists(json1) || !this.exists(json2)) { return null; } else { - for(var prop in json2) { - if(json2.hasOwnProperty(prop)) { + var prop; + for (prop in json2) { + if (json2.hasOwnProperty(prop)) { json1[prop] = json2[prop]; } } return json1; } - } + }; - tinyDOM.byID = function(id){ + tinyDOM.byID = function (id) { return tinyDOM(document.getElementById(id)); - } - - tinyDOM.ajax = function(options) { - var req = new XMLHttpRequest(); - var _this = this; - - var params = { - method: 'GET', - url: '', - async: true, - user: null, - password: null, - responseType: 'text', - data: null, - headers: [], - callbacks: {} - } + }; + + tinyDOM.ajax = function (options) { + var req = new XMLHttpRequest(), + _this = this, + ev, + i, + params = { + method: 'GET', + url: '', + async: true, + user: null, + password: null, + responseType: 'text', + data: null, + headers: [], + callbacks: {} + }; this.merge(params, options); req.responseType = params.responseType; - if(this.exists(params.callbacks)) { - for(var ev in params.callbacks) { - if(params.callbacks.hasOwnProperty(ev)) { + if (this.exists(params.callbacks)) { + for (ev in params.callbacks) { + if (params.callbacks.hasOwnProperty(ev)) { req.addEventListener(ev, params.callbacks[ev]); } } @@ -176,7 +184,7 @@ params.password ); - for(var i = 0; i < params.headers.length; i++) { + for (i = 0; i < params.headers.length; i++) { req.setRequestHeader(params.headers[i].header, params.headers[i].value); } @@ -184,10 +192,11 @@ return req; }; - if(!window.μ) { + if (!window.μ) { window.μ = tinyDOM; } - if(!window.mu) { + + if (!window.mu) { window.mu = tinyDOM; } -})(); +}()); diff --git a/js/tinyDOM.min.js b/js/tinyDOM.min.js index dcde192d49ff8fbc00b6c8f56b8e8ea102209d94..ec04fec79dda5e47d79194a1ff51da89822b547b 100644 --- a/js/tinyDOM.min.js +++ b/js/tinyDOM.min.js @@ -1 +1 @@ -!function(){if(Element&&!Element.prototype.matches){var a=Element.prototype;a.matches=a.matchesSelector||a.mozMatchesSelector||a.msMatchesSelector||a.oMatchesSelector||a.webkitMatchesSelector}var b=function(a){return new c(a)},c=function(a){if(null===a||"undefined"==typeof a)this.length=0;else if("string"==typeof a){var b=document.querySelectorAll(a);this.length=b.length;for(var c=0;c<b.length;c++){var d=b.item(c);"undefined"==typeof d.td_prop&&(d.td_prop={isHidden:!1}),this[c]=b.item(c)}}else this[0]=a,this.length=1;return this};b.fn=c.prototype={each:function(a){for(var b=this.length;b--;)a(b,this[b],this);return this},hide:function(){return this.each(function(a,b){b.td_prop.isHidden||(b.style.td_previousDisplay=b.style.display,b.style.display="none",b.td_prop.isHidden=!0)}),this},show:function(){return this.each(function(a,b){b.td_prop.isHidden===!0&&(b.style.display="undefined"!=typeof b.style.td_previousDisplay?b.style.td_previousDisplay:"block",b.td_prop.isHidden=!1)}),this},on:function(a,b,c){if("string"==typeof b)this.each(function(d,e){e.addEventListener(a,function(a){var d=a.target,e=!1;do d&&d.matches(b)?(c.call(d,a),e=!0):(d=d.parentNode,d&&d.matches||(e=!0));while(e!==!0)})});else{var c=b;this.each(function(b,d){d.addEventListener(a,c)})}return this},first:function(){return"undefined"!=typeof this[0]?this[0]:null},data:function(a,b){var c=this[0];return"undefined"!=typeof b?(c.setAttribute("data-"+a,b),this):c.getAttribute("data-"+a)}},b.exists=function(a){return null!==a&&"undefined"!=typeof a},b.merge=function(a,b){if(this.exists(a)&&this.exists(b)){for(var c in b)b.hasOwnProperty(c)&&(a[c]=b[c]);return a}return null},b.ajax=function(a){var b=new XMLHttpRequest,c=this,d={method:"GET",url:"",async:!0,user:null,password:null,responseType:"text",data:null,headers:[],success:null,error:null};this.merge(d,a),b.responseType=d.responseType,b.onreadystatechange=function(){b.readyState>1&&(b.status>=200&&b.status<400?4===b.readyState&&c.exists(d.success)&&d.success(b.response,b):c.exists(b.td_hasAborted)&&b.td_hasAborted||(c.exists(d.error)&&d.error({status:b.status,message:b.statusText},b),b.td_hasAborted=!0,b.abort()))},b.open(d.method,d.url,d.async,d.user,d.password);for(var e=0;e<d.headers.length;e++)b.setRequestHeader(d.headers[e].header,d.headers[e].value);return b.send(d.data),b},window.μ||(window.μ=b),window.mu||(window.mu=b)}(); +!function(){"use strict";var a,b,c;Element&&!Element.prototype.matches&&(a=Element.prototype,a.matches=a.matchesSelector||a.mozMatchesSelector||a.msMatchesSelector||a.oMatchesSelector||a.webkitMatchesSelector),b=function(a){if(null===a||"undefined"==typeof a)this.length=0;else if("string"==typeof a){var c,d,b=document.querySelectorAll(a);for(this.length=b.length,c=0;c<b.length;c+=1)d=b.item(c),"undefined"==typeof d.td_prop&&(d.td_prop={isHidden:!1}),this[c]=b.item(c)}else this[0]=a,this.length=1;return this},c=function(a){return new b(a)},c.fn=b.prototype={each:function(a){for(var b=this.length;b--;)a(b,this[b],this);return this},hide:function(){return this.each(function(a,b){b.td_prop.isHidden||(b.style.td_previousDisplay=b.style.display,b.style.display="none",b.td_prop.isHidden=!0)}),this},show:function(){return this.each(function(a,b){b.td_prop.isHidden===!0&&(b.style.display="undefined"!=typeof b.style.td_previousDisplay?b.style.td_previousDisplay:"block",b.td_prop.isHidden=!1)}),this},on:function(a,b,c){return"string"==typeof b?this.each(function(d,e){e.addEventListener(a,function(a){var d=a.target,f=!1;do d&&d.matches(b)?(c.call(d,a),f=!0):(d=d.parentNode,d&&d.matches&&d!==e||(f=!0));while(f!==!0)})}):(c=b,this.each(function(b,d){d.addEventListener(a,c)})),this},first:function(){return"undefined"!=typeof this[0]?this[0]:null},data:function(a,b){var c=this[0];return"undefined"!=typeof b?(c.setAttribute("data-"+a,b),this):c.getAttribute("data-"+a)}},c.exists=function(a){return null!==a&&"undefined"!=typeof a},c.merge=function(a,b){if(this.exists(a)&&this.exists(b)){var c;for(c in b)b.hasOwnProperty(c)&&(a[c]=b[c]);return a}return null},c.byID=function(a){return c(document.getElementById(a))},c.ajax=function(a){var d,e,b=new XMLHttpRequest,f={method:"GET",url:"",async:!0,user:null,password:null,responseType:"text",data:null,headers:[],callbacks:{}};if(this.merge(f,a),b.responseType=f.responseType,this.exists(f.callbacks))for(d in f.callbacks)f.callbacks.hasOwnProperty(d)&&b.addEventListener(d,f.callbacks[d]);for(b.open(f.method,f.url,f.async,f.user,f.password),e=0;e<f.headers.length;e++)b.setRequestHeader(f.headers[e].header,f.headers[e].value);return b.send(f.data),b},window.μ||(window.μ=c),window.mu||(window.mu=c)}();