| 1 | package Mojolicious::Plugin::JSUrlFor::Angular; | ||
| 2 | use Mojo::Base 'Mojolicious::Plugin'; | ||
| 3 | use JSON::PP; | ||
| 4 | |||
| 5 | my $json = JSON::PP->new->utf8(0)->pretty; | ||
| 6 | |||
| 7 | our $VERSION = '0.18'; | ||
| 8 | |||
| 9 | |||
| 10 | sub register { | ||
| 11 | my ( $self, $app, $config ) = @_; | ||
| 12 | |||
| 13 | $app->helper( | ||
| 14 | _js_url_for_code_only => sub { | ||
| 15 | my $c = shift; | ||
| 16 | my $endpoint_routes = $self->_collect_endpoint_routes( $app->routes ); | ||
| 17 | |||
| 18 | #~ my %names2paths; | ||
| 19 | my @names2paths; | ||
| 20 | foreach my $route (@$endpoint_routes) { | ||
| 21 | next unless $route->name; | ||
| 22 | |||
| 23 | my $path = $self->_get_path_for_route($route); | ||
| 24 | $path =~ s{^/*}{/}g; # TODO remove this quickfix | ||
| 25 | |||
| 26 | #~ $names2paths{$route->name} = $path; | ||
| 27 | push @names2paths, sprintf("'%s': '%s'", $route->name, $path); | ||
| 28 | |||
| 29 | #~ if ($route->name eq 'assetpack by topic') { | ||
| 30 | #~ push @names2paths, sprintf("'%s': '%s'", "t/$_", $app->url_for('assetpack by topic', topic=>$_)) | ||
| 31 | #~ for keys %{$app->assetpack->{by_topic}}; | ||
| 32 | #~ } | ||
| 33 | } | ||
| 34 | |||
| 35 | #~ my $json_routes = $c->render_to_string( json => \%names2paths ); | ||
| 36 | my $json_routes = $json->encode(\@names2paths); | ||
| 37 | $json_routes =~ s/"//g; | ||
| 38 | $json_routes =~ s/'/"/g; | ||
| 39 | $json_routes =~ s/\[/{/g; | ||
| 40 | $json_routes =~ s/\]/}/g; | ||
| 41 | #~ utf8::decode( $json_routes ); | ||
| 42 | |||
| 43 | my $js = <<"JS"; | ||
| 44 | (function () { | ||
| 45 | 'use strict'; | ||
| 46 | /* | ||
| 47 | Маршруты/Routes | ||
| 48 | |||
| 49 | // method url_for | ||
| 50 | appRoutes.url_for(route_name, captures, param) | ||
| 51 | returns url string | ||
| 52 | captures: either object, either array, either scalar | ||
| 53 | param: either object, either scalar | ||
| 54 | |||
| 55 | appRoutes.url_for('foo name', {id: 123}); // передача объекта подстановки | ||
| 56 | appRoutes.url_for('foo name', [123]); // передача массива подстановки | ||
| 57 | appRoutes.url_for('foo name', 123); // передача скаляра подстановки | ||
| 58 | appRoutes.url_for('foo name', ..., param); // передача параметров запроса (объект или готовая строка) | ||
| 59 | |||
| 60 | // url_for tests | ||
| 61 | appRoutes.routes['foo bar'] = 'foo=:foo/bar=:bar'; | ||
| 62 | console.log(appRoutes.url_for('foo bar', []) == 'foo=/bar='); | ||
| 63 | console.log(appRoutes.url_for('foo bar', [1,2]) == 'foo=1/bar=2'); | ||
| 64 | console.log(appRoutes.url_for('foo bar', [1]) == 'foo=1/bar='); | ||
| 65 | console.log(appRoutes.url_for('foo bar') == 'foo=/bar='); | ||
| 66 | console.log(appRoutes.url_for('foo bar', {}) == 'foo=/bar='); | ||
| 67 | console.log(appRoutes.url_for('foo bar', {foo:'ok', baz:'0'}) == 'foo=ok/bar='); | ||
| 68 | console.log(appRoutes.url_for('foo bar', {foo:'ok', bar:'0'}) == 'foo=ok/bar=0' ); | ||
| 69 | console.log(appRoutes.url_for('foo bar', 'ook') == 'foo=ook/bar='); | ||
| 70 | console.log(appRoutes.url_for('foo bar', null, 'param1') == 'foo=/bar=?param1'); | ||
| 71 | console.log(appRoutes.url_for('foo bar', {foo:'ok', bar:'0'}, {p1:1,p2:2}) == 'foo=ok/bar=0?p1=1&p2=2' ); | ||
| 72 | console.log(appRoutes.url_for('foo bar', {foo:'ok', bar:'0'}, {p1:1,"парам2":[1,2]}) == 'foo=ok/bar=0?p1=1&парам2=2&парам2=2' ); | ||
| 73 | |||
| 74 | // method baseURL | ||
| 75 | var base = appRoutes.baseURL(); | ||
| 76 | appRoutes.baseURL('http://host...'); | ||
| 77 | |||
| 78 | */ | ||
| 79 | |||
| 80 | var moduleName = "appRoutes"; | ||
| 81 | |||
| 82 | try { | ||
| 83 | if (angular.module(moduleName)) return function () {}; | ||
| 84 | } catch(err) { /* failed to require */ } | ||
| 85 | |||
| 86 | var routes = $json_routes | ||
| 87 | , arr_re = new RegExp('[:*]\\\\w+', 'g') | ||
| 88 | , _baseURL = ''; | ||
| 89 | |||
| 90 | function baseURL (base) {// set/get base URL prefix | ||
| 91 | if (base == undefined) return _baseURL; | ||
| 92 | _baseURL = base; | ||
| 93 | return base; | ||
| 94 | |||
| 95 | } | ||
| 96 | |||
| 97 | |||
| 98 | function url_for(route_name, captures, param) { | ||
| 99 | var pattern = routes[route_name]; | ||
| 100 | if(!pattern) { | ||
| 101 | console.log("[angular.appRoutes] Has none route for the name: "+route_name); | ||
| 102 | return baseURL() + route_name; | ||
| 103 | } | ||
| 104 | |||
| 105 | if ( captures == undefined ) captures = []; | ||
| 106 | if ( !angular.isObject(captures) ) captures = [captures]; | ||
| 107 | if ( angular.isArray(captures) ) { | ||
| 108 | var replacer = function () { | ||
| 109 | var c = captures.shift(); | ||
| 110 | if(c == undefined) c=''; | ||
| 111 | return c; | ||
| 112 | }; | ||
| 113 | pattern = pattern.replace(arr_re, replacer); | ||
| 114 | } else { | ||
| 115 | angular.forEach(captures, function(value, placeholder) { | ||
| 116 | var re = new RegExp('[:*]' + placeholder, 'g'); | ||
| 117 | pattern = pattern.replace(re, value); | ||
| 118 | }); | ||
| 119 | pattern = pattern.replace(/[:*][^/.]+/g, ''); // Clean not replaces placeholders | ||
| 120 | } | ||
| 121 | |||
| 122 | if ( param == undefined ) return baseURL() + pattern; | ||
| 123 | if ( !angular.isObject(param) ) return baseURL() + pattern + '?' + param; | ||
| 124 | var query = []; | ||
| 125 | angular.forEach(param, function(value, name) { | ||
| 126 | if ( angular.isArray(value) ) { angular.forEach(value, function(val) {query.push(name+'='+val);}); } | ||
| 127 | else { query.push(name+'='+value); } | ||
| 128 | }); | ||
| 129 | if (!query.length) return baseURL() + pattern; | ||
| 130 | return baseURL() + pattern + '?' + query.join('&'); | ||
| 131 | } | ||
| 132 | |||
| 133 | var factory = { | ||
| 134 | routes: routes, | ||
| 135 | baseURL: baseURL, | ||
| 136 | url_for: url_for | ||
| 137 | }; | ||
| 138 | |||
| 139 | angular.module(moduleName, []) | ||
| 140 | |||
| 141 | .run(function (\$window) { | ||
| 142 | \$window['angular.'+moduleName] = factory; | ||
| 143 | }) | ||
| 144 | |||
| 145 | .factory(moduleName, function () { | ||
| 146 | return factory; | ||
| 147 | }) | ||
| 148 | ; | ||
| 149 | |||
| 150 | }()); | ||
| 151 | JS | ||
| 152 | return $js; | ||
| 153 | } ); | ||
| 154 | } | ||
| 155 | |||
| 156 | |||
| 157 | sub _collect_endpoint_routes { | ||
| 158 | my ( $self, $route ) = @_; | ||
| 159 | my @endpoint_routes; | ||
| 160 | |||
| 161 | foreach my $child_route ( @{ $route->children } ) { | ||
| 162 | if ( $child_route->is_endpoint ) { | ||
| 163 | push @endpoint_routes, $child_route; | ||
| 164 | } else { | ||
| 165 | push @endpoint_routes, @{ $self->_collect_endpoint_routes($child_route) }; | ||
| 166 | } | ||
| 167 | } | ||
| 168 | return \@endpoint_routes | ||
| 169 | } | ||
| 170 | |||
| 171 | sub _get_path_for_route { | ||
| 172 | my ( $self, $parent ) = @_; | ||
| 173 | |||
| 174 | my $path = $parent->pattern->unparsed // ''; | ||
| 175 | |||
| 176 | while ( $parent = $parent->parent ) { | ||
| 177 | $path = ($parent->pattern->unparsed//'') . $path; | ||
| 178 | } | ||
| 179 | |||
| 180 | return $path; | ||
| 181 | } | ||
| 182 | |||
| 183 | 1; | ||
| 184 | __END__ |