TableLayout/Component.pm untested

filter: Statementstmt 0.0% 0 / 255 Branchbran 0.0% 0 / 50 Conditioncond 0.0% 0 / 33 MC/DCmcdc 0.0% 0 / 21 Subroutinesub 0.0% 0 / 49 Podpod 0.0% 0 / 36 totaltotal 0.0% 0 / 444 CC 26 SCAR 22.0
CC
26
Cov
0%
tl_getParameters
30.0
tl_setup
20.0
getAllChildren
20.0
CRAP 9.1
1 # ====================================================================
2 # Copyright (C) 1997,1998 Stephen Farrell <stephen@farrell.org>
3 #
4 # All rights reserved. This program is free software; you can
5 # redistribute it and/or modify it under the same terms as Perl
6 # itself.
7 #
8 # ====================================================================
9 # File: Component.pm
10 # Author: Stephen Farrell
11 # Created: August, 1997
12 # Locations: http://www.palefire.org/~sfarrell/TableLayout/
13 # CVS $Id: Component.pm,v 1.17 1998/09/20 21:05:28 sfarrell Exp $
14 # ====================================================================
15  
16  
17 ##
18 ## This class is abstract
19 ##
20 package HTML::TableLayout::Component;
21 use HTML::TableLayout::Symbols;
22 @HTML::TableLayout::Component::ISA=qw(HTML::TableLayout::TL_BASE);
23 use Carp;
24 use strict;
25  
26 ##
27 ## Default init
28 ##
29 sub tl_init {
30 my $this = shift;
31 ##
32 ## QUIZ--how do i avoid this temporary variable?
33 ##
34 my %params = @_;
35 $this->{TL_PARAMS} = \ %params;
36 $this->SUPER::tl_init();
37 }
38  
39 ##
40 ## tl_setContext(): Sets the context in the heirarchy when packing and
41 ## displaying. This is done "late"
42 ##
43 sub tl_setContext {
44 my ($this, $container) = @_;
45 my $window = $container->{TL_WINDOW};
46 my $form = $container->{TL_FORM};
47
48
49 ## ====================================================================
50 ##
51 ## DEBUGGING
52 ##
53
54 confess "container is null" unless $container;
55 confess "window is null" unless $window;
56
57 ##
58 ## it's ok for the form to be null, but if it is, we don't want to
59 ## clobber an existing value for it.
60 ##
61 ## ====================================================================
62
63 defined $container and $this->{TL_CONTAINER} = $container;
64 defined $window and $this->{TL_WINDOW} = $window;
65 defined $form and $this->{TL_FORM} = $form;
66 }
67  
68 ##
69 ## tl_getContainer(),tl_getWindow(),tl_getForm(): Accessors for the
70 ## above--notethat these might not be used much b/c we know the name
71 ## of the data very well.
72 ##
73 sub tl_getContainer { return shift->{TL_CONTAINER} }
74 sub tl_getWindow { return shift->{TL_WINDOW} }
75 sub tl_getForm { return shift->{TL_FORM} }
76  
77  
78 ##
79 ## tl_setup(): is called just before printing, and is meant to provide
80 ## "late" packing and searching for requirements in containers (like
81 ## looking for a Form). Actually, it's called everywhere before
82 ## anything prints, so if you want to play with values in your
83 ## neighboring components, have fun.
84 ##
85 ## If you override this, you must call your super's version. (like
86 ## $this->SUPER::tl_setup()). ok ok I'm lying right now b/c as you can
87 ## see, there is nothing here so obviously you don't HAVE to call it.
88 ## but I might add something later. Also, if your parent is a
89 ## componentcontainer, then you MUST call it (or do equivalent and
90 ## keep your fingers crossed for future versions).
91 ##
92 sub tl_setup { }
93  
94  
95 ##
96 ## tl_print(): uses i_print() and f_print() to display object.
97 ##
98 sub tl_print { }
99  
100  
101 ##
102 ## tl_breakAfter(): The component has a break "<BR>" after it. This
103 ## doesn't happen automatically--the component printing it needs to
104 ## check if it is there and print it itself.
105 ##
106  
107 sub tl_breakAfter { return shift->{TL_BREAK_AFTER} }
108  
109 sub tl_destroy {
110 my ($this) = @_;
111 undef $this->{TL_BREAK_AFTER};
112 undef $this->{TL_CONTAINER};
113 undef $this->{TL_WINDOW};
114 undef $this->{TL_FORM};
115 $this->SUPER::tl_destroy();
116 }
117  
118 # ---------------------------------------------------------------------
119  
120 package HTML::TableLayout::ComponentContainer;
121 use HTML::TableLayout::Symbols;
122 @HTML::TableLayout::ComponentContainer::ISA=qw(HTML::TableLayout::Component);
123  
124 sub tl_init {
125 my $this = shift;
126 $this->SUPER::tl_init(@_);
127 $this->{TL_COMPONENTS} = [];
128 $this->{TL_BREAKS} = [];
129 }
130  
131 ##
132 ## insert(): add a component. subclasses should always call this,
133 ## like tl_setup()
134 ##
135 sub insert {
136 my ($this, $obj, $br) = @_;
137  
138 if (! ref $obj) {
139 $obj = HTML::TableLayout::Component::Text->new($obj);
140 }
141  
142 if ($obj->isa("HTML::TableLayout::Form")) {
143 $this->{TL_FORM} = $obj;
144 $this->{form_is_mine} = $obj;
145 }
146 else {
147 push @{ $this->{TL_COMPONENTS} }, $obj;
148 push @{ $this->{TL_BREAKS} }, $br;
149 }
150 return $this;
151 }
152  
153 ##
154 ## insertLn(): add a component w/ <BR> afterwards. Generally I've
155 ## handled this as a wrapper method that calls insert with a second
156 ## argument of "1".
157 ##
158 sub insertLn { return shift->insert(shift,1) }
159  
160  
161 ##
162 ## tl_setup(): if you choose to override this method, then you must do
163 ## what is done here, or call $this->SUPER::tl_setup(). Of course, if
164 ## you replicate this method's functionality, you should be aware that
165 ## in the future this function might change, and you might need to
166 ## update your equivalent functionality in the future.... (yes, I'm
167 ## scrounging for hints on OO design!)
168 ##
169 sub tl_setup {
170 my ($this) = @_;
171  
172 $this->tl_setup_form();
173  
174 foreach my $cmp (@{ $this->{TL_COMPONENTS} }) {
175 die("null comp.") unless $cmp;
176 ##
177 ## Maybe it is a form input, in which case it needs to be inserted
178 ## into the appropriate form.
179 ##
180 if ($cmp->isa("HTML::TableLayout::FormComponent")) {
181 my $f = $this->{TL_FORM};
182 if ($f) {
183 $f->insert($cmp);
184 $cmp->tl_setContext($this);
185 }
186 else {
187 die("No Form to insert this FormComponent [$cmp] into [$this]");
188 }
189 }
190 $cmp->tl_setContext($this);
191 $cmp->tl_setup();
192 }
193 $this->SUPER::tl_setup();
194 }
195  
196 sub tl_setup_form {
197 my $this = shift;
198
199 ##
200 ## If we have a form, this is the time to set its context
201 ##
202 if ($this->{form_is_mine}) {
203 if ($this->{form_is_mine} ne $this->{TL_FORM}) {
204 die("Nested forms detected!");
205 }
206 else {
207 $this->{TL_FORM}->tl_setContext($this);
208 $this->{TL_WINDOW}->_incrementNumForms();
209 }
210 }
211 }
212  
213  
214 ##
215 ## this makes a ComponentContainer an implementable object--and a very
216 ## useful one at that. YOu can just stick stuff in it and it'll print
217 ## the various things with no added overhead. unfortunately,
218 ## subclasses will need to reproduce any behavior here...
219 ##
220 sub tl_print {
221 my ($this) = @_;
222  
223 $this->{form_is_mine} and $this->{TL_FORM}->tl_print();
224 foreach (0..$#{ $this->{TL_COMPONENTS} }) {
225 $this->{TL_COMPONENTS}->[$_]->tl_print();
226 $this->{TL_BREAKS}->[$_] and $this->{TL_WINDOW}->i_print("><BR");
227 }
228 $this->{form_is_mine} and $this->{TL_FORM}->_print_end();
229 }
230  
231 sub tl_destroy {
232 my ($this) = @_;
233 foreach(@{ $this->{TL_COMPONENTS} }) {
234 $_->tl_destroy();
235 }
236 undef $this->{TL_BREAKS};
237 undef $this->{TL_COMPONENTS};
238 $this->SUPER::tl_destroy();
239 }
240  
241 sub getAllChildren {
242 my ($this, $what) = @_;
243  
244 my @children;
245 if (scalar(@{ $this->{TL_COMPONENTS} })) {
246 foreach my $child (@{ $this->{TL_COMPONENTS} }) {
247 push @children, $child if (! $what or $child->isa($what));
248 push @children, $child->getAllChildren($what)
249 if $child->isa("HTML::TableLayout::ComponentContainer");
250 }
251 }
252 return @children;
253 }
254
255
256  
257 # ---------------------------------------------------------------------
258 ## clearly this is not what I meant... FIXME!
259 package HTML::TableLayout::ComponentCell;
260 @HTML::TableLayout::ComponentCell::ISA=qw(HTML::TableLayout::Cell);
261  
262 # ---------------------------------------------------------------------
263  
264 package HTML::TableLayout::ComponentTable;
265 @HTML::TableLayout::ComponentTable::ISA=qw(HTML::TableLayout::Table);
266  
267  
268 # ---------------------------------------------------------------------
269  
270 package HTML::TableLayout::Component::Text;
271 use HTML::TableLayout::Symbols;
272 use Carp;
273 @HTML::TableLayout::Component::Text::ISA=qw(HTML::TableLayout::Component);
274  
275 my %MARKUP = (bold => "B",
276 italic => "I",
277 big => "BIG",
278 small => "SMALL");
279  
280  
281 sub tl_init {
282 my $this = shift;
283 $this->{text} = shift;
284 $this->SUPER::tl_init(@_);
285 }
286  
287  
288 sub tl_getParameters {
289 my ($this) = @_;
290
291 confess("WAS DESTROYED") if $this->{WAS_DESTROYED};
292 confess("TL_PARAMS undef [$this]") unless $this->{TL_PARAMS};
293 confess("TL_WINDOW undef [$this]") unless $this->{TL_WINDOW};
294  
295 my %params = ($this->{TL_WINDOW}->{PARAMETERS}->get($this),
296 %{ $this->{TL_PARAMS} });
297 foreach("italic","bold", "big", "small") {
298 if (exists $params{$_}) {
299 delete $params{$_};
300 push @{ $this->{markup} }, $MARKUP{$_};
301 }
302 }
303 return (%params);
304 }
305  
306 sub tl_print {
307 my ($this) = @_;
308 my $w = $this->{TL_WINDOW};
309 my %p = $this->tl_getParameters();
310 $w->i_print();
311 my $m;
312 foreach $m (@{ $this->{markup} }) {
313 $w->f_print("><$m");
314 }
315
316 $w->f_print("><FONT".params(%p).">");
317 if ($this->{tl_do_not_pad}) {
318 $w->f_print($this->{"text"});
319 }
320 else {
321 $w->f_print(" " . $this->{"text"} . " ");
322 }
323 $w->f_print("</FONT");
324
325 foreach $m (reverse @{ $this->{markup} }) {
326 $w->f_print("></$m");
327 }
328 }
329  
330 ##
331 ## Yuck. Padding of text is a messy issue after moving to the ><
332 ## style tagging... the problem is that if we don't pad, the text is
333 ## glued together unexpectedly. if i do pad, then links look bad.
334 ## This function is here so a link can tell it's text components not
335 ## to pad.
336 ##
337 sub tl_do_not_pad { shift->{tl_do_not_pad} = 1 }
338  
339 # ---------------------------------------------------------------------
340  
341 package HTML::TableLayout::Component::Image;
342 use HTML::TableLayout::Symbols;
343 @HTML::TableLayout::Component::Image::ISA=qw(HTML::TableLayout::Component);
344  
345 sub tl_init {
346 my ($this, $url, %params) = @_;
347 $this->SUPER::tl_init(%params);
348 $this->{url} = $url;
349 }
350  
351 sub tl_print {
352 my ($this, %ops) = @_;
353 my $w = $this->{TL_WINDOW};
354 my $p = params($this->tl_getParameters()) || "";
355 $w->i_print(qq{><IMG SRC="$this->{url}" $p});
356 }
357  
358 # ---------------------------------------------------------------------
359  
360 package HTML::TableLayout::Component::Link;
361 use HTML::TableLayout::Symbols;
362 @HTML::TableLayout::Component::Link::ISA
363 =qw(HTML::TableLayout::ComponentContainer);
364  
365 sub tl_init {
366 my $this = shift;
367 $this->{href} = shift;
368 $this->{anchor} = shift;
369 $this->SUPER::tl_init(@_);
370
371 if (ref $this->{anchor}) {
372 $this->{TL_COMPONENTS}->[0] = $this->{anchor};
373 }
374 else {
375 $this->{TL_COMPONENTS}->[0]
376 = HTML::TableLayout::Component::Text->new($this->{anchor});
377 }
378 if ($this->{TL_COMPONENTS}->[0]->isa("HTML::TableLayout::Component::Text")) {
379 ##
380 ## see comment for tl_do_not_pad() method of Text
381 ##
382 $this->{TL_COMPONENTS}->[0]->tl_do_not_pad();
383 }
384 }
385  
386 sub passCGI {
387 my ($this, $cgi, @pass) = @_;
388 if (! (ref $cgi eq "HASH")) { die("malformed passcgi") }
389 $this->{href} .= "?";
390 my @p = scalar(@pass) ? @pass : keys %$cgi;
391  
392 my ($k, $v);
393 foreach (@p) {
394 if (/^([^=]+)=(.*)$/) {
395 ($k, $v) = ($1, $2);
396 }
397 else {
398 ($k, $v) = ($_, $cgi->{$_});
399 }
400  
401 $this->{href} .= $k . "=" . escape_url($v) . "&";
402 }
403 return $this;
404 }
405  
406 ##
407 ## stolen from cgi.pm
408 ##
409 sub escape_url {
410 my $s = shift; $s eq undef and return undef;
411 $s=~s/([^a-zA-Z0-9_.-])/uc sprintf("%%%02x",ord($1))/eg;
412 return $s
413 }
414  
415  
416 sub tl_print {
417 my ($this, %ops) = @_;
418
419 my $w = $this->{TL_WINDOW};
420 my $p = params($this->tl_getParameters()) || "";
421 $w->i_print(qq{><A HREF="$this->{href}" $p});
422 $this->{TL_COMPONENTS}->[0]->tl_print();
423 $w->f_print("></A");
424 }
425  
426 # ---------------------------------------------------------------------
427  
428 package HTML::TableLayout::Component::Preformat;
429 use HTML::TableLayout::Symbols;
430 @HTML::TableLayout::Component::Preformat::ISA=
431 qw(HTML::TableLayout::Component);
432  
433 sub tl_init {
434 my $this = shift;
435 $this->{pre} = shift;
436 $this->SUPER::tl_init(@_);
437 }
438  
439 sub tl_print {
440 my ($this) = @_;
441 my $w = $this->{TL_WINDOW};
442 $w->i_print("><PRE>");
443 $w->f_print($this->{"pre"}."");
444 $w->i_print("</PRE");
445 }
446 # ---------------------------------------------------------------------
447  
448 package HTML::TableLayout::Component::Comment;
449 use HTML::TableLayout::Symbols;
450 @HTML::TableLayout::Component::Comment::ISA=
451 qw(HTML::TableLayout::Component);
452  
453 sub tl_init {
454 my $this = shift;
455 $this->{comment} = shift;
456 $this->SUPER::tl_init(@_);
457 }
458  
459 sub tl_print {
460 my ($this) = @_;
461 ##
462 ## This is a pretty ugly hack--note fake tag "<x>"
463 ##
464 $this->{TL_WINDOW}->i_print("><!-- " . $this->{"comment"} . " --><x");
465 }
466  
467 # ---------------------------------------------------------------------
468  
469 package HTML::TableLayout::Component::HorizontalRule;
470 use HTML::TableLayout::Symbols;
471 @HTML::TableLayout::Component::HorizontalRule::ISA=
472 qw(HTML::TableLayout::Component);
473  
474 sub tl_print {
475 my ($this) = @_;
476 $this->{TL_WINDOW}->i_print("><HR".params($this->tl_getParameters())."");
477 }
478  
479 # ---------------------------------------------------------------------
480 package HTML::TableLayout::Component::Font;
481 use HTML::TableLayout::Symbols;
482 @HTML::TableLayout::Component::Font::ISA=
483 qw(HTML::TableLayout::ComponentContainer);
484  
485  
486 sub tl_print {
487 my $this = shift;
488  
489 my %p = $this->tl_getParameters();
490 $this->{TL_WINDOW}->i_print("><FONT".params(%p)."");
491 foreach (@{ $this->{TL_COMPONENTS} }) {
492 $this->{TL_WINDOW}->_indentIncrement();
493 $_->tl_print();
494 $this->{TL_WINDOW}->_indentDecrement();
495 }
496 $this->{TL_WINDOW}->i_print("></FONT>");
497 }
498
499  
500 # ---------------------------------------------------------------------
501 package HTML::TableLayout::Component::List;
502 use HTML::TableLayout::Symbols;
503 @HTML::TableLayout::Component::List::ISA=
504 qw(HTML::TableLayout::ComponentContainer);
505  
506 sub tl_init {
507 my $this = shift;
508 $this->{numbered} = shift;
509 $this->{delimited} = shift;
510 $this->SUPER::tl_init(@_);
511 }
512  
513 sub insert {
514 my ($this, $component, $br) = @_;
515 if (! ref $component) {
516 $component = HTML::TableLayout::Component::Text->new($component);
517 }
518  
519 push @{ $this->{TL_BREAKS} }, $br;
520  
521 $this->SUPER::insert($component);
522 }
523  
524  
525  
526 sub tl_print {
527 my ($this) = @_;
528
529 my $w = $this->{TL_WINDOW};
530 my $list_denoter;
531 if ($this->{numbered}) {
532 $list_denoter = "OL";
533 }
534 else {
535 $list_denoter = "UL";
536 }
537 $w->i_print("><$list_denoter");
538 my $i;
539 foreach $i (0..$#{ $this->{TL_COMPONENTS} }) {
540 my $c = $this->{TL_COMPONENTS}->[$i];
541  
542 if ($this->{delimited} and
543 ! $c->isa("HTML::TableLayout::Component::List")) {
544 $w->f_print("><LI");
545 }
546  
547 $w->_indentIncrement();
548 $c->tl_print();
549 $w->_indentDecrement();
550  
551 ## do this if the component is a list??
552 $this->{TL_BREAKS}->[$i] and $w->f_print("><BR");
553 }
554 $w->i_print("></$list_denoter");
555 }
556  
557  
558  
559 1;