Skip to content
Snippets Groups Projects
Commit 15f6f10e authored by Commander-lol's avatar Commander-lol
Browse files

Added ability to trigger custom events on arbitrary selectors, changed ajax...

Added ability to trigger custom events on arbitrary selectors,  changed ajax calls to callback(response, evt) from callback(evt)
parent b295fb2b
No related branches found
No related tags found
No related merge requests found
/*global Element */ /*global Element, CustomEvent */
/*jslint plusplus: true */ /*jslint plusplus: true */
/*jslint nomen: true*/ /*jslint nomen: true*/
...@@ -122,7 +122,16 @@ ...@@ -122,7 +122,16 @@
} else { } else {
return e.getAttribute('data-' + key); 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) { tinyDOM.exists = function (obj) {
...@@ -147,6 +156,12 @@ ...@@ -147,6 +156,12 @@
return tinyDOM(document.getElementById(id)); 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) { tinyDOM.ajax = function (options) {
var req = new XMLHttpRequest(), var req = new XMLHttpRequest(),
_this = this, _this = this,
...@@ -162,6 +177,11 @@ ...@@ -162,6 +177,11 @@
data: null, data: null,
headers: [], headers: [],
callbacks: {} callbacks: {}
},
makeListener = function (callback) {
return function (data) {
callback(data.currentTarget.response, data);
};
}; };
this.merge(params, options); this.merge(params, options);
...@@ -171,7 +191,7 @@ ...@@ -171,7 +191,7 @@
if (this.exists(params.callbacks)) { if (this.exists(params.callbacks)) {
for (ev in params.callbacks) { for (ev in params.callbacks) {
if (params.callbacks.hasOwnProperty(ev)) { if (params.callbacks.hasOwnProperty(ev)) {
req.addEventListener(ev, params.callbacks[ev]); req.addEventListener(ev, makeListener(params.callbacks[ev]));
} }
} }
} }
......
!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)}();
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment