From cd864a361c6bb09c11a3773ea16182c96609bfbf Mon Sep 17 00:00:00 2001 From: Vasilii Arnautov Date: Wed, 18 Sep 2019 18:13:37 +0300 Subject: [PATCH] larouteParameters Add default parameters to laroute.js --- src/templates/laroute.js | 7 ++++--- src/templates/laroute.min.js | 2 +- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/templates/laroute.js b/src/templates/laroute.js index f868098..2408aea 100644 --- a/src/templates/laroute.js +++ b/src/templates/laroute.js @@ -8,6 +8,7 @@ rootUrl: '$ROOTURL$', routes : $ROUTES$, prefix: '$PREFIX$', + parameters: window.larouteParameters || {}, route : function (name, parameters, route) { route = route || this.getByName(name); @@ -127,7 +128,7 @@ // Generate a url for a given controller action. // $NAMESPACE$.action('HomeController@getIndex', [params = {}]) action : function (name, parameters) { - parameters = parameters || {}; + parameters = Object.assign({}, routes.parameters, parameters || {}); return routes.route(name, parameters, routes.getByAction(name)); }, @@ -135,7 +136,7 @@ // Generate a url for a given named route. // $NAMESPACE$.route('routeName', [params = {}]) route : function (route, parameters) { - parameters = parameters || {}; + parameters = Object.assign({}, routes.parameters, parameters || {}); return routes.route(route, parameters); }, @@ -143,7 +144,7 @@ // Generate a fully qualified URL to the given path. // $NAMESPACE$.route('url', [params = {}]) url : function (route, parameters) { - parameters = parameters || {}; + parameters = Object.assign({}, routes.parameters, parameters || {}); return routes.url(route, parameters); }, diff --git a/src/templates/laroute.min.js b/src/templates/laroute.min.js index 2593912..591db55 100644 --- a/src/templates/laroute.min.js +++ b/src/templates/laroute.min.js @@ -1 +1 @@ -(function(){var a=function(){var a={absolute:$ABSOLUTE$,rootUrl:"$ROOTURL$",routes:$ROUTES$,prefix:"$PREFIX$",route:function(a,b,c){if(c=c||this.getByName(a))return this.toRoute(c,b)},url:function(a,b){b=b||[];var c=a+"/"+b.join("/");return this.getCorrectUrl(c)},toRoute:function(a,b){var c=this.replaceNamedParameters(a.uri,b),d=this.getRouteQueryString(b);return this.absolute&&this.isOtherHost(a)?"//"+a.host+"/"+c+d:this.getCorrectUrl(c+d)},isOtherHost:function(a){return a.host&&a.host!=window.location.hostname},replaceNamedParameters:function(a,b){return a=a.replace(/\{(.*?)\??\}/g,function(a,c){if(b.hasOwnProperty(c)){var d=b[c];return delete b[c],d}return a}),a=a.replace(/\/\{.*?\?\}/g,"")},getRouteQueryString:function(a){var b=[];for(var c in a)a.hasOwnProperty(c)&&b.push(c+"="+a[c]);return b.length<1?"":"?"+b.join("&")},getByName:function(a){for(var b in this.routes)if(this.routes.hasOwnProperty(b)&&this.routes[b].name===a)return this.routes[b]},getByAction:function(a){for(var b in this.routes)if(this.routes.hasOwnProperty(b)&&this.routes[b].action===a)return this.routes[b]},getCorrectUrl:function(a){var b=this.prefix+"/"+a.replace(/^\/?/,"");return this.absolute?this.rootUrl.replace("//?$/","")+b:b}},b=function(a){if(!a)return"";var b=[];for(var c in a)a.hasOwnProperty(c)&&b.push(c+'="'+a[c]+'"');return b.join(" ")},c=function(a,c,d){return c=c||a,d=b(d),'"+c+""};return{action:function(b,c){return c=c||{},a.route(b,c,a.getByAction(b))},route:function(b,c){return c=c||{},a.route(b,c)},url:function(b,c){return c=c||{},a.url(b,c)},link_to:function(a,b,d){return a=this.url(a),c(a,b,d)},link_to_route:function(a,b,d,e){var f=this.route(a,d);return c(f,b,e)},link_to_action:function(a,b,d,e){var f=this.action(a,d);return c(f,b,e)}}}.call(this);"function"==typeof define&&define.amd?define(function(){return a}):"object"==typeof module&&module.exports?module.exports=a:window.$NAMESPACE$=a}).call(this); \ No newline at end of file +(function(){var t=function(){function i(t,r,e){return r=r||t,'"+r+""}var e={absolute:$ABSOLUTE$,rootUrl:"$ROOTURL$",routes:$ROUTES$,prefix:"$PREFIX$",parameters:window.larouteParameters||{},route:function(t,r,e){if(e=e||this.getByName(t))return this.toRoute(e,r)},url:function(t,r){var e=t+"/"+(r=r||[]).join("/");return this.getCorrectUrl(e)},toRoute:function(t,r){var e=this.replaceNamedParameters(t.uri,r),n=this.getRouteQueryString(r);return this.absolute&&this.isOtherHost(t)?"//"+t.host+"/"+e+n:this.getCorrectUrl(e+n)},isOtherHost:function(t){return t.host&&t.host!=window.location.hostname},replaceNamedParameters:function(t,n){return t=(t=t.replace(/\{(.*?)\??\}/g,function(t,r){if(n.hasOwnProperty(r)){var e=n[r];return delete n[r],e}return t})).replace(/\/\{.*?\?\}/g,"")},getRouteQueryString:function(t){var r=[];for(var e in t)t.hasOwnProperty(e)&&r.push(e+"="+t[e]);return r.length<1?"":"?"+r.join("&")},getByName:function(t){for(var r in this.routes)if(this.routes.hasOwnProperty(r)&&this.routes[r].name===t)return this.routes[r]},getByAction:function(t){for(var r in this.routes)if(this.routes.hasOwnProperty(r)&&this.routes[r].action===t)return this.routes[r]},getCorrectUrl:function(t){var r=this.prefix+"/"+t.replace(/^\/?/,"");return this.absolute?this.rootUrl.replace("//?$/","")+r:r}};return{action:function(t,r){return r=Object.assign({},e.parameters,r||{}),e.route(t,r,e.getByAction(t))},route:function(t,r){return r=Object.assign({},e.parameters,r||{}),e.route(t,r)},url:function(t,r){return r=Object.assign({},e.parameters,r||{}),e.url(t,r)},link_to:function(t,r,e){return t=this.url(t),i(t,r,e)},link_to_route:function(t,r,e,n){var o=this.route(t,e);return i(o,r,n)},link_to_action:function(t,r,e,n){var o=this.action(t,e);return i(o,r,n)}}}.call(this);"function"==typeof define&&define.amd?define(function(){return t}):"object"==typeof module&&module.exports?module.exports=t:window.$NAMESPACE$=t}).call(this); \ No newline at end of file