File Coverage

blib/lib/Tags/HTML/InfoBox.pm
Criterion Covered Total %
statement 70 71 98.5
branch 14 18 77.7
condition 4 6 66.6
subroutine 15 15 100.0
pod 1 1 100.0
total 104 111 93.6


line stmt bran cond sub pod time code
1             package Tags::HTML::InfoBox;
2              
3 8     8   254833 use base qw(Tags::HTML);
  8         17  
  8         4470  
4 8     8   75182 use strict;
  8         21  
  8         215  
5 8     8   44 use warnings;
  8         44  
  8         2759  
6              
7 8     8   53 use Class::Utils qw(set_params split_params);
  8         24  
  8         462  
8 8     8   73 use Error::Pure qw(err);
  8         19  
  8         450  
9 8     8   4900 use Mo::utils::CSS 0.02 qw(check_css_class);
  8         85908  
  8         190  
10 8     8   771 use Scalar::Util qw(blessed);
  8         15  
  8         367  
11 8     8   4108 use Tags::HTML::Icon;
  8         8425  
  8         6995  
12              
13             our $VERSION = 0.01;
14              
15             # Constructor.
16             sub new {
17 22     22 1 3797898 my ($class, @params) = @_;
18              
19             # Create object.
20 22         121 my ($object_params_ar, $other_params_ar) = split_params(
21             ['css_class', 'lang'], @params);
22 22         459 my $self = $class->SUPER::new(@{$other_params_ar});
  22         170  
23              
24             # CSS style for info box.
25 17         689 $self->{'css_class'} = 'info-box';
26              
27 17         50 $self->{'lang'} = undef;
28              
29             # Process params.
30 17         32 set_params($self, @{$object_params_ar});
  17         60  
31              
32 17         150 check_css_class($self, 'css_class');
33              
34             # Object.
35 17         461 return $self;
36             }
37              
38             sub _cleanup {
39 1     1   11 my $self = shift;
40              
41 1         2 delete $self->{'_infobox'};
42 1         2 delete $self->{'_tags_icon'};
43              
44 1         2 return;
45             }
46              
47             sub _init {
48 7     7   8285 my ($self, @params) = @_;
49              
50 7         28 return $self->_set_infobox(@params);
51             }
52              
53             sub _prepare {
54 4     4   1775 my ($self, @params) = @_;
55              
56 4         16 return $self->_set_infobox(@params);
57             }
58              
59             # Process 'Tags'.
60             sub _process {
61 3     3   42 my $self = shift;
62              
63 3 100       26 if (! exists $self->{'_infobox'}) {
64 1         4 return;
65             }
66              
67             $self->{'tags'}->put(
68             ['b', 'table'],
69 2         22 ['a', 'class', $self->{'css_class'}],
70             );
71 2         210 foreach my $item (@{$self->{'_infobox'}->items}) {
  2         22  
72 7         1046 $self->{'tags'}->put(
73             ['b', 'tr'],
74             ['b', 'td'],
75             );
76 7 100       568 if (defined $item->icon) {
77 2         51 $self->{'_tags_icon'}->init($item->icon);
78 2         64 $self->{'_tags_icon'}->process;
79             }
80             $self->{'tags'}->put(
81             ['e', 'td'],
82              
83             ['b', 'td'],
84             (defined $self->{'lang'} && defined $item->text->lang
85 7 50 33     737 && $item->text->lang ne $self->{'lang'}) ? (
    50          
    50          
86              
87             ['a', 'lang', $self->text->lang],
88             ) : (),
89             defined $item->uri ? (
90             ['b', 'a'],
91             ['a', 'href', $item->uri],
92             ) : (),
93             ['d', $item->text->text],
94             defined $item->uri ? (
95             ['e', 'a'],
96             ) : (),
97             ['e', 'td'],
98             ['e', 'tr'],
99             );
100             }
101 2         384 $self->{'tags'}->put(
102             ['e', 'table'],
103             );
104              
105 2         87 return;
106             }
107              
108             sub _process_css {
109 2     2   31 my $self = shift;
110              
111 2 100       8 if (! exists $self->{'_infobox'}) {
112 1         2 return;
113             }
114              
115             $self->{'css'}->put(
116             ['s', '.'.$self->{'css_class'}],
117             ['d', 'background-color', '#32a4a8'],
118             ['d', 'padding', '1em'],
119             ['e'],
120              
121             ['s', '.'.$self->{'css_class'}.' .icon'],
122             ['d', 'text-align', 'center'],
123             ['e'],
124              
125 1         29 ['s', '.'.$self->{'css_class'}.' a'],
126             ['d', 'text-decoration', 'none'],
127             ['e'],
128             );
129 1         352 $self->{'_tags_icon'}->process_css;
130              
131 1         19 return;
132             }
133              
134             sub _set_infobox {
135 11     11   25 my ($self, $infobox) = @_;
136              
137 11 100       45 if (! defined $infobox) {
138 2         6 return;
139             }
140              
141 9 100 100     76 if (! blessed($infobox) || ! $infobox->isa('Data::InfoBox')) {
142 4         120 err "Info box object must be a instance of 'Data::InfoBox'.";
143             }
144 5 50       110 if (! $infobox->VERSION('0.04')) {
145 0         0 err "Info box object must have a minimal version >= 0.04.";
146             }
147              
148 5         36 $self->{'_infobox'} = $infobox;
149              
150             $self->{'_tags_icon'} = Tags::HTML::Icon->new(
151             'css' => $self->{'css'},
152 5         49 'tags' => $self->{'tags'},
153             );
154              
155 5         718 return;
156             }
157              
158             1;
159              
160             __END__
161              
162             =pod
163              
164             =encoding utf8
165              
166             =head1 NAME
167              
168             Tags::HTML::InfoBox - Tags helper for HTML info box.
169              
170             =head1 SYNOPSIS
171              
172             use Tags::HTML::InfoBox;
173              
174             my $obj = Tags::HTML::InfoBox->new(%params);
175             $obj->cleanup;
176             $obj->init($infobox);
177             $obj->prepare($infobox);
178             $obj->process;
179             $obj->process_css;
180              
181             =head1 METHODS
182              
183             =head2 C<new>
184              
185             my $obj = Tags::HTML::InfoBox->new(%params);
186              
187             Constructor.
188              
189             =over 8
190              
191             =item * C<css>
192              
193             L<CSS::Struct::Output> object for L<process_css> processing.
194              
195             Default value is undef.
196              
197             =item * C<css_class>
198              
199             Default value is 'info-box'.
200              
201             =item * C<lang>
202              
203             Language in ISO 639-1 code.
204              
205             Default value is undef.
206              
207             =item * C<tags>
208              
209             L<Tags::Output> object.
210              
211             Default value is undef.
212              
213             =back
214              
215             =head2 C<cleanup>
216              
217             $obj->cleanup;
218              
219             Process cleanup after page run.
220              
221             In this case cleanup internal representation of a set by L<init>.
222              
223             Returns undef.
224              
225             =head2 C<init>
226              
227             $obj->init($infobox);
228              
229             Process initialization in page run.
230              
231             Accepted C<$infobox> is L<Data::InfoBox>.
232              
233             Returns undef.
234              
235             =head2 C<prepare>
236              
237             $obj->prepare($infobox);
238              
239             Process initialization before page run.
240              
241             Accepted C<$infobox> is L<Data::InfoBox>.
242              
243             Returns undef.
244              
245             =head2 C<process>
246              
247             $obj->process;
248              
249             Process L<Tags> structure for HTML a element to output.
250              
251             Do nothing in case without inicialization by L<init>.
252              
253             Returns undef.
254              
255             =head2 C<process_css>
256              
257             $obj->process_css;
258              
259             Process L<CSS::Struct> structure for HTML a element to output.
260              
261             Do nothing in case without inicialization by L<init>.
262              
263             Returns undef.
264              
265             =head1 ERRORS
266              
267             new():
268             From Mo::utils::CSS::check_css_class():
269             Parameter '%s' has bad CSS class name.
270             Value: %s
271             Parameter '%s' has bad CSS class name (number on begin).
272             Value: %s
273             From Tags::HTML::new():
274             Parameter 'css' must be a 'CSS::Struct::Output::*' class.
275             Parameter 'tags' must be a 'Tags::Output::*' class.
276              
277             init():
278             Info box object must be a instance of 'Data::InfoBox'.
279              
280             prepare():
281             Info box object must be a instance of 'Data::InfoBox'.
282              
283             process():
284             From Tags::HTML::process():
285             Parameter 'tags' isn't defined.
286              
287             process_css():
288             From Tags::HTML::process_css():
289             Parameter 'css' isn't defined.
290              
291             =head1 EXAMPLE
292              
293             =for comment filename=create_and_print_infobox.pl
294              
295             use strict;
296             use warnings;
297              
298             use CSS::Struct::Output::Indent;
299             use Tags::HTML::InfoBox;
300             use Tags::Output::Indent;
301             use Test::Shared::Fixture::Data::InfoBox::Street;
302             use Unicode::UTF8 qw(encode_utf8);
303              
304             # Object.
305             my $css = CSS::Struct::Output::Indent->new;
306             my $tags = Tags::Output::Indent->new(
307             'xml' => 1,
308             );
309             my $obj = Tags::HTML::InfoBox->new(
310             'css' => $css,
311             'tags' => $tags,
312             );
313              
314             # Data object for info box.
315             my $infobox = Test::Shared::Fixture::Data::InfoBox::Street->new;
316              
317             # Initialize.
318             $obj->init($infobox);
319              
320             # Process.
321             $obj->process;
322             $obj->process_css;
323              
324             # Print out.
325             print "HTML:\n";
326             print encode_utf8($tags->flush);
327             print "\n\n";
328             print "CSS:\n";
329             print $css->flush;
330              
331             # Output:
332             # HTML:
333             # <table class="info-box">
334             # <tr>
335             # <td />
336             # <td>
337             # Nábřeží Rudoarmějců
338             # </td>
339             # </tr>
340             # <tr>
341             # <td />
342             # <td>
343             # Příbor
344             # </td>
345             # </tr>
346             # <tr>
347             # <td />
348             # <td>
349             # Česká republika
350             # </td>
351             # </tr>
352             # </table>
353             #
354             # CSS:
355             # .info-box {
356             # background-color: #32a4a8;
357             # padding: 1em;
358             # }
359             # .info-box .icon {
360             # text-align: center;
361             # }
362             # .info-box a {
363             # text-decoration: none;
364             # }
365              
366             =head1 DEPENDENCIES
367              
368             L<Class::Utils>,
369             L<Error::Pure>,
370             L<Mo::utils::CSS>,
371             L<Scalar::Util>,
372             L<Tags::HTML>,
373             L<Tags::HTML::Icon>.
374              
375             =head1 REPOSITORY
376              
377             L<https://github.com/michal-josef-spacek/Tags-HTML-InfoBox>
378              
379             =head1 AUTHOR
380              
381             Michal Josef Špaček L<mailto:skim@cpan.org>
382              
383             L<http://skim.cz>
384              
385             =head1 LICENSE AND COPYRIGHT
386              
387             © 2023-2025 Michal Josef Špaček
388              
389             BSD 2-Clause License
390              
391             =head1 VERSION
392              
393             0.01
394              
395             =cut