-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathnunjs.min.js
8 lines (6 loc) · 5.6 KB
/
nunjs.min.js
1
2
3
4
5
6
7
8
/**
nunjs 0.0.5
https://github.com/tvrzna/nunjs
Released under the MIT License.
**/
Nunjs={each:function(t,e){var n,i=0;if(Array.isArray(t)||t instanceof NodeList)for(n=t.length;i<n&&!1!==e.call(t[i],i,t[i]);i++);else e.call(t,0,t);return t},includes:function(t,e){return!(!t||!e)&&0<=t.indexOf(e)},matches:function(t,e){if(!e||!t||1!==t.nodeType)return!1;var n=t.matches||t.webkitMatchesSelector||t.mozMatchesSelector||t.oMatchesSelector||t.matchesSelector||t.msMatchesSelector;return!!n&&n.call(t,e)},removeWithChildren:function(t,e){$(e).find("*").each(Nunjs.removeWithChildren),$(e).remove()},_events:{}},window.$=function(t){var e,n;n="string"==typeof t?t.trim().startsWith("<")&&t.trim().endsWith(">")?((e=document.createElement("tmp")).innerHTML=t.trim(),e.firstChild):document.querySelectorAll(t):t;var i={nunjs:"0.0.5",addClass:function(t){return this.each(function(){this.classList.contains(t)||this.classList.add(t)}),this},appendTo:function(t){var n="string"==typeof t?document.querySelectorAll(t):t,i=[];return this.each(function(){for(var t=0;t<=n.length;t++){var e=this.cloneNode(!0);n[t].appendChild(e),i.push(e)}}),$(i)},attr:function(t,e){return null==e?this[0].getAttribute(t):(this.each(function(){this.setAttribute(t,e)}),this)},click:function(n){return null===n||"function"!=typeof n?this.each(function(){this.click()}):this.each(function(t,e){$(e).on("click",n)}),this},clone:function(){var e=[];return this.each(function(){var t=this.cloneNode(!0);e.push(t)}),$(e)},each:function(t){return Nunjs.each(n,t),this},find:function(n){var i=[];return this.each(function(){for(var t=this.querySelectorAll(n),e=0;e<t.length;e++)Nunjs.includes(i,t[e])||i.push(t[e])}),$(i)},hasClass:function(t){var e=!1;return this.each(function(){this.classList.contains(t)&&(e=!0)}),e},hasEvent:function(t){return void 0!==Nunjs._events[this[0]]&&void 0!==Nunjs._events[this[0]][t]},hide:function(){return this.each(function(){this.style.display="none"}),this},html:function(t){return void 0===t?this[0].innerHTML:(this.find("*").each(Nunjs.removeWithChildren),this.each(function(){this.innerHTML=t}),this)},insertAfter:function(t){var n="string"==typeof t?document.querySelectorAll(t):t,i=[];return this.each(function(){for(var t=0;t<=n.length;t++){var e=this.cloneNode(!0);n.parent()[0].insertBefore(e,n[t].nextSibling),i.push(e)}}),$(i)},insertBefore:function(t){var n="string"==typeof t?document.querySelectorAll(t):t,i=[];return this.each(function(){for(var t=0;t<=n.length;t++){var e=this.cloneNode(!0);n.parent()[0].insertBefore(e,n[t]),i.push(e)}}),$(i)},is:function(t){return Nunjs.matches(this[0],t)},off:function(e,n){return this.each(function(){if(void 0===n){for(var t=0;t<Nunjs._events[this][e].length;t++)this.removeEventListener(e,Nunjs._events[this][e][t]);Nunjs._events[this][e]=[]}else this.removeEventListener(e,n)}),this},on:function(t,e){return this.each(function(){this in Nunjs._events||(Nunjs._events[this]={}),t in Nunjs._events[this]||(Nunjs._events[this][t]=[]),Nunjs._events[this][t].push(e),this.addEventListener(t,e)}),this},parent:function(i){var s=[];return this.each(function(t,e){if(void 0!==e.parentElement&&(i||Nunjs.includes(s,e.parentElement)||s.push(e.parentElement),i))if(Nunjs.matches(e.parentElement,i)&&!Nunjs.includes(s,e.parentElement))s.push(e.parentElement);else if(!Nunjs.matches(e,i)){var n=e.parentElement;do{if(Nunjs.matches(n,i)){Nunjs.includes(s,n)||s.push(n);break}}while("undefined"!==(n=n.parentNode))}}),$(s)},prependTo:function(t){var n="string"==typeof t?document.querySelectorAll(t):t,i=[];return this.each(function(){for(var t=0;t<=n.length;t++){var e=this.cloneNode(!0);n[t].insertBefore(e,n[t].childNodes[0]),i.push(e)}}),$(i)},prop:function(t,e){return null==e?this[0][t]:(this.each(function(){this[t]=e}),this)},ready:function(t){return this.each(function(){this.addEventListener("DOMContentLoaded",t)}),this},remove:function(){this.each(function(){Nunjs._events[this]=[],this.remove()})},removeClass:function(t){return this.each(function(){this.classList.contains(t)&&this.classList.remove(t)}),this},removeAttr:function(t){return this.each(function(){this.removeAttribute(t)}),this},removeProp:function(t){return this.each(function(){this[t]=void 0}),this},show:function(){return this.each(function(){"none"==this.style.display&&(this.style.display=""),"none"==getComputedStyle(this,"").getPropertyValue("display")&&(this.style.display="block")}),this},submit:function(){return this.each(function(){this.submit()}),this},text:function(t){var e="";return this.each(function(){void 0===t?e+=this.textContent:this.textContent=t}),void 0===t?e:this},toggle:function(){var t="none"==getComputedStyle(this[0],"").getPropertyValue("display");return this.each(function(){t?$(this).show():$(this).hide()}),this},val:function(t){return void 0===t?this[0].value:(this.each(function(){this.value=t}),this)},length:0};return Nunjs.each(n,function(t,e){i[t]=e,i.length=t}),i},window.$.ajax=function(t,e,n){var i="object"==typeof t?t:"object"==typeof e?e:{};"string"==typeof t&&(i.url=t),void 0!==n&&"string"==typeof n&&(i.type=n);var s=new XMLHttpRequest;if(s.onreadystatechange=function(){4==this.readyState&&(200==this.status?void 0!==i.success&&i.success(this.responseText,this):void 0!==i.error&&i.error(this),void 0!==i.complete&&i.complete())},s.open(void 0!==i.type?i.type:"GET",i.url,void 0===i.async||i.async),void 0!==i.headers)for(var r in i.headers)s.setRequestHeader(r,i.headers[r]);return i.data?(n="object"==typeof i.data?JSON.stringify(i.data):i.data,s.send(n)):s.send(),s},window.$.post=function(t,e){return window.$.ajax(t,e,"POST")},window.$.get=function(t,e){return window.$.ajax(t,e,"GET")},window.$.put=function(t,e){return window.$.ajax(t,e,"PUT")};