dollar-js
Version:
A lighter, faster, modular jQuery replacement (manipulate DOM, bind events, and more...)
6 lines • 16.3 kB
JavaScript
/*!
* DollarJS 2.2.1 -- a light, fast, modular, jQuery replacement
* Github: https://github.com/seebigs/dollar-js
* Released under the MIT license: https://opensource.org/licenses/MIT
*/
(function(){var t,n,e=function(t,n){return new e.fn.init(t,n)},i="string",r="function",o=window,f=o.Element.prototype,s=Object.prototype,a=s.toString,u=s.hasOwnProperty,l=Array.prototype,c=l.slice,h=document,p=document.documentElement,d=/[\s\t\r\n\f]+/g;function y(){var t,i,r,o=e();return n.each(c.call(arguments),function(n){if(n)for(t=0,i=n.length;t<i;t++)r=n[t],-1===o.indexOf(r)&&o.push(r)}),o}e.isDollar=!0,e.fn={isDollar:!0,indexOf:l.indexOf,push:l.push,pop:l.pop,shift:l.shift,unshift:l.unshift,slice:l.slice,splice:l.splice},e.fn.init=function(t,e){if(this.length=0,!t)return this;if(!e){if(/^#[\w-]+$/.test(t)){var i=h.getElementById(t.substr(1));return i&&(this[0]=i,this.length=1),this}if(/^[a-z]+$/.test(t)){for(var r=h.getElementsByTagName(t),o=r.length,f=0;f<o;f++)this[f]=r[f];return this.length=o,this}}return n.merge(this,g(t,e))},e.fn.init.prototype=e.fn;var v={contains:function(t,n,e){var i=e[1]&&e[1].replace(/["')]/g,"");return i?T(b(t,n),function(t){return-1!==(t.textContent||t.innerText).indexOf(i)}):[]},hidden:function(t,n){return T(b(t,n),function(t){return 1===t.nodeType&&!(t.offsetWidth||t.offsetHeight||t.getClientRects().length)})},visible:function(t,n){return T(b(t,n),function(t){return 1===t.nodeType&&!!(t.offsetWidth||t.offsetHeight||t.getClientRects().length)})},even:function(t,n){return c.call(n.querySelectorAll(t+":nth-child(even)"))},odd:function(t,n){return c.call(n.querySelectorAll(t+":nth-child(odd)"))},has:function(t,n,e){var r=typeof e[1]===i&&e[1].replace(")","");return r?T(g(t,n),function(t){return 1===t.nodeType&&!!g(r,t).length}):[]},not:function(t,e,i){e===p&&(e=h);var r=t;"*"!==t&&" "===t[t.length-1]&&(r=t+"*");var o=i[1].split(")"),f=o[1],s=(o[0]||"").split(",");f&&(r+=f);var a=[],u=b(r,e);return n.each(u,function(t){var e=!0;n.each(s,function(n){if(E.call(t,n))return e=!1,!1}),e&&a.push(t)}),a}};function m(t,e){if(e){var i=N(e),o=[];return n.each(i,function(e){n.each(t,function(t){typeof e.contains===r&&e.contains(t)&&o.push(t)})}),o}return t}function g(t,e){if(typeof t===i)return b(t,e);if(t.isDollar)return m(t.get(),e);if(t.nodeType)return m([t],e);if(t===t.window)return[t];if(t.length){for(var o,s=[],a=0,u=t.length;a<u;a++)o=t[a],n.isElement(o)&&s.push(o);return m(s,e)}function l(){var t=h.readyState;return"interactive"===t||"complete"===t}return typeof t===r&&(l()?t():f.addEventListener?h.addEventListener("DOMContentLoaded",t,!1):h.attachEvent("onreadystatechange",function(){l()&&t()})),[]}function b(t,e){if(e){var i=[];if((e=N(e)).length>1){for(var o=0,f=e.length;o<f;o++)n.merge(i,b(t,e[o]));return i}e=e[0]}else e=p;if(!e)return[];var s=/^\s*(?:#([\w-]+)|(\w+)|\.([\w-]+)|(<[\w\W]+>)[^>]*)\s*$/.exec(t);if(s){if(t=s[1]){var a=h.getElementById(t);return a&&e!==a&&e.contains(a)?[a]:[]}if(t=s[2])return e.getElementsByTagName(t);if(t=s[3])return typeof e.getElementsByClassName===r?e.getElementsByClassName(t):e.querySelectorAll(t);if(t=s[4])return[x(t)]}else{var u=/(.*):(.+)/.exec(t);if(u){var l=u[1]||"*",d=u[2].split("("),y=v[d[0]];if(y)return y(l,e,d)}}return c.call(e.querySelectorAll(t))}var E=f.matches||f.webkitMatchesSelector||f.mozMatchesSelector||f.msMatchesSelector||f.oMatchesSelector||w;function w(t){var n=b(t);return-1!==l.indexOf.call(n,this)}function C(t,n,e){return!(!n||!t||1!==t.nodeType)&&(typeof n!==i?n.nodeType?t===n:typeof n===r?!!n.call(t,t,e):!!n.length&&-1!==n.indexOf(t):E.call(t,n))}function T(t,n){for(var e=[],i=0,r=t.length;i<r;i++)C(t[i],n,i)&&e.push(t[i]);return e}function x(t){var n=/^<(\w+)\s*\/?>(?:<\/\1>|)$/.exec(t);if(n)return h.createElement(n[1]);var e=h.createElement("div");return e.innerHTML=t,e.childNodes[0]}function N(t){return typeof t===i?b(t):t.isDollar?t.get():1===t.nodeType||9===t.nodeType?[t]:Array.isArray(t)?t:[p]}var O="dollar-node-id",S=1,A={},D={};function j(t){var n=t&&t.nodeType;return n&&3!==n&&8!==n&&2!==n}function H(n){return 9===n.nodeType&&(n=n.documentElement),j(n)?n:t}function L(n,e){if(n)return n===n.window?n[e]:(n=H(n))&&n.hasAttribute(e)?n.getAttribute(e):t}function M(t,n,e){return t===t.window&&(t[n]=e),(t=H(t))&&t.setAttribute(n,e)}function q(n,e){return n===n.window&&(n[e]=t),(n=H(n))&&n.removeAttribute(e)}function B(n){return Number(L(n,O))||t}function W(t,n){return M(t,O,n)}function $(t,n,e){var i=B(n);if(i)return e?t[i]&&t[i][e]:t[i]}function k(t,n,e,i){var r=B(n);r||(W(n,r=S),S++),t[r]||(t[r]={}),t[r][e]=i}function I(t,n,e,i){var r=$(t,n,e)||[];r.push(i),k(t,n,e,r)}function z(t,n,e){var i=n+" "+F(e),r=t.style.transition?t.style.transition.split(/,\s?/):[],o=!1;return r.forEach(function(t,e){0===t.indexOf(n+" ")&&(r[e]=i,o=!0)}),o||r.push(i),r.join(", ")}function V(t,n){var e=[];return t.style.transition.split(/,\s?/).forEach(function(t){0!==t.indexOf(n+" ")&&e.push(t)}),e.join(", ")}function F(t){var n=[];return n.push(typeof t.duration===i?t.duration:(parseInt(t.duration)||400)+"ms"),n.push(t.easing||"ease"),n.push(typeof t.delay===i?t.delay:(parseInt(t.delay)||0)+"ms"),n.join(" ")}function P(t,n){var e,o,f,s,a,u,l=(t=[].concat.apply([],t)).length,c=this.length;function p(t){a.appendChild(t),f=!0}for(o=0;o<c;o++){for(f=!1,a=h.createDocumentFragment(),e=0;e<l;e++)(s=t[e])&&(typeof s===i?(u=x(s))&&p(u):1===s.nodeType||11===s.nodeType?p(s):s.isDollar?s.each(p):typeof s===r&&(typeof(u=s(this[o],o))===i&&(u=x(u)),u&&p(u)));f&&n(this[o],a)}return this}function R(t){return t&&t.dataset||function(){var e,i={},r=t.attributes,o=/^data-[a-z_\-\d]*$/i;for(var f in r)r.hasOwnProperty(f)&&(e=r[f].name,o.test(e)&&(i[e=n.format.dashToCamel(e.substr(5))]=r[f].value));return i}()}function U(n,e){return j(n)?n[e]:t}e.utils=n=function(){var n="[object ";function e(t){return!!t&&(1===t.nodeType||9===t.nodeType)}function i(t){return a.call(t)===n+"Object]"}function r(n,e){if(n)if(n.length!==t){for(var i=0,r=n.length;i<r;i++)if(!1===e.call(n[i],n[i],i,n))return}else for(var o in n)if(u.call(n,o)&&!1===e.call(n[o],n[o],o,n))return}function o(){var n=arguments[0],e=function(e,i){e!==t&&(n[i]=e)};return r(c.call(arguments,1),function(t){r(t,e)}),n}function f(){var t,n,e=arguments[0];return r(c.call(arguments,1),function(i){if(i)for(t=0,n=i.length;t<n;t++)-1===e.indexOf(i[t])&&e.push(i[t])}),e}return{isElement:e,isObject:i,each:r,extend:o,merge:f,format:{camelToDash:function(t){return t.replace(/([A-Z])/g,"-$1").toLowerCase()},dashToCamel:function(t){return t.replace(/-(.)/g,function(t,n){return n.charAt(0).toUpperCase()})}}}}(),e.fn.closest=function(t,n){if(!t)return e();for(var i,r=g(t,n),o=[],f=0,s=this.length;f<s;f++)for(i=this[f];i&&i!==n;){if(-1!==l.indexOf.call(r,i)){o.push(i);break}i=i.parentNode}return y(o)},e.fn.each=e.fn.forEach=function(t){return n.each(this,t),this},e.fn.eq=function(t){return t=Array.isArray(t)?NaN:parseInt(t,10),e(t>=0?this[t]:this[this.length+t])},e.fn.filter=function(t){return this.length&&t?y(T(this,t)):e()},e.fn.find=function(t){return t&&this.length?y(g(t,this)):e()},e.fn.get=function(n){return n===t?c.call(this,0):n<0?this[n+this.length]:this[n]},e.fn.reverse=l.reverse,e.fn.animate=function(t,e,i){n.isObject(e)||(e={duration:e});var o="transitionend";return this.each(function(f,s){n.each(t,function(t,n){f.style.transition=z(f,n,e),f.style[n]=t;var a=function(t){f.removeEventListener(o,a,!0),f.style.transition=V(f,t),typeof i===r&&i.call(f,f,s)};f.addEventListener(o,a,!0)})}),this},e.fn.fadeIn=function(t,n){return this.animate({opacity:1},t,n)},e.fn.fadeOut=function(t,n){return this.animate({opacity:0},t,n)},e.fn.add=function(t,n){return t?y(this,e(t,n)):this},e.fn.concat=function(){var t=c.call(arguments);return t.unshift(this),y.apply(this,t)},e.fn.has=function(t){return t?this.filter(function(){return!!g(t,this).length}):e()},e.fn.is=function(t){return!(!t||!this.filter(t).length)},e.fn.map=function(t){if(typeof t!==r)return this;for(var e,i=[],o=0,f=this.length;o<f;o++){if(e=t.call(this[o],this[o],o,this),!n.isElement(e))throw new Error(".map fn should return an Element, not "+typeof e);i.push(e)}return y.call(this,i)},e.fn.not=function(t){return t?(n=typeof t===r?function(n,e){return!t.call(n,e,n)}:function(n,e){return!C(n,t,e)},y(this.filter(n))):this;var n},e.fn.after=function(){return P.call(this,arguments,function(t,n){var e=t.parentNode;e&&(t.nextSibling?e.insertBefore(n,t.nextSibling):e.appendChild(n))})},e.fn.append=function(){return P.call(this,arguments,function(t,n){t.appendChild(n)})},e.fn.before=function(){return P.call(this,arguments,function(t,n){t.parentNode&&t.parentNode.insertBefore(n,t)})},e.fn.clone=function(){var t,n,e=this.length;for(n=0;n<e;n++)1!==(t=this[n]).nodeType&&11!==t.nodeType||(this[n]=t.cloneNode(!0));return this},e.fn.empty=function(){var t,n,e=this.length;for(n=0;n<e;n++)1===(t=this[n]).nodeType&&(t.textContent="");return this},e.fn.html=function(n){var e,i,r=this.length,o=this[0];if(n===t)return o&&1===o.nodeType?o.innerHTML:t;try{for(i=0;i<r;i++)1===(e=this[i]).nodeType&&(e.innerHTML=n)}catch(t){this.empty().append(n)}return this},e.fn.prepend=function(){return P.call(this,arguments,function(t,n){t.firstChild?t.insertBefore(n,t.firstChild):t.appendChild(n)})},e.fn.remove=function(n){var e,i,r=this.length;if(n===t)for(i=0;i<r;i++)(e=this[i]).parentNode&&e.parentNode.removeChild(e);else for(i=0;i<r;i++)C(e=this[i],n,i)&&e.parentNode&&e.parentNode.removeChild(e);return this},e.fn.attr=function(n,e){return e===t?L(this[0],n):(this.each(function(t,i){j(t)&&(typeof e===r&&(e=e(L(t,n),i)),t.setAttribute(n,e))}),this)},e.fn.data=function(e,r){if(!this.length)return t;var o,f={};if(!e)return n.extend({},R(this[0]),$(A,this[0]));if(typeof e===i){if(r===t){var s=$(A,this[0],e);return s===t?R(this[0])[e]:s}f[e]=r}else n.isObject(e)&&(f=e);function a(t,n){k(A,o,n,t)}for(var u=0,l=this.length;u<l;u++)o=this[u],n.each(f,a);return this},e.fn.prop=function(n,e){return e===t?U(this[0],n):(this.each(function(t,i){j(t)&&(typeof e===r&&(e=e(U(t,n),i)),t[n]=e)}),this)},e.fn.removeAttr=function(t){return this.each(function(){this.removeAttribute(t)}),this},e.fn.removeData=function(t){for(var e,i,r=0,o=this.length;r<o;r++)i=B(e=this[r]),t?(i&&delete A[i][t],e&&(e.dataset?e.dataset[t]&&delete e.dataset[t]:q(e,"data-"+n.format.camelToDash(t)))):A[i]={};return this},e.fn.removeProp=function(t){return this.each(function(){j(this)&&delete this[t]}),this},e.fn.text=function(n){if(n!==t)return this.each(function(t,e){1!==t.nodeType&&11!==t.nodeType&&9!==t.nodeType||(t.textContent=typeof n===r?n(t.textContent,e):n)}),this;var e="";return this.each(function(t){var n=t.nodeType;1===n||9===n||11===n?typeof t.textContent===i&&(e+=t.textContent):3!==n&&4!==n||(e+=t.nodeValue)}),e},e.fn.val=function(n){if(n===t)return this[0]?this[0].value:t;for(var e=0;e<this.length&&1===this[e].nodeType;e++)typeof n===r&&(n=n.call(this[e],this[e].value,e)),this[e].value=n;return this};var Z=o.getComputedStyle!==t?_:G;function _(t,n){return t&&typeof n===i?t.ownerDocument.defaultView.opener?t.ownerDocument.defaultView.getComputedStyle(t,null)[n]||"":o.getComputedStyle(t,null)[n]||t.style[n]||"":""}function G(t,e){var r;return e=typeof e===i?e:"",t?(r="float"===e?"styleFloat":n.format.dashToCamel(e.replace(/^-ms-/,"ms-")),t.currentStyle[r]):""}function J(t){var n=t.style.display;if(n&&"none"!==n||(n=$(D,t,"nonHiddenDisplayValue")||""),!n&&t.parentNode){var e=h.createElement(t.nodeName);t.parentNode.appendChild(e),n=Z(e,"display"),t.parentNode.removeChild(e),k(D,t,"nonHiddenDisplayValue",n)}return n}function K(){return Math.max(p.offsetHeight,p.scrollHeight)}function Q(){return Math.max(p.offsetWidth,p.scrollWidth)}function X(){return Math.max(p.clientHeight,o.innerHeight)}function Y(){return Math.max(p.clientWidth,o.innerWidth)}e.fn.addClass=function(t){if(!t)return this;var e,o,f,s=this.length;if(typeof t===i)for(e=t.trim().split(" "),f=0;f<s;f++)o=this[f].className.trim().replace(d," ").split(" "),this[f].className=n.merge([],o,e).join(" ");else if(typeof t===r)for(f=0;f<s;f++)e=t.call(this[f],this[f].className,f).split(" "),o=this[f].className.trim().replace(d," ").split(" "),this[f].className=n.merge([],o,e).join(" ");return this},e.fn.css=function(e,o){if(!e)return this;var f,s,a=this.length,u={};if(typeof e===i){if(o===t)return Z(this[0],e);u[e]=o}else n.isObject(e)&&(u=e);function l(t,n){s.style[n]=typeof t===r?t.call(s,Z(s,n),f):t}for(f=0;f<a;f++)s=this[f],n.each(u,l);return this},e.fn.hasClass=function(t){if(!t)return!1;t=" "+t.trim()+" ";for(var n=0,e=this.length;n<e;n++)if(1===this[n].nodeType&&-1!==(" "+this[n].className+" ").replace(d," ").indexOf(t))return!0;return!1},e.fn.height=function(){var t=this[0];return t===window?X():t===document?K():parseFloat(this.eq(0).css("height"))||0},e.fn.hide=function(){return this.each(function(){this.style.display="none"}),this},e.fn.removeClass=function(t){var e,o,f,s;function a(t){-1===s.indexOf(t)&&o.push(t)}for(var u=0,l=this.length;u<l;u++)e=this[u],o=[],t?(s=typeof t===r?t.call(e,e.className,u):t).length&&(s=typeof s===i?s.trim().split(" "):s,f=e.className.replace(d," ").split(" "),n.each(f,a),e.className=o.join(" ")):e.className="";return this},e.fn.show=function(){return this.each(function(){this.style.display=J(this),this.style.visibility="visible",this.style.opacity=1}),this},e.fn.width=function(){var t=this[0];return t===window?Y():t===document?Q():parseFloat(this.eq(0).css("width"))||0},e.fn.children=function(t){for(var i=[],r=0,o=this.length;r<o;r++)n.merge(i,this[r].children);return y(t?e.fn.filter.call(i,t):i)},e.fn.first=function(){return this.eq(0)},e.fn.last=function(){return this.eq(-1)},e.fn.next=function(t){for(var n,i=[],r=0,o=this.length;r<o;r++)(n=this[r].nextElementSibling)&&i.push(n);return y(t?e.fn.filter.call(i,t):i)},e.fn.parent=function(t){for(var n,i=[],r=0,o=this.length;r<o;r++)(n=this[r].parentNode)&&i.push(n);return y(t?e.fn.filter.call(i,t):i)},e.fn.prev=function(t){for(var n,i=[],r=0,o=this.length;r<o;r++)(n=this[r].previousElementSibling)&&i.push(n);return y(t?e.fn.filter.call(i,t):i)},e.fn.siblings=function(t){for(var n,i=[],r=0,o=this.length;r<o;r++)for(this[r].parentNode&&(n=this[r].parentNode.firstChild);n;)n!==this[r]&&1===n.nodeType&&i.push(n),n=n.nextSibling;return y(t?e.fn.filter.call(i,t):i)};var tt="activeEventListeners",nt=[];function et(t,e){if(typeof t!==i||typeof e!==r)return this;function o(){if(nt.length)try{e.apply(this,c.call(arguments))}catch(t){n.each(nt,function(n){typeof n===r&&n(t)})}else e.apply(this,c.call(arguments))}var f,s,a;return e.wrappedHandler=o,t=t.split(" "),this.each(function(){for(f=this.addEventListener||this.attachEvent,s=0,a=t.length;s<a;s++)f.call(this,t[s],o,!1),I(D,this,tt,e)}),this}function it(t,n){return typeof t!==i?this:(t=t.split(" "),this.each(function(){for(u=this.removeEventListener||this.detachEvent,e=0,o=t.length;e<o;e++)for(f=typeof n===r?[n]:$(D,this,tt)||[],s=0,a=f.length;s<a;s++)u.call(this,t[e],f[s].wrappedHandler,!1)}),this);var e,o,f,s,a,u}function rt(t,e,i){var r,f={bubbles:!0,cancelable:!0};i&&i.length&&(f.detail=i),n.each(e,function(e){n.each(t,function(t){"click"===e?t.click():"focus"===e?t.focus():"blur"===e?t.blur():(r=new o.CustomEvent(e,f),t.dispatchEvent(r))})})}function ot(t,n){return typeof n===r?et.call(this,t,n):(rt(this,t.split(" ")),this)}e.onEventError=function(t){nt.push(t)},e.fn.click=function(t){return ot.call(this,"click",t)},e.fn.focus=function(t){return ot.call(this,"focus",t)},e.fn.blur=function(t){return ot.call(this,"blur",t)},e.fn.change=function(t){return ot.call(this,"change",t)},e.fn.resize=function(t){return ot.call(this,"resize",t)},e.fn.off=e.fn.unbind=it,e.fn.on=e.fn.bind=et,e.fn.trigger=function(t){return typeof t!==i?this:(rt(this,t=t.split(" "),c.call(arguments,1)),this)},function(t){function n(t,n){n=n||{bubbles:!1,cancelable:!1};var e=document.createEvent("CustomEvent");return e.initCustomEvent(t,n.bubbles,n.cancelable,n.detail),e}typeof t.CustomEvent!==r&&(n.prototype=t.Event.prototype,t.CustomEvent=n)}(o),function(){var t=window;"undefined"!=typeof module&&module.exports?module.exports=e:typeof t.define===r&&t.define.amd?t.define(function(){return e}):t.$=e}.call(this)}).call(this);