Reactor/HTML/Layout.pm untested

filter: Statementstmt 0.0% 0 / 180 Branchbran 0.0% 0 / 62 Conditioncond 0.0% 0 / 65 MC/DCmcdc 0.0% 0 / 38 Subroutinesub 0.0% 0 / 13 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 366 CC 32 SCAR 51.8
CC
32
Cov
0%
html_table
380.0
html_layout_2lr
42.0
html_layout_2lr_flex
42.0
CRAP 177.0
1 ##############################################################################
2 ##
3 ## Web::Reactor application machinery
4 ## Copyright (c) 2013-2022 Vladi Belperchinov-Shabanski "Cade"
5 ## <cade@noxrun.com> <cade@bis.bg> <cade@cpan.org>
6 ## http://cade.noxrun.com
7 ##
8 ## LICENSE: GPLv2
9 ## https://github.com/cade-vs/perl-web-reactor
10 ##
11 ##############################################################################
12 ##
13 ## HTML Layout
14 ##
15 ##############################################################################
16 package Web::Reactor::HTML::Layout;
17 use strict;
18  
19 use Exception::Sink;
20 use Data::Tools;
21 use Web::Reactor::HTML::Utils;
22  
23 use Exporter;
24 our @ISA = qw( Exporter );
25 our @EXPORT = qw(
26  
27 html_table
28  
29 html_layout_grid
30 html_layout_hbox
31 html_layout_vbox
32  
33 html_layout_hbox_flex
34  
35 html_layout_2lr
36 html_layout_2lr_flex
37  
38 );
39  
40 ### LAYOUT ###################################################################
41  
42 =pod
43  
44 takes two-dimensional perl array and formats it in html table
45  
46 DEMO:
47  
48 my @data;
49  
50 push @data, [
51 '123',
52 { ARGS => 'align=right', DATA => 'asd' },
53 'qwe'
54 ];
55 push @data, {
56 CLASS => 'grid',
57 DATA => [
58 '123',
59 { ARGS => 'align=center', DATA => 'asd' },
60 { CLASS => 'fmt-img', DATA => 'qwe' },
61 ],
62 };
63 push @data, {
64 # columns class list (CCL), used only for current row
65 # use PCCL for permanent (for the rest of the rows)
66 CCL => [ 'view-name-h', 'view-value-h' ],
67 DATA => [ 'name', 'value' ],
68 };
69 push @data, {
70 PCCL => [ 'view-name-h', 'view-value-h' ],
71 # set only PCCL and skip this row
72 SKIP => YES,
73 };
74  
75 $text .= html_table( \@data, ARGS => 'width=100%' );
76  
77 =cut
78  
79  
80 sub html_table
81 {
82 my $rows = shift;
83 my %opt = @_;
84  
85 hash_uc_ipl( \%opt );
86  
87 # t_* table attr
88 # r_* row attr
89 # c_* cell attr
90  
91 my $t_args;
92 $t_args ||= $opt{ 'ARGS' };
93 $t_args ||= 'class=' . $opt{ 'CLASS' } if $opt{ 'CLASS' };
94  
95 my $tr1 = $opt{ 'TR1' } || $opt{ 'TR-1' } || 'tr-1';
96 my $tr2 = $opt{ 'TR2' } || $opt{ 'TR-2' } || 'tr-2';
97 my $trh = $opt{ 'TRH' };
98 my $tdh = $opt{ 'TDH' };
99  
100 my $ccl = $opt{ 'CCL' } || undef;
101 my $pccl = $opt{ 'PCCL' } || undef;
102  
103 my $t_cmt = $opt{ 'COMMENT' };
104  
105 my $text;
106 $text .= "\n\n\n";
107 $text .= "<!--- BEGIN TABLE: $t_cmt --->\n" if $t_cmt;
108 $text .= "<table $t_args>\n<tbody>\n";
109  
110 my $r_class = $tr1;
111  
112 my $row_num = 0;
113 for my $row ( @$rows )
114 {
115 my $cols;
116 $r_class = $r_class eq $tr1 ? $tr2 : $tr1;
117 my $r_args;
118  
119 $r_class = $trh if $trh and $row_num == 0;
120  
121 if ( ! ref( $row ) ) # SCALAR
122 {
123 # fallback
124 $row = [ $row ];
125 }
126  
127 if ( ref( $row ) eq 'ARRAY' )
128 {
129 $cols = $row;
130 $r_args = "class='$r_class'";
131 }
132 elsif ( ref( $row ) eq 'HASH' )
133 {
134 $row = hash_uc( $row );
135 $cols = $row->{ 'DATA' };
136 $r_args ||= $row->{ 'ARGS' };
137 $r_args ||= 'class=' . ( $row->{ 'CLASS' } || $r_class );
138 $ccl = $row->{ 'CCL' } if $row->{ 'CCL' };
139 $pccl = $row->{ 'PCCL' } if $row->{ 'PCCL' };
140  
141 next if $row->{ 'SKIP' };
142 }
143 else
144 {
145 boom "invalid row type, expected HASH or ARRAY reference";
146 next;
147 }
148  
149 $text .= " <tr $r_args>\n";
150  
151 $ccl = $pccl if $pccl and ! $ccl; # use permanent cols class list if permanent specified and not local one
152  
153 my $col_num = 0;
154 for my $cell ( @$cols )
155 {
156 my $c_class;
157 my $c_args;
158 my $val;
159  
160 $c_class = $tdh if $tdh and $row_num == 0;
161 $c_class = $ccl->[ $col_num ] if $ccl and $ccl->[ $col_num ];
162  
163 if ( ! ref( $cell ) ) # SCALAR
164 {
165 $val = $cell;
166 }
167 elsif( ref( $cell ) eq 'HASH' )
168 {
169 $cell = hash_uc( $cell );
170 $val = $cell->{ 'DATA' };
171 $c_args = $cell->{ 'ARGS' };
172 $c_args .= " class='" . $cell->{ 'CLASS' } . "'" if $cell->{ 'CLASS' };
173 $c_args .= " width='" . $cell->{ 'WIDTH' } . "'" if $cell->{ 'WIDTH' };
174 }
175 else
176 {
177 # FIXME: carp croak boom :)
178 next;
179 }
180  
181 $c_args ||= "class='" . $c_class . "'";
182 $text .= " <td $c_args>$val</td>\n";
183 $col_num++;
184 }
185  
186 $ccl = undef;
187  
188 $text .= " </tr>\n";
189 $row_num++;
190 }
191  
192 $text .= "</tbody>\n</table>\n";
193 $text .= "<!--- END TABLE: $t_cmt --->\n" if $t_cmt;
194 $text .= "\n\n\n";
195  
196 return $text;
197 }
198  
199 ##############################################################################
200  
201 sub html_layout_grid
202 {
203 my $data = shift;
204
205 my $text;
206
207 $text .= "<table border=0 cellspacing=0 cellpadding=0 width=100%>";
208  
209 for my $row ( @$data )
210 {
211 my $row_args;
212 if( ref( $row ) eq 'HASH' )
213 {
214 $row_args = $row->{ 'ARGS' };
215 $row = $row->{ 'DATA' };
216 }
217
218 $text .= "<tr $row_args>";
219 for my $col ( @$row )
220 {
221 my $col_args;
222 if( ref( $row ) eq 'HASH' )
223 {
224 $col_args = $col->{ 'ARGS' };
225 $col = $col->{ 'DATA' };
226 }
227 $text .= "<td $col_args>$col</td>";
228 }
229 $text .= "</tr>";
230 }
231
232 $text .= "</table>";
233
234 return $text;
235 }
236  
237 sub html_layout_hbox
238 {
239 my $data = shift;
240
241 return html_layout_grid( [ { DATA => $data, ARGS => "valign=top" } ] );
242 }
243  
244 sub html_layout_vbox
245 {
246 my $data = shift;
247
248 my @data;
249 push @data, [ $_ ] for @$data;
250  
251 return html_layout_grid( \@data );
252 }
253  
254 sub html_layout_hbox_flex
255 {
256 my $opt = ${ shift() } if ref( $_[0] ) eq 'SCALAR';
257 my @data = @_;
258
259 my @opt = split /,/, $opt;
260
261 my $text;
262
263 $text .= "<div style='display: flex; border: solid 1px #f00;'>";
264
265 while( @data )
266 {
267 my $data = shift @data;
268 my $opt = shift @opt || 1;
269 $text .= "<div style='flex:$opt; border: solid 2px #0f0;'>$data</div>";
270 }
271
272 $text .= "</div>";
273
274 return $text;
275 }
276  
277 sub html_layout_vbox
278 {
279 my $data = shift;
280
281 my @data;
282 push @data, [ $_ ] for @$data;
283  
284 return html_layout_grid( \@data );
285 }
286  
287 #-----------------------------------------------------------------------------
288  
289 =pod
290  
291 formats pair for left/right boxes aligned within specific width
292  
293 format is: 'left-spec=right-spec'
294  
295 left-spec is formatting for the left data.
296 right-spec is formatting for the right data.
297  
298 both specs are:
299  
300 align-symbol . width-len%
301  
302 examples:
303  
304 <50%=50%> -- left is left aligned, right is right aligned, equal length
305 <=> -- the same
306 >20=> -- left is right aligned, 20% width, right is right aligned, 80%
307 =1% -- same as <99=1> or <99%=1%>
308  
309 using '==' instead of '=' enables no-word-wrap style
310  
311 =cut
312  
313 sub html_layout_2lr
314 {
315 my $ld = shift; # left data
316 my $rd = shift; # right data
317 my $fm = shift || '<=>'; # format: '[<>]nn%=nn%[<>]'
318
319 my $la; # left align
320 my $ra; # right align
321 my $lw; # left width
322 my $rw; # right width
323 my $nw; # no-wrap
324 if( $fm =~ /^([<>]?)((\d+)%?)?=(=)?((\d+)%?)?([<>]?)$/ )
325 {
326 $la = $1;
327 $lw = $3;
328 $nw = $4;
329 $rw = $6;
330 $ra = $7;
331 }
332 else
333 {
334 boom "invalid format [$fm]";
335 }
336
337 $la = { '<' => 'align=left', '>' => 'align=right' }->{ $la };
338 $ra = { '<' => 'align=left', '>' => 'align=right' }->{ $ra };
339  
340 $lw = $rw = 50 if $lw == 0 and $rw == 0;
341 $lw = int( 100 - $rw ) if $lw == 0 and $rw > 0;
342 $rw = int( 100 - $lw ) if $rw == 0 and $lw > 0;
343
344 $lw = "width=$lw%";
345 $rw = "width=$rw%";
346  
347 $nw = "style='white-space: nowrap'" if $nw;
348  
349 return "<table width=100% cellspacing=0 cellpadding=0 border=0 $nw><tr><td $la $lw>$ld</td><td $ra $rw>$rd</td></tr></table>";
350 }
351  
352 sub html_layout_2lr_flex
353 {
354 my $ld = shift; # left data
355 my $rd = shift; # right data
356 my $fm = shift || '<=>'; # format: '[<>]nn%=nn%[<>]'
357  
358 return "<div style='display: flex;'><div style='flex: 99; text-align: left;'>$ld</div><div style='flex: 1; text-align: right; white-space: nowrap;'>$rd</div></div>";
359
360 my $la; # left align
361 my $ra; # right align
362 my $lw; # left width
363 my $rw; # right width
364 my $nw; # no-wrap
365 if( $fm =~ /^([<>]?)((\d+)%?)?=(=)?((\d+)%?)?([<>]?)$/ )
366 {
367 $la = $1;
368 $lw = $3;
369 $nw = $4;
370 $rw = $6;
371 $ra = $7;
372 }
373 else
374 {
375 boom "invalid format [$fm]";
376 }
377
378 $la = { '<' => 'align=left', '>' => 'align=right' }->{ $la };
379 $ra = { '<' => 'align=left', '>' => 'align=right' }->{ $ra };
380  
381 $lw = $rw = 50 if $lw == 0 and $rw == 0;
382 $lw = int( 100 - $rw ) if $lw == 0 and $rw > 0;
383 $rw = int( 100 - $lw ) if $rw == 0 and $lw > 0;
384
385 $lw = "width=$lw%";
386 $rw = "width=$rw%";
387  
388 $nw = "style='white-space: nowrap'" if $nw;
389  
390 return "<table width=100% cellspacing=0 cellpadding=0 border=0 $nw><tr><td $la $lw>$ld</td><td $ra $rw>$rd</td></tr></table>";
391 }
392  
393 ### EOF ######################################################################
394 1;