blib/lib/Catalyst/View/Component/jQuery.pm

filter: Statementstmt 87.7% 50 / 57 Branchbran 41.6% 5 / 12 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 11 / 11 Podpod 100% 1 / 1 totaltotal 82.7% 67 / 81 CC 8 SCAR 17.3
CC
8
Cov
80%
_build__jquery_obj
11.5
BEGIN
1.0
BEGIN
1.0
CRAP 5.7
↑ Summary
1 package Catalyst::View::Component::jQuery;
2  
3 4 ▶use 5.008_000;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use Carp ('croak');
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 requires qw( render );
10  
11 =begin foolcpants
12  
13 use warnings;
14 use strict;
15  
16 =cut
17  
18 4 ▶use JavaScript::Framework::jQuery;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 4 our $VERSION;
21 4 $VERSION = '0.05';
22  
23 4 has '_jquery_obj' => (
24 is => 'ro',
25 isa => 'JavaScript::Framework::jQuery',
26 lazy_build => 1,
27 );
28  
29 7 ▶sub _build__jquery_obj {
Subroutine _build__jquery_obj: called CC=8 SCAR=24.4
30 7 my ( $self ) = @_;
31  
32 7 my $cfg = $self->config;
33 7 my @try_key = qw(
34 JavaScript::Framework::jQuery
35 Catalyst::View::Component::jQuery
36 );
37 7 my @tried;
38  
39 7 for my $key (@try_key) {
40 7 ▶ if (exists $cfg->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $cfg->{$key}) { } 7 0 7
41 7 $cfg = $cfg->{$key};
42 7 last;
43 }
44 else {
45 0 push @tried, $key;
46 }
47 }
48  
49 7 ▶ unless (defined $cfg) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $cfg) 0 7 7
50 0 local $" = ', ';
51 0 croak "No configuration found in config hash, tried: @tried";
52 }
53  
54 7 my %optional;
55  
56 7 ▶ if (exists $cfg->{xhtml}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $cfg->{"xhtml"}) 0 7 7
57 0 $optional{xhtml} = $cfg->{xhtml};
58 }
59 7 ▶ if (exists $cfg->{plugins}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $cfg->{"plugins"}) 7 0 7
60 7 $optional{plugins} = $cfg->{plugins};
61 }
62  
63 7 my $obj;
64 7 eval {
65 $obj = JavaScript::Framework::jQuery->new(
66 transient_plugins => 1,
67 library => $cfg->{library},
68 7 %optional,
69 );
70 };
71 7 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 7 0 7
72 7 croak "JavaScript::Framework::jQuery constructor failed because $@";
73 }
74 0 ▶ unless ($obj) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($obj) 0 0 0
75 0 croak "JavaScript::Framework::jQuery constructor failed but did not give a reason.";
76 }
77  
78 0 return $obj;
79 }
80  
81 =head1 NAME
82  
83 Catalyst::View::Component::jQuery - Add a JavaScript::Framework::jQuery object to TT Views
84  
85 =head1 VERSION
86  
87 Version 0.01
88  
89 =cut
90  
91 =head1 SYNOPSIS
92  
93 package MyApp::View::TT;
94  
95 use Moose;
96 extends 'Catalyst::View::TT';
97 with 'Catalyst::View::Component::jQuery';
98  
99 In your Controller:
100  
101 $c->view('TT')->jquery->construct_plugin(
102 name => 'Superfish',
103 target_selector => '#navbar',
104 );
105  
106 I<#navbar> is the document id of a UL element containing navigation links.
107  
108 See L<CatalystX::Menu::Suckerfish> for one method for generating such a
109 UL element automatically by decorating C<action> methods with
110 attributes.
111  
112 In your template:
113  
114 [% jquery.script_src_elements %]
115 [% jquery.link_elements %]
116  
117 [% jquery.document_ready %]
118  
119 Will insert something like:
120  
121 <link type="text/css" href="/css/jquery-ui.css" rel="stylesheet" media="all" />
122 <link type="text/css" href="/css/superfish.css" rel="stylesheet" media="all" />
123 <script type="text/javascript" src="/js/jquery.js" />
124 <script type="text/javascript" src="/js/superfish.js" />
125  
126 <script type="text/javascript">
127 <![CDATA[
128 $(document).ready(function (){
129 $("#foobar").superfish();
130 });
131 ]]>
132 </script>
133  
134 =cut
135  
136 =head1 DESCRIPTION
137  
138 This role lazily constructs a L<JavaScript::Framework::jQuery> object and provides an
139 interface to that object to the role consumer (your Catalyst::View::TT View component).
140  
141 To use this role, you must use L<Moose> in your View component:
142  
143 package MyApp::View::TT;
144  
145 use Moose;
146 extends 'Catalyst::View::TT';
147 with 'Catalyst::View::Component::jQuery';
148  
149 Lazy construction means that the JavaScript::Framework::jQuery object is not
150 allocated until the accessor is called. If you don't use the C<jquery> method
151 in your template the object will not be created.
152  
153 =cut
154  
155 =head1 CONFIGURATION
156  
157 The package config hash supplied to your View module should contain a
158 'JavaScript::Framework::jQuery' key with a valid
159 L<JavaScript::Framework::jQuery> configuration hash.
160  
161 If the JavaScript::Framework::jQuery key isn't found, a key named
162 'Catalyst::View::Component::jQuery' is searched for.
163  
164 If neither key is found an exception is raised.
165  
166 If you're using Catalyst::Plugin::ConfigLoader in your application
167 the configuration may be included in your application .conf file,
168 application module (via __PACKAGE__->config(...)) or any other location
169 ConfigLoader searches for configurations.
170  
171 Calling __PACKAGE__->config() in the application module is probably
172 the best alternative. The expression of an array of anonymous hash
173 references has proven difficult in the human-readable config
174 formats.
175  
176 See L<JavaScript::Framework::jQuery> for a description of the data that
177 must be included in the config hash.
178  
179 =head1 METHODS
180  
181 =cut
182  
183 =head2 render( ) [around]
184  
185 A Moose C<around> method modifier wraps the Catalyst::View::TT render method so
186 we can add the C<jquery> method in templates:
187  
188 # in your template
189  
190 [% jquery.script_src_elements %]
191  
192 # will insert your <script src="..." /> markup elements.
193  
194 See L<Moose::Manual::MethodModifiers> for more information.
195  
196 =cut
197  
198 5 ▶around 'render' => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
199 5 my $next = shift;
200 5 my ($self, $c, @args) = @_;
201 # stash method called 4 times
202 5 ▶ $c->stash->{jquery} = sub { $self->_jquery_obj };
Subroutine __ANON__: called CC=1 SCAR=0.0
203 5 $self->$next( $c, @args );
204 4 };
205  
206 4 ▶no Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
207  
208 =head2 jquery( )
209  
210 Adds C<jquery> method to the role-consuming View component:
211  
212 # in your Controller:
213  
214 $c->view('TT')->jquery->construct_plugin(...);
215  
216 =cut
217  
218 3 ▶sub jquery {
Subroutine jquery: called CC=1 SCAR=0.0
Pod documented
219 3 $_[0]->_jquery_obj;
220 }
221  
222 4 1; # End of Catalyst::View::Component::jQuery
223  
224 =pod
225  
226 =head1 AUTHOR
227  
228 David P.C. Wollmann, C<< <converter42 at gmail.com> >>
229  
230 =head1 BUGS
231  
232 Please report any bugs or feature requests to C<bug-catalyst-view-role-jquery at rt.cpan.org>, or through
233 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Catalyst-View-Component-jQuery>. I will be notified, and then you'll
234 automatically be notified of progress on your bug as I make changes.
235  
236 =head1 SUPPORT
237  
238 You can find documentation for this module with the perldoc command.
239  
240 perldoc Catalyst::View::Component::jQuery
241  
242  
243 You can also look for information at:
244  
245 =over 4
246  
247 =item * RT: CPAN's request tracker
248  
249 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Catalyst-View-Component-jQuery>
250  
251 =item * AnnoCPAN: Annotated CPAN documentation
252  
253 L<http://annocpan.org/dist/Catalyst-View-Component-jQuery>
254  
255 =item * CPAN Ratings
256  
257 L<http://cpanratings.perl.org/d/Catalyst-View-Component-jQuery>
258  
259 =item * Search CPAN
260  
261 L<http://search.cpan.org/dist/Catalyst-View-Component-jQuery/>
262  
263 =back
264  
265 =cut
266  
267 =head1 SEE ALSO
268  
269 L<JavaScript::Framework::jQuery>, L<CatalystX::Menu::Suckerfish>, L<Moose>, L<Moose::Role>, L<Catalyst>, L<perl>
270  
271 =head1 COPYRIGHT AND LICENSE
272  
273 Copyright 2009 David P.C. Wollmann, all rights reserved.
274  
275 This program is free software; you can redistribute it and/or modify it
276 under the same terms as Perl itself.
277  
278 =cut
279  
↑ Summary