-
Notifications
You must be signed in to change notification settings - Fork 137
/
bonzo.min.js
6 lines (6 loc) · 11.1 KB
/
bonzo.min.js
1
2
3
4
5
6
/*!
* Bonzo: DOM Utility (c) Dustin Diaz 2012
* https://github.com/ded/bonzo
* License MIT
*/
(function(e,t,n){typeof module!="undefined"&&module.exports?module.exports=n():typeof define=="function"&&define.amd?define(n):t[e]=n()})("bonzo",this,function(){function L(e,n){var r=null,i=t.defaultView.getComputedStyle(e,"");return i&&(r=i[n]),e.style[n]||r}function A(e){return e&&e.nodeName&&(e.nodeType==1||e.nodeType==11)}function O(e,t,n){var r,i,s;if(typeof e=="string")return Q.create(e);A(e)&&(e=[e]);if(n){s=[];for(r=0,i=e.length;r<i;r++)s[r]=V(t,e[r]);return s}return e}function M(e){return new RegExp("(^|\\s+)"+e+"(\\s+|$)")}function _(e,t,n,r){var i,s=0,o=e.length;for(;s<o;s++)i=r?e.length-s-1:s,t.call(n||e[i],e[i],i,e);return e}function D(e,t,n){for(var r=0,i=e.length;r<i;r++)A(e[r])&&(D(e[r].childNodes,t,n),t.call(n||e[r],e[r],r,e));return e}function P(e){return e.replace(/-(.)/g,function(e,t){return t.toUpperCase()})}function H(e){return e?e.replace(/([a-z])([A-Z])/g,"$1-$2").toLowerCase():e}function B(e){e[S]("data-node-uid")||e[E]("data-node-uid",++g);var t=e[S]("data-node-uid");return m[t]||(m[t]={})}function j(e){var t=e[S]("data-node-uid");t&&delete m[t]}function F(e){var t;try{return e===null||e===undefined?undefined:e==="true"?!0:e==="false"?!1:e==="null"?null:(t=parseFloat(e))==e?t:e}catch(n){}return undefined}function I(e,t,n){for(var r=0,i=e.length;r<i;++r)if(t.call(n||null,e[r],r,e))return!0;return!1}function q(e){return e=="transform"&&(e=x.transform)||/^transform-?[Oo]rigin$/.test(e)&&(e=x.transform+"Origin"),e?P(e):null}function R(e,t,n,r){var i=0,s=t||this,o=[],u=k&&typeof e=="string"&&e.charAt(0)!="<"?k(e):e;return _(O(u),function(e,t){_(s,function(r){n(e,o[i++]=t>0?V(s,r):r)},null,r)},this,r),s.length=i,_(o,function(e){s[--i]=e},null,!r),s}function U(e,t,n){var r=Q(e),i=r.css("position"),s=r.offset(),o="relative",u=i==o,a=[parseInt(r.css("left"),10),parseInt(r.css("top"),10)];i=="static"&&(r.css("position",o),i=o),isNaN(a[0])&&(a[0]=u?0:e.offsetLeft),isNaN(a[1])&&(a[1]=u?0:e.offsetTop),t!=null&&(e.style.left=t-s.left+a[0]+w),n!=null&&(e.style.top=n-s.top+a[1]+w)}function z(e,t){return typeof t=="function"?t.call(e,e):t}function W(t,n,r){var i=this[0];return i?t==null&&n==null?($(i)?J():{x:i.scrollLeft,y:i.scrollTop})[r]:($(i)?e.scrollTo(t,n):(t!=null&&(i.scrollLeft=t),n!=null&&(i.scrollTop=n)),this):this}function X(e){this.length=0;if(e){e=typeof e!="string"&&!e.nodeType&&typeof e.length!="undefined"?e:[e],this.length=e.length;for(var t=0;t<e.length;t++)this[t]=e[t]}}function V(e,t){var n=t.cloneNode(!0),r,i,s;if(e.$&&typeof e.cloneEvents=="function"){e.$(n).cloneEvents(t),r=e.$(n).find("*"),i=e.$(t).find("*");for(s=0;s<i.length;s++)e.$(r[s]).cloneEvents(i[s])}return n}function $(t){return t===e||/^(?:body|html)$/i.test(t.tagName)}function J(){return{x:e.pageXOffset||n.scrollLeft,y:e.pageYOffset||n.scrollTop}}function K(e){var t=document.createElement("script"),n=e.match(o);return t.src=n[1],t}function Q(e){return new X(e)}var e=window,t=e.document,n=t.documentElement,r="parentNode",i=/^(checked|value|selected|disabled)$/i,s=/^(select|fieldset|table|tbody|tfoot|td|tr|colgroup)$/i,o=/\s*<script +src=['"]([^'"]+)['"]>/,u=["<table>","</table>",1],a=["<table><tbody><tr>","</tr></tbody></table>",3],f=["<select>","</select>",1],l=["_","",0,1],c={thead:u,tbody:u,tfoot:u,colgroup:u,caption:u,tr:["<table><tbody>","</tbody></table>",2],th:a,td:a,col:["<table><colgroup>","</colgroup></table>",2],fieldset:["<form>","</form>",1],legend:["<form><fieldset>","</fieldset></form>",2],option:f,optgroup:f,script:l,style:l,link:l,param:l,base:l},h=/^(checked|selected|disabled)$/,p,d,v,m={},g=0,y=/^-?[\d\.]+$/,b=/^data-(.+)$/,w="px",E="setAttribute",S="getAttribute",x=function(){var e=t.createElement("p");return{transform:function(){var t=["transform","webkitTransform","MozTransform","OTransform","msTransform"],n;for(n=0;n<t.length;n++)if(t[n]in e.style)return t[n]}(),classList:"classList"in e}}(),T=/\s+/,N=String.prototype.toString,C={lineHeight:1,zoom:1,zIndex:1,opacity:1,boxFlex:1,WebkitBoxFlex:1,MozBoxFlex:1},k=t.querySelectorAll&&function(e){return t.querySelectorAll(e)};return x.classList?(p=function(e,t){return e.classList.contains(t)},d=function(e,t){e.classList.add(t)},v=function(e,t){e.classList.remove(t)}):(p=function(e,t){return M(t).test(e.className)},d=function(e,t){e.className=(e.className+" "+t).trim()},v=function(e,t){e.className=e.className.replace(M(t)," ").trim()}),X.prototype={get:function(e){return this[e]||null},each:function(e,t){return _(this,e,t)},deepEach:function(e,t){return D(this,e,t)},map:function(e,t){var n=[],r,i;for(i=0;i<this.length;i++)r=e.call(this,this[i],i),t?t(r)&&n.push(r):n.push(r);return n},html:function(e,t){var n=t?"textContent":"innerHTML",r=this,i=function(t,n){_(O(e,r,n),function(e){t.appendChild(e)})},o=function(r,o){try{if(t||typeof e=="string"&&!s.test(r.tagName))return r[n]=e}catch(u){}i(r,o)};return typeof e!="undefined"?this.empty().each(o):this[0]?this[0][n]:""},text:function(e){return this.html(e,!0)},append:function(e){var t=this;return this.each(function(n,r){_(O(e,t,r),function(e){n.appendChild(e)})})},prepend:function(e){var t=this;return this.each(function(n,r){var i=n.firstChild;_(O(e,t,r),function(e){n.insertBefore(e,i)})})},appendTo:function(e,t){return R.call(this,e,t,function(e,t){e.appendChild(t)})},prependTo:function(e,t){return R.call(this,e,t,function(e,t){e.insertBefore(t,e.firstChild)},1)},before:function(e){var t=this;return this.each(function(n,i){_(O(e,t,i),function(e){n[r].insertBefore(e,n)})})},after:function(e){var t=this;return this.each(function(n,i){_(O(e,t,i),function(e){n[r].insertBefore(e,n.nextSibling)},null,1)})},insertBefore:function(e,t){return R.call(this,e,t,function(e,t){e[r].insertBefore(t,e)})},insertAfter:function(e,t){return R.call(this,e,t,function(e,t){var n=e.nextSibling;n?e[r].insertBefore(t,n):e[r].appendChild(t)},1)},replaceWith:function(e){var t=this;return this.each(function(n,i){_(O(e,t,i),function(e){n[r]&&n[r].replaceChild(e,n)})})},clone:function(e){var t=[],n,r;for(r=0,n=this.length;r<n;r++)t[r]=V(e||this,this[r]);return Q(t)},addClass:function(e){return e=N.call(e).split(T),this.each(function(t){_(e,function(e){e&&!p(t,z(t,e))&&d(t,z(t,e))})})},removeClass:function(e){return e=N.call(e).split(T),this.each(function(t){_(e,function(e){e&&p(t,z(t,e))&&v(t,z(t,e))})})},hasClass:function(e){return e=N.call(e).split(T),I(this,function(t){return I(e,function(e){return e&&p(t,e)})})},toggleClass:function(e,t){return e=N.call(e).split(T),this.each(function(n){_(e,function(e){e&&(typeof t!="undefined"?t?!p(n,e)&&d(n,e):v(n,e):p(n,e)?v(n,e):d(n,e))})})},show:function(e){return e=typeof e=="string"?e:"",this.each(function(t){t.style.display=e})},hide:function(){return this.each(function(e){e.style.display="none"})},toggle:function(e,t){return t=typeof t=="string"?t:"",typeof e!="function"&&(e=null),this.each(function(n){n.style.display=n.offsetWidth||n.offsetHeight?"none":t,e&&e.call(n)})},first:function(){return Q(this.length?this[0]:[])},last:function(){return Q(this.length?this[this.length-1]:[])},next:function(){return this.related("nextSibling")},previous:function(){return this.related("previousSibling")},parent:function(){return this.related(r)},related:function(e){return Q(this.map(function(t){t=t[e];while(t&&t.nodeType!==1)t=t[e];return t||0},function(e){return e}))},focus:function(){return this.length&&this[0].focus(),this},blur:function(){return this.length&&this[0].blur(),this},css:function(n,r){function o(e,t,n){for(var r in s)if(s.hasOwnProperty(r)){n=s[r],(t=q(r))&&y.test(n)&&!(t in C)&&(n+=w);try{e.style[t]=z(e,n)}catch(i){}}}var i,s=n;return r===undefined&&typeof n=="string"?(r=this[0],r?r===t||r===e?(i=r===t?Q.doc():Q.viewport(),n=="width"?i.width:n=="height"?i.height:""):(n=q(n))?L(r,n):null:null):(typeof n=="string"&&(s={},s[n]=r),this.each(o))},offset:function(e,n){if(!e||typeof e!="object"||typeof e.top!="number"&&typeof e.left!="number"){if(typeof e=="number"||typeof n=="number")return this.each(function(t){U(t,e,n)});if(!this[0])return{top:0,left:0,height:0,width:0};var r=this[0],i=r.ownerDocument.documentElement,s=r.getBoundingClientRect(),o=J(),u=r.offsetWidth,a=r.offsetHeight,f=s.top+o.y-Math.max(0,i&&i.clientTop,t.body.clientTop),l=s.left+o.x-Math.max(0,i&&i.clientLeft,t.body.clientLeft);return{top:f,left:l,height:a,width:u}}return this.each(function(t){U(t,e.left,e.top)})},dim:function(){if(!this.length)return{height:0,width:0};var e=this[0],t=e.nodeType==9&&e.documentElement,n=!t&&!!e.style&&!e.offsetWidth&&!e.offsetHeight?function(t){var n={position:e.style.position||"",visibility:e.style.visibility||"",display:e.style.display||""};return t.first().css({position:"absolute",visibility:"hidden",display:"block"}),n}(this):null,r=t?Math.max(e.body.scrollWidth,e.body.offsetWidth,t.scrollWidth,t.offsetWidth,t.clientWidth):e.offsetWidth,i=t?Math.max(e.body.scrollHeight,e.body.offsetHeight,t.scrollHeight,t.offsetHeight,t.clientHeight):e.offsetHeight;return n&&this.first().css(n),{height:i,width:r}},attr:function(e,t){var n=this[0],r;if(typeof e=="string"||e instanceof String)return typeof t=="undefined"?n?i.test(e)?h.test(e)&&typeof n[e]=="string"?!0:n[e]:n[S](e):null:this.each(function(n){i.test(e)?n[e]=z(n,t):n[E](e,z(n,t))});for(r in e)e.hasOwnProperty(r)&&this.attr(r,e[r]);return this},removeAttr:function(e){return this.each(function(t){h.test(e)?t[e]=!1:t.removeAttribute(e)})},val:function(e){return typeof e=="string"||typeof e=="number"?this.attr("value",e):this.length?this[0].value:null},data:function(e,t){var n=this[0],r,i;return typeof t=="undefined"?n?(r=B(n),typeof e=="undefined"?(_(n.attributes,function(e){(i=(""+e.name).match(b))&&(r[P(i[1])]=F(e.value))}),r):(typeof r[e]=="undefined"&&(r[e]=F(this.attr("data-"+H(e)))),r[e])):null:this.each(function(n){B(n)[e]=t})},remove:function(){return this.deepEach(j),this.detach()},empty:function(){return this.each(function(e){D(e.childNodes,j);while(e.firstChild)e.removeChild(e.firstChild)})},detach:function(){return this.each(function(e){e[r]&&e[r].removeChild(e)})},scrollTop:function(e){return W.call(this,null,e,"y")},scrollLeft:function(e){return W.call(this,e,null,"x")}},Q.setQueryEngine=function(e){k=e,delete Q.setQueryEngine},Q.aug=function(e,t){for(var n in e)e.hasOwnProperty(n)&&((t||X.prototype)[n]=e[n])},Q.create=function(e){return typeof e=="string"&&e!==""?function(){if(o.test(e))return[K(e)];var n=e.match(/^\s*<([^\s>]+)/),i=t.createElement("div"),s=[],u=n?c[n[1].toLowerCase()]:null,a=u?u[2]+1:1,f=u&&u[3],l=r;i.innerHTML=u?u[0]+e+u[1]:e;while(a--)i=i.firstChild;f&&i&&i.nodeType!==1&&(i=i.nextSibling);do(!n||i.nodeType==1)&&s.push(i);while(i=i.nextSibling);return _(s,function(e){e[l]&&e[l].removeChild(e)}),s}():A(e)?[e.cloneNode(!0)]:[]},Q.doc=function(){var e=Q.viewport();return{width:Math.max(t.body.scrollWidth,n.scrollWidth,e.width),height:Math.max(t.body.scrollHeight,n.scrollHeight,e.height)}},Q.firstChild=function(e){for(var t=e.childNodes,n=0,r=t&&t.length||0,i;n<r;n++)t[n].nodeType===1&&(i=t[r=n]);return i},Q.viewport=function(){return{width:e.innerWidth,height:e.innerHeight}},Q.isAncestor="compareDocumentPosition"in n?function(e,t){return(e.compareDocumentPosition(t)&16)==16}:function(e,t){return e!==t&&e.contains(t)},Q})