-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdom.min.js
8 lines (8 loc) · 12.8 KB
/
dom.min.js
1
2
3
4
5
6
7
8
/**
* Copyright (c) 2016 Santhosh Kumar, Krishnan
* @license The MIT License (MIT)
* @description Perform DOM manipulation.
* @author Santhosh Kumar Krishnan, https://www.linkedin.com/in/ksankumar
* @version v1.0.1
**/
!function(t,e){"function"==typeof define&&define.amd?define("dom",e(t)):"object"==typeof exports?module.exports=e(t):t.$=t.dom=e(t)}("undefined"!=typeof global?global:this.window||this.global,function(t){"use strict";function e(t,e){return this.length=t?t.length:0,this.selector=e||null,e?b.extend(this,t):this}var n,i,r,o,s=t.document,a=Element.prototype,u=["webkitMatchesSelector","mozMatchesSelector","msMatchesSelector","oMatchesSelector"],c=function(){},f=["innerHeight"in t?t.innerHeight:s.documentElement.clientHeight,"innerWidth"in t?t.innerWidth:s.documentElement.clientWidth],h=window.addEventListener?"addEventListener":"attachEvent",l=window.removeEventListener?"removeEventListener":"detachEvent",d="addEventListener"!==h?"on":"",p={TAGS:"body header section aside article nav address abbr area audio b base basefont bdi bdo blockquote button canvas center cite code col colgroup datalist dd delement details dfn dialog div dl dt em embed fielementdset figcaptain figure font footer form h1 h2 h3 h4 h5 h6 head hr i iframe img input lable legend li link main map mark menu menuitem meter object ol optgroup option output p param pre progress q rp rt s selementect small source span strong sub summary sup table thead tbody tr th td teatarea tfoot time title track u ul video wbr".split(" "),ID:/^(?:#([\w-]*))dom/,CLASS:/^(?:#([\w-]*))dom/,TAG:/<([\w-]*)>/},g=/^(?:input|select|textarea|button)$/i,y={animationIterationCount:1,columnCount:1,fillOpacity:1,flexGrow:1,flexShrink:1,fontWeight:1,lineHeight:1,opacity:1,order:1,orphans:1,widows:1,zIndex:1,zoom:1},m={matches:function(){if(a.matches)return"matches";if(a.matchesSelector)return"matchesSelector";for(var t,e=0,n=u.length;n>e;e++)if(t=u[e],a[t])return t}(),toCamelCase:function(t){return t.replace(/^-ms-/,"ms-").replace(/-([\da-z])/gi,function(t,e){return e.toUpperCase()})},isArray:function(t){return"[object Array]"===Object.prototype.toString.call(t)},uniqueSort:function(t){var e,n,i,r=[],o=t.length,s=!1;for(e=0;o>e;e++){for(n=e+1;o>n;n++)t[e]===t[n]&&(n=++e);r.push(t[e])}return r=r.sort(function(t,e){return t===e?(s=!0,0):(i=!t.compareDocumentPosition-!e.compareDocumentPosition)?i:(i=(t.ownerDocument||t)===(e.ownerDocument||e)?t.compareDocumentPosition(e):1,4&i?-1:1)})},sibling:function(t,e){do t=t[e];while(t&&1!==t.nodeType);return t},siblings:function(t,e){for(var n=[];t;t=t.nextSibling)1==t.nodeType&&t!=e&&n.push(t);return n},siblingsList:function(t,e){for(var n=[];(t=t[e])&&9!==t.nodeType;)1===t.nodeType&&n.push(t);return n},filter:function(t,e){for(var n=t.length,i=0,r=[];n>i;)t[i].matches(e)&&r.push(t[i]),++i;return r},setStyle:function(t,e,n,i){if("string"===b.type(n)){var r,o=this.toCamelCase(e);if("number"==typeof n?n+=y[o]?"":"px":"string"==typeof n&&null!==(r=n.match(/^(\d+)%?$/i))&&(n=y[o]?"":r[0]+"px"),void 0===n)return t.style[e];try{t.style[e]=n}catch(s){}}else if("function"===b.type(n)){var a=this.computeStyle(t,e);return n.call(t,i,a)}},showHide:function(t,e){var n=t.style.display;e?"none"===n?t.style.display="":""===n&&(t.style.display="block"):"none"!==n&&(t.style.display="none")},isHidden:function(t){return"none"===(window.getComputedStyle?getComputedStyle(t,null):t.currentStyle).display},computeStyle:function(t,e){return window.getComputedStyle?getComputedStyle(t,null)[e]:t.currentStyle[e]},focus:function(t){return g.test(t.nodeName)&&s.activeElement&&(!s.hasFocus||s.hasFocus())&&!!(t.type||t.href||~t.tabIndex)}},v={events:{},add:function(t,e,n,i,r){this.events[t.replace(/\s+/g,"_")+r+n]={event:n,callBack:i||c,target:e,exist:1}},unbind:function(t,e,n,i,r){var o,s=t.replace(/\s+/g,"_")+r+n;return o=this.events[s]||{target:e,event:n,callBack:i||c},o.exist&&delete this.events[s],o}};"classList"in s.documentElement?(n=function(t,e){return t.classList.contains(e)},i=function(t,e){t.classList.add(e)},r=function(t,e){t.classList.remove(e)},o=function(t,e){t.classList.toggle(e)}):(n=function(t,e){return new RegExp("(^|\\s)"+e+"(\\s|dom)").test(t.className)},i=function(t,e){n(t,e)||(t.className+=(t.className?" ":"")+e)},r=function(t,e){n(t,e)&&(t.className=t.className.replace(new RegExp("(^|\\s)*"+e+"(\\s|dom)*","g"),""))},o=function(t,e){(n(t,e)?r:i)(t,e)}),e.prototype={each:function(t){return b.each(this,t)},on:function(t,e){for(var n=this.length,i=this.selector;n--;)this[n][l](d+t,e,!1),this[n][h](d+t,e,!1),v.add(i,this[n],t,e,n);return this},fire:function(t,e){var n,i=this.length;if("createEvent"in s)for(;i--;)n=s.createEvent("HTMLEvents"),n.initEvent(t,!1,!0),this[i].dispatchEvent(n);else for(;i--;)n=s.createEventObject(),this[i].fireEvent("on"+t,n);return this},off:function(t,e){for(var n,i=this.length,r=this.selector;i--;)n=v.unbind(r,this[i],t,e,i),n.target[l](d+n.event,n.callBack,!1);return this},html:function(t){if(void 0===t)return 1===this.nodeType?this.innerHTML.replace(/(?:null|\d+)/g,""):this[0].innerHTML.replace(/(?:null|\d+)/g,"");if("string"==typeof t)for(var e=0,n=this.length;n>e;e++)this[e].innerHTML=t;return this},text:function(t){var e=0,n=this.length;if(void 0===t)return 1===this.nodeType?this.innerText||this.textContent:b.each(this,function(t,e){return e.innerText});if("string"==typeof t){for(;n>e;e++)this[e].innerText=t;return this}},attr:function(t,e){var n,i=this[0];return n=i.nodeType,3!==n&&8!==n&&2!==n?(t=t.toLowerCase(),void 0!==e?i.setAttribute(t,e):i.getAttribute(t)||i.getAttributeNode(t)):void 0},data:function(t,e){if("string"===b.type(t))return t=t.replace(/[A-Z]/g,"-$&").toLowerCase(),void 0!==t?this.attr("data-"+t,e):this.attr("data",e);var n,i,r=this[0],o={};return 1===r.nodeType?(n=r&&r.attributes,b.each(n,function(t,e){i=e.name,/^data-/.test(i)&&(i=m.toCamelCase(i.substr(5)),o[i]=e.value)}),o):void 0},removeAttr:function(t){var e=this.length;for(t=t.toLowerCase();e--;)this[e].removeAttribute(t);return this},append:function(t){for(var e=0,n=this.length;n>e;e++)this[e].insertAdjacentHTML("beforeend",t);return this},prepend:function(t){for(var e=0,n=this.length;n>e;e++)this[e].insertAdjacentHTML("afterbegin",t);return this},before:function(t){for(var e=0,n=this.length;n>e;e++)this[e].insertAdjacentHTML("beforebegin",t);return this},after:function(t){for(var e=0,n=this.length;n>e;e++)this[e].insertAdjacentHTML("afterend",t);return this},empty:function(){return this.each(function(t,e){e.innerHTML=""})},remove:function(t){var e=this;if(1===this.nodeType)this.parentNode.removeChild(e);else for(var n=0,i=this.length;i>n;n++)e=this[n],this[n].parentNode.removeChild(e);return this},hasClass:function(t){if(1===this.nodeType)return n(this,t);for(var e,i=0;e=this[i++];)if(n(e,t))return!0;return!1},addClass:function(t){if(1===this.nodeType)i(this,t);else for(var e=this.length;e--;)i(this[e],t);return this},removeClass:function(t){if(1===this.nodeType)r(this,t);else for(var e=this.length;e--;)r(this[e],t);return this},toggleClass:function(t){if(1===this.nodeType)o(this,t);else for(var e=this.length;e--;)o(this[e],t);return this},parent:function(){var t=this[0].parentNode;return t&&11!==t.nodeType?b(t):null},parents:function(){for(var t=[],e=this[0];e.parentNode;)t.push(e.parentNode),e=e.parentNode;return b(t)},next:function(){return b.each(this,function(t,e){return m.sibling(e,"nextSibling")})},nextAll:function(t){var e=b.each(this,function(t,e){return m.siblingsList(e,"nextSibling")});return e=m.filter(e,t),b(e)},prev:function(){return b.each(this,function(t,e){return m.sibling(e,"previousSibling")})},prevAll:function(t){var e=b.each(this,function(t,e){return m.siblingsList(e,"previousSibling")});return e=m.filter(e,t),b(e)},siblings:function(){var t=b.each(this,function(t,e){return m.siblings((e.parentNode||{}).firstChild,e)});return 1<t.length&&(t=m.uniqueSort(t)),b(t)},children:function(){var t=b.each(this,function(t,e){return m.siblings(e.firstChild)});return 1<t.length&&(t=m.uniqueSort(t)),b(t)},closest:function(t){for(var e,n=0,i=this.length,r=[];i>n;n++)e=this[n].parentNode,e.nodeType<11&&1===e.nodeType&&e[m.matches](t)&&r.push(e);return 1<r.length&&(r=m.uniqueSort(r)),r},focus:function(t){if(m.focus(this[0])){if(!b.isFunction(t))return this[0].focus(),!1;this.on("focus",t)}},focusIn:function(t){m.focus(this[0])&&this.on("focusin",t)},focusOut:function(t){m.focus(this[0])&&this.on("focusout",t)},first:function(){return this.eq(0)},last:function(){return this.eq(-1)},eq:function(t){var e=this.length;return t=+t+(0>t?e:0),$(this[t])},get:function(t){return void 0!==t?0>t?this[t+this.length]:this[t]:[].slice.call(this)},css:function(t,e){return b.each(this,function(n,i){return i&&3!==i.nodeType&&8!==i.nodeType&&i.style?"string"===b.type(t)&&void 0!==e?m.setStyle(i,t,e,n):b.each(t,function(t,e){return m.setStyle(i,t,e,n)}):void 0}),this},getElementMetrics:function(){var t,e=this[0],n=e.offsetParent;for(t=e,offsetTop=0,offsetLeft=0;t.offsetParent;t=t.parentNode)offsetTop+=t.offsetTop,offsetLeft+=t.offsetLeft;return{height:e.offsetHeight,width:e.offsetWidth,absolute:{bottom:f[0]-(e.offsetHeight+offsetTop),left:offsetLeft,right:f[1]-(e.offsetWidth+offsetLeft),top:offsetTop},relative:{bottom:n.offsetHeight-(e.offsetHeight+e.offsetTop),left:e.offsetLeft,right:n.offsetWidth-(e.offsetWidth+e.offsetLeft),top:e.offsetTop}}},hide:function(){return b.each(this,function(t){m.showHide(t)})},show:function(){return b.each(this,function(t){m.showHide(t,!0)})},isHidden:function(){return m.isHidden(this[0])},toggle:function(t){return"boolean"==typeof t?t?this.show():this.hide():b.each(this,function(t,e){m.isHidden(e)?m.showHide(e,!0):m.showHide(e)})},fadeIn:function(t,e){e=e||c;var n,i=this.length-1;return b.each(this,function(r,o){n=0,o.style.opacity=0,o.style.display="block",b.animate({duration:t||300,delta:function(t){return b.easing.swing(t)},complete:function(){r===i&&b.isFunction(e)&&e()},step:function(t){o.style.opacity=n+t}})})},fadeOut:function(t,e){e=e||c;var n,i=this.length-1;return b.each(this,function(r,o){n=1,b.animate({duration:t||300,delta:function(t){return b.easing.swing(t)},complete:function(){r===i&&b.isFunction(e)&&e()},step:function(t){o.style.opacity=n-t}})})},log:function(t){return t&&(t+=" "),console.info(t,this),this}};var b=function(t){var n,i,r={".":function(){return n=t.match(p.CLASS),n&&n[1]?s.getElementByClassName(n[1]):s.querySelectorAll(t)},"#":function(){return n=t.match(p.ID),n&&n[1]?[s.getElementById(n[1])]:s.querySelectorAll(t)},"<":function(){var e;if(n=t.match(p.TAG),null===n||void 0===n)throw"Invalid selector / Node";return e=n[0].replace("<","").replace(">",""),s.createElement(e)}},o=t[0];if(i=t.nodeType?[t]:"function"!=typeof r[o]?"string"==typeof t?p.TAGS.indexOf(t)>-1?s.getElementsByTagName(t):s.querySelectorAll(t):t:r[o](),!i.length)throw t+" Node not found in DOM.";return new e(i,t)};return b.ready=b.prototype.ready=function(t){"interactive"===s.readyState||"complete"===s.readyState?t():s.addEventListener?s.addEventListener("DOMContentLoaded",t):s.attachEvent("onreadystatechange",t)},b.each=b.prototype.each=function(t,e){var n,i=0;if(this.isArray(t))for(n=t.length;n>i&&e.call(t[i],i,t[i])!==!1;i++);else for(i in t)if(e.call(t[i],i,t[i])===!1)break;return t},b.extend=b.prototype.extend=function(){var t,e={},n=arguments.length,i=0;if(!n)return e;for(;n>i;i++){t=arguments[i];for(var r in t)e[r]=t[r]}return t=null,e},b.merge=b.prototype.merge=function(t,e){for(var n=+e.length,i=0,r=t.length;n>i;i++)t[r++]=e[i];return t.length=r,t},b.trim=b.prototype.trim=function(t){return t.replace(/^\s+|\s+$/g,"")},b.parseJSON=b.prototype.parseJSON=JSON.parse,b.stringify=b.prototype.stringify=JSON.stringify,b.isFunction=b.prototype.isFunction=function(t){return t instanceof Function||"function"==typeof t},b.when=b.prototype.when=function(t){return new Promise(t)},b.type=b.prototype.type=function(t){return"undefined"==typeof t?"undefined":"object"!=typeof t||t?{}.toString.call(t).match(/\s([a-zA-Z]+)/)[1].toLowerCase():"null"},b.isArray=b.prototype.isArray=function(t){var e=!!t&&"length"in t&&t.length,n=this.type(t);return"array"===n||0===e||"number"==typeof e&&e>0&&e-1 in t},b.easing=b.prototype.easing={linear:function(t){return t},quadratic:function(t){return Math.pow(t,2)},swing:function(t){return.5-Math.cos(t*Math.PI)/2},circ:function(t){return 1-Math.sin(Math.acos(t))},bounce:function(t){for(var e=0,n=1;1;e+=n,n/=2)if(t>=(7-4*e)/11)return-Math.pow((11-6*e-11*t)/4,2)+Math.pow(n,2)},elastic:function(t,e){return Math.pow(2,10*(t-1))*Math.cos(20*Math.PI*e/3*t)}},b.animate=b.prototype.animate=function(t,e){var n,i,r,o,s=new Date;n=setInterval(function(){r=new Date-s,o=r/t.duration,o>1&&(o=1),t.progress=o,i=t.delta(o),t.step(i),1==o&&(clearInterval(n),t.complete())},t.delay||10)},b.clean=b.prototype.clean=function(t){t=t?$(t)[0]:s;for(var e,n=0,i=t.childNodes.length;i>n;n++)e=t.childNodes[n],e&&(8===e.nodeType||3===e.nodeType&&!/\S/.test(e.nodeValue)?(t.removeChild(e),n--):1===e.nodeType&&this.clean(e))},b});