| blib/lib/Mojolicious/Plugin/JQuery.pm | |||
|---|---|---|---|
| Criterion | Covered | Total | % |
| statement | 38 | 38 | 100.0 |
| branch | 6 | 8 | 75.0 |
| condition | 2 | 2 | 100.0 |
| subroutine | 9 | 9 | 100.0 |
| pod | 3 | 3 | 100.0 |
| total | 58 | 60 | 96.6 |
| line | stmt | bran | cond | sub | pod | time | code |
|---|---|---|---|---|---|---|---|
| 1 | package Mojolicious::Plugin::JQuery; | ||||||
| 2 | 3 | 3 | 1494 | use File::Find; | |||
| 3 | 2 | ||||||
| 3 | 145 | ||||||
| 3 | 3 | 3 | 11 | use File::Basename (); | |||
| 3 | 3 | ||||||
| 3 | 70 | ||||||
| 4 | |||||||
| 5 | =head1 NAME | ||||||
| 6 | |||||||
| 7 | Mojolicious::Plugin::JQuery - Mojolicious + http://jquery.com/ | ||||||
| 8 | |||||||
| 9 | =head1 VERSION | ||||||
| 10 | |||||||
| 11 | 3.11001 | ||||||
| 12 | |||||||
| 13 | =head1 DESCRIPTION | ||||||
| 14 | |||||||
| 15 | L |
||||||
| 16 | JavaScript files into your project. | ||||||
| 17 | |||||||
| 18 | This is done using L |
||||||
| 19 | |||||||
| 20 | =head1 SYNOPSIS | ||||||
| 21 | |||||||
| 22 | =head2 Mojolicious::Lite | ||||||
| 23 | |||||||
| 24 | use Mojolicious::Lite; | ||||||
| 25 | plugin "JQuery"; | ||||||
| 26 | get "/" => "index"; | ||||||
| 27 | app->start; | ||||||
| 28 | |||||||
| 29 | =head2 Mojolicious | ||||||
| 30 | |||||||
| 31 | sub startup { | ||||||
| 32 | my $self = shift; | ||||||
| 33 | |||||||
| 34 | $self->plugin("JQuery"); | ||||||
| 35 | } | ||||||
| 36 | |||||||
| 37 | =head2 Template | ||||||
| 38 | |||||||
| 39 | |||||||
| 40 | |||||||
| 41 | |||||||
| 42 | |||||||
| 43 | |
||||||
| 44 | |||||||
| 54 | %= asset "jquery.js" | ||||||
| 55 | |||||||
| 56 | |||||||
| 57 | div class="notMe" |
||||||
| 58 | div class="myClass" |
||||||
| 59 | span class="myClass" | ||||||
| 60 | |||||||
| 63 | |||||||
| 64 | |||||||
| 65 | |||||||
| 66 | =head1 STATIC FILE STRUCTURE | ||||||
| 67 | |||||||
| 68 | Following the list of the static files of this project. All js are uncompressed | ||||||
| 69 | for developing. | ||||||
| 70 | |||||||
| 71 | js/jquery-3.1.1.js | ||||||
| 72 | js/jquery-migrate-1.4.1.js | ||||||
| 73 | |||||||
| 74 | =head1 Versions installed | ||||||
| 75 | |||||||
| 76 | This module ship the following version of JQuery and JQuery Migrate: | ||||||
| 77 | |||||||
| 78 | jquery-3.1.1.js | ||||||
| 79 | jquery-migrate-1.4.1.js | ||||||
| 80 | |||||||
| 81 | =over 4 | ||||||
| 82 | |||||||
| 83 | =item * jquery-migrate | ||||||
| 84 | |||||||
| 85 | The JQuery migrate plugin allow to use old plugin restoring the deprecated functions | ||||||
| 86 | on JQuery 3.x. You can use it simply enabling the migrate option on this plugin. | ||||||
| 87 | |||||||
| 88 | =back | ||||||
| 89 | |||||||
| 90 | =cut | ||||||
| 91 | |||||||
| 92 | 3 | 3 | 7 | use Mojo::Base 'Mojolicious::Plugin'; | |||
| 3 | 3 | ||||||
| 3 | 15 | ||||||
| 93 | 3 | 3 | 413 | use File::Spec::Functions 'catdir'; | |||
| 3 | 5 | ||||||
| 3 | 113 | ||||||
| 94 | 3 | 3 | 11 | use Cwd (); | |||
| 3 | 2 | ||||||
| 3 | 1050 | ||||||
| 95 | |||||||
| 96 | our $VERSION = '3.11001'; | ||||||
| 97 | |||||||
| 98 | =head1 METHODS | ||||||
| 99 | |||||||
| 100 | =head2 asset_path | ||||||
| 101 | |||||||
| 102 | $path = Mojolicious::Plugin::JQuery->asset_path(); | ||||||
| 103 | $path = $self->asset_path(); | ||||||
| 104 | |||||||
| 105 | Returns the base path to the assets bundled with this module. | ||||||
| 106 | |||||||
| 107 | =cut | ||||||
| 108 | |||||||
| 109 | sub asset_path { | ||||||
| 110 | 6 | 6 | 1 | 26 | my $class = $_; | ||
| 111 | 6 | 352 | my $path = Cwd::abs_path(__FILE__); | ||||
| 112 | |||||||
| 113 | 6 | 28 | $path =~ s!\.pm$!!; | ||||
| 114 | |||||||
| 115 | 6 | 19 | return $path; | ||||
| 116 | } | ||||||
| 117 | |||||||
| 118 | =head2 register | ||||||
| 119 | |||||||
| 120 | $app->plugin( | ||||||
| 121 | JQuery => { | ||||||
| 122 | migrate => $bool, # default false | ||||||
| 123 | jquery_1 => $bool # default false (prevent migrate inclusion) | ||||||
| 124 | }, | ||||||
| 125 | ); | ||||||
| 126 | |||||||
| 127 | Default values: | ||||||
| 128 | |||||||
| 129 | =over 4 | ||||||
| 130 | |||||||
| 131 | =item * migrate | ||||||
| 132 | |||||||
| 133 | This will include the last JQuery Migrate version shipped with this plugin. | ||||||
| 134 | Set this to 1 if you want to include this js. | ||||||
| 135 | |||||||
| 136 | =back | ||||||
| 137 | |||||||
| 138 | =cut | ||||||
| 139 | |||||||
| 140 | sub register { | ||||||
| 141 | 3 | 3 | 1 | 87 | my ( $self, $app, $config ) = @_; | ||
| 142 | 3 | 2 | my $file_type = "js"; | ||||
| 143 | |||||||
| 144 | 3 | 50 | 4 | $app->plugin('AssetPack') unless eval { $app->asset }; | |||
| 3 | 27 | ||||||
| 145 | |||||||
| 146 | 3 | 100 | 80858 | $config->{migrate} //= 0; | |||
| 147 | |||||||
| 148 | 3 | 7 | my $location = "/" . $file_type . "/"; | ||||
| 149 | my @files | ||||||
| 150 | 3 | 11 | = $self->find_files( [ $self->asset_path . '/' . $file_type ], | ||||
| 151 | $file_type ); | ||||||
| 152 | |||||||
| 153 | 3 | 5 | push @{ $app->static->paths }, $self->asset_path; | ||||
| 3 | 12 | ||||||
| 154 | $app->asset( | ||||||
| 155 | 'jquery.js' => ( $location . ( grep /^jquery-3\.(\d+)\.(\d+)\.js$/, @files )[0] ), | ||||||
| 156 | $config->{migrate} | ||||||
| 157 | 3 | 100 | 46 | ? ( $location | |||
| 158 | . ( grep /^jquery-migrate-(\d+)\.(\d+)\.(\d+)\.js$/, @files )[0] | ||||||
| 159 | ) | ||||||
| 160 | : (), | ||||||
| 161 | ); | ||||||
| 162 | } | ||||||
| 163 | |||||||
| 164 | =head2 find_files | ||||||
| 165 | |||||||
| 166 | @files = Mojolicious::Plugin::JQuery->find_files($dir,$type); | ||||||
| 167 | @files = $self->find_files($dir,$type); | ||||||
| 168 | |||||||
| 169 | Search a given file type in all directories of the array. | ||||||
| 170 | |||||||
| 171 | Required parameters: | ||||||
| 172 | |||||||
| 173 | =over 4 | ||||||
| 174 | |||||||
| 175 | =item * $dir | ||||||
| 176 | |||||||
| 177 | This must be a reference to array of directories where we are looking for | ||||||
| 178 | our files. | ||||||
| 179 | |||||||
| 180 | =item * $type | ||||||
| 181 | |||||||
| 182 | This is a string of the file's extension that we are looking for. | ||||||
| 183 | |||||||
| 184 | =back | ||||||
| 185 | |||||||
| 186 | =cut | ||||||
| 187 | |||||||
| 188 | sub find_files { | ||||||
| 189 | 3 | 3 | 1 | 8 | my $self = shift; | ||
| 190 | 3 | 4 | my $dir = shift; | ||||
| 191 | 3 | 4 | my $type = shift; | ||||
| 192 | 3 | 4 | my @files; | ||||
| 193 | find( | ||||||
| 194 | { follow => 1, | ||||||
| 195 | no_chdir => 1, | ||||||
| 196 | wanted => sub { | ||||||
| 197 | 9 | 100 | 9 | 383 | return unless -f; | ||
| 198 | 6 | 50 | 387 | push @files, File::Basename::basename($_) if $_ =~ /\.$type$/; | |||
| 199 | }, | ||||||
| 200 | }, | ||||||
| 201 | 3 | 17 | @{$dir}, | ||||
| 3 | 396 | ||||||
| 202 | ); | ||||||
| 203 | 3 | 18 | return @files; | ||||
| 204 | } | ||||||
| 205 | |||||||
| 206 | =head1 CREDITS | ||||||
| 207 | |||||||
| 208 | L |
||||||
| 209 | a lot of L |
||||||
| 210 | |||||||
| 211 | Thanks even to L |
||||||
| 212 | on his L |
||||||
| 213 | |||||||
| 214 | =head1 LICENSE | ||||||
| 215 | |||||||
| 216 | JQuery is licensed under L |
||||||
| 217 | |||||||
| 218 | This code is licensed under Artistic License version 2.0. | ||||||
| 219 | |||||||
| 220 | =head1 AUTHOR | ||||||
| 221 | |||||||
| 222 | Ferro - C |
||||||
| 223 | |||||||
| 224 | =cut | ||||||
| 225 | |||||||
| 226 | 1; |