Skip to content
Snippets Groups Projects
tinyDOM.min.js 2.96 KiB
Newer Older
!function(){"use strict";var e,t,n;Element&&!Element.prototype.matches&&(e=Element.prototype,e.matches=e.matchesSelector||e.mozMatchesSelector||e.msMatchesSelector||e.oMatchesSelector||e.webkitMatchesSelector),t=function(e){var t,n,s;if(null===e||"undefined"==typeof e)this.length=0;else if("string"==typeof e)for(t=document.querySelectorAll(e),this.length=t.length,n=0;n<t.length;n++)s=t.item(n),this[n]=s;else if(e.length)for(n=0;n<e.length;n++)s=e[n],this[n]=s;else this[0]=e,this.length=1;return this},n=function(e){return new t(e)},n.fn=t.prototype={each:function(e){for(var t=this.length;t--;)e(t,this[t],this);return this},on:function(e,t,n){return"string"==typeof t?this.each(function(s,r){r.addEventListener(e,function(e){var s=e.target,i=!1;do s&&s.matches(t)?(n.call(s,e),i=!0):(s=s.parentNode,s&&s.matches&&s!==r||(i=!0));while(i!==!0)})}):(n=t,this.each(function(t,s){s.addEventListener(e,n)})),this},first:function(){return"undefined"!=typeof this[0]?n(this[0]):null},parent:function(e){var t=this[0].parentNode,s=!0;if(n.exists(e)){for(;null!==t&&t!==document;){if(t.matches(e)){s=!1;break}t=t.parentNode}t=s?null:t}return n(t)},children:function(){var e,t=this[0].childNodes,s=[];for(e=0;e<t.length;e++)n.isElement(t[e])&&s.push(t[e]);return n(s)},data:function(e,t){return"undefined"!=typeof t?(this.each(function(n,s){s.setAttribute("data-"+e,t)}),this):this[0].getAttribute("data-"+e)},attr:function(e,t){return"undefined"!=typeof t?(this.each(function(n,s){s.setAttribute(e,t)}),this):this[0].getAttribute(e)},"class":function(e,t){return this.each(n.exists(t)?function(n,s){s.classList.toggle(e,t)}:function(t,n){n.classList.toggle(e)}),this},trigger:function(e,t,s,r){s=n.exists(s)?s:!0,r=n.exists(r)?r:!0;var i=new CustomEvent(e,t,s,r);return this.each(function(e,t){t.dispatchEvent(i)}),this}},n.isElement=function(e){var t=!1;try{t=e instanceof HTMLElement}catch(n){t=e.nodeType&&1===e.nodeType}return t},n.isJson=function(e){try{return JSON.parse(e),null!==e&&"undefined"!=typeof e}catch(t){return!1}},n.exists=function(e){return null!==e&&"undefined"!=typeof e},n.merge=function(e,t){if(this.exists(e)&&this.exists(t)){var n;for(n in t)t.hasOwnProperty(n)&&(e[n]=t[n]);return e}return null},n.byID=function(e){return n(document.getElementById(e))},n.triggerOn=function(e,t,s,r,i){r=n.exists(r)?r:!0,i=n.exists(i)?i:!0,e.dispatchEvent(new CustomEvent(t,s,r,i))},n.ajax=function(e){var t,n,s=new XMLHttpRequest,r={method:"GET",url:"",async:!0,user:null,password:null,responseType:"text",data:null,headers:[],callbacks:{}},i=function(e){return function(t){e(t.currentTarget.response,t)}};if(this.merge(r,e),s.responseType=r.responseType,this.exists(r.callbacks))for(t in r.callbacks)r.callbacks.hasOwnProperty(t)&&s.addEventListener(t,i(r.callbacks[t]));for(s.open(r.method,r.url,r.async,r.user,r.password),n=0;n<r.headers.length;n++)s.setRequestHeader(r.headers[n].header,r.headers[n].value);return s.send(r.data),s},window.μ||(window.μ=n),window.mu||(window.mu=n)}();
//# sourceMappingURL=tinyDOM.min.js.map