-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdoc.min.js
1 lines (1 loc) · 6.71 KB
/
doc.min.js
1
define("doc",["event"],function(c){"use strict";function r(t,e){return e=e.replace(/^\s+|\s+$/g,""),s(e)&&t.getElementsByTagName?u(t.getElementsByTagName(e)):i(e)?(e=e.replace("#",""),t.getElementById?t.getElementById(e):document.getElementById(e)):n(e)&&t.getElementsByClassName?(e=e.replace(".",""),u(t.getElementsByClassName(e))):u(t.querySelectorAll(e))}function o(t,e,n){var i,r,o;if(document.createEvent)(r=document.createEvent("Event")).initEvent(e,!1,!0),r.detail=n,i=t.dispatchEvent(r);else if(n){(r=document.createEventObject()).detail=n;var s=t._event[e];for(var c in s)for(var u=0;u<s[c].length;u++)s[c][u](r)}else i=t.fireEvent("on"+e);i&&((o=t[e])&&o.call&&o.apply)&&t[e]()}function t(t){return t?a("string"==typeof t?r(document,t):t):t}var n=function(t){return t.match(/^\.[\w-]+$/)},i=function(t){return t.match(/^#[\w-]+$/)},s=function(t){return t.match(/^\w+$/)},u=function(e){try{return Array.prototype.slice.call(e)}catch(t){for(var n=[],i=e.length,r=0;r<i;r++)n.push(e[r]);return n}},a=function(t){var e=[];return t&&(t instanceof Array?e=t:t instanceof HTMLCollection?e=u(t):t instanceof NodeList?e=function(t){for(var e=t.length,n=[],i=0;i<e;i++){var r=t[i];r.nodeType===Node.ELEMENT_NODE&&n.push(r)}return n}(t):e.push(t)),{els:e,size:e.length,each:function(t){for(var e=0;e<this.size;e++)t(this.els[e],e);return this},data:function(e,n){return void 0!==n?this.each(function(t){t.setAttribute("data-"+e,n)}):this.first()?this.first().getAttribute("data-"+e):""},val:function(e){return void 0!==e?this.each(function(t){t.value=e}):this.first()?this.first().value:""},html:function(e){return void 0!==e?this.each(function(t){t.innerHTML=e}):this.first()?this.first().innerHTML:""},prepend:function(t){return this.each(function(e){if("object"==typeof t){if(t.els)return void t.each(function(t){e.insertAdjacentElement("afterbegin",t)});e.insertAdjacentElement("afterbegin",t)}else e.insertAdjacentHTML("afterbegin",t)})},append:function(e){var n;return this.each(function(t){n=t.appendChild(e)}),a(n)},text:function(e){var n=void 0!==document.getElementsByTagName("body")[0].innerText,i="";return this.each(function(t){void 0===e?i=t.textContent||t.innerText:n?t.innerText=e:t.textContent=e}),i.trim()},attr:function(e,n){if(void 0!==n)return"string"==typeof e&&this.each(function(t){t.setAttribute(e,n)}),this;if("object"!=typeof e)return this.first().getAttribute(e);for(var i in e)this.each(function(t){t.setAttribute(i,e[i])});return this},removeAttr:function(t){var n=t.split(" ");return this.each(function(e){n.forEach(function(t){e.removeAttribute(t)})})},hasClass:function(e){var n=!1;return this.each(function(t){t.classList.contains(e)&&(n=!0)}),n},addClass:function(t){var n=t.split(" ");return this.each(function(e){n.forEach(function(t){e.classList.add(t)})})},removeClass:function(t){var n=t.split(" ");return this.each(function(e){n.forEach(function(t){e.classList.remove(t)})})},toggleClass:function(t){var n=t.split(" ");return this.each(function(e){n.forEach(function(t){e.classList.toggle(t)})})},removeItem:function(){return this.each(function(t){t.remove?t.remove():t.parentNode.removeChild(t),t=null}),this.els=[],this.size=0,this},find:function(n){var i=[];return this.each(function(t){var e=r(t,n);-1===i.indexOf(e)&&(i=i.concat(r(t,n)))}),a(i)},filter:function(t){for(var e=[],n=0;n<this.size;n++)("function"==typeof t&&t(this.els[n])||"string"==typeof t&&this.els[n][t])&&e.push(this.els[n]);return a(e)},closest:function(e){var n=[],i=HTMLElement.prototype.matches||HTMLElement.prototype.webkitMatchesSelector||HTMLElement.prototype.mozMatchesSelector||HTMLElement.prototype.msMatchesSelector||HTMLElement.prototype.oMatchesSelector,r=function(t){return!t||t instanceof Element&&i.call(t,e)?t:r(t.parentNode)};return this.each(function(t){var e=r(t.parentNode);e&&-1===n.indexOf(e)&&n.push(e)}),a(n)},first:function(){return this.els[0]},last:function(){return this.els[this.size-1]},previous:function(){var t=this.els[0];if(t.previousElementSibling)return a(t.previousElementSibling);for(;t=t.previousSibling;)if(1===t.nodeType)return a(t)},next:function(){var t=this.els[0];if(t.nextElementSibling)return a(t.nextElementSibling);for(;t=t.nextSibling;)if(1===t.nodeType)return a(t)},parent:function(){for(var t=[],e=0;e<this.size;e++)t.push(this.els[e].parentElement);return a(t)},isPresent:function(){return void 0!==this.els&&0<this.els.length},checked:function(n){if(void 0!==n&&"boolean"!=typeof n)throw Error("Parameter type is not valid");return"boolean"==typeof n?this.each(function(t){var e=t.getAttribute("type");if(!e||!e.match("(checkbox|radio)"))throw Error('Element "'+t.outerHTML+'" is not checkbox nor radio');t.checked=n}):this.first().checked},isEmpty:function(){return!this.isPresent()},on:function(t,e,n){var i=t.split(" ");c.boundEvents=c.boundEvents||{};for(var r=0;r<i.length;r++){var o=i[r];this.each(function(t){c.addEvent(t,o,e,n)}),c.boundEvents[o]=c.boundEvents[o]||[];var s=c.boundEvents[o];this.each(function(t){-1===s.indexOf(t)&&s.push(t)})}return this},throttle:function(t,n,i,e){i=i||1e3;var r=0;return this.on(t,function(t){var e=+new Date;r+i<=e&&(n.apply(this,arguments),r=+new Date),t.preventDefault()},e)},debounce:function(t,e,n,i){n=n||1e3;var r=0;return this.on(t,function(t){clearTimeout(r),r=setTimeout(e.bind(this),n),t.preventDefault()},i)},off:function(e,n){this.each(function(t){c.removeEvent(t,e,n)}),c.boundEvents=c.boundEvents||{},c.boundEvents[e]=c.boundEvents[e]||[];var i=c.boundEvents[e],r=-1;return this.each(function(t){-1!==(r=i.indexOf(t))&&i.splice(r,1)})},trigger:function(e,n){return this.each(function(t){o(t,e,n)})},selectedText:function(){var t=this.els[0],e=this.attr("type"),n=!!e&&e.match("(text|search|password|tel|url)");return document.activeElement!==t?null:n&&void 0!==t.selectionStart&&0<t.selectionEnd?t.value.substring(t.selectionStart,t.selectionEnd):window.getSelection?window.getSelection().toString():document.getSelection?document.getSelection().toString():document.selection?document.selection.createRange().text?document.selection.createRange().text:document.selection.createRange().htmlText:null},focus:function(){return this.els[0].focus(),this},scrollIntoView:function(t){return t="boolean"==typeof t?t:t||!0,this.els[0].scrollIntoView(t),this},insertBefore:function(t){"string"==typeof t&&(t=a(r(document,t))),t.each(function(t){if(!t.parentNode)throw Error("Trying to insert element before element without parent");t.insertAdjacentHTML("beforebegin",this.els[0].outerHTML)}.bind(this))},insertAfter:function(t){"string"==typeof t&&(t=a(r(document,t))),t.each(function(t){if(!t.parentNode)throw Error("Trying to insert element after element without parent");t.insertAdjacentHTML("afterend",this.els[0].outerHTML)}.bind(this))}}};return t.broadcast=function(t,e){for(var n=c.boundEvents[t]||[],i=0;i<n.length;i++){o(n[i],t,e)}},t});