Source: core.min.js

/**
 * @license
 * BSD 3-Clause License
 * 
 * Copyright (c) 2019, Alexis Puga Ruíz
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * 
 * 1. Redistributions of source code must retain the above copyright notice, this
 *    list of conditions and the following disclaimer.
 * 
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 * 
 * 3. Neither the name of the copyright holder nor the names of its
 *    contributors may be used to endorse or promote products derived from
 *    this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
/**
 * @preserve Copyright 2019-2020 Alexis Puga and contributors. See {@link https://github.com/justjs/just/contributors}.
 * @file Just js: The core for all your javascript proyects.
 * @version 1.0.0
 */
(function(e){"function"==typeof define&&define.amd?define("just",[],e):"object"==typeof module&&module.exports?module.exports=e():this.just=e()}).call(this,function(){function o(e,t,n){n=Object.assign({},n||{}),n=("just"+e+" is deprecated"+(n.since?" since "+n.since:"")+". "+(n.message||"")).trim();if("warning"===t)console.warn(n);else{if("error"===t)throw new Error(n);console.error(n)}}function e(e,t,n,r){var o=Object.assign({override:!0,mutate:!1},r),i=Array.isArray(t)?t:[t],r=o.mutate?e:Object.assign({},e),s=r,c=!1,t=i[i.length-1];return i.slice(0,-1).forEach(function(e,t){var n=s[e];if(!(n instanceof Object)){if("undefined"!=typeof n&&null!==n&&!o.override)throw new TypeError(e+" is not an object.");c=!0,s[e]=n={}}s=n}),e=t in s&&!c,n?n.call(r,s,t,e,i):s[t]}function i(t,e){return[].slice.call(arguments,1).some(function(e){return[e,t].some(function(e){return null==e})?e===t:e.constructor===t.constructor})}function t(e){if(!(this instanceof t))return new t(e);r(this,{element:e})}function d(n,e,t){var r=Object.assign({ignoreDefaultKeys:!1,checkLooks:!0,checkDeepLooks:!0,ignoreNull:!1},t);return r.ignoreNull&&null===e&&void 0!==n?n:r.checkLooks?i(n,e)?(i(n,{})&&r.checkDeepLooks&&h(e,function(e,t){"undefined"!=typeof n[t]?n[t]=d(n[t],e,r):r.ignoreDefaultKeys||(n[t]=e)}),n):e:typeof n==typeof e?n:e}function r(e,t){return h(t,function(e,t){n(this,t,e)},e),e}function n(e,t,n){var r=Object(n),o=Object.keys(r);return(!o.length||o.some(function(e){return-1===this.indexOf(e)},["value","writable","get","set","configurable","enumerable"]))&&(r={value:n}),Object.defineProperty(e,t,r),e}function h(e,t,n,r){var o,i=Object(e),s=Object.assign({addNonOwned:!1},r),c=!1;if("function"!=typeof t)throw new TypeError(t+" is not a function.");for(o in i){if(c)break;(s.addNonOwned||{}.hasOwnProperty.call(i,o))&&(c=Boolean(t.call(n,i[o],o,i)))}return c}function p(e,t){return[].slice.call((t||document).querySelectorAll(e))}function s(e,t,n,r){var o=e,i=0;if(!(e instanceof Node))throw new TypeError(e+" is not a Node.");for(n instanceof Node||(n=document.documentElement);o;){if((0<i||r)&&t.call(o,i,n))return o;if(o===n)return null;o=o.parentNode,i++}return null}function c(e){return!h(e,function(){return!0})}function a(){return Boolean("ontouchstart"in document.body||0<window.navigator.maxTouchPoints||0<window.navigator.msMaxTouchPoints||"DocumentTouch"in window&&document instanceof DocumentTouch)}function u(e,t,n,r){var o=Object("object"==typeof t?t:null),i=/link/i.test(e)?"href":"src",s="string"==typeof t?o[i]=t:o[i],c=g(s).origin!==window.location.origin,a=!("crossOrigin"in o)&&-1!==["video","img","script","link"].indexOf(e),u=/link/i.test(e),l=!("rel"in o),f=p(e+"["+i+'="'+(s||"")+'"]')[0]||null,i=E(e,o),o=function e(t){return y(this,["load","error"],e),n.call(this,t)};if("string"!=typeof s||""===s.trim())throw new TypeError(s+" is not valid url.");return n&&m(i,["load","error"],o),c&&a&&(i.crossOrigin="anonymous"),u&&l&&(i.rel="stylesheet"),"function"==typeof r?r.call(i,f,s):f||((r=r||document.head).appendChild(i),i)}function l(e,t){if(!(this instanceof l))return new l(e,t);r(this,{consent:Boolean(e),isExplicit:d(t,"undefined"!=typeof e)})}function m(e,t,n,r){var o=r||!1,r=[];return"string"==typeof e&&(e=p(e)),Array.isArray(t)||(t=[t]),Array.isArray(e)||(e=[e]),e.forEach(function(e){"addEventListener"in Object(e)&&(t.forEach(function(e){this.addEventListener(e,n,o)},e),this.push(e))},r),r}function y(e,t,n,r){var o=r||!1;Array.isArray(e)||(e=[e]),Array.isArray(t)||(t=[t]),e.forEach(function(e){t.forEach(function(e){this.removeEventListener(e,n,o)},e)})}function g(e){var t,n,r={},o=window.location;return e=e||o.href,/^blob:/i.test(e)?(n=g(e.replace(/^blob:/i,"")),Object.assign(n,{protocol:"blob:",href:"blob:"+n.href,host:"",hostname:"",port:"",pathname:n.origin+n.pathname})):(/^(:)?\/\//.test(e)?e="//"===(e=e.replace(/^:/,""))?o.origin:o.protocol+e:/^(\?|#|\/)/.test(e)?e=o.origin+e:/:\/\//.test(e)||(e=o.protocol+"//"+e),e=(o=((n=(e||"").split(/(\?.*#?|#.*\??).*/))[1]||"").split("#"))[1]||"",r.search=o[0]||"",r.hash=e&&"#"+e,e=((o=(n[0]||"").split("://"))[1]||"").split(/(\/.*)/),r.username="",r.password="",/@/.test(e[0])&&(n=(t=e[0].split("@"))[0].split(":"),r.username=n[0]||"",r.password=n[1]||"",e[0]=t[1]),r.host=e[0]||"",r.pathname=e[1]||"",e=r.host.split(/:([0-9]+)/),r.hostname=e[0]||"",r.port="undefined"!=typeof e[1]?e[1]:"",r.protocol=o[0]+":",r.origin=r.protocol+"//"+r.host,r.href=(t?r.protocol+"//"+r.username+":"+r.password+"@"+r.host:r.origin)+r.pathname+r.search+r.hash,r)}function b(e){var t;if("object"==typeof e)return e;try{t=JSON.parse(e)}catch(e){t=null}return t}function f(e){return/(?:^\{|\}$)/.test(String(e).trim())&&b(e)||{}}function w(e){var t={};if(o(".toObjectLiteral()","warning",{since:"1.0.0-rc.22"}),i(e,{},null))return Object.assign({},e);if(!i(e,[]))throw new TypeError(e+" must be either null, an object literal or an Array.");return e.forEach(function(e){var t,n;if(i(e,[]))t=e[0],n=e[1],this[t]=n;else{if(!i(e,{}))throw new TypeError(e+" must be either an object literal or an Array.");Object.assign(this,e)}},t),t}function v(t){"loading"===document.readyState?m(document,"DOMContentLoaded",function e(){y(document,"DOMContentLoaded",e),t()}):setTimeout(t)}function E(e,t){e=document.createElement(e);return Object.assign(e,t),e}function j(e,r,t){var o="json"in Object(t)?t.json:/\.json$/i.test(e),n=d(t,{json:o,data:null,method:"GET",async:!0,user:null,pwd:null,headers:Object.assign({"X-Requested-With":"XMLHttpRequest"},o?{"Content-Type":"application/json"}:null),props:Object.assign({},o?{responseType:"json"}:null),send:function(e){return this.send(e)}},{ignoreNull:!0}),i=n.data,s=n.method,c=n.async,a=n.user,u=n.pwd,l=n.props,f=n.headers,p=new XMLHttpRequest;return/GET/i.test(s)&&i&&(e=j.appendData(e,i),i=null),p.open(s,e,c,a,u),h(f,function(e,t){this.setRequestHeader(t,e)},p),Object.assign(p,l),p.onreadystatechange=function(e){var t,n;this.readyState===XMLHttpRequest.DONE&&(this.onreadystatechange=null,n=this.status,t="response"in this?this.response:this.responseText,n=(n<200||400<=n)&&0!==n?new Error("Bad status: "+n):null,o&&"object"!=typeof t&&(t=b(t)),r&&r.call(this,n,t))},n.send.call(p,i,t)}function T(e,t){Object.defineProperty(O,e,{value:t})}var O={};T("register",T),T("deprecate",o),T("access",e),T("prop",e),r(i,{throwable:function(e,t){var n=[].slice.call(arguments),r=this;if(o(".check.throwable()","warning",{since:"1.0.0-rc.24"}),!i.apply(this,n))throw r instanceof String||(r=e+" must be like one of the following values: "+n.slice(1).map(function(e){return e+""}).join(", ")),new TypeError(r);return e}}),T("check",i),T("is",i),r(t,{apply:function(e,t,n){var n="number"==typeof n?[n]:[].slice.call(n),r=e.classList;if(!/(?:add|remove)/.test(t))return r[t](n[0],n[1]);n.forEach(function(e){r[t](e)})}}),r(t.prototype,{add:function(){return t.apply(this.element,"add",arguments),this},remove:function(){return t.apply(this.element,"remove",arguments),this},toggle:function(){return t.apply(this.element,"toggle",arguments),this},replace:function(){return t.apply(this.element,"replace",arguments),this},contains:function(){return t.apply(this.element,"contains",arguments)},item:function(){return t.apply(this.element,"item",arguments)}}),T("ClassList",t),T("defaults",d),T("from",d),T("defineProperties",r),T("defProps",r),T("defineProperty",n),T("defProp",n),T("eachProperty",h),T("eachProp",h),T("findElements",p),T("el",p),T("getRemoteParent",s),T("parent",s),T("isEmptyObject",c),T("emptyObj",c),T("isTouchSupported",a),T("supportsTouch",a),T("isWindow",function(e){return o(".isWindow()","warning",{since:"1.0.0-rc.24"}),Boolean(e===window||e instanceof Object&&e.document&&e.setInterval)}),T("loadElement",u),T("load",u),r(l,{DNT:{get:function(){var e=","+[navigator.doNotTrack,navigator.msDoNotTrack,window.doNotTrack]+",";return!!/,(yes|1),/i.test(e)||!/,(no|0),/i.test(e)&&void 0}},cookieExists:function(e){return new RegExp("; "+e+"(=|;)").test("; "+document.cookie+";")},getCookie:function(e){return!/=/.test(e)&&l.cookieExists(e)?("; "+document.cookie).split("; "+e+"=").pop().split(";")[0]:null}}),r(l.prototype,{setCookie:function(e,t,n){function r(e,t){o+=e+("undefined"!=typeof t?"="+t:"")+"; "}var o="",n=d(n,{secure:"https:"===location.protocol});return!!this.consent&&(n.secure&&r("secure"),delete n.secure,n.expires&&(n.expires=new Date(n.expires).toGMTString()),r(e,t),h(n,function(e,t){r(t,e)}),document.cookie=o.trim(),!0)},removeCookie:function(e,t){t=d(t,{expires:new Date(0)});return!l.cookieExists(e)||this.setCookie(e,"",t)},isStorageAvailable:function(e,t,n){var r,o=d(t,"_"),i=d(n,"_");if(!this.consent)return!1;if(/cookie/i.test(e))return this.setCookie(o,i)&&l.getCookie(o)===i&&this.removeCookie(o);try{(r=window[e]).setItem(o,i),r.removeItem(o)}catch(e){try{r.removeItem(o)}catch(e){return!1}return!1}return!0}}),T("LocalStorage",l),T("addEventListener",m),T("on",m),T("removeEventListener",y),T("off",y),T("parseUrl",g),T("parseJSON",b),T("stringToJSON",f),T("toJSON",b),T("toObjectLiteral",w),T("toObj",w),T("onDocumentReady",v),T("createElement",E),T("create",E),r(j,{appendData:function(e,t){e=g(e),t=j.dataToUrl(t),t=((/\?.+/.test(e.search)?e.search+"&":"?")+t).replace(/[?&]$/,"");return[e.origin,e.pathname,t,e.hash].join("")},dataToUrl:function(e){var t=Object(e);if("object"!=typeof e)throw new TypeError(e+" is not an object.");return Object.keys(t).map(function(e){return encodeURIComponent(e)+"="+encodeURIComponent(t[e])}).join("&")}}),T("request",j),T("ajax",j);var A,k,D=(k={},r(q,{STATE_DEFINED:-1,STATE_NON_CALLED:0,STATE_CALLING:1,STATE_CALLED:2,urls:{value:{},writable:!0},nonScripts:{value:{},writable:!0},globals:{value:{},writable:!0},isDefined:M,load:S,configure:function(e){return Object.assign(q,e),q},clear:function(){return q.globals={},q.nonScripts={},q.urls={},q.handleError=U,q.clearModules(),q},clearModules:function(){return k={},this},clearModule:function(e){return delete k[e],this},handleError:{value:U,writable:!0},init:function(){return q.configure({urls:q.findUrlsInDocument("data-just-Define")}),h(q.urls,function(e,t){q.load(t)}),q},findUrlsInDocument:function(t,e){var n={};return p("*["+t+"]",e).forEach(function(n){var e=f((n.getAttribute(t)+"").replace(/\[([^\]]+)\]/gi,function(e,t){return n.getAttribute(t)}));Object.assign(this,e)},n),n}}),r(q.prototype,{load:function(){return S.apply(null,[].slice.call(arguments)),this}}),q);function L(e,t){var n=t[e];return e in t&&!M(e)&&(new q(e,[],n),!0)}function N(e,t){return!M(e)&&(e in q.urls&&!t?S(e):e in q.nonScripts?L(e,q.nonScripts):e in q.globals&&L(e,q.globals))}function S(n,r){function o(e){var t="error"===Object(e).type;if(y(this,["load","error"],o),t||N(n,!0),"function"==typeof r)return r.call(this,e);t&&q.handleError.call(null,new Error("Error loading "+i))}var e=q.urls[n],t=Object(e),i="object"==typeof e?t.src||t.href:"string"==typeof e?e:n,s=(g(i).pathname.match(/\.(.+)$/)||["js"])[0],c="tagName"in t?t.tagName:!("src"in t)&&("href"in t||/css$/i.test(s))?"link":"script",t="object"==typeof e?(delete e.tagName,e):null;return n in q.urls||(q.urls[n]=i),i!==n&&(s=n,M(e=i)||new q(e,[s],function(e){return e})),u(c,t||i,o,function(e){return"script"===c||n in q.nonScripts||(q.nonScripts[n]=this),e?(m(e,["load","error"],o),!1):(document.head.appendChild(this),!0)})}function C(e){return e.map(function(e){return N(e=e),k[e]||null})}function x(e){return Object(e).state===q.STATE_CALLED}function I(e,t){return Object(t).id===e}function _(t){var n,e,r,o=t.handler,i=t.dependencies;if(!x(t)&&(t.state=q.STATE_NON_CALLED,r=i.every(function(e){return e&&e.state!==q.STATE_DEFINED}),i.every(x)||r&&((r=t)&&r.dependencies.some(I.bind(null,r.id))||(r=t)&&r.dependencies.some(function(e){return e&&e.dependencies.some(I.bind(null,this.id))},r)))){e=i.map(function(e){return e.exports}),t.state=q.STATE_CALLING;try{t.exports=o.apply(t,e)}catch(e){n=q.handleError.call(t,e)}return t.state=q.STATE_CALLED,"boolean"!=typeof n||n}}function P(e){return"string"==typeof e&&""!==e.trim()}function M(e){return e in k}function R(e){return null===e||"undefined"==typeof e}function q(e,t,n){if(!arguments.length||"function"!=typeof n&&[e,t].every(R))throw new TypeError("Not enough arguments.");if(2===arguments.length?(n=arguments[1],Array.isArray(arguments[0])?(t=arguments[0],e=void 0):t=void 0):1===arguments.length&&"function"==typeof arguments[0]&&(n=arguments[0],t=e=void 0),!P(e)){if(!R(e))throw new TypeError("The given id ("+e+") is invalid.");e=Math.random().toString()}if(R(t)?t=[]:Array.isArray(t)||(t=[t]),t.some(function(e){return!P(e)}))throw new TypeError("If present, the ids for the dependencies must be valid ids.");if(!(this instanceof q))return new q(e,t,n);r(k[e]=this,{id:e,handler:"function"==typeof n?n:function(){return n},dependencyIDs:t,dependencies:{get:function(){return C(this.dependencyIDs)}},state:{value:q.STATE_DEFINED,writable:!0},exports:{value:"function"==typeof n?this:n,writable:!0}}),clearTimeout(A),A=setTimeout(function t(){h(k,function(e){if(_(e))return t(),!0})})}function U(e){throw e}return v(D.init),T("Define",D),T("Def",D),T("version","1.0.0"),T("just",O),O});