File Coverage

blib/lib/RapidApp/Role/AssetControllers.pm
Criterion Covered Total %
statement 37 37 100.0
branch n/a
condition n/a
subroutine 13 13 100.0
pod 0 1 0.0
total 50 51 98.0


line stmt bran cond sub pod time code
1             package RapidApp::Role::AssetControllers;
2              
3 4     4   2360 use strict;
  4         8  
  4         125  
4 4     4   19 use warnings;
  4         8  
  4         159  
5              
6             # This class must declare the version because we declared it before (and PAUSE knows)
7             our $VERSION = '0.99301';
8              
9 4     4   20 use Moose::Role;
  4         7  
  4         35  
10 4     4   19501 use namespace::autoclean;
  4         8  
  4         42  
11              
12             # This Role handles setting up AutoAssets controllers needed for the proper functioning
13             # of the RapidApp system
14              
15 4     4   353 use RapidApp::Util qw(:all);
  4         8  
  4         2055  
16              
17 4     4   28 use CatalystX::InjectComponent;
  4         20  
  4         87  
18 4     4   21 use Catalyst::Utils;
  4         6  
  4         136  
19 4     4   23 use Path::Class qw(dir);
  4         6  
  4         174  
20 4     4   21 use RapidApp;
  4         18  
  4         102  
21 4     4   1712 use Alien::Web::ExtJS::V3;
  4         3321  
  4         135  
22 4     4   28 use Time::HiRes qw(gettimeofday tv_interval);
  4         8  
  4         34  
23              
24 4     4   2601 use Catalyst::Controller::AutoAssets 0.40;
  4         143328  
  4         2824  
