-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDOM.min.js
More file actions
1 lines (1 loc) · 12.7 KB
/
DOM.min.js
File metadata and controls
1 lines (1 loc) · 12.7 KB
1
var DOM=function(e){return new DANDOM(e)},DANDOM=function(e){e&&(e.dandom?this.elements=e.elements:"[object Array]"!==Object.prototype.toString.call(e)?this.elements=[e]:this.elements=e)};DANDOM.prototype.dandom=!0,DANDOM.prototype.id=function(e){if(this.elements){if(void 0===e)return this.elements[0].id;this.elements[0].id=e}else{var t=document.getElementById(e);t&&(this.elements=[t])}return this},DANDOM.prototype.select=function(e){var t=this.elements?this.elements[0]:document;void 0!==typeof t||(t=document);var n=new DANDOM;return n.elements=[].slice.call(t.querySelectorAll(e)),n},DANDOM.prototype.find=DANDOM.prototype.select,DANDOM.prototype.byClass=function(e){var t=this.elements?this.elements[0]:document;void 0!==typeof t||(t=document);var n=new DANDOM;return n.elements=[].slice.call(t.getElementsByClassName(e)),n},DANDOM.prototype.byTag=function(e){var t=this.elements?this.elements[0]:document;void 0!==typeof t||(t=document);var n=new DANDOM;return n.elements=[].slice.call(t.getElementsByTagName(e)),n},DANDOM.prototype.children=function(){var e=new DANDOM;return e.elements=[],this.elements.forEach(function(t){e.elements=e.elements.concat([].slice.call(t.children))}),e},DANDOM.prototype.parent=function(e){var t=!e||this.elements[0].parentNode.parentNode.querySelector(e);return t?new DANDOM(this.elements[0].parentNode):new DANDOM(this.elements[0].parentNode).parent(e)},DANDOM.prototype.new=function(e,t){if(e||(e="div"),t){var n="http://www.w3.org/2000/svg";"svg"!==t&&(n=t),this.elements=[document.createElementNS(n,e)],this.elements[0].setAttribute("xmlns",n)}else this.elements=[document.createElement(e)];return this},DANDOM.prototype.newOrFind=function(e,t,n){var o=(new DANDOM).id(t);return o.elements||(o=(new DANDOM).new(e,n).id(t)),o},DANDOM.prototype.class=function(e){return this.elements.forEach(function(t){e?t.className=e:t.removeAttribute("class")}),this},DANDOM.prototype.addClass=function(e){var t=this;return e.split(/[\s,]+/).forEach(function(e){e&&t.elements.forEach(function(t){t.classList?t.classList.add(e):t.className+=" "+e})}),this},DANDOM.prototype.removeClass=function(e){var t=this;return e.split(/[\s,]+/).forEach(function(n){n&&t.elements.forEach(function(t){t.classList?t.classList.remove(n):e&&(t.className=t.className.replace(/clsName| clsName/,""))})}),this},DANDOM.prototype.clone=function(e){return e||(e=!1),new DANDOM(this.elements[0].cloneNode(e))},DANDOM.prototype.css=function(e){return"string"!=typeof e?(this.elements.forEach(function(t){for(var n in e)"transform"!==n?t.style.setProperty(n,e[n]+""):(t.style.setProperty(n,e[n]+""),t.style.webkitTransform=e[n]+"")}),this):this.elements[0].style[e]||""},DANDOM.prototype.attr=function(e){return"object"!=typeof e?this.elements[0].getAttribute(e):(this.elements.forEach(function(t){for(var n in e)"boolean"!=typeof e[n]?e[n]||0===e[n]?t.setAttribute(n,e[n]):t.removeAttribute(n):t[n]=e[n]}),this)},DANDOM.prototype.val=function(e){return void 0!==e?(this.elements.forEach(function(t){t.value=e}),this):this.elements[0].value||""},DANDOM.prototype.transform=function(e){return this.elements.forEach(function(t){var n,o,i,s,r,a=t.style.transform,l={},c="";if(a){n=a.trim().split(")"),o=n.length;for(i in n)n[i]&&(s=n[i].trim().split("("),s[0]&&s[1]&&(l[s[0]]=s[1]));for(r in e)l[r]=e[r]+"";for(r in l)c+=r+"("+l[r]+") ";t.style.transform=c,t.style.webkitTransform=c}}),this},DANDOM.prototype.animate=function(){var e,t={},n=250,o="ease-in-out",i=!1,s=DANDOM.prototype.clientIs("iOS"),r=DANDOM.prototype.clientIs("Safari"),a=function(e,t,n){window.requestAnimationFrame(function(){e.style.setProperty(t,n+""),"transform"===t&&(e.style.webkitTransform=n+"")})};return[].slice.call(arguments).forEach(function(e){var s=typeof e;switch(s){case"number":n=e;break;case"string":o=e;break;case"function":i=e;break;case"object":t=e}}),this.elements.forEach(function(l){l.transitionEndCalled=!1;var c="",u=!0;t.transform?(u=!1,t.transform.indexOf("translate")<0?t.transform+=" translate3d(0,0,0)":t.transform.indexOf("translateZ")<0&&t.transform.indexOf("translate3d")<0&&(t.transform+=" translateZ(0)")):l.style.transform&&l.style.transform.indexOf(/translate/)&&(u=!1),u&&l.style.setProperty("transform",(l.style.transform||"")+" translate3d(0,0,0)");for(e in t)c&&(c+=", "),c+="transform"===e&&(s||r)?"-webkit-"+e+" "+n/1e3+"s "+o:e+" "+n/1e3+"s "+o;l.style.transition=c;for(e in t)a(l,e,t[e]);if(i){l.removeEventListener("transitionend",f);var f=function(e){l.transitionEndCalled=!0,l.removeEventListener("transitionend",f),i(e)};l.addEventListener("transitionend",f),setTimeout(function(){l.transitionEndCalled||f({target:l})},n+10)}}),this},DANDOM.prototype.html=function(e){return void 0!==e?(this.elements.forEach(function(t){t.innerHTML=e}),this):this.elements[0].innerHTML},DANDOM.prototype.text=function(e){return void 0!==e?(this.elements.forEach(function(t){t.textContent=e+""}),this):this.elements[0].textContent},DANDOM.prototype.remove=function(){this.elements&&(this.elements.forEach(function(e){e.parentNode&&e.parentNode.removeChild(e)}),this.elements=!1)},DANDOM.prototype.on=function(e,t){var n=this,o=this.elements;return e.split(/[\s,]+/).forEach(function(e){"click"===e&&/iPhone|iPad|iPod/i.test(navigator.userAgent)&&(n.touch(function(e){"tap"===e.danTouch.what&&t(e)}),o&&o.forEach(function(e){e.addEventListener("click",function(t){e.href&&t.preventDefault()},!1)}),e=""),e&&o.forEach(function(n){o.events||(n.events={}),n.events[e]||(n.events[e]=[]),n.events[e].push(t),n.addEventListener(e,t,!1),"animationend"!==e||setTimeout(function(){n.addEventListener("webkitAnimationEnd",t)},1)})}),this},DANDOM.prototype.off=function(e,t){var n=this.elements;return e.split(/[\s,]+/).forEach(function(e){n.forEach(function(o){"function"==typeof t?o.removeEventListener(e,t):o.events&&n.events[e]&&n.events[e].forEach(function(t){o.removeEventListener(e,t)})})}),this},DANDOM.prototype.touch=function(e){return!!this.exists()&&(this.elements.forEach(function(t){var n={started:!1},o=function(t){n.what="start",n.started||(n.started=!0,n.tapCancel=!1,t.danTouch=n,n.x=t.touches?t.touches[0].pageX:t.pageX,n.y=t.touches?t.touches[0].pageY:t.pageY,n.ox=n.lx=1*n.x,n.oy=n.ly=1*n.y,n.diffx=0,n.diffy=0,n.timeStart=(new Date).getTime(),e(t),t.type.match(/touch/)?(window.addEventListener("touchmove",a),window.addEventListener("touchend",r),window.addEventListener("touchcancel",i)):(window.addEventListener("mousemove",a),window.addEventListener("mouseup",r),document.addEventListener("mouseout",s)))},i=function(e){n.tapCancel=!0},s=function(e){e=e?e:window.event;var t=e.relatedTarget||e.toElement;t&&"HTML"!=t.nodeName||r(e)},r=function(t){n.what="end",t.danTouch=n,n.timeEnd=(new Date).getTime(),n.timeElapsed=n.timeEnd-n.timeStart,e(t),n.timeStart&&n.timeElapsed&&!n.tapCancel&&(t.danTouch.what="tap",e(t)),window.removeEventListener("touchmove",a),window.removeEventListener("touchend",r),window.removeEventListener("touchcancel",i),window.removeEventListener("mousemove",a),window.removeEventListener("mouseup",r),document.removeEventListener("mouseout",s),setTimeout(function(){n.started=!1},10)},a=function(t){n.what="move",n.tapCancel=!0,t.danTouch=n,n.x=t.touches?t.touches[0].pageX:t.pageX,n.y=t.touches?t.touches[0].pageY:t.pageY,n.x>n.lx?(n.swipe="r",n.dirx="r"):(n.swipe="l",n.dirx="l"),n.y>n.ly?n.diry="d":n.diry="u",Math.abs(n.diffy)>Math.abs(n.diffx)&&(n.y>n.ly?n.swipe="d":n.swipe="u"),n.lx=1*n.x,n.ly=1*n.y,n.diffx=n.x-n.ox,n.diffy=n.y-n.oy,e(t)};/iPhone|iPad|iPod/i.test(navigator.userAgent)?t.addEventListener("touchstart",o):(t.addEventListener("touchstart",o),t.addEventListener("mousedown",o))}),this)},DANDOM.prototype.pos=function(){var e=this.elements[0].getBoundingClientRect();e.pageY=e.top+document.body.scrollTop||document.documentElement.scrollTop,e.pageX=e.left+document.body.scrollLeft||document.documentElement.scrollLeft,e.scrollHeight=this.elements[0].scrollHeight,e.scrollWidth=this.elements[0].scrollWidth,e.offsetTop=this.elements[0].offsetTop,e.offsetHeight=this.elements[0].offsetHeight,e.offsetWidth=this.elements[0].offsetWidth;for(var t=this.elements[0],n=0,o=0;null!==t;n+=t.offsetTop,o+=t.offsetLeft,t=t.offsetParent);return e.offsetTop=n,e.offsetLeft=o,e},DANDOM.prototype.trigger=function(e,t){var n;return document["on"+e]?(n=document.createEvent("HTMLEvents"),n.initEvent(e,!0,!1)):"function"==typeof window.CustomEvent?n=new CustomEvent(e,{detail:t}):(n=document.createEvent("CustomEvent"),n.initCustomEvent(e,!0,!0,t)),this.elements.forEach(function(e){e.dispatchEvent(n)}),this},DANDOM.prototype.exists=function(e){return!(!this.elements||!this.elements.length)&&("function"==typeof e&&e(this),!0)},DANDOM.prototype.inDOM=function(e){return!!(this.elements&&this.elements.length&&document.body.contains(this.elements[0]))&&("function"==typeof e&&e(this),!0)},DANDOM.prototype.isVisible=function(e){if(this.elements&&this.elements.length&&document.body.contains(this.elements[0])&&null!==this.elements[0].offsetParent&&"none"!==window.getComputedStyle(this.elements[0]).display){var t=this.elements[0].getBoundingClientRect();if(t.left<window.innerWidth&&t.left+t.width>0&&t.top<window.innerHeight&&t.top+t.height>0)return"function"==typeof e&&e(this),!0}return!1},DANDOM.prototype.appendIf=function(e,t){return document.contains(this.elements[0])||this[e](t),this},DANDOM.prototype.appendTo=function(e){var t=e.elements[0];return this.elements.forEach(function(e){t.appendChild(e)}),this},DANDOM.prototype.append=function(e){return this.elements.forEach(function(t){e.elements.forEach(function(e){t.appendChild(e)})}),this},DANDOM.prototype.appendFirst=function(e){return this.elements.forEach(function(t){e.elements.forEach(function(e){t.firstChild?t.insertBefore(e,t.firstChild):t.appendChild(e)})}),this},DANDOM.prototype.insertBefore=function(e){var t=e.elements[0];return this.elements.forEach(function(e){t.parentNode.insertBefore(e,t)}),this},DANDOM.prototype.insertAfter=function(e){var t=e.elements[0].nextSibling;return this.elements.forEach(function(n){t?t.parentNode.insertBefore(n,t):e.elements[0].parentNode.appendChild(n)}),this},DANDOM.prototype.insertFirst=function(e){var t=e.elements[0];return this.elements.forEach(function(e){t.firstChild?t.insertBefore(e,t.firstChild):t.appendChild(e)}),this},DANDOM.prototype.appendSort=function(e){return this.elements.forEach(function(t){if(e.elements[0].firstChild){var n=!1,o=!1,i=0,s=!1,r=t.getAttribute("data-sort-value");for(isNaN(r)?(s=!0,r+="",r=r.toLowerCase()):r=1*r;!n;){var a=e.elements[0].childNodes[i];if(a&&a.getAttribute){if(s){var l=a.getAttribute("data-sort-value")||"";r<l.toLowerCase()&&(n=!0)}else r>1*a.getAttribute("data-sort-value")&&(n=!0);n&&(n=!0,o=!0,e.elements[0].insertBefore(t,a))}i++,i>=e.elements[0].childNodes.length&&(n=!0)}o||e.elements[0].appendChild(t)}else e.elements[0].appendChild(t)}),this},DANDOM.prototype.sort=function(e){var t=this,n=!1,o=function(e,t){return n?((e.sort||"")+"").localeCompare(t.sort):e.sort-t.sort},i=[].slice.call(this.elements[0].children).map(function(e){var t=e.getAttribute("data-sort-value");return isNaN(t)?n=!0:t=1*t,{sort:t,el:e}}).sort(o);return e&&i.reverse(),i.forEach(function(e){t.elements[0].appendChild(e.el)}),this},DANDOM.prototype.scrollTo=function(e,t,n){t||(t=250),n||(n="scrollTop"),this.elements.forEach(function(o){var i=o[n],s=1e3/60,r=Math.round(t/s),a=e-i||0,l=a/r,c=i,u=e<i,f=Math.round(a/l),d=0,m=0;if(a&&l&&f)var h=setInterval(function(){c+=l,d++,d>f&&(m=1),u?o[n]<=e&&(m=1):o[n]>=e&&(m=1),m?clearInterval(h):window.requestAnimationFrame(function(){o[n]=c})},s)})},DANDOM.prototype.http=function(e){if(e.url){e.type||(e.data?e.type="POST":e.type="GET"),e.data||(e.data="");var t=new XMLHttpRequest;t.open(e.type,e.url,!0),t.timeout=e.timeout||5e3,t.onload=function(){var n=!1;if(t.status>=200&&t.status<400&&(n=t.responseText),e.json)try{n=JSON.parse(t.responseText)}catch(e){console.error(e),n=!1}e.callback&&e.callback(n)},t.onerror=function(){e.callback&&e.callback(!1)},t.ontimeout=function(){e.callback&&e.callback(!1)},t.send(e.data)}},DANDOM.prototype.clientIs=function(e){return window.dandomClient||(window.dandomClient={}),void 0!==typeof window.dandomClient[e]&&("iOS"===e?window.dandomClient[e]=/iPhone|iPad|iPod/i.test(navigator.userAgent):"Safari"===e&&(window.dandomClient[e]=navigator.userAgent.indexOf("Safari")>-1&&navigator.userAgent.indexOf("Chrome")<0)),window.dandomClient[e]},DANDOM.prototype.polyFill=function(){window.isIE=window.navigator.userAgent.indexOf("MSIE")>-1,window.requestAnimationFrame||(document.all&&!window.atob&&(CSSStyleDeclaration.prototype.setProperty=function(e,t){return this.setAttribute(e,t)},window.history.pushState=function(e,t,n){return window.location.href=n,!1}),requestAnimationFrame=function(){return window.webkitRequestAnimationFrame||window.mozRequestAnimationFrame||window.oRequestAnimationFrame||window.msRequestAnimationFrame||function(e,t){window.setTimeout(e,1e3/60)}}())};