diff --git a/js/tinyDOM.js b/js/tinyDOM.js index bfbf53b64d9f72dd768e79844e2691ce432d70c9..53f4c098f57e775e8289eb204e5de8b5028c5912 100644 --- a/js/tinyDOM.js +++ b/js/tinyDOM.js @@ -1,4 +1,4 @@ -/*global Element */ +/*global Element, CustomEvent */ /*jslint plusplus: true */ /*jslint nomen: true*/ @@ -122,7 +122,16 @@ } else { return e.getAttribute('data-' + key); } - } + }, + trigger: function (eventName, data, bubbles, cancelable) { + bubbles = tinyDOM.exists(bubbles) ? bubbles : true; + cancelable = tinyDOM.exists(cancelable) ? cancelable : true; + + var event = new CustomEvent(eventName, data, bubbles, cancelable); + this.each(function (i, e) { + e.dispatchEvent(event); + }); + } }; tinyDOM.exists = function (obj) { @@ -147,6 +156,12 @@ return tinyDOM(document.getElementById(id)); }; + tinyDOM.triggerOn = function (target, eventName, data, bubbles, cancelable) { + bubbles = tinyDOM.exists(bubbles) ? bubbles : true; + cancelable = tinyDOM.exists(cancelable) ? cancelable : true; + target.dispatchEvent(new CustomEvent(eventName, data, bubbles, cancelable)); + }; + tinyDOM.ajax = function (options) { var req = new XMLHttpRequest(), _this = this, @@ -162,6 +177,11 @@ data: null, headers: [], callbacks: {} + }, + makeListener = function (callback) { + return function (data) { + callback(data.currentTarget.response, data); + }; }; this.merge(params, options); @@ -171,7 +191,7 @@ if (this.exists(params.callbacks)) { for (ev in params.callbacks) { if (params.callbacks.hasOwnProperty(ev)) { - req.addEventListener(ev, params.callbacks[ev]); + req.addEventListener(ev, makeListener(params.callbacks[ev])); } } } diff --git a/js/tinyDOM.min.js b/js/tinyDOM.min.js index ec04fec79dda5e47d79194a1ff51da89822b547b..3b6df2db4b9e7b55685c34c99ec3027c295ee66e 100644 --- a/js/tinyDOM.min.js +++ b/js/tinyDOM.min.js @@ -1 +1 @@ -!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)}(); +!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)},trigger:function(a,b,d,e){d=c.exists(d)?d:!0,e=c.exists(e)?e:!0;var f=new CustomEvent(a,b,d,e);this.each(function(a,b){b.dispatchEvent(f)})}},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.triggerOn=function(a,b,d,e,f){e=c.exists(e)?e:!0,f=c.exists(f)?f:!0,a.dispatchEvent(new CustomEvent(b,d,e,f))},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:{}},g=function(a){return function(b){a(b.currentTarget.response,b)}};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,g(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)}();