| blib/lib/Mojolicious/Plugin/JQuery.pm | |||
|---|---|---|---|
| Criterion | Covered | Total | % | 
| statement | 39 | 39 | 100.0 | 
| branch | 8 | 10 | 80.0 | 
| condition | 6 | 7 | 85.7 | 
| subroutine | 9 | 9 | 100.0 | 
| pod | 3 | 3 | 100.0 | 
| total | 65 | 68 | 95.5 | 
| line | stmt | bran | cond | sub | pod | time | code | 
|---|---|---|---|---|---|---|---|
| 1 | package Mojolicious::Plugin::JQuery; | ||||||
| 2 | 4 | 4 | 3270 | use File::Find; | |||
| 4 | 8 | ||||||
| 4 | 276 | ||||||
| 3 | 4 | 4 | 19 | use File::Basename (); | |||
| 4 | 8 | ||||||
| 4 | 139 | ||||||
| 4 | |||||||
| 5 | =head1 NAME | ||||||
| 6 | |||||||
| 7 | Mojolicious::Plugin::JQuery - Mojolicious + http://jquery.com/ | ||||||
| 8 | |||||||
| 9 | =head1 VERSION | ||||||
| 10 | |||||||
| 11 | 2.14002 | ||||||
| 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-1.11.3.js | ||||||
| 72 | js/jquery-2.1.4.js | ||||||
| 73 | js/jquery-migrate-1.2.1.js | ||||||
| 74 | |||||||
| 75 | =head1 Versions installed | ||||||
| 76 | |||||||
| 77 | This module ship the following version of JQuery and JQuery Migrate: | ||||||
| 78 | |||||||
| 79 | jquery-1.11.3.js | ||||||
| 80 | jquery-2.1.4.js | ||||||
| 81 | jquery-migrate-1.2.1.js | ||||||
| 82 | |||||||
| 83 | =over 4 | ||||||
| 84 | |||||||
| 85 | =item * jquery-migrate | ||||||
| 86 | |||||||
| 87 | The JQuery migrate plugin allow to use old plugin restoring the deprecated functions | ||||||
| 88 | on JQuery 2.x. You can use it simply enabling the migrate option on this plugin. | ||||||
| 89 | |||||||
| 90 | =back | ||||||
| 91 | |||||||
| 92 | =cut | ||||||
| 93 | |||||||
| 94 | 4 | 4 | 20 | use Mojo::Base 'Mojolicious::Plugin'; | |||
| 4 | 6 | ||||||
| 4 | 31 | ||||||
| 95 | 4 | 4 | 804 | use File::Spec::Functions 'catdir'; | |||
| 4 | 7 | ||||||
| 4 | 230 | ||||||
| 96 | 4 | 4 | 23 | use Cwd (); | |||
| 4 | 6 | ||||||
| 4 | 2410 | ||||||
| 97 | |||||||
| 98 | our $VERSION = '2.14002'; | ||||||
| 99 | |||||||
| 100 | =head1 METHODS | ||||||
| 101 | |||||||
| 102 | =head2 asset_path | ||||||
| 103 | |||||||
| 104 | $path = Mojolicious::Plugin::JQuery->asset_path(); | ||||||
| 105 | $path = $self->asset_path(); | ||||||
| 106 | |||||||
| 107 | Returns the base path to the assets bundled with this module. | ||||||
| 108 | |||||||
| 109 | =cut | ||||||
| 110 | |||||||
| 111 | sub asset_path { | ||||||
| 112 | 8 | 8 | 1 | 50 | my $class = $_; | ||
| 113 | 8 | 1108 | my $path = Cwd::abs_path(__FILE__); | ||||
| 114 | |||||||
| 115 | 8 | 39 | $path =~ s!\.pm$!!; | ||||
| 116 | |||||||
| 117 | 8 | 39 | return $path; | ||||
| 118 | } | ||||||
| 119 | |||||||
| 120 | =head2 register | ||||||
| 121 | |||||||
| 122 | $app->plugin( | ||||||
| 123 | JQuery => { | ||||||
| 124 | migrate => $bool, # default false | ||||||
| 125 | jquery_1 => $bool # default false (prevent migrate inclusion) | ||||||
| 126 | }, | ||||||
| 127 | ); | ||||||
| 128 | |||||||
| 129 | Default values: | ||||||
| 130 | |||||||
| 131 | =over 4 | ||||||
| 132 | |||||||
| 133 | =item * migrate | ||||||
| 134 | |||||||
| 135 | This will include the last JQuery Migrate version shipped with this plugin. | ||||||
| 136 | Set this to 1 if you want to include this js. | ||||||
| 137 | |||||||
| 138 | =item * jquery_1 | ||||||
| 139 | |||||||
| 140 | This will include the last 1.x.x JQuery version shipped with this plugin. | ||||||
| 141 | Set this to 1 if you want to use this version. | ||||||
| 142 | (This option will prevent JQuery Migrate inclusion) | ||||||
| 143 | |||||||
| 144 | =back | ||||||
| 145 | |||||||
| 146 | =cut | ||||||
| 147 | |||||||
| 148 | sub register { | ||||||
| 149 | 4 | 4 | 1 | 160 | my ( $self, $app, $config ) = @_; | ||
| 150 | 4 | 10 | my $file_type = "js"; | ||||
| 151 | |||||||
| 152 | 4 | 50 | 7 | $app->plugin('AssetPack') unless eval { $app->asset }; | |||
| 4 | 64 | ||||||
| 153 | |||||||
| 154 | 4 | 100 | 128844 | $config->{migrate} //= 0; | |||
| 155 | 4 | 100 | 41 | $config->{jquery_1} //= 0; | |||
| 156 | |||||||
| 157 | 4 | 15 | my $location = "/" . $file_type . "/"; | ||||
| 158 | my @files | ||||||
| 159 | 4 | 21 | = $self->find_files( [ $self->asset_path . '/' . $file_type ], | ||||
| 160 | $file_type ); | ||||||
| 161 | |||||||
| 162 | 4 | 11 | push @{ $app->static->paths }, $self->asset_path; | ||||
| 4 | 22 | ||||||
| 163 | $app->asset( | ||||||
| 164 | 'jquery.js' => $config->{jquery_1} | ||||||
| 165 | ? ( $location . ( grep /^jquery-1\.(\d+)\.(\d+)\.js$/, @files )[0] ) | ||||||
| 166 | : ( $location . ( grep /^jquery-2\.(\d+)\.(\d+)\.js$/, @files )[0] ), | ||||||
| 167 | $config->{migrate} && !( $config->{jquery_1} ) | ||||||
| 168 | 4 | 100 | 66 | 97 | ? ( $location | ||
| 100 | |||||||
| 169 | . ( grep /^jquery-migrate-(\d+)\.(\d+)\.(\d+)\.js$/, @files )[0] | ||||||
| 170 | ) | ||||||
| 171 | : (), | ||||||
| 172 | ); | ||||||
| 173 | |||||||
| 174 | } | ||||||
| 175 | |||||||
| 176 | =head2 find_files | ||||||
| 177 | |||||||
| 178 | @files = Mojolicious::Plugin::JQuery->find_files($dir,$type); | ||||||
| 179 | @files = $self->find_files($dir,$type); | ||||||
| 180 | |||||||
| 181 | Search a given file type in all directories of the array. | ||||||
| 182 | |||||||
| 183 | Required parameters: | ||||||
| 184 | |||||||
| 185 | =over 4 | ||||||
| 186 | |||||||
| 187 | =item * $dir | ||||||
| 188 | |||||||
| 189 | This must be a reference to array of directories where we are looking for | ||||||
| 190 | our files. | ||||||
| 191 | |||||||
| 192 | =item * $type | ||||||
| 193 | |||||||
| 194 | This is a string of the file's extension that we are looking for. | ||||||
| 195 | |||||||
| 196 | =back | ||||||
| 197 | |||||||
| 198 | =cut | ||||||
| 199 | |||||||
| 200 | sub find_files { | ||||||
| 201 | 4 | 4 | 1 | 9 | my $self = shift; | ||
| 202 | 4 | 11 | my $dir = shift; | ||||
| 203 | 4 | 8 | my $type = shift; | ||||
| 204 | 4 | 9 | my @files; | ||||
| 205 | find( | ||||||
| 206 | { follow => 1, | ||||||
| 207 | no_chdir => 1, | ||||||
| 208 | wanted => sub { | ||||||
| 209 | 16 | 100 | 16 | 894 | return unless -f; | ||
| 210 | 12 | 50 | 958 | push @files, File::Basename::basename($_) if $_ =~ /\.$type$/; | |||
| 211 | }, | ||||||
| 212 | }, | ||||||
| 213 | 4 | 31 | @{$dir}, | ||||
| 4 | 665 | ||||||
| 214 | ); | ||||||
| 215 | 4 | 32 | return @files; | ||||
| 216 | } | ||||||
| 217 | |||||||
| 218 | =head1 CREDITS | ||||||
| 219 | |||||||
| 220 | L | ||||||
| 221 | a lot of L | ||||||
| 222 | |||||||
| 223 | Thanks even to L | ||||||
| 224 | on his L | ||||||
| 225 | |||||||
| 226 | =head1 LICENSE | ||||||
| 227 | |||||||
| 228 | JQuery is licensed under L | ||||||
| 229 | |||||||
| 230 | This code is licensed under Artistic License version 2.0. | ||||||
| 231 | |||||||
| 232 | =head1 AUTHOR | ||||||
| 233 | |||||||
| 234 | Ferro - C | ||||||
| 235 | |||||||
| 236 | =cut | ||||||
| 237 | |||||||
| 238 | 1; |