UNPKG

5.2 kBJavaScriptView Raw
1!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):t.isLoading=e()}(this,function(){"use strict";function t(t){return t&&t.__esModule?t.default:t}function e(t,e){return e={exports:{}},t(e,e.exports),e.exports}function i(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}var o=e(function(t,e){function i(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"div",e=t.match(/^[a-z]+/i),i=t.match(/#([a-z]+[a-z0-9-]*)/gi),n=t.match(/\.([a-z]+[a-z0-9-]*)/gi),a=t.match(/\[([a-z][a-z-]+)(=['|"]?([^\]]*)['|"]?)?\]/gi),r=e?e[0]:"div";if(i&&i.length>1)throw o("only 1 ID is allowed");var s=document.createElement(r);if(i&&(s.id=i[0].replace("#","")),n){var l=n.join(" ").replace(/\./g,"");s.setAttribute("class",l)}a&&a.forEach(function(t){t=t.slice(0,-1).slice(1);var e=t.split("="),i=e[0],o=e[1];o&&(o=o.replace(/^['"](.*)['"]$/,"$1")),s.setAttribute(i,o||"")});for(var u=arguments.length,p=Array(u>1?u-1:0),d=1;d<u;d++)p[d-1]=arguments[d];return p.forEach(function(t){"string"==typeof t||"number"==typeof t?s.appendChild(document.createTextNode(t)):t.nodeType===document.ELEMENT_NODE&&s.appendChild(t)}),s}function o(t){this.message=t,this.name="CreateElementException"}e.__esModule=!0,e.default=i,t.exports=e.default}),n=t(o),a=Object.assign||function(t){for(var e=1;e<arguments.length;e++){var i=arguments[e];for(var o in i)Object.prototype.hasOwnProperty.call(i,o)&&(t[o]=i[o])}return t},r=function(){function t(t,e){for(var i=0;i<e.length;i++){var o=e[i];o.enumerable=o.enumerable||!1,o.configurable=!0,"value"in o&&(o.writable=!0),Object.defineProperty(t,o.key,o)}}return function(e,i,o){return i&&t(e.prototype,i),o&&t(e,o),e}}(),s=function(){for(var t=arguments.length,e=Array(t),i=0;i<t;i++)e[i]=arguments[i];return new p(e)},l=["form","input","textarea","label","fieldset","select","button"],u={type:"switch",text:"loading",disableSource:!0,disableList:[]},p=function(){function t(e){i(this,t);var o={};0===e.length||1===e.length&&!e[0].nodeType?(this._target=null,o=a({},e[0],{type:"full-overlay"})):(this._target=e[0],o=e[1]),this._options=a({},u,o),this._fullOverlayId="is-loading-full-overlay"}return t.prototype.loading=function(){switch(this._options.type){case"replace":this._onReplaceType();break;case"full-overlay":this._onFullOverlayType();break;case"overlay":this._onElementOverlayType();break;default:this._onSwitchType()}},t.prototype.restoreContent=function(){var t=this._target.getAttribute("data-is-loading-content");this.isTargetValue?this._target.value=t:this._target.textContent=t},t.prototype._onSwitchType=function(){this._toggleElements(!1),this._target.setAttribute("data-is-loading-content",this.targetContent),this.targetContent=this._options.text},t.prototype._onReplaceType=function(){this._toggleElements(!1),this._target.setAttribute("data-is-loading-content",this.targetContent),this._target.innerHTML="",this._target.appendChild(n("span.is-loading.is-loading-target",this._options.text))},t.prototype._onElementOverlayType=function(){this._toggleElements(!1);var t=".is-loading-element-overlay";if("static"===this._prop("position")&&(this._target.setAttribute("data-is-loading-position","static"),this._target.classList.add("is-loading-element-overlay-target")),!this._target.querySelector(t)){var e=n(t,n(".is-loading-text-wrapper",this._options.text));e.style.borderRadius=this._prop("border-radius"),this._target.appendChild(e)}},t.prototype._onFullOverlayType=function(){this._toggleElements(!1),this._showFullOverlay()},t.prototype._showFullOverlay=function(){var t=document.querySelector(this._fullOverlayId);t||(t=n("#"+this._fullOverlayId,n(".is-loading-text-wrapper",this._options.text)),document.querySelector("body").appendChild(t))},t.prototype._prop=function(t){return window.getComputedStyle(this._target).getPropertyValue(t)},t.prototype._toggleElements=function(){var t=!(arguments.length>0&&void 0!==arguments[0])||arguments[0],e=[].concat(this._options.disableList);this._target&&this._options.disableSource===!0&&e.unshift(this._target),e.forEach(function(e){l.includes(e.tagName.toLowerCase())&&(t===!0?e.removeAttribute("disabled"):e.setAttribute("disabled","disabled")),t===!0?e.classList.remove("disabled"):e.classList.add("disabled")})},t.prototype.remove=function(){if(this._toggleElements(!0),"switch"===this._options.type&&this.restoreContent(),this._target&&this._target.removeAttribute("data-is-loading-content"),"full-overlay"===this._options.type){var t=document.getElementById(this._fullOverlayId);t&&document.querySelector("body").removeChild(t)}this._target&&this._target.getAttribute("data-is-loading-position")&&this._target.classList.remove("is-loading-element-overlay-target")},r(t,[{key:"targetContent",get:function(){return this.isTargetValue?this._target.value:this._target.textContent},set:function(t){this.isTargetValue?this._target.value=t:this._target.textContent=t}},{key:"isTargetValue",get:function(){var t=this._target.nodeName.toLowerCase(),e=this._target.attributes.type;return"input"===t&&e&&("button"===e.value.toLowerCase()||"submit"===e.value.toLowerCase())}}]),t}();return s});