| 1 | package Mojolicious::Plugin::JSUrlFor; | ||||||||||||||||||||||||||||
| 2 | 4 | ▶ | use Mojo::Base 'Mojolicious::Plugin'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | 4 | our $VERSION = '0.17'; | |||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 4 | ▶ | use Mojo::ByteStream qw/b/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 4 | ▶ | use Data::Dumper; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 4 | ▶ | use v5.10; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 5 | ▶ | sub register { | ||||||||||||||||||||||||||
|
Subroutine
register: called CC=2 SCAR=7.9
Pod
documented
| |||||||||||||||||||||||||||||
| 11 | 5 | my ( $self, $app, $config ) = @_; | |||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 5 | ▶ | if ( $config->{route} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 14 | 2 | ▶ | $app->routes->get( $config->{route} => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 2 | my $c = shift; | |||||||||||||||||||||||||||
| 16 | 2 | $c->render( | |||||||||||||||||||||||||||
| 17 | inline => $c->app->_js_url_for_code_only(), | ||||||||||||||||||||||||||||
| 18 | format => 'js' | ||||||||||||||||||||||||||||
| 19 | ); | ||||||||||||||||||||||||||||
| 20 | 1 | } )->name('js_url_for'); | |||||||||||||||||||||||||||
| 21 | } | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | $app->helper( | ||||||||||||||||||||||||||||
| 24 | 0 | ▶ | js_url_for => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 25 | 0 | my $c = shift; | |||||||||||||||||||||||||||
| 26 | 0 | state $b_js; # bytestream for $js | |||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 0 | ▶ | if ( $b_js && $app->mode eq 'production' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$b_js and $app->mode eq "production"
MC/DC0%0/2
$b_js and $app->mode eq "production"
$b_js
$app->mode eq "production"
Truth table0%0/3
A and B
A= $b_jsB= $app->mode eq "production"
| |||||||||||||||||||||||||||||
| 29 | 0 | return $b_js; | |||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 0 | my $js = $app->_js_url_for_code_only; | |||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 0 | $b_js = b('<script type="text/javascript">'.$js.'</script>'); | |||||||||||||||||||||||||||
| 35 | 0 | return $b_js; | |||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||
| 37 | 5 | ); | |||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | $app->helper( | ||||||||||||||||||||||||||||
| 40 | 3 | ▶ | _js_url_for_code_only => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 41 | 3 | my $c = shift; | |||||||||||||||||||||||||||
| 42 | 3 | my $endpoint_routes = $self->_collect_endpoint_routes( $app->routes ); | |||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 3 | my %names2paths; | |||||||||||||||||||||||||||
| 45 | 3 | foreach my $route (@$endpoint_routes) { | |||||||||||||||||||||||||||
| 46 | 5 | ▶ | next unless $route->name; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 5 | my $path = $self->_get_path_for_route($route); | |||||||||||||||||||||||||||
| 49 | 5 | $path =~ s{^/*}{/}g; # TODO remove this quickfix | |||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 5 | $names2paths{$route->name} = $path; | |||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 3 | my $json_routes = $c->render_to_string( json => \%names2paths ); | |||||||||||||||||||||||||||
| 55 | 3 | utf8::decode( $json_routes ); | |||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 3 | my $js = <<"JS"; | |||||||||||||||||||||||||||
| 58 | var mojolicious_routes = $json_routes; | ||||||||||||||||||||||||||||
| 59 | function url_for(route_name, captures) { | ||||||||||||||||||||||||||||
| 60 | var pattern = mojolicious_routes[route_name]; | ||||||||||||||||||||||||||||
| 61 | if(!pattern) return route_name; | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | // Fill placeholders with values | ||||||||||||||||||||||||||||
| 64 | if (!captures) captures = {}; | ||||||||||||||||||||||||||||
| 65 | for (var placeholder in captures) { // TODO order placeholders from longest to shortest | ||||||||||||||||||||||||||||
| 66 | var re = new RegExp('[:*]' + placeholder, 'g'); | ||||||||||||||||||||||||||||
| 67 | pattern = pattern.replace(re, captures[placeholder]); | ||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | // Clean not replaces placeholders | ||||||||||||||||||||||||||||
| 71 | pattern = pattern.replace(/[:*][^/.]+/g, ''); | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | return pattern; | ||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | JS | ||||||||||||||||||||||||||||
| 76 | 3 | return $js; | |||||||||||||||||||||||||||
| 77 | 5 | } ); | |||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 3 | ▶ | sub _collect_endpoint_routes { | ||||||||||||||||||||||||||
|
Subroutine
_collect_endpoint_routes: called CC=3 SCAR=11.6
| |||||||||||||||||||||||||||||
| 82 | 3 | my ( $self, $route ) = @_; | |||||||||||||||||||||||||||
| 83 | 3 | my @endpoint_routes; | |||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 3 | foreach my $child_route ( @{ $route->children } ) { | |||||||||||||||||||||||||||
| 86 | 5 | ▶ | if ( $child_route->is_endpoint ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 87 | 5 | push @endpoint_routes, $child_route; | |||||||||||||||||||||||||||
| 88 | } else { | ||||||||||||||||||||||||||||
| 89 | 0 | push @endpoint_routes, @{ $self->_collect_endpoint_routes($child_route) }; | |||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | return \@endpoint_routes | ||||||||||||||||||||||||||||
| 93 | 3 | } | |||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | 5 | ▶ | sub _get_path_for_route { | ||||||||||||||||||||||||||
|
Subroutine
_get_path_for_route: called CC=4 SCAR=14.1
| |||||||||||||||||||||||||||||
| 96 | 5 | my ( $self, $parent ) = @_; | |||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 5 | ▶ | my $path = $parent->pattern->unparsed // ''; | ||||||||||||||||||||||||||
|
Condition50%1/2
$parent->pattern->unparsed // ""
MC/DC0%0/1
$parent->pattern->unparsed // ""
$parent->pattern->unparsed
Truth table50%1/2
A
A= $parent->pattern->unparsed
| |||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | 5 | ▶ | while ( $parent = $parent->parent ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 101 | 5 | ▶ | $path = ($parent->pattern->unparsed//'') . $path; | ||||||||||||||||||||||||||
|
Condition50%1/2
$parent->pattern->unparsed // ""
MC/DC0%0/1
$parent->pattern->unparsed // ""
$parent->pattern->unparsed
Truth table50%1/2
A
A= $parent->pattern->unparsed
| |||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | 5 | return $path; | |||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 4 | 1; | |||||||||||||||||||||||||||
| 108 | __END__ | ||||||||||||||||||||||||||||