diff --git a/src/Console/Commands/LarouteGeneratorCommand.php b/src/Console/Commands/LarouteGeneratorCommand.php index 5e3d939..cf0ed67 100644 --- a/src/Console/Commands/LarouteGeneratorCommand.php +++ b/src/Console/Commands/LarouteGeneratorCommand.php @@ -101,10 +101,10 @@ protected function getTemplatePath() protected function getTemplateData() { $namespace = $this->getOptionOrConfig('namespace'); - $routes = $this->routes->toJSON(); - $absolute = $this->config->get('laroute.absolute', false); - $rootUrl = $this->config->get('app.url', ''); - $prefix = $this->config->get('laroute.prefix', ''); + $routes = $this->option('empty') ? json_encode([]) : $this->routes->toJSON(); + $absolute = $this->option('empty') ? false : $this->config->get('laroute.absolute', false); + $rootUrl = $this->option('empty') ? '' : $this->config->get('app.url', ''); + $prefix = $this->option('empty') ? '' : $this->config->get('laroute.prefix', ''); return compact('namespace', 'routes', 'absolute', 'rootUrl', 'prefix'); } @@ -162,13 +162,24 @@ protected function getOptions() [ 'namespace', null, - InputOption::VALUE_OPTIONAL, sprintf('Javascript namespace for the functions (think _.js) (default: "%s")', $this->config->get('laroute.namespace')) + InputOption::VALUE_OPTIONAL, + sprintf( + 'Javascript namespace for the functions (think _.js) (default: "%s")', + $this->config->get('laroute.namespace') + ) ], [ 'prefix', 'pr', - InputOption::VALUE_OPTIONAL, sprintf('Prefix for the generated URLs (default: "%s")', $this->config->get('laroute.prefix')) + InputOption::VALUE_OPTIONAL, + sprintf('Prefix for the generated URLs (default: "%s")', $this->config->get('laroute.prefix')) ], + [ + 'empty', + null, + InputOption::VALUE_NONE, + 'Whether to hard-code configuration values in the generated JavaScript file.' + ] ]; } } \ No newline at end of file diff --git a/src/templates/laroute.js b/src/templates/laroute.js index d562484..811e302 100644 --- a/src/templates/laroute.js +++ b/src/templates/laroute.js @@ -161,6 +161,30 @@ var url = this.action(action, parameters); return getHtmlLink(url, title, attributes); + }, + + // Whether we're using absolute URLs or not. + // $NAMESPACE$.set_absolute(true) + set_absolute : function (absolute) { + routes.absolute = absolute; + }, + + // Set a base url for all routes. + // $NAMESPACE$.set_root_url('http://localhost') + set_root_url : function (url) { + routes.rootUrl = url; + }, + + // JSON encoded route collection. + // $NAMESPACE$.set_routes([]) + set_routes : function (json) { + routes.routes = json; + }, + + // Add a prefix to all URLs. + // $NAMEPSACE$.set_prefix('my-prefix') + set_prefix : function (prefix) { + routes.prefix = prefix; } }; diff --git a/src/templates/laroute.min.js b/src/templates/laroute.min.js index 37a7fac..2ba4b2a 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){return c=c||this.getByName(a),c?this.toRoute(c,b):void 0},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.getCorrectUrl(c+d)},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 a=function(){var a={absolute:$ABSOLUTE$,rootUrl:"$ROOTURL$",routes:$ROUTES$,prefix:"$PREFIX$",route:function(a,b,c){return(c=c||this.getByName(a))?this.toRoute(c,b):void 0},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.getCorrectUrl(c+d)},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)},set_absolute:function(b){a.absolute=b},set_root_url:function(b){a.rootUrl=b},set_routes:function(a){a.routes=a},set_prefix:function(b){a.prefix=b}}}.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