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
|
9
|
|
|
9
|
|
3955
|
use strict; |
|
9
|
|
|
|
|
9
|
|
|
9
|
|
|
|
|
210
|
|
6
|
9
|
|
|
9
|
|
29
|
use warnings; |
|
9
|
|
|
|
|
14
|
|
|
9
|
|
|
|
|
224
|
|
7
|
|
|
|
|
|
|
|
8
|
9
|
|
|
9
|
|
26
|
use parent 'Mojolicious::Plugin'; |
|
9
|
|
|
|
|
17
|
|
|
9
|
|
|
|
|
62
|
|
9
|
|
|
|
|
|
|
|
10
|
9
|
|
|
9
|
|
4079
|
use HTML::ParseBrowser; |
|
9
|
|
|
|
|
17236
|
|
|
9
|
|
|
|
|
280
|
|
11
|
9
|
|
|
9
|
|
46
|
use Mojo::ByteStream; |
|
9
|
|
|
|
|
11
|
|
|
9
|
|
|
|
|
378
|
|
12
|
9
|
|
|
9
|
|
3294
|
use version 0.77; |
|
9
|
|
|
|
|
11390
|
|
|
9
|
|
|
|
|
44
|
|
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
our $VERSION = 0.06; |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
sub register { |
17
|
9
|
|
|
9
|
1
|
5343
|
my ($self, $app, $config) = @_; |
18
|
|
|
|
|
|
|
|
19
|
9
|
|
100
|
|
|
52
|
my $base = $config->{base} || ''; |
20
|
|
|
|
|
|
|
|
21
|
9
|
100
|
66
|
|
|
60
|
if ( $base and substr( $base, -1 ) ne '/' ) { |
22
|
3
|
|
|
|
|
5
|
$base .= '/'; |
23
|
|
|
|
|
|
|
} |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
$app->helper( js_load => sub { |
26
|
39
|
|
|
39
|
|
219682
|
my $c = shift; |
27
|
|
|
|
|
|
|
|
28
|
39
|
100
|
|
|
|
93
|
return '' unless _match_browser($c, @_); |
29
|
|
|
|
|
|
|
|
30
|
35
|
100
|
|
|
|
195
|
if ( $_[1]->{check} ) { |
31
|
|
|
|
|
|
|
my $asset = $c->app->static->file( |
32
|
7
|
50
|
|
|
|
19
|
$_[1]->{no_base} ? $_[0] : "$base$_[0]" |
33
|
|
|
|
|
|
|
); |
34
|
|
|
|
|
|
|
|
35
|
7
|
100
|
|
|
|
875
|
return '' if !$asset; |
36
|
|
|
|
|
|
|
} |
37
|
|
|
|
|
|
|
|
38
|
32
|
100
|
33
|
|
|
141
|
if ( $_[1] && $_[1]->{inplace} ) { |
39
|
7
|
|
|
|
|
11
|
my ($file,$config) = @_; |
40
|
7
|
50
|
|
|
|
13
|
my $local_base = $config->{no_base} ? '' : $base; |
41
|
|
|
|
|
|
|
|
42
|
7
|
50
|
|
|
|
12
|
$local_base = $c->url_for( $local_base ) if $local_base; |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
my $js = $config->{no_file} ? |
45
|
7
|
50
|
|
|
|
16
|
qq~~ : |
46
|
|
|
|
|
|
|
qq~~; |
47
|
7
|
|
|
|
|
38
|
return Mojo::ByteStream->new( $js ); |
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
|
50
|
25
|
|
|
|
|
22
|
push @{ $c->stash->{__JSLOADERFILES__} }, [ @_ ]; |
|
25
|
|
|
|
|
64
|
|
51
|
9
|
|
|
|
|
69
|
} ); |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
$app->hook( after_render => sub { |
54
|
26
|
|
|
26
|
|
1655
|
my ($c, $content, $format) = @_; |
55
|
|
|
|
|
|
|
|
56
|
26
|
50
|
|
|
|
72
|
return if $format ne 'html'; |
57
|
26
|
100
|
|
|
|
63
|
return if !$c->stash->{__JSLOADERFILES__}; |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
my $load_js = join "\n", |
60
|
|
|
|
|
|
|
map{ |
61
|
25
|
|
|
|
|
357
|
my ($file,$config) = @{ $_ }; |
|
25
|
|
|
|
|
43
|
|
62
|
25
|
100
|
|
|
|
57
|
my $local_base = $config->{no_base} ? '' : $base; |
63
|
|
|
|
|
|
|
|
64
|
25
|
100
|
|
|
|
63
|
$local_base = $c->url_for( $local_base ) if $local_base; |
65
|
|
|
|
|
|
|
|
66
|
25
|
100
|
66
|
|
|
1509
|
if ( $config->{no_file} and $config->{on_ready} ) { |
67
|
1
|
|
|
|
|
4
|
$file = sprintf '$(document).ready( function(){%s});', $file; |
68
|
|
|
|
|
|
|
} |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
$config->{no_file} ? |
71
|
25
|
100
|
|
|
|
100
|
qq~~ : |
72
|
|
|
|
|
|
|
qq~~; |
73
|
|
|
|
|
|
|
} |
74
|
15
|
50
|
|
|
|
148
|
@{ $c->stash->{__JSLOADERFILES__} || [] }; |
|
15
|
|
|
|
|
37
|
|
75
|
|
|
|
|
|
|
|
76
|
15
|
50
|
|
|
|
768
|
return if !$load_js; |
77
|
|
|
|
|
|
|
|
78
|
15
|
|
|
|
|
18
|
${$content} =~ s!( |