View/DHTML.pm

filter: Statementstmt 99.3% 150 / 151 Branchbran 97.9% 47 / 48 Conditioncond 100% 6 / 6 MC/DCmcdc 100% 4 / 4 Subroutinesub 100% 25 / 25 Podpod 100% 4 / 4 totaltotal 99.1% 236 / 238 CC 27 SCAR 11.4
CC
27
Cov
99%
_buildListItemFunction
6.0
__ANON__
5.0
__ANON__
5.0
CRAP 3.1
« View/HTML.pm ↑ Summary View.pm »
1  
2 package Tree::Simple::View::DHTML;
3  
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 our $VERSION = '0.20';
8  
9 1 ▶use parent 'Tree::Simple::View::HTML';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 ▶use Tree::Simple::View::Exceptions;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 1 ▶use constant OPEN_TAG => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use constant CLOSE_TAG => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use constant EXPANDED => 3;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 ## private methods
18  
19 17 ▶sub _init {
Subroutine _init: called CC=1 SCAR=0.0
20 17 my ($self, @args) = @_;
21 17 $self->SUPER::_init(@args);
22 17 $self->{list_counter_id} = 0;
23 17 ($self->{obj_id}) = ("$self" =~ /\((.*?)\)$/);
24 }
25  
26 85 ▶sub _createUID {
Subroutine _createUID: called CC=2 SCAR=6.9
27 85 my ($self, $tree) = @_;
28 85 ▶ return $tree->getUID() if $self->{config}->{use_tree_uids};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"config"}{"use_tree_uids"} 1 84 85
29 84 $self->{list_counter_id}++;
30 84 return join "_" => ($self->{obj_id}, $self->{list_counter_id});
31 }
32  
33 ## public methods
34  
35 3 ▶sub expandPathSimple {
Subroutine expandPathSimple: called CC=3 SCAR=11.0
Pod documented
36 3 my ($self, $tree, $current_path, @path) = @_;
37 3 my @results = ("<UL>");
38 3 my $root_depth = $tree->getDepth() + 1;
39 3 my $last_depth = -1;
40 46 ▶ my $traversal_sub = sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
41 46 my ($t) = @_;
42 46 my $display_style = "none";
43 46 ▶ if (defined $current_path && $self->_compareNodeToPath($current_path, $t)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $current_path and $self->_compareNodeToPath($current_path, $t)) 5 41 46
Condition100%3/3
defined $current_path and $self->_compareNodeToPath($current_path, $t)
!ll&&!rl&&r
3925
MC/DC100%2/2
defined $current_path and $self->_compareNodeToPath($current_path, $t)
defined $current_path $self->_compareNodeToPath($current_path, $t)
Truth table100%3/3
A and B
A= defined $current_pathB= $self->_compareNodeToPath($current_path, $t)
ABresult
0X0
100
111
44 5 $display_style = "block";
45 5 $current_path = shift @path;
46 }
47 46 my $current_depth = $t->getDepth();
48 46 ▶ push @results => ("</UL>" x ($last_depth - $current_depth)) if ($last_depth > $current_depth);
Branch: 1 branch
BranchTrueFalseTotal
if $last_depth > $current_depth 12 34 46
49 46 ▶ unless ($t->isLeaf()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($t->isLeaf) { } 30 16 46
50 16 my $uid = $self->_createUID($t);
51 16 push @results => ("<LI><A HREF='javascript:void(0);' onClick='toggleList(\"$uid\")'>" .
52 $t->getNodeValue() . "</A></LI>");
53 16 push @results => "<UL ID='$uid' STYLE='display: $display_style;'>";
54 }
55 else {
56 30 push @results => ("<LI>" . $t->getNodeValue() . "</LI>");
57 }
58 46 $last_depth = $current_depth;
59 3 };
60 3 ▶ $traversal_sub->($self->{tree}) if $self->{include_trunk};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"include_trunk"} 1 2 3
61 3 $self->{tree}->traverse($traversal_sub);
62 3 $last_depth -= $root_depth;
63 3 ▶ $last_depth++ if $self->{include_trunk};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"include_trunk"} 1 2 3
64 3 push @results => ("</UL>" x ($last_depth + 1));
65  
66 3 return (join "\n" => @results);
67 }
68  
69 5 ▶sub expandPathComplex {
Subroutine expandPathComplex: called CC=3 SCAR=11.0
Pod documented
70 5 my ($self, $tree, $config, $current_path, @path) = @_;
71  
72 5 my ($list_func, $list_item_func) = $self->_processConfig($config);
73  
74 5 my @results = $list_func->(OPEN_TAG);
75 5 my $root_depth = $tree->getDepth() + 1;
76 5 my $last_depth = -1;
77 76 ▶ my $traversal_sub = sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
78 76 my ($t) = @_;
79 76 my $display_style = "none";
80 76 ▶ if (defined $current_path && $self->_compareNodeToPath($current_path, $t)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $current_path and $self->_compareNodeToPath($current_path, $t)) 9 67 76
Condition100%3/3
defined $current_path and $self->_compareNodeToPath($current_path, $t)
!ll&&!rl&&r
52159
MC/DC100%2/2
defined $current_path and $self->_compareNodeToPath($current_path, $t)
defined $current_path $self->_compareNodeToPath($current_path, $t)
Truth table100%3/3
A and B
A= defined $current_pathB= $self->_compareNodeToPath($current_path, $t)
ABresult
0X0
100
111
81 9 $display_style = "block";
82 9 $current_path = shift @path;
83 }
84 76 my $current_depth = $t->getDepth();
85 76 ▶ push @results => ($list_func->(CLOSE_TAG) x ($last_depth - $current_depth)) if ($last_depth > $current_depth);
Branch: 1 branch
BranchTrueFalseTotal
if $last_depth > $current_depth 20 56 76
86 76 ▶ unless ($t->isLeaf()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($t->isLeaf) { } 50 26 76
87 26 my $uid = $self->_createUID($t);
88 26 push @results => ($list_item_func->($t, EXPANDED, $uid));
89 26 push @results => $list_func->(OPEN_TAG, $uid, $display_style);
90 }
91 else {
92 50 push @results => ($list_item_func->($t));
93 }
94 76 $last_depth = $current_depth;
95 5 };
96 5 ▶ $traversal_sub->($self->{tree}) if $self->{include_trunk};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"include_trunk"} 1 4 5
97 5 $self->{tree}->traverse($traversal_sub);
98 5 $last_depth -= $root_depth;
99 5 ▶ $last_depth++ if $self->{include_trunk};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"include_trunk"} 1 4 5
100 5 push @results => ($list_func->(CLOSE_TAG) x ($last_depth + 1));
101 5 return (join "\n" => @results);
102 }
103  
104 2 ▶sub expandAllSimple {
Subroutine expandAllSimple: called CC=3 SCAR=11.0
Pod documented
105 2 my ($self) = @_;
106 2 my @results = ("<UL>");
107 2 my $root_depth = $self->{tree}->getDepth() + 1;
108 2 my $last_depth = -1;
109 31 ▶ my $traversal_sub = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
110 31 my ($t) = @_;
111 31 my $current_depth = $t->getDepth();
112 31 ▶ push @results => ("</UL>" x ($last_depth - $current_depth)) if ($last_depth > $current_depth);
Branch: 1 branch
BranchTrueFalseTotal
if $last_depth > $current_depth 8 23 31
113 31 ▶ unless ($t->isLeaf()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($t->isLeaf) { } 20 11 31
114 11 my $uid = $self->_createUID($t);
115 11 push @results => ("<LI><A HREF='javascript:void(0);' onClick='toggleList(\"$uid\")'>" .
116 $t->getNodeValue() . "</A></LI>");
117 11 push @results => "<UL ID='$uid'>";
118 }
119 else {
120 20 push @results => ("<LI>" . $t->getNodeValue() . "</LI>");
121 }
122 31 $last_depth = $current_depth;
123 2 };
124 2 ▶ $traversal_sub->($self->{tree}) if $self->{include_trunk};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"include_trunk"} 1 1 2
125 2 $self->{tree}->traverse($traversal_sub);
126 2 $last_depth -= $root_depth;
127 2 ▶ $last_depth++ if $self->{include_trunk};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"include_trunk"} 1 1 2
128 2 push @results => ("</UL>" x ($last_depth + 1));
129 2 return (join "\n" => @results);
130 }
131  
132 7 ▶sub expandAllComplex {
Subroutine expandAllComplex: called CC=3 SCAR=11.0
Pod documented
133 7 my ($self, $config) = @_;
134  
135 7 my ($list_func, $list_item_func) = $self->_processConfig($config);
136  
137 7 my @results = $list_func->(OPEN_TAG);
138 7 my $root_depth = $self->{tree}->getDepth() + 1;
139 7 my $last_depth = -1;
140 94 ▶ my $traversal_sub = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
141 94 my ($t) = @_;
142 94 my $current_depth = $t->getDepth();
143 94 ▶ push @results => ($list_func->(CLOSE_TAG) x ($last_depth - $current_depth)) if ($last_depth > $current_depth);
Branch: 1 branch
BranchTrueFalseTotal
if $last_depth > $current_depth 25 69 94
144 94 ▶ unless ($t->isLeaf()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($t->isLeaf) { } 62 32 94
145 32 my $uid = $self->_createUID($t);
146 32 push @results => ($list_item_func->($t, EXPANDED, $uid));
147 32 push @results => $list_func->(OPEN_TAG, $uid);
148 }
149 else {
150 62 push @results => ($list_item_func->($t));
151 }
152 94 $last_depth = $current_depth;
153 7 };
154 7 ▶ $traversal_sub->($self->{tree}) if $self->{include_trunk};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"include_trunk"} 1 6 7
155 7 $self->{tree}->traverse($traversal_sub);
156 7 $last_depth -= $root_depth;
157 7 ▶ $last_depth++ if $self->{include_trunk};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"include_trunk"} 1 6 7
158 7 push @results => ($list_func->(CLOSE_TAG) x ($last_depth + 1));
159 7 return (join "\n" => @results);
160 }
161  
162 # code strings
163  
164 1 ▶use constant LIST_FUNCTION_CODE_STRING => q|
Subroutine BEGIN: called CC=1 SCAR=0.0
165 sub {
166 my ($tag_type, $list_id, $display_style) = @_;
167 # allow this to be found quickly
168 return "</${list_type}>" if ($tag_type == CLOSE_TAG);
169 # test the most functional first
170 if ($tag_type == OPEN_TAG && $list_id && $display_style) {
171 my $temp_list_css;
172 if ($list_css && $list_css !~ /CLASS/) {
173 # in case someone has already set the list_css
174 # property, we need to add out display property
175 # to it, so we need to do a little text mangling
176 $temp_list_css = $list_css;
177 chop($temp_list_css);
178 $temp_list_css .= " display: $display_style;'";
179 }
180 elsif ($list_css) {
181 $temp_list_css = "${list_css} STYLE='display: $display_style;'"
182 }
183 else {
184 $temp_list_css = " STYLE='display: $display_style;'"
185 }
186 return "<${list_type}${temp_list_css} ID='${list_id}'>"
187 }
188 # next...
189 return "<${list_type}${list_css} ID='${list_id}'>"
190 if ($tag_type == OPEN_TAG && $list_id);
191 # and finally, something that does nothing really
192 return "<${list_type}${list_css}>" if ($tag_type == OPEN_TAG);
193 }
194 1 |;
195  
196 12 ▶sub _buildListItemFunction {
Subroutine _buildListItemFunction: called CC=6 SCAR=18.0
197 12 my ($self, %config) = @_;
198 # process the configuration directives
199 12 my ($list_item_css, $expanded_item_css, $node_formatter) = $self->_processListItemConfig(%config);
200  
201 12 my $link_css = "";
202 12 ▶ if (exists $config{link_css}) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $config{'link_css'}) { } 2 10 12
elsif (exists $config{'link_css_class'}) { } 3 7 10
203 2 $link_css = " STYLE='" . $config{link_css}. "'";
204 }
205 elsif (exists $config{link_css_class}) {
206 3 $link_css = " CLASS='" . $config{link_css_class} . "'";
207 }
208  
209 12 my $form_element_formatter;
210 12 ▶ if (exists $config{form_element_formatter}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $config{'form_element_formatter'}) { } 0 12 12
211 0 $form_element_formatter = $config{form_element_formatter};
212 }
213 else {
214 12 ▶ if (exists $config{radio_button}) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $config{'radio_button'}) { } 1 11 12
elsif (exists $config{'checkbox'}) { } 1 10 11
215 1 $form_element_formatter = $self->_makeRadioButtonFormatter($config{radio_button});
216 }
217 elsif (exists $config{checkbox}) {
218 1 $form_element_formatter = $self->_makeCheckBoxFormatter($config{checkbox});
219 }
220 }
221  
222 # now compile the subroutine in the current environment
223 12 return eval $self->LIST_ITEM_FUNCTION_CODE_STRING;
224 }
225  
226 1 ▶sub _makeRadioButtonFormatter {
Subroutine _makeRadioButtonFormatter: called CC=1 SCAR=0.0
227 1 my ($self, $radio_button_id) = @_;
228 15 ▶ return sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
229 15 my ($t) = @_;
230 15 return "<INPUT TYPE='radio' NAME='$radio_button_id' VALUE='" . $t->getUID() . "'>";
231 }
232 1 }
233  
234 1 ▶sub _makeCheckBoxFormatter {
Subroutine _makeCheckBoxFormatter: called CC=1 SCAR=0.0
235 1 my ($self, $checkbox_id) = @_;
236 15 ▶ return sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
237 15 my ($t) = @_;
238 15 return "<INPUT TYPE='checkbox' NAME='$checkbox_id' VALUE='" . $t->getUID() . "'>";
239 }
240 1 }
241  
242 1 ▶use constant LIST_ITEM_FUNCTION_CODE_STRING => q|;
Subroutine BEGIN: called CC=1 SCAR=0.0
243 sub {
244 my ($t, $is_expanded, $tree_id) = @_;
245 my $item_css = $list_item_css;
246 if ($is_expanded) {
247 $item_css = $expanded_item_css if $expanded_item_css;
248 return "<LI${item_css}>" .
249 (($form_element_formatter) ? $form_element_formatter->($t) : "") .
250 "<A${link_css} HREF='javascript:void(0);' onClick='toggleList(\"${tree_id}\")'>" .
251 (($node_formatter) ? $node_formatter->($t) : $t->getNodeValue()) .
252 "</A></LI>";
253 }
254 return "<LI${item_css}>" .
255 (($form_element_formatter) ? $form_element_formatter->($t) : "") .
256 (($node_formatter) ? $node_formatter->($t) : $t->getNodeValue()) .
257 "</LI>";
258 }
259 1 |;
260  
261 1 ▶use constant javascript => q|
Subroutine BEGIN: called CC=1 SCAR=0.0
262 <SCRIPT LANGUAGE="javascript">
263 function toggleList(tree_id) {
264 var element = document.getElementById(tree_id);
265 if (element) {
266 if (element.style.display == 'none') {
267 element.style.display = 'block';
268 }
269 else {
270 element.style.display = 'none';
271 }
272 }
273 }
274 </SCRIPT>
275 1 |;
276  
277 1 1;
278  
279 __END__
« View/HTML.pm ↑ Summary View.pm »