lib/Reaction/UI/ViewPort.pm

filter: Statementstmt 79.1% 72 / 91 Branchbran 42.8% 12 / 28 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 80.0% 16 / 20 Podpod 76.9% 10 / 13 totaltotal 70.7% 111 / 157 CC 20 SCAR 13.5
CC
20
Cov
70%
create_tangent
6.0
focus_tangent
6.0
_dump_event
6.0
CRAP 3.9
« lib/Reaction/InterfaceModel/Search/Spec.pm ↑ Summary lib/Reaction/UI/Widget/Field/Mutable/ChooseOne.pm »
1 package Reaction::UI::ViewPort;
2  
3 6 ▶use Reaction::Class;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use Scalar::Util qw/blessed/;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 6 ▶use namespace::clean -except => [ qw(meta) ];
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8  
9 10 ▶sub DEBUG_EVENTS () { $ENV{REACTION_UI_VIEWPORT_DEBUG_EVENTS} }
Subroutine DEBUG_EVENTS: called CC=1 SCAR=0.0
Pod undocumented
10  
11 6 has location => (isa => 'Str', is => 'rw', required => 1);
12 6 has layout => (isa => 'Str', is => 'rw', lazy_build => 1);
13 26 ▶has layout_args => (isa => 'HashRef', is => 'ro', default => sub { {} });
Subroutine __ANON__: called CC=1 SCAR=0.0
14 6 has outer => (isa => 'Reaction::UI::ViewPort', is => 'rw', weak_ref => 1);
15 6 has inner => (isa => 'Reaction::UI::ViewPort', is => 'rw');
16 6 has focus_stack => (
17 isa => 'Reaction::UI::FocusStack', is => 'rw', weak_ref => 1
18 );
19 has _tangent_stacks => (
20 27 ▶ isa => 'HashRef', is => 'ro', default => sub { {} }
Subroutine __ANON__: called CC=1 SCAR=0.0
21 6 );
22 6 has ctx => (isa => 'Catalyst', is => 'ro', weak_ref => 1); #, required => 1);
23  
24 22 ▶sub _build_layout {
Subroutine _build_layout: called CC=1 SCAR=0.0
25 22 '';
26 }
27  
28 0 ▶sub create_tangent {
Subroutine create_tangent: not called CC=2 SCAR=17.9
Pod documented
29 0 my ($self, $name) = @_;
30 0 my $t_map = $self->_tangent_stacks;
31 0 ▶ if (exists $t_map->{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $t_map->{$name}) 0 0 0
32 0 confess "Can't create tangent with already existing name ${name}";
33 }
34 0 my $loc = join('.', $self->location, $name);
35 0 my $tangent = Reaction::UI::FocusStack->new(loc_prefix => $loc);
36 0 $t_map->{$name} = $tangent;
37 0 return $tangent;
38 }
39  
40 0 ▶sub focus_tangent {
Subroutine focus_tangent: not called CC=2 SCAR=17.9
Pod documented
41 0 my ($self, $name) = @_;
42 0 ▶ if (my $tangent = $self->_tangent_stacks->{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $tangent = $self->_tangent_stacks->{$name}) { } 0 0 0
43 0 return $tangent;
44 } else {
45 0 return;
46 }
47 }
48  
49 0 ▶sub focus_tangents {
Subroutine focus_tangents: not called CC=1 SCAR=6.9
Pod documented
50 0 return keys %{shift->_tangent_stacks};
51 }
52  
53 10 ▶sub child_event_sinks {
Subroutine child_event_sinks: called CC=1 SCAR=0.0
Pod undocumented
54 10 my $self = shift;
55 10 return values %{$self->_tangent_stacks};
56 }
57  
58 10 ▶sub apply_events {
Subroutine apply_events: called CC=2 SCAR=7.0
Pod documented
59 10 my ($self, $events) = @_;
60 10 ▶ return unless keys %$events;
Branch: 1 branch
BranchTrueFalseTotal
unless keys %$events 0 10 10
61 10 $self->apply_child_events($events);
62 10 $self->apply_our_events($events);
63 }
64  
65 10 ▶sub apply_child_events {
Subroutine apply_child_events: called CC=5 SCAR=16.8
Pod documented
66 10 my ($self, $events) = @_;
67 10 ▶ return unless keys %$events;
Branch: 1 branch
BranchTrueFalseTotal
unless keys %$events 0 10 10
68 10 foreach my $child ($self->child_event_sinks) {
69 8 ▶ confess blessed($child) ."($child) is not a valid object"
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($child) and $child->can("apply_events") 0 8 8
Condition33%1/3
builtin::blessed($child) and $child->can("apply_events")
!ll&&!rl&&r
008
MC/DC0%0/2
builtin::blessed($child) and $child->can("apply_events")
builtin::blessed($child) $child->can("apply_events")
Truth table33%1/3
A and B
A= builtin::blessed($child)B= $child->can("apply_events")
ABresult
0X0
100
111
70 unless blessed($child) && $child->can('apply_events');
71 8 my $loc = $child->location;
72 9 my %child_events = map { $_ => delete $events->{$_} }
73 8 grep { /^${loc}[-:]/ } keys %$events;
74 8 $child->apply_events(\%child_events);
75 }
76 }
77  
78 10 ▶sub apply_our_events {
Subroutine apply_our_events: called CC=4 SCAR=14.1
Pod documented
79 10 my ($self, $events) = @_;
80 10 my $loc = $self->location;
81 10 my %our_events;
82 10 foreach my $key (keys %$events) {
83 11 ▶ if ($key =~ m/^${loc}:(.*)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key =~ /^$loc:(.*)$/) 11 0 11
84 11 $our_events{$1} = delete $events->{$key};
85 }
86 }
87 10 ▶ $self->handle_events(\%our_events) if keys %our_events;
Branch: 1 branch
BranchTrueFalseTotal
if keys %our_events 10 0 10
88 }
89  
90 10 ▶sub handle_events {
Subroutine handle_events: called CC=5 SCAR=16.1
Pod documented
91 10 my ($self, $events) = @_;
92 10 my $exists = exists $events->{exists};
93 10 ▶ if ($exists) {
Branch: 1 branch
BranchTrueFalseTotal
if ($exists) 2 8 10
94 2 my %force = $self->force_events;
95 2 my @need = grep { !exists $events->{$_} } keys %force;
96 2 @{$events}{@need} = @force{@need};
97 }
98 10 foreach my $event ($self->accept_events) {
99 20 ▶ if (exists $events->{$event}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $events->{$event}) 10 10 20
100 10 ▶ $self->_dump_event($event, $events->{$event}) if DEBUG_EVENTS;
Branch: 1 branch
BranchTrueFalseTotal
if &DEBUG_EVENTS() 0 10 10
101 10 $self->$event($events->{$event});
102 }
103 }
104 }
105  
106 0 ▶sub _dump_event {
Subroutine _dump_event: not called CC=2 SCAR=17.9
107 0 my ( $self, $name, $value ) = @_;
108 0 my $vp_name = join(' at ', $self, $self->location);
109 0 ▶ print STDERR
Branch: 1 branch
BranchTrueFalseTotal
defined $value ? : 0 0 0
110 "Applying Event: $name on $vp_name with value: "
111 . (defined $value ? $value : '<undef>') . "\n";
112 }
113  
114 19 ▶sub accept_events { () }
Subroutine accept_events: called CC=1 SCAR=0.0
Pod documented
115  
116 2 ▶sub force_events { () }
Subroutine force_events: called CC=1 SCAR=0.0
Pod undocumented
117  
118 66 ▶sub event_id_for {
Subroutine event_id_for: called CC=1 SCAR=0.0
Pod documented
119 66 my ($self, $name) = @_;
120 66 return join(':', $self->location, $name);
121 }
122  
123 3 ▶sub sort_by_spec {
Subroutine sort_by_spec: called CC=5 SCAR=17.8
Pod documented
124 3 my ($self, $spec, $items) = @_;
125 3 ▶ return [sort @$items] unless $spec;
Branch: 1 branch
BranchTrueFalseTotal
unless $spec 0 3 3
126  
127 3 my @order;
128 3 ▶ if (ref $spec eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $spec eq 'ARRAY') { } 3 0 3
elsif (not ref $spec) { } 0 0 0
129 3 @order = @$spec;
130 } elsif (not ref $spec) {
131 0 @order = split /\s+/, $spec;
132 }
133  
134 3 my %order_map = map {$_ => 0} @$items;
135 3 for my $order_num (0..$#order) {
136 0 $order_map{ $order[$order_num] } = ($#order - $order_num) + 1;
137 }
138  
139 3 return [sort {$order_map{$b} <=> $order_map{$a}} @$items];
140 }
141  
142 6 __PACKAGE__->meta->make_immutable;
143  
144  
145 6 1;
146  
147  
148 =head1 NAME
149  
150 Reaction::UI::ViewPort - Page layout building block
151  
152 =head1 SYNOPSIS
153  
154 # Create a new ViewPort:
155 # $stack isa Reaction::UI::FocusStack object
156 my $vp = $stack->push_viewport('Reaction::UI::ViewPort', layout => 'xhtml');
157  
158 # Fetch ViewPort higher up the stack (further out)
159 my $outer = $vp->outer();
160  
161 # Fetch ViewPort lower down (further in)
162 my $inner = $vp->inner();
163  
164 # Create a named tangent stack for this ViewPort
165 my $substack = $vp->create_tangent('name');
166  
167 # Retrieve a tangent stack for this ViewPort
168 my $substack = $vp->forcus_tangent('name');
169  
170 # Get the names of all the tangent stacks for this ViewPort
171 my @names = $vp->focus_tangents();
172  
173 # Fetch all the tangent stacks for this ViewPort
174 # This is called by apply_events
175 my $stacks = $vp->child_event_sinks();
176  
177  
178 ### The following methods are all called automatically when using
179 ### Reaction::UI::Controller(s)
180 # Resolve current events with this ViewPort
181 $vp->apply_events($ctx, $param_hash);
182  
183 # Apply current events to all tangent stacks
184 # This is called by apply_events
185 $vp->apply_child_events($ctx, $params_hash);
186  
187 # Apply current events to this ViewPort
188 # This is called by apply_events
189 $vp->apply_our_events($ctx, $params_hash);
190  
191 =head1 DESCRIPTION
192  
193 A ViewPort describes part of a page, it can be a field, a form or
194 an entire page. ViewPorts are created on a
195 L<Reaction::UI::FocusStack>, usually belonging to a controller or
196 another ViewPort. Each ViewPort knows it's own position in the stack
197 it is in, as well as the stack containing it.
198  
199 Each ViewPort has a specific location in the heirarchy of viewports
200 making up a page. The hierarchy is determined as follows: The first
201 ViewPort in a stack is labeled C<0>, the second is C<1> and so on. If
202 a ViewPort is in a named tangent, it's location will contain the name
203 of the tangent in it's location.
204  
205 For example, the first ViewPort in the 'left' tangent of the main
206 ViewPort has location C<0.left.0>.
207  
208 Several ViewPort attributes are set by
209 L<Reaction::UI::FocusStack/push_viewport> when new ViewPorts are
210 created, these are as follows:
211  
212 =over
213  
214 =item Automatic:
215  
216 =over
217  
218 =item outer
219  
220 The outer attribute is set to the previous ViewPort in the stack when
221 creating a ViewPort, if the ViewPort is the first in the stack, it
222 will be undef.
223  
224 =item inner
225  
226 The inner attribute is set to the next ViewPort down in the stack when
227 it is created, if this is the last ViewPort in the stack, it will be
228 undef.
229  
230 =item focus_stack
231  
232 The focus_stack attribute is set to the L<Reaction::UI::FocusStack>
233 object that created the ViewPort.
234  
235 =item ctx
236  
237 The ctx attribute will be passed automatically when using
238 L<Reaction::UI::Controller/push_viewport> to create a ViewPort in the
239 base stack of a controller. When creating tangent stacks, you may have
240 to pass it in yourself.
241  
242 =back
243  
244 =item Optional:
245  
246 =over
247  
248 =item location
249  
250 =item layout
251  
252 The layout attribute can either be specifically passed when calling
253 C<push_viewport>, or it will be determined using the last part of the
254 ViewPorts classname.
255  
256 =item layout_args
257  
258 This read-only hashref attribute will pass all it's keys as variables to the
259 layout at render time. They should be accessible from both the layout templates
260 and the widget, if applicable, through the C<%_> hash.
261  
262 $controller->push_viewport(VPName, layout => 'foo', layout_args => { bar => 'bar'});
263 $_{bar} #in widget
264 [% bar %] in template
265  
266 =item column_order
267  
268 This is generally used by more specialised ViewPorts such as the
269 L<ListView|Reaction::UI::ViewPort::ListView> or
270 L<Action|Reaction::UI::ViewPort::Action>. It can be either a
271 space separated list of column names, or an arrayref of column names.
272  
273 =back
274  
275 =back
276  
277 =head1 METHODS
278  
279 =head2 outer
280  
281 =over
282  
283 =item Arguments: none
284  
285 =back
286  
287 Fetch the ViewPort outside this one in the page hierarchy.
288  
289 =head2 inner
290  
291 =over
292  
293 =item Arguments: none
294  
295 =back
296  
297 Fetch the ViewPort inside this one in the page hierarchy.
298  
299 =head2 create_tangent
300  
301 =over
302  
303 =item Arguments: $tangent_name
304  
305 =back
306  
307 Create a new named L<Reaction::UI::FocusStack> inside this
308 ViewPort. The created FocusStack is returned.
309  
310 =head2 focus_tangent
311  
312 =over
313  
314 =item Arguments: $tangent_name
315  
316 =back
317  
318 Fetch a named FocusStack from this ViewPort.
319  
320 =head2 focus_tangents
321  
322 =over
323  
324 =item Arguments: none
325  
326 =back
327  
328 Returns a list of names of all the known tangents in this ViewPort.
329  
330 =head2 focus_stack
331  
332 Return the L<Reaction::UI::FocusStack> object that this ViewPort is in.
333  
334 =head2 apply_events
335  
336 =over
337  
338 =item Arguments: $ctx, $params_hashref
339  
340 =back
341  
342 This method is called by the FocusStack object to resolve all events
343 for the ViewPort.
344  
345 =head2 apply_child_events
346  
347 =over
348  
349 =item Arguments: $ctx, $params_hashref
350  
351 =back
352  
353 Resolve the given events for all the tangents of this ViewPort. Called
354 by L<apply_events>.
355  
356 =head2 apply_our_events
357  
358 =over
359  
360 =item Arguments: $ctx, $events
361  
362 =back
363  
364 Resolve the given events that match the location of this
365 ViewPort. Called by L<apply_events>.
366  
367 =head2 handle_events
368  
369 =over
370  
371 =item Arguments: $events
372  
373 =back
374  
375 Actually call the event handlers for this ViewPort. Called by
376 L<apply_our_events>. By default this will do nothing, subclass
377 ViewPort and implement L<accept_events>.
378  
379 =head2 accept_events
380  
381 =over
382  
383 =item Arguments: none
384  
385 =back
386  
387 Implement this method in a subclass and return a list of events that
388 your ViewPort is accepting.
389  
390 =head2 event_id_for
391  
392 =over
393  
394 =item Arguments: $name
395  
396 =back
397  
398 Create an id for the given event name and this ViewPort. Generally
399 returns the location and the name, joined with a colon.
400  
401 =head2 sort_by_spec
402  
403 =over
404  
405 =item Arguments: $spec, $items
406  
407 =back
408  
409 Sorts the given list of items such that the ones that also appear in
410 the spec are at the beginning. This is called by
411 L<Reaction::UI::ViewPort::Action> and
412 L<Reaction::UI::ViewPort::ListView>, and gets passed L<column_order>
413 as the spec argument.
414  
415 =head1 AUTHORS
416  
417 See L<Reaction::Class> for authors.
418  
419 =head1 LICENSE
420  
421 See L<Reaction::Class> for the license.
422  
423 =cut
« lib/Reaction/InterfaceModel/Search/Spec.pm ↑ Summary lib/Reaction/UI/Widget/Field/Mutable/ChooseOne.pm »