/** * remit * https://github.com/nikeMadrid/remit * @author Nike Madrid * @version 1.3.4 * @licence MIT */ Object.values||(Object.values=function(e){var t=[];for(var r in e)e.hasOwnProperty(r)&&t.push(e[r]);return t}),Array.prototype.filter||(Array.prototype.filter=function(e){"use strict";if(void 0===this||null===this)throw new TypeError;var t=Object(this),r=t.length>>>0;if("function"!=typeof e)throw new TypeError;for(var i=[],n=arguments.length>=2?arguments[1]:void 0,o=0;r>o;o++)if(o in t){var s=t[o];e.call(n,s,o,t)&&i.push(s)}return i});var xhr=null;!function(global){"use strict";return global.remit=function(){function templateRequest(e,t){var r=new Promise(function(t,r){var i=new XMLHttpRequest;i.open("GET",e),i.send(),i.onload=function(){200==this.status?t(this.response):r(this.statusText)},i.onerror=function(){r(this.statusText)}}).then(t);return r}function pregQuote(e,t){return null!==e.match(/\//g)&&(e=String(e).replace(new RegExp("[.\\*$=!<>|\\"+(t||"")+"]","g"),"\\$&")),e}function each(e,t){if(e instanceof Array)for(var r in e)t.call(null,r,e[r])}function infoHTTP(){var e,t="",r=/\/(.*?)\.[html|php]*/i;return r.test(_SERVER.path)&&(e=_SERVER.path.match(r))&&(t=e[0].substr(1),_SERVER.path=_SERVER.path.replace(t,"")),_SERVER.filename=""!=t?t.substr(1):t,_SERVER}function errorAccessMethod(e,t){return"error: access method not allowed
error: access method not allowed

url: "+t+"

method permitted: "+e+"

type error: method not allowed

"}function pageNotFount(e){return'error: 404 page not fount
Error: 404 Page Not Fount

code: 404

type error: page not fount

urls:

^'+e.join("
^").replace(/[\\]|\/\?/g,function(){return""})+"

"}function renderer(e,t){var r=Object.prototype.hasOwnProperty;r.call(e,"render")?(r.call(e,"middleware"),e.render.apply(this,t)):e.apply(this,t)}function captureRgx(url,obj){var regex_group=/\(\?\P\<(.*)?\>(.*)?\)/g,matches;if(regex_group.test(url)&&(matches=/\(\?\P\<(.*)?\>(.*)?\)/g.exec(url))){var expression=matches.shift(),name=matches.shift(),rgx=matches.shift();obj.capture={expression:String(expression).replace(/&/g,"&").replace(//g,">"),name:name,typeCapture:rgx},url=url.replace(expression,"([\\"+rgx+"])")}var quote=pregQuote(url,"/");return function(){var m=quote.match(globalRgx);if(null!==m)for(var i in m)quote=sString.test(m[i]&&m[i].match(sString))?quote.replace(m[i],"([a-zA-Z]+)"):sNumber.test(m[i]&&m[i].match(sNumber))?quote.replace(m[i],"([0-9]+)"):sLower.test(m[i]&&m[i].match(sLower))?quote.replace(m[i],"([a-z]+)"):sUpper.test(m[i]&&m[i].match(sUpper))?quote.replace(m[i],"([A-Z]+)"):quote.replace(m[i],"([0-9a-zA-Z_\\+\\-%\\:]+)");return obj.urls.push(quote),eval("/"+quote+"\\/?/")}()}function compruebeEmulatorAccessRequestMethod(e){return!(""!=self.Cookie.first("req_met")&&"false"!=self.Cookie.first("req_met")||0!=e)}function emulatorAccessMethod(){return!(""==self.Cookie.first("req_met")||"false"==self.Cookie.first("req_met"))}function eventForm(e,t,r){if(null==t)return void console.warn("context in null");var i=e instanceof Array?new self.validation(e[0],e[1]):e;i.ifNotSubmit(),t.addEventListener("submit",function(){var e=Object.values(i.errors).filter(function(e){return 1==e});Object.defineProperty(window,"srcStorage",{enumerable:!1,writable:!1,configurable:!0,value:t.getAttribute("action")||r}),0==e.length?(self.Cookie.remove("req_met",window.srcStorage),self.Cookie("req_met",Date.now(),{path:window.srcStorage})):self.Cookie("req_met",!1,{path:window.srcStorage})})}function dispatch(e){if("object"==typeof e){var t=this,r=0,i=[],n=new t.Request,o=new t.Response,s=!0,a=[];e.forEach(function(e,o){if(a.push(e.urls[0]),Object.keys(e.capture).length>0&&(a[o]=e.urls[0].replace("([\\"+e.capture.typeCapture+"])",e.capture.expression).replace(e.capture.typeCapture,"\"+e.capture.typeCapture)+'/$ [name="'+e.capture.name+'"]'),t.m=dispatch.matches(e.getUrl())){var c=t.m.shift(),l=t.m,u=e.getFunc(),h=e.getMethod(),p=t.selector=document.querySelector(u.form),f=t.validatable=u.validation;if(s=!1,t.collection=e.capture,t.Request.isGet(h))i.push(h),t.event.validatable=p&&p.getAttribute("action")||c?eventForm.bind(t,f,p,c):Function,emulatorAccessMethod()&&"OPTIONS"!=h||n.env(c,h,u,l);else if(t.Request.isAllNotGet(h)){var d=-1!=i.indexOf("GET");try{var m=setInterval(function(){if(compruebeEmulatorAccessRequestMethod(d)&&0==r)throw r=1,document.querySelector("html").innerHTML=errorAccessMethod(h,c),new Error("Error: access method not allowed");emulatorAccessMethod()&&(n.env(c,h,u,l),clearInterval(m))},10)}catch(g){console.error(g)}}}}),setTimeout(o.send.bind(o,n,t),10),s&&(document.querySelector("html").innerHTML=pageNotFount(a))}}if(global.XMLHttpRequest)xhr=new XMLHttpRequest;else try{xhr=new new ActiveXObject("Microsoft.XMLHTTP")}catch(e){console.warn(e)}var globalRgx=/{([0-9a-zA-Z_\+\-%\:]+)}/g,sUpper=/{((.*?):upper)}/g,sLower=/{((.*?):lower)}/g,sString=/{((.*?):string)}/g,sNumber=/{((.*?):number)}/g,_SERVER={path:location.pathname||"/",hash:location.hash.replace(/\#\//g,"")||"/",protocol:location.protocol.replace(":",""),beforeUrl:location.origin||"/",params:location.search||"",host:location.hostname},self=this,context=[],hasOwnProperty=Object.prototype.hasOwnProperty;this.event={validatable:Function},this.collection={};var count=0,PRINTED=new RegExp("({[\\\\{{)]([json|raw|escape]+)*[|]?(.*?)[(\\\\}}]})","g"),CONCATOBJECT=new RegExp("([.*?].(.+))","gi"),MAINTAIN=new RegExp("({%[^{%)]([include|yield|block|extends]+)(.*?)[%}]})","g");return this.detached=function(e,t){this.treplace=[],this.tsection={type:[],name:[]},this.data=t,this.compileData=this.resolver(e)},this.detached.compile=function(e,t){return new this(e,t)},this.detached.view=function(e,t,r){"undefined"==typeof r&&(r=t,t=null),templateRequest(e,function(e){var i=self.detached.compile(e,r);if(t)i.context=t;else{var n=document.querySelector(".outlet"),o=n;n||(o=document.createElement("div"),o.setAttribute("class","outlet"),document.body.appendChild(o),o=document.querySelector(".outlet")),i.context=o}var s=i.tsection.name,a=i.tsection.type;a.forEach(function(e,t,r){"include"==e?(i.includeContent(s[t],i.compileData,t),delete i.tsection.name[t],delete i.tsection.type[t]):"extends"==e&&-1==r.indexOf("include")&&(i.extend(i.compileData),delete i.tsection.name[t],delete i.tsection.type[t])})})},this.detached.prototype={resolver:function(e){return PRINTED.test(e)&&(e=this.rdata(e)),MAINTAIN.test(e)&&(e=this.maintainContent(e)),e},extend:function(e){var t,r=this;if(-1!=(t=this.tsection.type.indexOf("extends"))){this.treplace[t];templateRequest(this.tsection.name[t],function(t){var i=[],n=e.split(/{%\s*(block\s*(.*))\s*%}/gm),o=t.match(/{%\s*yield\s*(.*)\s*%}/gm);n.forEach(function(e,t,r){/(block+\s*(.*))\s*/g.test(e)||i.push(r[1+t].replace(/{%\s*endblock\s*%}|\n+/g,""))}),i.shift();for(var s in i)t=t.replace(o[s],i[s]);r.context.innerHTML=r.resolver(t)})}},includeContent:function(e,t,r){var i=this;templateRequest(e,function(e){e=i.resolver(e),t=t.replace(i.treplace[r],e),i.extend(t)})},maintainContent:function(e){return e.replace(MAINTAIN,function(e){var t=/(\{\%[^\{\%)]([include|yield|block|extends]+)(.*?)[\%\}]\})/g.exec(e),r=t[2].replace(/\s+/g,""),i=t[3].replace(/\s+/g,"");return this.tsection.type.push(r),this.tsection.name.push(i),this.treplace.push(e),e}.bind(this))},rdata:function(e){return e.replace(PRINTED,function(e){var t="",r=/(json|raw|escape)+/g.exec(e)||[],i=e.replace(/\s+/g,"").replace((r[0]||"")+"|","").replace(/\{\{(.*)?\}\}/g,"$1"),n=function(e){switch(r[0]){case"json":t=this.json(e);break;case"raw":t=e.toString();break;case"escape":t=this.escapeHTML(e)}return t||e};if(r&&!CONCATOBJECT.test(i)){if(!this.data[i])throw new TypeError("scope var not exists "+i);return n.call(this,this.data[i])}var o=i.split("."),s=this.data;for(var a in o){if(!s[o[a]])throw new TypeError("property "+o[a]+" not exists");s=s[o[a]]}return"function"==typeof s?n.call(this,s.call(this.data)):n.call(this,s)}.bind(this))},json:function(e){return JSON.stringify(e)},escapeHTML:function(e){var t=e;return t=t.replace(/&/g,"&"),t=t.replace(//g,">"),t=t.replace(/"/g,""")}},this.validation=function(e,t){return"undefined"==typeof this.iterate?new this.validation(e,t):(this.msg={max:"max value {max}",min:"min value {min}",required:"input is required"},this.values={max:[],min:[]},this.errors=[],this.form=document.querySelector(e),this.keysNameInput=Object.keys(t),this.typeValidator=Object.values(t),this.iterate(),this)},this.validation.prototype={iterate:function(){for(var e in this.typeValidator)this.splitRules(this.keysNameInput[e],this.typeValidator[e])},setErrors:function(e,t){this.errors[e]=t},splitRules:function(e,t){t.split("|").map(function(t){if(/\d+/g.test(t)){var r=t.match(/\D+/g)[0].replace(/\:/g,""),i=t.match(/\d+/g)[0],n=this[r];"min"==r?this.values.min[e]=parseInt(i):"max"==r&&(this.values.max[e]=parseInt(i)),this.setErrors(e,!0);var o=document.querySelector('input[name="'+e+'"]');this.rules.call(this,o,this.compareValue,parseInt(i))}else{var n=this[t];if("function"==typeof n){"required"==t&&this.setErrors("required_"+e,!0);var o=document.querySelector('input[name="'+e+'"]');this.rules.call(this,o,n)}}}.bind(this))},rules:function(e,t,r){setInterval(t.bind(this,e,r),450)},compareValue:function(e){var t=this.values.max[e.name],r=this.values.min[e.name];!t&&r&&e.value.length>=r||!r&&t&&e.value.length<=t||r&&t&&e.value.length>=r&&e.value.length<=t?this.setErrors(e.name,!1):this.setErrors(e.name,!0)},required:function(e){var t=e.name;0!=e.value.length?this.setErrors("required_"+t,!1):this.setErrors("required_"+t,!0)},ifNotSubmit:function(){var e=this,t=function(t,r){return Object.prototype.hasOwnProperty.call(e.msg,t)?e.msg[t].replace(/\{[max|min]*\}/g,r):""};this.form.addEventListener("submit",function(r){var i=Object.keys(e.errors).filter(function(r){if(e.errors[r]){var i=document.createElement("div");i.setAttribute("class","errors");var n=document.querySelector('input[name="'+r.replace("required_","")+'"]');if(-1==r.indexOf("required")){var o=e.values.min[r],s=e.values.max[r];o&&(i.innerHTML='

'+t("min",o)+"

"),s&&(i.innerHTML+='

'+t("max",s)+"

")}else e.errors[r]&&(i.innerHTML='

'+t("required")+"

");setTimeout(function(){var e=document.querySelector(".errors");null!=typeof e.parentNode&&e.parentNode.removeChild(e)},2050),n.parentNode.insertBefore(i,n)}return 1==e.errors[r]});i.length>0&&r.preventDefault()})}},this.Cookie=function t(e,r,i){return t.key=e,t.value=r,t.options=i,t.injectCookie(),this},this.Cookie.assign=function(e,t,r){this(e,t,r)},this.Cookie.first=function(e){return this.all()[e]||""},this.Cookie.remove=function(e,t){var r=new Date,t=t?"path="+t+";":"";r.setTime(r.getTime()-r.getTime()),document.cookie=e+"=;"+t+"expires="+r.toUTCString()},this.Cookie.all=function(){var e,t=document.cookie,r=[],i=-1!=t.indexOf(";")?t.replace(/\s+/g,"").split(";"):t.split("=");return t&&i.forEach(function(t){e=-1!=t.indexOf("=")?t.split("="):i,r[e[0]]=e[1]}),r},this.Cookie.buildAgeCookie=function(e){var t=e,r=0;return"number"==typeof t?r=e:"oneYear"==t?r=31536e3:"oneHour"==t&&(r=3600),r},this.Cookie.injectCookie=function(){var e=this.key+"="+this.value,t=this.options,r={path:"/",domain:"",age:"",expires:"",secure:!1};for(var i in t)if(Object.prototype.hasOwnProperty.call(r,i))switch(i){case"path":e+=";path="+t[i];break;case"domain":e+=";domain="+t[i];break;case"age":e+=";max-age="+this.buildAgeCookie(t[i]);break;case"expires":e+=";expires="+t[i];break;case"secure":e+=";secure="+t[i]}this.first(this.key)||(document.cookie=e)},this.localData=function r(e,t,i){t="object"==typeof e?e:function(){var r=[];return r[e]=t,r}(),r.action(t,i||"local","set")},this.localData.set=function(e,t,r){this(e,t,r)},this.localData.get=function(e,t){return this.action(e,t||"local","get")},this.localData.remove=function(e,t){this.action(e,t||"local","remove")},this.localData.exists=function(e,t){return this.action(e,t||"local","exists")},this.localData.action=function(e,t,r){var i,n=function(t){return"string"==typeof e?!!i[t]:null};switch(t){case"local":i=localStorage;break;case"session":i=sessionStorage}if("exists"==r)return n(e);if("get"==r)return"string"==typeof e?i[e]||null:null;if("set"==r){if("object"==typeof e)for(var o in e)n(o)||i[r+"Item"](o,e[o])}else i[r+"Item"](e)},this.Request=function(){var e=infoHTTP();return this.request={}||[],this.isMobile=function(){return-1!=["android","ipad","iphone"].indexOf(this.systemName)},this.isIphone=function(){return-1!=["iphone"].indexOf(this.systemName)},this.isIpad=function(){return-1!=["ipad"].indexOf(this.systemName)},this.isAndroid=function(){return-1!=["android"].indexOf(this.systemName)},this.isPC=function(){return 0==this.isMobile()},this.getBrowser=function(){var e=[],t=window.navigator.userAgent.match(/(([\\(](.*?)?[\\)]))/g);return t instanceof Array&&t.length>0&&(e=t[0].replace(/\((.*)?\)/g,"$1").split(";")),e},this.env=function(e,t,r,i){this.request=[e,t,r,i]},this.getParams=function(t){var r,i=[];if(this.slice=e.params.slice(e.params.indexOf("?")+1),0!=parseInt(this.slice))for(var n=this.slice.split("&"),o=0;o0){var i=t.request[0],n=t.request[1],o=t.request[2],s=t.request[3];r.event.validatable(),renderer(o,[t,this].concat(s)),self.Request.isAllNotGet(n)&&(window.onbeforeunload=function(){self.Cookie.remove("req_met",window.srcStorage||i)}),this.propagation=!0,delete window.srcStorage}else 0==this.propagation&&self.Cookie.first("req_met")&&self.Cookie.remove("req_met",window.srcStorage||i||e.path)},this.view=function(e,t,r){self.detached.view(e,t,r)},this.viewCompileData=function(e,t){return self.detached.compile(e,t)},this.resCookie=function(e){return self.Cookie.first(e)},this},this.Router=function(e,t,r){return this.urls=[],this.capture={},this.setMethod(e),this.setUrl(t),this.setFunc(r),this},this.Router.prototype.setUrl=function(e){this.url=e},this.Router.prototype.setMethod=function(e){this.method=e},this.Router.prototype.setFunc=function(e){this.func=e},this.Router.prototype.matchUrl=function(){return this.setUrl(captureRgx(this.getUrl(),this)),this},this.Router.prototype.getUrl=function(){return this.url},this.Router.prototype.getMethod=function(){return this.method},this.Router.prototype.getFunc=function(){return this.func},this.Route=function(){return this},this.Route.prototype.group=function(e,t){this.groupUrl=e,t.call(this,this)},this.Route.prototype.get=function(e,t){this.controller("GET",e,t)},this.Route.prototype.post=function(e,t){this.controller("POST",e,t)},this.Route.prototype.put=function(e,t){this.controller("PUT",e,t)},this.Route.prototype.patch=function(e,t){this.controller("PATCH",e,t)},this.Route.prototype["delete"]=function(e,t){this.controller("DELETE",e,t)},this.Route.prototype.options=function(e,t){this.controller("OPTIONS",e,t)},this.Route.prototype.controller=function(e,t,r){context.push(new self.Router(e,(this.groupUrl||"")+t,r).matchUrl())},dispatch.matches=function(e){var t=null;if((t=e.exec(infoHTTP().path))&&e.lastIndex==t.index){if(t[0].length==t.input.length)return delete t.index,delete t.input,t;++e.lastIndex}return!1},this.run=function(){return dispatch.bind(this)(context)},this.loadFile=function(e,t,r){var i=/(.*?)\.([html|php|json|xml|txt]+)/i;if("function"==typeof t&&(r=t),i.test(e)&&(t=e,e=null),e&&(e="object"==typeof e?e:-1!=e.indexOf("#")||-1!=e.indexOf(".")?document.querySelector(e):null,null==e))throw"element in null";xhr.open("GET",t),xhr.onreadystatechange=function(){return 4==xhr.readyState?void(r&&r.call(xhr,xhr.responseText)||e&&(e.innerHTML=xhr.responseText,e)):void 0},xhr.send()},this},global}(window||{});