lib/Reaction/UI/Widget.pm

filter: Statementstmt 89.9% 89 / 99 Branchbran 75.0% 18 / 24 Conditioncond 66.6% 6 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 100% 16 / 16 Podpod 33.3% 1 / 3 totaltotal 82.8% 130 / 157 CC 17 SCAR 15.5
CC
17
Cov
86%
__ANON__
10.4
_render_dispatch_order
5.3
render
3.3
CRAP 4.7
« lib/Reaction/UI/Window.pm ↑ Summary lib/Reaction/UI/View/TT.pm »
1 package Reaction::UI::Widget;
2  
3 4 ▶use Reaction::Class;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use aliased 'Reaction::UI::ViewPort';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use aliased 'Reaction::UI::View';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use aliased 'Reaction::UI::LayoutSet';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 4 ▶use namespace::clean -except => [ qw(meta) ];
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10  
11 555 ▶sub DEBUG_FRAGMENTS () { $ENV{REACTION_UI_WIDGET_DEBUG_FRAGMENTS} }
Subroutine DEBUG_FRAGMENTS: called CC=1 SCAR=0.0
Pod undocumented
12 520 ▶sub DEBUG_LAYOUTS () { $ENV{REACTION_UI_WIDGET_DEBUG_LAYOUTS} }
Subroutine DEBUG_LAYOUTS: called CC=1 SCAR=0.0
Pod undocumented
13  
14 4 has 'view' => (isa => View, is => 'ro', required => 1);
15 4 has 'layout_set' => (isa => LayoutSet, is => 'ro', required => 1);
16 4 has 'fragment_names' => (is => 'ro', lazy_build => 1);
17 4 has 'basic_layout_args' => (is => 'ro', lazy_build => 1);
18 9 ▶sub _build_fragment_names {
Subroutine _build_fragment_names: called CC=1 SCAR=0.0
19 9 my ($self) = shift;
20 return [
21 58 map { /^_fragment_(.*)/; $1; }
22 321 grep { /^_fragment_/ }
23 9 map { $_->{name} }
24 $self->meta->get_all_methods
25 ];
26 };
27 555 ▶sub render {
Subroutine render: called CC=3 SCAR=11.8
Pod documented
28 555 my ($self, $fragment_name, $rctx, $passed_args) = @_;
29 555 ▶ confess "\$passed_args not hashref" unless ref($passed_args) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $passed_args eq "HASH" 0 555 555
30 555 ▶ if (DEBUG_FRAGMENTS) {
Branch: 1 branch
BranchTrueFalseTotal
if (&DEBUG_FRAGMENTS()) 0 555 555
31 0 my $vp = $passed_args->{viewport};
32 0 $self->view->app->log->debug(
33 "Rendering fragment ${fragment_name} for ".ref($self)
34 ." for VP ${vp} at ".$vp->location
35 );
36 }
37 555 my $args = { self => $self, %$passed_args };
38 555 my $new_args = { %$args };
39 555 my $render_tree = $self->_render_dispatch_order(
40 $fragment_name, $args, $new_args
41 );
42 555 $rctx->dispatch($render_tree, $new_args);
43 };
44 555 ▶sub _method_for_fragment_name {
Subroutine _method_for_fragment_name: called CC=1 SCAR=0.0
45 555 my ($self, $fragment_name) = @_;
46 555 return $self->can("_fragment_${fragment_name}");
47 };
48 555 ▶sub _render_dispatch_order {
Subroutine _render_dispatch_order: called CC=5 SCAR=16.7
49 555 my ($self, $fragment_name, $args, $new_args) = @_;
50  
51 555 my @render_stack = (my $render_deep = (my $render_curr = []));
52 555 my @layout_order = $self->layout_set->widget_order_for($fragment_name);
53  
54 555 ▶ if (my $f_meth = $self->_method_for_fragment_name($fragment_name)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $f_meth = $self->_method_for_fragment_name($fragment_name)) 447 108 555
55 447 my @wclass_stack;
56 97 ▶ my $do_render = sub {
Subroutine __ANON__: called CC=9 SCAR=23.4
57 97 my $package = shift;
58 97 ▶ if (@layout_order) {
Branch: 1 branch
BranchTrueFalseTotal
if (@layout_order) 28 69 97
59 28 ▶ while ($package eq $layout_order[0][0]
Branch: 1 branch
BranchTrueFalseTotal
if ($package eq $layout_order[0][0] or $layout_order[0][0]->isa($package)) 31 0 31
Condition66%2/3
$package eq $layout_order[0][0] or $layout_order[0][0]->isa($package)
l!l&&r!l&&!r
18130
MC/DC0%0/2
$package eq $layout_order[0][0] or $layout_order[0][0]->isa($package)
$package eq $layout_order[0][0] $layout_order[0][0]->isa($package)
Truth table66%2/3
A or B
A= $package eq $layout_order[0][0]B= $layout_order[0][0]->isa($package)
ABresult
1X1
011
000
60 || $layout_order[0][0]->isa($package)) {
61 31 my $new_curr = [];
62 31 my @l = @{shift(@layout_order)};
63 31 ▶ if (DEBUG_LAYOUTS) {
Branch: 1 branch
BranchTrueFalseTotal
if (&DEBUG_LAYOUTS()) 0 31 31
64 0 $self->view->app->log->debug(
65 0 "Layout ${fragment_name} in ${\$l[1]->name} from ${\$l[1]->source_file}"
66 );
67 }
68 31 push(@$render_curr, [ -layout, $l[1], $fragment_name, $new_curr ]);
69 31 push(@render_stack, $new_curr);
70 31 push(@wclass_stack, $l[0]);
71 31 $render_deep = $render_curr = $new_curr;
72 31 ▶ last unless @layout_order;
Branch: 1 branch
BranchTrueFalseTotal
unless @layout_order 28 3 31
73 }
74 }
75 97 ▶ if (@wclass_stack) {
Branch: 1 branch
BranchTrueFalseTotal
if (@wclass_stack) 28 69 97
76 28 ▶ while ($package ne $wclass_stack[-1]
Branch: 1 branch
BranchTrueFalseTotal
if ($package ne $wclass_stack[-1] and $package->isa($wclass_stack[-1])) 0 28 28
Condition66%2/3
$package ne $wclass_stack[-1] and $package->isa($wclass_stack[-1])
!ll&&!rl&&r
18100
MC/DC0%0/2
$package ne $wclass_stack[-1] and $package->isa($wclass_stack[-1])
$package ne $wclass_stack[-1] $package->isa($wclass_stack[-1])
Truth table66%2/3
A and B
A= $package ne $wclass_stack[-1]B= $package->isa($wclass_stack[-1])
ABresult
0X0
100
111
77 && $package->isa($wclass_stack[-1])) {
78 0 pop(@wclass_stack);
79 0 $render_curr = pop(@render_stack);
80 }
81 }
82 97 push(@{$render_curr}, [ '-render', @_ ]);
83 447 };
84 447 $self->$f_meth($do_render, $args, $new_args);
85 }
86 # if we had no fragment method or if we still have layouts left
87 555 ▶ if (@layout_order) {
Branch: 1 branch
BranchTrueFalseTotal
if (@layout_order) 441 114 555
88 441 ▶ while (my $l = shift(@layout_order)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $l = shift @layout_order) 489 441 930
89 489 ▶ if (DEBUG_LAYOUTS) {
Branch: 1 branch
BranchTrueFalseTotal
if (&DEBUG_LAYOUTS()) 0 489 489
90 0 $self->view->app->log->debug(
91 0 "Layout ${fragment_name} in ${\$l->[1]->name} from ${\$l->[1]->source_file}"
92 );
93 }
94 489 push(@$render_deep, [
95 -layout => $l->[1], $fragment_name, ($render_deep = [])
96 ]);
97 }
98 }
99  
100 555 return $render_stack[0];
101 };
102 9 ▶sub _build_basic_layout_args {
Subroutine _build_basic_layout_args: called CC=3 SCAR=11.0
103 9 my ($self) = @_;
104 9 my $args;
105 9 foreach my $name (@{$self->fragment_names},
106 9 @{$self->layout_set->layout_names}) {
107 135 ▶ $args->{$name} ||= sub { $self->render($name, @_); };
Subroutine __ANON__: called CC=1 SCAR=0.1
Condition66%2/3
$args->{$name} ||= sub { $self->render($name, @_); }
l!l&&r!l&&!r
211140
MC/DC0%0/2
$args->{$name} ||= sub { $self->render($name, @_); }
$args->{$name} sub { $self->render($name, @_); }
Truth table66%2/3
A ||= B
A= $args->{$name}B= sub { $self->render($name, @_); }
ABresult
1X1
011
000
108 }
109 9 return $args;
110 };
111 19 ▶sub _fragment_viewport {
Subroutine _fragment_viewport: called CC=1 SCAR=0.0
112 19 my ($self, $do_render, $args, $new_args) = @_;
113 19 my $vp = $args->{'_'};
114 19 my ($widget, $merge_args) = $self->view->render_viewport_args($vp);
115 19 $merge_args->{outer} = { %$new_args };
116 19 delete @{$new_args}{keys %$new_args}; # fresh start
117 19 @{$new_args}{keys %$merge_args} = values %$merge_args;
118 19 $do_render->(Widget, $widget, 'widget');
119 };
120 23 ▶sub _fragment_widget {
Subroutine _fragment_widget: called CC=1 SCAR=0.0
121 23 my ($self, $do_render, $args, $new_args) = @_;
122 23 my $merge = $self->basic_layout_args;
123 #warn "Merge: ".join(', ', keys %$merge)." into: ".join(', ', keys %$new_args);
124 23 delete @{$merge}{keys %$new_args}; # nuke 'self' and 'viewport'
125 23 @{$new_args}{keys %$merge} = values %$merge;
126 };
127  
128 4 __PACKAGE__->meta->make_immutable;
129  
130  
131 4 1;
132  
133 =head1 NAME
134  
135 Reaction::UI::Widget - The base widget.
136  
137 =head1 DESCRIPTION
138  
139 This is the base class for all widgets. It provides common functionality and
140 fragments. It is also concerned with the rendering of the fragments.
141  
142 =head1 FRAGMENTS
143  
144 =head2 widget
145  
146 This is the root fragment for every widget.
147  
148 =head2 viewport
149  
150 This fragment is used to render another viewport from inside a fragment. It
151 assumes the viewport is stored in the C<_> argument.
152  
153 =head1 ENVIRONMENT FLAGS
154  
155 =over
156  
157 =item REACTION_UI_WIDGET_DEBUG_FRAGMENTS
158  
159 Log additional debugging output for fragment processing.
160  
161 =item REACTION_UI_WIDGET_DEBUUG_LAYOUTS
162  
163 Log additional debugging output for layout processing.
164  
165 =back
166  
167 =head1 ATTRIBUTES
168  
169 =head2 view
170  
171 The widget's view object. Is required, readonly and must be a L<Reaction::UI::View>.
172  
173 =head2 layout_set
174  
175 The widget's layout set. Is required, readonly and must be a L<Reaction::UI::LayoutSet>.
176  
177 =head2 fragment_names
178  
179 List of names of known fragments for the current widget. Lazily computed from all
180 methods that are named in the pattern C<_fragment_$name>.
181  
182 =head2 basic_layout_args
183  
184 A lazily built hash reference containing the rendered fragments defined in both the widget and
185 the layout set, keyed by the fragments' names.
186  
187 =head
188  
189 =head1 METHODS
190  
191 =head2 render
192  
193 $widget->render('fragment_name', $reaction_ctx, \%passed_args);
194  
195 This method is concerned with rendering a fragment.
196  
197 =head1 SEE ALSO
198  
199 =over 4
200  
201 =item * L<Reaction::Manual::Widgets>
202  
203 =back
204  
205 =head1 AUTHORS
206  
207 See L<Reaction::Class> for authors.
208  
209 =head1 LICENSE
210  
211 See L<Reaction::Class> for the license.
212  
213 =cut
« lib/Reaction/UI/Window.pm ↑ Summary lib/Reaction/UI/View/TT.pm »