line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Mojolicious::Plugin::JSLoader; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
# ABSTRACT: move js loading to the end of the document |
4
|
|
|
|
|
|
|
|
5
|
11
|
|
|
11
|
|
7924
|
use strict; |
|
11
|
|
|
|
|
22
|
|
|
11
|
|
|
|
|
317
|
|
6
|
11
|
|
|
11
|
|
53
|
use warnings; |
|
11
|
|
|
|
|
19
|
|
|
11
|
|
|
|
|
298
|
|
7
|
|
|
|
|
|
|
|
8
|
11
|
|
|
11
|
|
54
|
use parent 'Mojolicious::Plugin'; |
|
11
|
|
|
|
|
19
|
|
|
11
|
|
|
|
|
65
|
|
9
|
|
|
|
|
|
|
|
10
|
11
|
|
|
11
|
|
5815
|
use HTML::ParseBrowser; |
|
11
|
|
|
|
|
30134
|
|
|
11
|
|
|
|
|
405
|
|
11
|
11
|
|
|
11
|
|
78
|
use Mojo::ByteStream; |
|
11
|
|
|
|
|
23
|
|
|
11
|
|
|
|
|
506
|
|
12
|
11
|
|
|
11
|
|
4458
|
use version 0.77; |
|
11
|
|
|
|
|
20041
|
|
|
11
|
|
|
|
|
80
|
|
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
our $VERSION = '0.07'; |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
sub register { |
17
|
11
|
|
|
11
|
1
|
7791
|
my ($self, $app, $config) = @_; |
18
|
|
|
|
|
|
|
|
19
|
11
|
|
100
|
|
|
70
|
my $base = $config->{base} || ''; |
20
|
|
|
|
|
|
|
|
21
|
11
|
100
|
100
|
|
|
81
|
if ( $base and substr( $base, -1 ) ne '/' ) { |
22
|
3
|
|
|
|
|
7
|
$base .= '/'; |
23
|
|
|
|
|
|
|
} |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
$app->helper( js_load => sub { |
26
|
55
|
|
|
55
|
|
459674
|
my $c = shift; |
27
|
|
|
|
|
|
|
|
28
|
55
|
100
|
|
|
|
163
|
return '' unless _match_browser($c, @_); |
29
|
|
|
|
|
|
|
|
30
|
51
|
100
|
|
|
|
304
|
if ( $_[1]->{check} ) { |
31
|
|
|
|
|
|
|
my $asset = $c->app->static->file( |
32
|
7
|
50
|
|
|
|
20
|
$_[1]->{no_base} ? $_[0] : "$base$_[0]" |
33
|
|
|
|
|
|
|
); |
34
|
|
|
|
|
|
|
|
35
|
7
|
100
|
|
|
|
871
|
return '' if !$asset; |
36
|
|
|
|
|
|
|
} |
37
|
|
|
|
|
|
|
|
38
|
48
|
100
|
66
|
|
|
284
|
if ( $_[1] && $_[1]->{inplace} ) { |
39
|
7
|
|
|
|
|
16
|
my ($file,$config) = @_; |
40
|
7
|
50
|
|
|
|
21
|
my $local_base = $config->{no_base} ? '' : $base; |
41
|
|
|
|
|
|
|
|
42
|
7
|
50
|
|
|
|
19
|
$local_base = $c->url_for( $local_base ) if $local_base; |
43
|
|
|
|
|
|
|
|
44
|
7
|
50
|
|
|
|
17
|
$config->{no_file} = 1 if $config->{js}; |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
my $js = $config->{no_file} ? |
47
|
7
|
50
|
|
|
|
48
|
qq~~ : |
48
|
|
|
|
|
|
|
qq~~; |
49
|
7
|
|
|
|
|
54
|
return Mojo::ByteStream->new( $js ); |
50
|
|
|
|
|
|
|
} |
51
|
|
|
|
|
|
|
|
52
|
41
|
|
|
|
|
61
|
push @{ $c->stash->{__JSLOADERFILES__} }, [ @_ ]; |
|
41
|
|
|
|
|
111
|
|
53
|
11
|
|
|
|
|
142
|
} ); |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
$app->hook( after_render => sub { |
56
|
32
|
|
|
32
|
|
2796
|
my ($c, $content, $format) = @_; |
57
|
|
|
|
|
|
|
|
58
|
32
|
50
|
|
|
|
110
|
return if $format ne 'html'; |
59
|
32
|
100
|
|
|
|
140
|
return if !$c->stash->{__JSLOADERFILES__}; |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
my $load_js = join "\n", |
62
|
|
|
|
|
|
|
map{ |
63
|
41
|
|
|
|
|
562
|
my ($file,$config) = @{ $_ }; |
|
41
|
|
|
|
|
74
|
|
64
|
41
|
100
|
|
|
|
116
|
my $local_base = $config->{no_base} ? '' : $base; |
65
|
|
|
|
|
|
|
|
66
|
41
|
100
|
|
|
|
91
|
$config->{no_file} = 1 if $config->{js}; |
67
|
|
|
|
|
|
|
|
68
|
41
|
100
|
|
|
|
128
|
$local_base = $c->url_for( $local_base ) if $local_base; |
69
|
|
|
|
|
|
|
|
70
|
41
|
100
|
100
|
|
|
4324
|
if ( $config->{no_file} and $config->{on_ready} ) { |
71
|
3
|
|
|
|
|
21
|
$file = sprintf '$(document).ready( function(){%s});', $file; |
72
|
|
|
|
|
|
|
} |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
$config->{no_file} ? |
75
|
41
|
100
|
|
|
|
237
|
qq~~ : |
76
|
|
|
|
|
|
|
qq~~; |
77
|
|
|
|
|
|
|
} |
78
|
21
|
50
|
|
|
|
261
|
@{ $c->stash->{__JSLOADERFILES__} || [] }; |
|
21
|
|
|
|
|
70
|
|
79
|
|
|
|
|
|
|
|
80
|
21
|
50
|
|
|
|
2492
|
return if !$load_js; |
81
|
|
|
|
|
|
|
|
82
|
21
|
|
|
|
|
35
|
${$content} =~ s!( |