Jifty/Web/PageRegion.pm

filter: Statementstmt 11.7% 17 / 145 Branchbran 0.0% 0 / 52 Conditioncond 0.0% 0 / 31 MC/DCmcdc 0.0% 0 / 17 Subroutinesub 26.3% 5 / 19 Podpod 100% 14 / 14 totaltotal 12.9% 36 / 278 CC 44 SCAR 41.4
CC
44
Cov
7%
enter
132.0
make_body
132.0
new
56.0
CRAP 62.9
« Jifty/Plugin/ClassLoader.pm ↑ Summary Jifty/I18N.pm »
1 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3
4 package Jifty::Web::PageRegion;
5  
6 =head1 NAME
7  
8 Jifty::Web::PageRegion - Defines a page region
9  
10 =head1 DESCRIPTION
11  
12 Describes a region of the page which contains a Mason fragment which
13 can be updated via AJAX or via query parameters.
14  
15 =cut
16  
17 1 ▶use base qw/Jifty::Object Class::Accessor::Fast/;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 __PACKAGE__->mk_accessors(qw(name force_path force_arguments default_path default_arguments qualified_name parent region_wrapper lazy loading_path class));
19 1 ▶use Jifty::JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 ▶use Encode ();
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 =head2 new PARAMHASH
23  
24 Creates a new page region. The possible arguments in the C<PARAMHASH>
25 are:
26  
27 =over
28  
29 =item name
30  
31 The (unqualified) name of the region. This is used to generate a
32 unique id -- it should consist of only letters and numbers.
33  
34 =item path
35  
36 The path to the fragment that this page region contains. Defaults to
37 C</__jifty/empty>, which, as its name implies, is empty.
38  
39 =item arguments (optional) (formerly 'defaults')
40  
41 Specifies an optional set of parameter defaults. These should all be
42 simple scalars, as they might be passed across HTTP if AJAX is used.
43  
44 See L<Jifty::Web::Form::Element> for a list of the supported parameters.
45  
46 =item force_arguments (optional)
47  
48 Specifies an optional set of parameter values. They will override anything
49 sent by the user or set via AJAX.
50  
51 =item force_path (optional)
52  
53 A fixed path to the fragment that this page region contains. Overrides anything set by the user.
54  
55 =item parent (optional)
56  
57 The parent L<Jifty::Web::PageRegion> that this region is enclosed in.
58  
59 =item region_wrapper (optional)
60  
61 A boolean; whether or not the region, when rendered, will include the
62 HTML region preamble that makes Javascript aware of its presence.
63 Defaults to true.
64  
65 =item lazy (optional)
66  
67 Delays the loading of the fragment until client render-time.
68 Obviously, does not work with downlevel browsers which don't support
69 javascript.
70  
71 =item loading_path (optional)
72  
73 The fragment to display while the client fetches the actual region.
74 Make this lightweight, or you'll be losing most of the benefits of
75 lazy loading!
76  
77 =back
78  
79 =cut
80  
81 0 ▶sub new {
Subroutine new: not called CC=7 SCAR=40.3
Pod documented
82 0 my $class = shift;
83 0 my $self = bless {}, $class;
84  
85 0 my %args = (
86 name => undef,
87 path => "/__jifty/empty",
88 defaults => {},
89 parent => undef,
90 force_arguments => {},
91 force_path => undef,
92 region_wrapper => 1,
93 lazy => 0,
94 loading_path => undef,
95 @_
96 );
97  
98  
99 0 ▶ $args{'arguments'} ||= delete $args{'defaults'};
Condition0%0/3
$args{"arguments"} ||= delete $args{"defaults"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$args{"arguments"} ||= delete $args{"defaults"}
$args{"arguments"} delete $args{"defaults"}
Truth table0%0/3
A ||= B
A= $args{"arguments"}B= delete $args{"defaults"}
ABresult
1X1
011
000
100  
101 # Name is required
102 0 ▶ if (not defined $args{name}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $args{"name"}) 0 0 0
103 0 warn "Name is required for page regions.";
104 0 return;
105 }
106  
107 # References don't go over HTTP very well
108 0 ▶ if (grep {ref $_} values %{$args{arguments}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {ref $_;} values %{$args{"arguments"};}) 0 0 0
109 warn "Reference '$args{arguments}{$_}' passed as default for '$_' to region '$args{name}'"
110 0 for grep {ref $args{arguments}{$_}} keys %{$args{arguments}};
111 0 return;
112 }
113  
114 0 $self->name($args{name});
115 0 $self->qualified_name(Jifty->web->qualified_region($self));
116 0 $self->default_path($args{path});
117 0 $self->default_arguments($args{arguments});
118 0 $self->force_arguments($args{force_arguments});
119 0 $self->force_path($args{force_path});
120 0 $self->arguments({});
121 0 ▶ $self->parent($args{parent} || Jifty->web->current_region);
Condition0%0/3
$args{'parent'} || 'Jifty'->web->current_region
l!l&&r!l&&!r
000
MC/DC0%0/2
$args{'parent'} || 'Jifty'->web->current_region
$args{'parent'} 'Jifty'->web->current_region
Truth table0%0/3
A || B
A= $args{'parent'}B= 'Jifty'->web->current_region
ABresult
1X1
011
000
122 0 $self->region_wrapper($args{region_wrapper});
123 0 $self->lazy($args{lazy});
124 0 $self->loading_path($args{loading_path});
125 0 $self->class($args{class});
126  
127 # Keep track of the fully qualified name (which should be unique)
128 0 ▶ $self->log->warn("Repeated region: " . $self->qualified_name)
Branch: 1 branch
BranchTrueFalseTotal
if "Jifty"->web->get_region($self->qualified_name) 0 0 0
129 if Jifty->web->get_region( $self->qualified_name );
130 0 Jifty->web->{'regions'}{ $self->qualified_name } = $self;
131  
132 0 return $self;
133 }
134  
135 =head2 name [NAME]
136  
137 Gets or sets the name of the page region.
138  
139 =cut
140  
141 =head2 qualified_name [NAME]
142  
143 Gets or sets the fully qualified name of the page region. This should
144 be unique on a page. This is usually set by L</enter>, based on the
145 page regions that this region is inside. See
146 L<Jifty::Web/qualified_region>.
147  
148 =cut
149  
150 =head2 default_path [PATH]
151  
152 Gets or sets the default path of the fragment. This is overridden by
153 L</path>.
154  
155 =cut
156  
157 =head2 path [PATH]
158  
159 Gets or sets the path that the fragment actually contains. This
160 overrides L</default_path>.
161  
162 =cut
163  
164 0 ▶sub path {
Subroutine path: not called CC=3 SCAR=24.8
Pod documented
165 0 my $self = shift;
166 0 ▶ $self->{path} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
167 0 ▶ return $self->{path} || $self->default_path;
Condition0%0/3
$self->{'path'} || $self->default_path
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{'path'} || $self->default_path
$self->{'path'} $self->default_path
Truth table0%0/3
A || B
A= $self->{'path'}B= $self->default_path
ABresult
1X1
011
000
168 }
169  
170 =head2 default_argument NAME [VALUE]
171  
172 Gets or sets the default value of the C<NAME> argument. This is used
173 as a fallback, and also to allow generated links to minimize the
174 amount of state they must transmit.
175  
176 =cut
177  
178 0 ▶sub default_argument {
Subroutine default_argument: not called CC=3 SCAR=24.8
Pod documented
179 0 my $self = shift;
180 0 my $name = shift;
181 0 ▶ $self->{default_arguments}{$name} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
182 0 ▶ return $self->{default_arguments}{$name} || '';
Condition0%0/2
$self->{'default_arguments'}{$name} || ''
l!l
00
MC/DC0%0/1
$self->{'default_arguments'}{$name} || ''
$self->{'default_arguments'}{$name}
Truth table0%0/2
A
A= $self->{'default_arguments'}{$name}
Aresult
11
00
183 }
184  
185 =head2 argument NAME [VALUE]
186  
187 Gets or sets the actual run-time value of the page region. This
188 usually comes from HTTP parameters. It overrides the
189 L</default_argument> of the same C<NAME>.
190  
191 =cut
192  
193 0 ▶sub argument {
Subroutine argument: not called CC=4 SCAR=30.0
Pod documented
194 0 my $self = shift;
195 0 my $name = shift;
196 0 ▶ $self->{arguments}{$name} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
197 0 ▶ return $self->force_arguments->{$name}||$self->{arguments}{$name} || $self->default_argument($name);
Condition0%0/6
$self->force_arguments->{$name} || $self->{'arguments'}{$name} || $self->default_argument($name)
l!l&&r!l&&!r
000
$self->force_arguments->{$name} or $self->{"arguments"}{$name}
l!l&&r!l&&!r
000
MC/DC0%0/3
$self->force_arguments->{$name} || $self->{'arguments'}{$name} || $self->default_argument($name)
$self->force_arguments->{$name} $self->{"arguments"}{$name} $self->default_argument($name)
Truth table0%0/4
A or B || C
A= $self->force_arguments->{$name}B= $self->{"arguments"}{$name}C= $self->default_argument($name)
ABCresult
1XX1
01X1
0011
0000
198 }
199  
200 =head2 arguments [HASHREF]
201  
202 Sets all arguments at once, or returns all arguments. The latter will
203 also include all default arguments.
204  
205 =cut
206  
207 0 ▶sub arguments {
Subroutine arguments: not called CC=2 SCAR=17.9
Pod documented
208 0 my $self = shift;
209 0 ▶ $self->{arguments} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
210 0 return { %{$self->{default_arguments}}, %{$self->{arguments}}, %{$self->force_arguments}};
211 }
212  
213 =head2 enter
214  
215 Enters the region; this sets the qualified name based on
216 L<Jifty::Web/qualified_region>, and uses that to pull runtime values
217 for the L</path> and L</argument>s from the
218 L<Jifty::Request/state_variables> before overriding them with the "force" versions.
219  
220 =cut
221  
222 0 ▶sub enter {
Subroutine enter: not called CC=11 SCAR=48.8
Pod documented
223 0 my $self = shift;
224  
225 # Add ourselves to the region stack
226 0 push @{Jifty->web->{'region_stack'}}, $self;
227  
228 # Merge in the settings passed in via state variables
229 0 for my $var (Jifty->web->request->state_variables) {
230 0 my $key = $var->key;
231 0 ▶ my $value = $var->value || '';
Condition0%0/2
$var->value || ''
l!l
00
MC/DC0%0/1
$var->value || ''
$var->value
Truth table0%0/2
A
A= $var->value
Aresult
11
00
232  
233 0 ▶ if ($key =~ /^region-(.*?)\.(.*)/ and $1 eq $self->qualified_name and $value ne $self->default_argument($2)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key =~ /^region-(.*?)\.(.*)/ and $1 eq $self->qualified_name and $value ne $self->default_argument($2)) 0 0 0
Condition0%0/6
$key =~ /^region-(.*?)\.(.*)/ and $1 eq $self->qualified_name and $value ne $self->default_argument($2)
!ll&&!rl&&r
000
$key =~ /^region-(.*?)\.(.*)/ and $1 eq $self->qualified_name
!ll&&!rl&&r
000
MC/DC0%0/3
$key =~ /^region-(.*?)\.(.*)/ and $1 eq $self->qualified_name and $value ne $self->default_argument($2)
$key =~ /^region-(.*?)\.(.*)/ $1 eq $self->qualified_name $value ne $self->default_argument($2)
Truth table0%0/4
A and B and C
A= $key =~ /^region-(.*?)\.(.*)/B= $1 eq $self->qualified_nameC= $value ne $self->default_argument($2)
ABCresult
0XX0
10X0
1100
1111
234 0 $self->argument($2 => $value);
235 }
236 0 ▶ if ($key =~ /^region-(.*)$/ and $1 eq $self->qualified_name and $value ne $self->default_path) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key =~ /^region-(.*)$/ and $1 eq $self->qualified_name and $value ne $self->default_path) 0 0 0
Condition0%0/6
$key =~ /^region-(.*)$/ and $1 eq $self->qualified_name and $value ne $self->default_path
!ll&&!rl&&r
000
$key =~ /^region-(.*)$/ and $1 eq $self->qualified_name
!ll&&!rl&&r
000
MC/DC0%0/3
$key =~ /^region-(.*)$/ and $1 eq $self->qualified_name and $value ne $self->default_path
$key =~ /^region-(.*)$/ $1 eq $self->qualified_name $value ne $self->default_path
Truth table0%0/4
A and B and C
A= $key =~ /^region-(.*)$/B= $1 eq $self->qualified_nameC= $value ne $self->default_path
ABCresult
0XX0
10X0
1100
1111
237 0 $self->path(URI::Escape::uri_unescape($value));
238 }
239  
240 # We should always inherit the state variables from the uplevel request.
241 0 Jifty->web->set_variable($key => $value);
242 }
243  
244 0 for my $argument (keys %{$self->force_arguments}) {
245 0 $self->argument($argument => $self->force_arguments->{$argument});
246 }
247  
248 0 ▶ $self->path($self->force_path) if ($self->force_path);
Branch: 1 branch
BranchTrueFalseTotal
if $self->force_path 0 0 0
249 }
250  
251 =head2 exit
252  
253 Exits the page region, if it is the most recent one. Normally, you
254 won't need to call this by hand; however, if you are calling L</enter>
255 by hand, you will need to call the corresponding C<exit>.
256  
257 =cut
258  
259 0 ▶sub exit {
Subroutine exit: not called CC=2 SCAR=17.9
Pod documented
260 0 my $self = shift;
261  
262 0 ▶ if (Jifty->web->current_region != $self) {
Branch: 1 branch
BranchTrueFalseTotal
if ('Jifty'->web->current_region != $self) { } 0 0 0
263 0 $self->log->warn("Attempted to exit page region ".$self->qualified_name." when it wasn't the most recent");
264 } else {
265 0 pop @{Jifty->web->{'region_stack'}};
266 }
267 }
268  
269 =head2 as_string
270  
271 Deals with the bulk of the effort to show a page region. Returns a
272 string of the fragment and associated javascript (if any).
273  
274 =cut
275  
276 0 ▶sub as_string {
Subroutine as_string: not called CC=1 SCAR=6.9
Pod documented
277 0 my $self = shift;
278 0 Jifty->handler->buffer->push(private => 1, from => "PageRegion render of ".$self->path);
279 0 $self->make_body;
280 0 return Jifty->handler->buffer->pop;
281 }
282  
283 =head2 render
284  
285 Calls L</enter>, outputs the results of L</as_string>, and then calls
286 L</exit>. Returns an empty string.
287  
288 =cut
289  
290 0 ▶sub render {
Subroutine render: not called CC=1 SCAR=6.9
Pod documented
291 0 my $self = shift;
292  
293 0 $self->enter;
294 0 $self->make_body;
295 0 $self->exit;
296  
297 0 return '';
298 }
299  
300 =head2 make_body
301  
302 Outputs the results of the region to the current buffer.
303  
304 =cut
305  
306 0 ▶sub make_body {
Subroutine make_body: not called CC=11 SCAR=48.8
Pod documented
307 0 my $self = shift;
308 0 my $buffer = Jifty->handler->buffer;
309  
310 0 my %arguments = %{ $self->arguments };
311  
312 # undef arguments cause warnings. We hatesses the warnings, we do.
313 0 ▶ defined $arguments{$_} or delete $arguments{$_} for keys %arguments;
Branch: 1 branch
BranchTrueFalseTotal
defined $arguments{$_} or delete $arguments{$_} 0 0 0
314  
315 # We need to tell the browser this is a region and what its
316 # default arguments are as well as the path of the "fragment". We
317 # do this by passing in a snippet of javascript which encodes this
318 # information. We only render this region wrapper if we're asked
319 # to (which is true by default)
320 0 ▶ if ( $self->region_wrapper ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->region_wrapper) 0 0 0
321 0 ▶ $buffer->append(qq|<script type="text/javascript">\n|
Branch: 2 branches
BranchTrueFalseTotal
$self->parent ? : 0 0 0
'Jifty'->web->form->is_open ? : 0 0 0
322 . qq|new Region('| . $self->qualified_name . qq|',|
323 . Jifty::JSON::encode_json( \%arguments ) . qq|,|
324 . qq|'| . $self->path . qq|',|
325 . ( $self->parent ? qq|'| . $self->parent->qualified_name . qq|'| : q|null|)
326 . qq|,| . (Jifty->web->form->is_open ? '1' : 'null')
327 . qq|);\n|
328 . qq|</script>|);
329 0 ▶ if ($self->lazy) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->lazy) 0 0 0
330 0 $buffer->append(qq|<script type="text/javascript">|
331 . qq|jQuery(function(){ Jifty.update( { 'fragments': [{'region': '|.$self->qualified_name.qq|', 'mode': 'Replace'}], 'actions': {}}, document.getElementById('region-|.$self->qualified_name.qq|'))})|
332 . qq|</script>|);
333 }
334  
335 0 my $class = 'jifty-region';
336 0 ▶ $class .= ' ' . $self->class if $self->class;
Branch: 1 branch
BranchTrueFalseTotal
if $self->class 0 0 0
337 0 $buffer->append(qq|<div id="region-| . $self->qualified_name . qq|" class="| . $class . qq|">|);
338  
339 0 ▶ if ($self->lazy) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->lazy) 0 0 0
340 0 ▶ if ($self->loading_path) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->loading_path) 0 0 0
341 0 local $self->{path} = $self->loading_path;
342 0 $self->render_as_subrequest(\%arguments);
343 }
344 0 $buffer->append(qq|</div>|);
345 0 return;
346 }
347 }
348  
349 0 $self->render_as_subrequest(\%arguments);
350 0 ▶ $buffer->append(qq|</div>|) if ( $self->region_wrapper );
Branch: 1 branch
BranchTrueFalseTotal
if $self->region_wrapper 0 0 0
351 }
352  
353 =head2 render_as_subrequest
354  
355 =cut
356  
357 0 ▶sub render_as_subrequest {
Subroutine render_as_subrequest: not called CC=7 SCAR=40.3
Pod documented
358 0 my ($self, $arguments, $enable_actions) = @_;
359  
360 # Make a fake request and throw it at the dispatcher
361 0 my $subrequest = Jifty->web->request->clone;
362 0 $subrequest->argument( region => $self );
363 # XXX: use ->arguments?
364 0 $subrequest->argument( $_ => $arguments->{$_}) for keys %$arguments;
365 0 $subrequest->template_arguments({});
366 0 $subrequest->path( $self->path );
367 0 $subrequest->top_request( Jifty->web->request->top_request );
368  
369 0 my %args;
370 0 ▶ if ($self->path =~ m/\?/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->path =~ /\?/) 0 0 0
371 # XXX: this only happens if we are redirect within region AND
372 # with continuation, which is already taken care of by the
373 # clone.
374 0 my ($path, $arg) = split(/\?/, $self->path, 2);
375 0 $subrequest->path( $path );
376 0 %args = (map { split /=/, $_ } split /&/, $arg);
377 0 ▶ if ($args{'J:C'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"J:C"}) 0 0 0
378 0 $subrequest->continuation($args{'J:C'});
379 }
380 }
381 # Remove all of the actions
382 0 ▶ unless ($enable_actions) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($enable_actions) 0 0 0
383 0 $_->active(0) for ($subrequest->actions);
384 }
385 # $subrequest->clear_actions;
386 0 local Jifty->web->{request} = $subrequest;
387 0 ▶ if ($args{'J:RETURN'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"J:RETURN"}) 0 0 0
388 0 my $top = Jifty->web->request->top_request;
389 0 my $cont = Jifty->web->session->get_continuation($args{'J:RETURN'});
390 0 $cont->return;
391 # need to set this as subrequest again as it's clobbered by the return
392 0 Jifty->web->request->top_request($top);
393 }
394  
395 # Call into the dispatcher
396 0 Jifty->handler->dispatcher->handle_request;
397  
398 0 return;
399 }
400  
401 =head2 get_element [RULES]
402  
403 Returns a CSS2 selector which selects only elements under this region
404 which fit the C<RULES>. This method is used by AJAX code to specify
405 where to add new regions.
406  
407 =cut
408  
409 0 ▶sub get_element {
Subroutine get_element: not called CC=1 SCAR=6.9
Pod documented
410 0 my $self = shift;
411 0 return "#region-" . $self->qualified_name . ' ' . join(' ', @_);
412 }
413  
414 =head2 client_cacheable
415  
416 Returns the client cacheable state of the regions path. Returns false if the template has not been marked as client cacheable. Otherwise it returns the string "static" or "action" based on the cacheable attribute set on the template.
417  
418 =cut
419  
420 0 ▶sub client_cacheable {
Subroutine client_cacheable: not called CC=2 SCAR=17.9
Pod documented
421 0 my $self = shift;
422 0 ▶ my ($jspr) = Jifty->find_plugin('Jifty::Plugin::JSPageRegion') or return;
Branch: 1 branch
BranchTrueFalseTotal
my($jspr) = "Jifty"->find_plugin("Jifty::Plugin::JSPageRegion") or return 0 0 0
423  
424 0 return $jspr->client_cacheable($self->path);
425 }
426  
427 =head2 client_cache_content
428  
429 Returns the template as JavaScript code.
430  
431 =cut
432  
433 0 ▶sub client_cache_content {
Subroutine client_cache_content: not called CC=2 SCAR=17.9
Pod documented
434 0 my $self = shift;
435 0 ▶ my ($jspr) = Jifty->find_plugin('Jifty::Plugin::JSPageRegion') or return;
Branch: 1 branch
BranchTrueFalseTotal
my($jspr) = "Jifty"->find_plugin("Jifty::Plugin::JSPageRegion") or return 0 0 0
436  
437 0 return $jspr->compile_to_js($self->path);
438 }
439  
440 1 1;
« Jifty/Plugin/ClassLoader.pm ↑ Summary Jifty/I18N.pm »