From 3c70635ddbd818eb868ad76999691d3c2d8af063 Mon Sep 17 00:00:00 2001 From: TAHRI Ahmed R Date: Sun, 12 Jan 2020 23:07:33 +0100 Subject: [PATCH] Bugfix when baseurl is missing or empty in json (#371) * window.location fallback when baseurl is empty or missing --- Resources/js/router.js | 2 +- Resources/public/js/router.js | 2 +- Resources/public/js/router.min.js | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Resources/js/router.js b/Resources/js/router.js index d32801dd..1eda10ee 100644 --- a/Resources/js/router.js +++ b/Resources/js/router.js @@ -46,7 +46,7 @@ class Router { * @param {Object} data */ setRoutingData(data) { - this.setBaseUrl(data['base_url']); + this.setBaseUrl((data['base_url'] && data['base_url'].length > 0) ? data['base_url'] : window.location.host); this.setRoutes(data['routes']); if ('prefix' in data) { diff --git a/Resources/public/js/router.js b/Resources/public/js/router.js index 4fc427f3..79551c2b 100644 --- a/Resources/public/js/router.js +++ b/Resources/public/js/router.js @@ -67,7 +67,7 @@ var Router = function () { * @param {Object} data */ value: function setRoutingData(data) { - this.setBaseUrl(data['base_url']); + this.setBaseUrl(data['base_url'] && data['base_url'].length > 0 ? data['base_url'] : window.location.host); this.setRoutes(data['routes']); if ('prefix' in data) { diff --git a/Resources/public/js/router.min.js b/Resources/public/js/router.min.js index a6a303a9..b2ba3c45 100644 --- a/Resources/public/js/router.min.js +++ b/Resources/public/js/router.min.js @@ -1 +1 @@ -!function(e,t){var n=t();"function"==typeof define&&define.amd?define([],n.Routing):"object"==typeof module&&module.exports?module.exports=n.Routing:(e.Routing=n.Routing,e.fos={Router:n.Router})}(this,function(){"use strict";function e(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}var t=Object.assign||function(e){for(var t=1;t2&&void 0!==arguments[2]&&arguments[2],i=this.getRoute(e),r=n||{},s=t({},r),u="",c=!0,a="",f="undefined"==typeof this.getPort()||null===this.getPort()?"":this.getPort();if(i.tokens.forEach(function(t){if("text"===t[0])return u=t[1]+u,void(c=!1);{if("variable"!==t[0])throw new Error('The token type "'+t[0]+'" is not supported.');var n=i.defaults&&t[3]in i.defaults;if(!1===c||!n||t[3]in r&&r[t[3]]!=i.defaults[t[3]]){var o=void 0;if(t[3]in r)o=r[t[3]],delete s[t[3]];else{if(!n){if(c)return;throw new Error('The route "'+e+'" requires the parameter "'+t[3]+'".')}o=i.defaults[t[3]]}var a=!0===o||!1===o||""===o;if(!a||!c){var f=encodeURIComponent(o).replace(/%2F/g,"/");"null"===f&&null===o&&(f=""),u=t[1]+f+u}c=!1}else n&&t[3]in s&&delete s[t[3]]}}),""===u&&(u="/"),i.hosttokens.forEach(function(e){var t=void 0;return"text"===e[0]?void(a=e[1]+a):void("variable"===e[0]&&(e[3]in r?(t=r[e[3]],delete s[e[3]]):i.defaults&&e[3]in i.defaults&&(t=i.defaults[e[3]]),a=e[1]+t+a))}),u=this.context_.base_url+u,i.requirements&&"_scheme"in i.requirements&&this.getScheme()!=i.requirements._scheme?u=i.requirements._scheme+"://"+(a||this.getHost())+(""===f?"":":"+f)+u:"undefined"!=typeof i.schemes&&"undefined"!=typeof i.schemes[0]&&this.getScheme()!==i.schemes[0]?u=i.schemes[0]+"://"+(a||this.getHost())+(""===f?"":":"+f)+u:a&&this.getHost()!==a+(""===f?"":":"+f)?u=this.getScheme()+"://"+a+(""===f?"":":"+f)+u:o===!0&&(u=this.getScheme()+"://"+this.getHost()+(""===f?"":":"+f)+u),Object.keys(s).length>0){var l=void 0,h=[],y=function(e,t){t="function"==typeof t?t():t,t=null===t?"":t,h.push(encodeURIComponent(e)+"="+encodeURIComponent(t))};for(l in s)this.buildQueryParams(l,s[l],y);u=u+"?"+h.join("&").replace(/%20/g,"+")}return u}}],[{key:"getInstance",value:function(){return r}},{key:"setData",value:function(e){var t=i.getInstance();t.setRoutingData(e)}}]),i}();i.Route,i.Context;var r=new i;return{Router:i,Routing:r}}); \ No newline at end of file +!function(e,t){var n=t();"function"==typeof define&&define.amd?define([],n.Routing):"object"==typeof module&&module.exports?module.exports=n.Routing:(e.Routing=n.Routing,e.fos={Router:n.Router})}(this,function(){"use strict";function e(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}var t=Object.assign||function(e){for(var t=1;t0?e.base_url:window.location.host),this.setRoutes(e.routes),"prefix"in e&&this.setPrefix(e.prefix),"port"in e&&this.setPort(e.port),"locale"in e&&this.setLocale(e.locale),this.setHost(e.host),this.setScheme(e.scheme)}},{key:"setRoutes",value:function(e){this.routes_=Object.freeze(e)}},{key:"getRoutes",value:function(){return this.routes_}},{key:"setBaseUrl",value:function(e){this.context_.base_url=e}},{key:"getBaseUrl",value:function(){return this.context_.base_url}},{key:"setPrefix",value:function(e){this.context_.prefix=e}},{key:"setScheme",value:function(e){this.context_.scheme=e}},{key:"getScheme",value:function(){return this.context_.scheme}},{key:"setHost",value:function(e){this.context_.host=e}},{key:"getHost",value:function(){return this.context_.host}},{key:"setPort",value:function(e){this.context_.port=e}},{key:"getPort",value:function(){return this.context_.port}},{key:"setLocale",value:function(e){this.context_.locale=e}},{key:"getLocale",value:function(){return this.context_.locale}},{key:"buildQueryParams",value:function(e,t,o){var i=this,r=void 0,s=new RegExp(/\[\]$/);if(t instanceof Array)t.forEach(function(t,r){s.test(e)?o(e,t):i.buildQueryParams(e+"["+("object"===("undefined"==typeof t?"undefined":n(t))?r:"")+"]",t,o)});else if("object"===("undefined"==typeof t?"undefined":n(t)))for(r in t)this.buildQueryParams(e+"["+r+"]",t[r],o);else o(e,t)}},{key:"getRoute",value:function(e){var t=this.context_.prefix+e,n=e+"."+this.context_.locale,o=this.context_.prefix+e+"."+this.context_.locale,i=[t,n,o,e];for(var r in i)if(i[r]in this.routes_)return this.routes_[i[r]];throw new Error('The route "'+e+'" does not exist.')}},{key:"generate",value:function(e,n){var o=arguments.length>2&&void 0!==arguments[2]&&arguments[2],i=this.getRoute(e),r=n||{},s=t({},r),u="",a=!0,c="",f="undefined"==typeof this.getPort()||null===this.getPort()?"":this.getPort();if(i.tokens.forEach(function(t){if("text"===t[0])return u=t[1]+u,void(a=!1);{if("variable"!==t[0])throw new Error('The token type "'+t[0]+'" is not supported.');var n=i.defaults&&t[3]in i.defaults;if(!1===a||!n||t[3]in r&&r[t[3]]!=i.defaults[t[3]]){var o=void 0;if(t[3]in r)o=r[t[3]],delete s[t[3]];else{if(!n){if(a)return;throw new Error('The route "'+e+'" requires the parameter "'+t[3]+'".')}o=i.defaults[t[3]]}var c=!0===o||!1===o||""===o;if(!c||!a){var f=encodeURIComponent(o).replace(/%2F/g,"/");"null"===f&&null===o&&(f=""),u=t[1]+f+u}a=!1}else n&&t[3]in s&&delete s[t[3]]}}),""===u&&(u="/"),i.hosttokens.forEach(function(e){var t=void 0;return"text"===e[0]?void(c=e[1]+c):void("variable"===e[0]&&(e[3]in r?(t=r[e[3]],delete s[e[3]]):i.defaults&&e[3]in i.defaults&&(t=i.defaults[e[3]]),c=e[1]+t+c))}),u=this.context_.base_url+u,i.requirements&&"_scheme"in i.requirements&&this.getScheme()!=i.requirements._scheme?u=i.requirements._scheme+"://"+(c||this.getHost())+(""===f?"":":"+f)+u:"undefined"!=typeof i.schemes&&"undefined"!=typeof i.schemes[0]&&this.getScheme()!==i.schemes[0]?u=i.schemes[0]+"://"+(c||this.getHost())+(""===f?"":":"+f)+u:c&&this.getHost()!==c+(""===f?"":":"+f)?u=this.getScheme()+"://"+c+(""===f?"":":"+f)+u:o===!0&&(u=this.getScheme()+"://"+this.getHost()+(""===f?"":":"+f)+u),Object.keys(s).length>0){var l=void 0,h=[],y=function(e,t){t="function"==typeof t?t():t,t=null===t?"":t,h.push(encodeURIComponent(e)+"="+encodeURIComponent(t))};for(l in s)this.buildQueryParams(l,s[l],y);u=u+"?"+h.join("&").replace(/%20/g,"+")}return u}}],[{key:"getInstance",value:function(){return r}},{key:"setData",value:function(e){var t=i.getInstance();t.setRoutingData(e)}}]),i}();i.Route,i.Context;var r=new i;return{Router:i,Routing:r}}); \ No newline at end of file