-
Notifications
You must be signed in to change notification settings - Fork 2
/
jqlite.min.js
5 lines (4 loc) · 9.35 KB
/
jqlite.min.js
1
2
3
4
5
/*!
* GIT: https://github.com/shrekshrek/jqlite
**/
!function(t,n){"object"==typeof exports&&"undefined"!=typeof module?module.exports=n():"function"==typeof define&&define.amd?define(n):t.$=n()}(this,function(){"use strict";function t(t){return t.replace(/([A-Z])/g,"-$1").toLowerCase()}function n(n,e){return h(e)&&!R[t(n)]?e+"px":e}function e(t){return t._jqlid||(t._jqlid=M++)}function i(t,n,i,r){var s=e(t),a=P[s]||(P[s]=[]),c=o(n);return c.sel=r,c.fn=i,c.id=a.length,a.push(c),c}function r(t,n,i,r){return n=o(n),(P[e(t)]||[]).filter(function(t){return t&&(!n.e||t.e===n.e)&&(!n.ns||n.ns===t.ns)&&(!i||e(t.fn)===e(i))&&(!r||t.sel===r)})}function o(t){var n=(""+t).split(".");return{e:n[0],ns:n.slice(1).sort().join(" ")}}function s(t){return null!=t&&t===t.window}function a(t){return"object"==typeof t}function c(t){return a(t)&&!s(t)&&Object.getPrototypeOf(t)==Object.prototype}function u(t){return"function"==typeof t}function f(t){return"string"==typeof t}function h(t){return"number"==typeof t}function l(t){return"boolean"==typeof t}function d(t){return Array.isArray(t)}function p(t){return t instanceof Date}function v(t){return t instanceof RegExp}function y(t){return t&&1===t.nodeType}function g(t){return d(t)||void 0!==t.length&&h(t.length)}function m(t){(document.attachEvent?"complete"===document.readyState:"loading"!==document.readyState)?t():document.addEventListener("DOMContentLoaded",t,!1)}function b(t,n){var e;if(!t)return new j;if(b.is$(t))return t;if(f(t))t=t.trim(),"<"===t[0]?(E=S.test(t)&&RegExp.$1,E in H||(E="*"),T=H[E],T.innerHTML=""+t,e=T.children):e=b.query(n||document,t);else if(d(t)||t instanceof NodeList||t instanceof HTMLCollection)e=t;else if(a(t))e=[t];else if(u(t))return m(t);return new j(e)}function j(t){var n,e=t?t.length:0;for(n=0;n<e;n++)this[n]=t[n];this.length=e}function w(t,n,e){for(var i in n)e&&(c(n[i])||d(n[i]))?(c(n[i])&&!c(t[i])&&(t[i]={}),d(n[i])&&!d(t[i])&&(t[i]=[]),w(t[i],n[i],e)):void 0!==n[i]&&(t[i]=n[i])}function x(t){if(a(t)){var n=[];for(var e in t)n.push(encodeURIComponent(e)+"="+encodeURIComponent(t[e]));return n.join("&")}return t}function C(t,n,e,i){return b.isFunction(n)&&(i=e,e=n,n=void 0),b.isFunction(e)||(i=e,e=void 0),{url:t,data:n,success:e,dataType:i}}var T,E,L=[],O=L.slice,N=L.indexOf,S=/^\s*<(\w+|!)[^>]*>/,k=document.createElement("table"),q=document.createElement("tr"),H={tr:document.createElement("tbody"),tbody:k,thead:k,tfoot:k,td:q,th:q,"*":document.createElement("div")},A=document.querySelector("head"),M=1,P={},R={"column-count":1,columns:1,"font-weight":1,"line-height":1,opacity:1,"z-index":1,zoom:1};return Object.assign(b,{is$:function(t){return t instanceof j},query:function(t,n){return t.querySelectorAll(n)},extend:function(t){var n,e=O.call(arguments,1);return"boolean"==typeof t&&(n=t,t=e.shift()),e.forEach(function(e){w(t,e,n)}),t},isObject:a,isPlainObject:c,isFunction:u,isString:f,isNumber:h,isBoolean:l,isArray:d,isDate:p,isRegExp:v,isElement:y,contains:function(t,n){return t!==n&&t.contains(n)},each:function(t,n,e){if(g(t)){for(var i=0,r=t.length;i<r;i++)if(!1===n.call(e||t[i],i,t[i]))return t}else for(var o in t)if(!1===n.call(e||t[o],o,t[o]))return t}}),Object.assign(b,{get:function(){return b.ajax(C.apply(null,arguments))},post:function(){var t=C.apply(null,arguments);return t.type="POST",b.ajax(t)},getJSON:function(){var t=C.apply(null,arguments);return t.dataType="json",b.ajax(t)},ajaxJSONP:function(t){t.jsonp=t.jsonp||"callback",t.jsonpCallback=t.jsonpCallback||"jsonpCallback_"+Date.now(),t.type="GET",t.data[t.jsonp]=t.jsonpCallback;var n,e=x(t.data),i=document.createElement("script");A.appendChild(i),window[t.jsonpCallback]=function(e){A.removeChild(i),window[t.jsonpCallback]=null,n=e},i.onload=function(){if(void 0==n)throw"ajax response data is wrong!";t.success&&t.success(n)},i.onerror=function(){t.error&&t.error()},i.src=(t.url+"&"+e).replace(/[&?]{1,2}/,"?")},ajax:function(t){if(void 0!==t){if(t.data=t.data||{},"jsonp"==t.dataType)return b.ajaxJSONP(t);t.type=t.type?t.type.toUpperCase():"GET";var n=new XMLHttpRequest;return n.open(t.type,t.url,!0),("POST"==t.type||t.contentType)&&n.setRequestHeader("Content-Type",t.contentType||"application/x-www-form-urlencoded; charset=UTF-8"),n.onload=function(){if(this.status>=200&&this.status<400){var n;if("arraybuffer"==this.responseType||"blob"==this.responseType)n=this.response;else switch(n=this.responseText,t.dataType){case"json":n=JSON.parse(n);break;case"xml":n=this.responseXML;break;case"script":(0,eval)(n)}t.success&&t.success(n)}},n.onerror=function(){t.error&&t.error()},n.send(x(t.data)),n}}}),Object.assign(j.prototype,{ready:m,get:function(t){return void 0===t?this:this[t]},eq:function(t){return b(-1===t?O.call(this,t):O.call(this,t,t+1))},first:function(){return this.length?this.eq(0):null},last:function(){return this.length?this.eq(this.length-1):null},find:function(t){if(t){var n=new j;return this.each(function(e,i){n.add(b(t,i))}),n}return this},add:function(t,n){for(var e=b(t,n),i=0;i<e.length;i++)this[this.length++]=e[i];return this},each:function(t){return b.each(this,t),this},empty:function(){return this.each(function(t,n){n.innerHTML=""})},parent:function(){var t=new j;return this.each(function(n,e){t.add(e.parentNode)}),t},children:function(){var t=new j;return this.each(function(n,e){t.add(e.children)}),t},clone:function(){var t=new j;return this.each(function(n,e){t.add(e.cloneNode(!0))}),t},remove:function(){return this.each(function(t,n){null!=n.parentNode&&n.parentNode.removeChild(n)})},html:function(t){return 0 in arguments?this.each(function(n,e){e.innerHTML=t}):0 in this?this[0].innerHTML:null},text:function(t){return 0 in arguments?this.each(function(n,e){e.textContent=t}):0 in this?this.textContent:null},val:function(t){return void 0===t?"select"===this[0].nodeName?this[0].options[this[0].selectedIndex].value:this[0].value||this[0].getAttribute("value"):(this.each(function(n,e){if("select"===e.nodeName){for(var i=0,r=e.options.length;i<r;i++)if(e.options[i].value===t){e.options[i].selected=!0;break}}else void 0!==e.value?e.value=t:e.setAttribute("value",t)}),this)},data:function(t,n){if(void 0!==n)this.each(function(e,i){i.dataset[t]=n});else if(t instanceof Object)for(var e in t)this.data(e,t[e]);else if(this[0])return this[0].dataset[t];return this},attr:function(t,n){if(void 0!==n)this.each(function(e,i){i.setAttribute(t,n)});else if(t instanceof Object)for(var e in t)this.attr(e,t[e]);else if(this[0])return this[0].getAttribute(t);return this},prop:function(t,n){if(void 0!==n)this.each(function(e,i){i[t]=n});else if(t instanceof Object)for(var e in t)this.prop(e,t[e]);else if(this[0])return this[0][t];return this},css:function(t,e){if(void 0!==e)e=u(e)?e():n(t,e),this.each(function(n,i){i.style[t]=e});else if(a(t))for(var i in t)this.css(i,t[i]);else if(this[0])return this[0].style[t]||window.getComputedStyle(this[0])[t];return this},width:function(t){if(!this.length)return null;if(s(this[0]))return this[0].innerWidth;if(t){var n=window.getComputedStyle(this[0]);return parseInt(n.marginLeft)+parseInt(n.marginRight)+this[0].offsetWidth}return this[0].offsetWidth},height:function(t){if(!this.length)return null;if(s(this[0]))return this[0].innerHeight;if(t){var n=window.getComputedStyle(this[0]);return(t?parseInt(n.marginTop)+parseInt(n.marginBottom):0)+this[0].offsetHeight}return this[0].offsetHeight},show:function(){this.css("display","block")},hide:function(){this.css("display","none")},toggle:function(){return this.each(function(t,n){"none"==b(n).css("display")?n.show():n.hide()})},index:function(t){return t?N.call(this,b(t)[0]):N.call(this.parent().children(),this[0])},hasClass:function(t){var n=!1;return this.each(function(e,i){i.classList.contains(t)&&(n=!0)}),n},addClass:function(t){return this.each(function(n,e){e.classList.add(t)})},removeClass:function(t){return this.each(function(n,e){e.classList.remove(t)})},toggleClass:function(t){return this.each(function(n,e){e.classList.toggle(t)})},append:function(t){var n=b(t);return this.each(function(t,e){t>0&&(n=n.clone()),n.each(function(t,n){e.appendChild(n)})})},appendTo:function(t){return b(t).append(this),this},prepend:function(t){var n=b(t);return this.each(function(t,e){t>0&&(n=n.clone()),n.each(function(t,n){e.insertBefore(n,e.firstChild)})})},prependTo:function(t){return b(t).prepend(this),this},before:function(t){var n=b(t);return this.each(function(t,e){t>0&&(n=n.clone()),n.each(function(t,n){e.parentNode.insertBefore(n,e.parentNode.firstChild)})})},insertBefore:function(t){return b(t).before(this),this},after:function(t){var n=b(t);return this.each(function(t,e){t>0&&(n=n.clone()),n.each(function(t,n){e.parentNode.insertBefore(n,e.nextSibling)})})},insertAfter:function(t){return b(t).after(this),this},replaceWith:function(t){return this.before(t).remove()},on:function(t,n,e){var r,o,s,a=this;return void 0===e?(o="",s=n,r=function(t){!1===s.call(t.target,t)&&(t.preventDefault(),t.stopPropagation())}):(o=n,s=e,r=function(t){if(-1!==N.call(a.find(o),t.target)){!1===s.call(t.target,t)&&(t.preventDefault(),t.stopPropagation())}}),this.each(function(n,e){var a=i(e,t,s,o);a.proxy=r,e.addEventListener(a.e,a.proxy)})},off:function(t,n,i){var o,s="";return void 0===i?o=n:(s=n,o=i),this.each(function(n,i){var a=e(i);r(i,t,o,s).forEach(function(t){delete P[a][t.id],i.removeEventListener(t.e,t.proxy)})})},trigger:function(t,n){return this.each(function(e,i){var r=document.createEvent("HTMLEvents");r.data=n,r.initEvent(t,!0,!0),i.dispatchEvent(r)})}}),j.prototype.$=j.prototype.find,b});