25             with 'Catalyst::Plugin::AutoAssets';
26              
27 4     4 0 60 sub get_extjs_dir { Alien::Web::ExtJS::V3->dir->stringify }
28              
29              
30             around 'inject_asset_controllers' => sub {
31             my ($orig,$c,@args) = @_;
32            
33             my $start = [gettimeofday];
34             $c->log->debug("RapidApp - Injecting Asset Controllers...") if ($c->debug);
35            
36             my %defaults = (
37             sha1_string_length => 15,
38             use_etags => 1,
39             );
40            
41             my $share_dir = RapidApp->share_dir;
42            
43             my $assets = [
44             {
45             controller => 'Assets::ExtJS',
46             type => 'Directory',
47             include => $c->get_extjs_dir,
48             html_head_css_subfiles => [qw(
49             resources/css/ext-all.css
50             resources/css/xtheme-gray.css
51             examples/ux/fileuploadfield/css/fileuploadfield.css
52             )],
53             html_head_js_subfiles => [qw(
54             adapter/ext/ext-base.js
55             ext-all-debug.js
56             src/debug.js
57             examples/ux/fileuploadfield/FileUploadField.js
58             )],
59             persist_state => 1,
60             },
61             {
62             controller => 'Assets::RapidApp::CSS',
63             type => 'CSS',
64             include => $share_dir . '/assets/css',
65             },
66             {
67             controller => 'Assets::RapidApp::CSS::ScopedReset',
68             type => 'CSS',
69             include => $share_dir . '/assets/css-scoped/ra-scoped-reset.css',
70             scopify => ['.ra-scoped-reset', merge => ['html','body']]
71             },
72             {
73             controller => 'Assets::RapidApp::CSS::ScopedDoc',
74             type => 'CSS',
75             include => $share_dir . '/assets/css-scoped/ra-doc.css',
76             scopify => ['.ra-doc', merge => ['html','body']]
77             },
78             {
79             controller => 'Assets::RapidApp::JS',
80             type => 'JS',
81             include => $share_dir . '/assets/js',
82             },
83             {
84             controller => 'Assets::RapidApp::Icons',
85             type => 'IconSet',
86             include => $share_dir . '/assets/icons',
87             css_file_name => 'ra-icons.css',
88             icon_name_prefix => 'ra-icon-'
89             },
90             {
91             controller => 'Assets::RapidApp::Filelink',
92             type => 'Directory',
93             include => $share_dir . '/assets/filelink',
94             html_head_css_subfiles => ['filelink.css']
95             },
96             {
97             controller => 'Assets::RapidApp::FontAwesome',
98             type => 'Directory',
99             include => $share_dir . '/assets/font-awesome',
100             html_head_css_subfiles => ['css/font-awesome.min.css']
101             },
102             {
103             controller => 'Assets::RapidApp::Misc',
104             type => 'Directory',
105             include => $share_dir . '/assets/misc',
106             allow_static_requests => 1,
107             use_etags => 1,
108             static_response_headers => {
109             'Cache-Control' => 'max-age=3600, must-revalidate, public'
110             }
111             },
112             ];
113            
114             ## -----------
115             # Easy automatic setup of local assets
116            
117             # Default to true if not set(i.e. can be set to 0/false to disable)
118             my $auto_setup = (
119             ! exists $c->config->{'Model::RapidApp'}->{auto_local_assets} ||
120             $c->config->{'Model::RapidApp'}->{auto_local_assets}
121             ) ? 1 : 0;
122            
123             my $home = dir( Catalyst::Utils::home($c) );
124             my $cfged_dir = $c->config->{'Model::RapidApp'}->{local_assets_dir};
125            
126             # We can't setup auto local assets if we have no home dir and not manually cfged:
127             $auto_setup = 0 unless ($cfged_dir || ($home && -d $home));
128            
129             if($auto_setup) {
130            
131             # New, automatic 'local_asset_dir' can now be specified via config:
132             my $dir = dir( $cfged_dir || 'root/assets' );
133            
134             # If relative, make relative to app home (unless manually cfged and already exists as-is):
135             unless($cfged_dir && -d $dir) {
136             $dir = $home->subdir($dir) if ($home && $dir->is_relative);
137             }
138            
139             # Add local assets if asset include dirs exist in the App directory
140             push @$assets, {
141             controller => 'Assets::Local::CSS',
142             type => 'CSS',
143             include => "$dir/css",
144             } if (-d $dir->subdir('css'));
145            
146             push @$assets, {
147             controller => 'Assets::Local::JS',
148             type => 'JS',
149             include => "$dir/js",
150             } if (-d $dir->subdir('js'));
151            
152             push @$assets, {
153             controller => 'Assets::Local::Icons',
154             type => 'IconSet',
155             include => "$dir/icons",
156             } if (-d $dir->subdir('icons'));
157            
158             push @$assets, {
159             controller => 'Assets::Local::Misc',
160             type => 'Directory',
161             include => "$dir/misc",
162             allow_static_requests => 1,
163             } if (-d $dir->subdir('misc'));
164             }
165             #
166             ## -----------
167            
168             # Check for any configs in the existing local app config:
169             my $existing = $c->config->{'Plugin::AutoAssets'}->{assets};
170             push @$assets, @$existing if ($existing);
171            
172             # apply defaults:
173             %$_ = (%defaults,%$_) for (@$assets);
174            
175             $c->config( 'Plugin::AutoAssets' => { assets => $assets } );
176            
177             my $ret = $c->$orig(@args);
178            
179             # -----------
180             # New: Setup a JS global to contain the list of all auto-generated iconcls values
181             my @icon_names = sort { $a cmp $b } uniq(
182             map { $_->{icon_name} }
183             map { values %{ $c->controller($_)->manifest } }
184             map { $_->{controller} } grep { $_->{type} eq 'IconSet' } @$assets
185             );
186            
187             my $JsCode = join("\n",'',
188             'Ext.ns("Ext.ux.RapidApp");',
189             'Ext.ux.RapidApp.AllIconAssetClsNames = [',
190             join(",\n", map { " '$_'" } @icon_names),
191             '];'
192             );
193            
194             $c->_inject_single_asset_controller({
195             controller => 'Assets::RapidApp::GenJS',
196             type => 'JS',
197             include => \$JsCode,
198             });
199            
200             # tweak the order so the icon list is available to JS early
201             @{$c->asset_controllers} = uniq(
202             $c->asset_controllers->[0], # Keep the ExtJS assets first
203             'Assets::RapidApp::GenJS', # Make us second
204             @{$c->asset_controllers} # Then the rest of the assets
205             );
206             # -----------
207              
208            
209             $c->log->debug(sprintf(
210             "RapidApp - Asset Controllers Setup in %0.3f seconds",
211             tv_interval($start)
212             )) if ($c->debug);
213              
214             return $ret;
215             };
216              
217             1;