1 package HTML::Widgets::SelectLayers;
10 HTML::Widgets::SelectLayers - Perl extension for selectable HTML layers
14 use HTML::Widgets::SelectLayers;
17 tie my %options, 'Tie::IxHash',
18 'value' => 'Select One',
19 'value2' => 'Select Two',
22 $widget = new HTML::Widgets::SelectLayers(
23 'options' => \%options,
24 'form_name' => 'dummy',
25 'form_action' => 'process.cgi',
26 'form_text' => [ qw( textfield1 textfield2 ) ],
27 'form_checkbox' => [ qw( checkbox1 ) ],
28 'form_radio' => [ qw( radio1 ) ],
29 'form_select' => [ qw( select1 ) ],
30 'layer_callback' => sub {
32 my $html = qq!<INPUT TYPE="hidden" NAME="layer" VALUE="$layer">!;
33 $html .= $other_stuff;
38 print '<FORM NAME=dummy STYLE="margin-top: 0; margin-bottom: 0">'.
39 '<INPUT TYPE="text" NAME="textfield1">'.
40 '<INPUT TYPE="text" NAME="textfield2">'.
41 '<INPUT TYPE="checkbox" NAME="checkbox1" VALUE="Y">'.
46 This module implements an HTML widget with multiple layers. Only one layer
47 is visible at any given time, controlled by a E<lt>SELECTE<gt> box. For an
48 example see http://www.420.am/selectlayers/
50 This HTML generated by this module uses JavaScript, but nevertheless attempts
51 to be as cross-browser as possible. The 0.05 release drops Navigator 4
52 compatibility and has been tested under Mozilla Firefox 1.0.6, MSIE 6.0,
53 Konqueror 3.3.2, and Opera 8.0.2.
57 My understanding is that forms cannot span E<lt>DIVE<gt>s elements. The
58 generated HTML will have a E<lt>/FORME<gt> tag before the layers and will
59 generate E<lt>FORME<gt> and E<lt>/FORME<gt> tags for each layer. To facilitate
60 E<lt>SUBMITE<gt> buttons located within the layers, you can pass a form name
61 and element names, and the relevant values will be copied to the layer's form.
62 See the B<form_> options below.
68 =item new KEY, VALUE, KEY, VALUE...
70 Options are passed as name/value pairs:
72 options - Hash reference of layers and labels for the E<lt>SELECTE<gt>. See
73 L<Tie::IxHash> to control ordering.
74 In HTML: E<lt>OPTION VALUE="$layer"E<gt>$labelE<lt>/OPTIONE<gt>
76 layer_callback - subroutine reference to create each layer. The layer name
77 is passed as an option in I<@_>
79 selected_layer - (optional) initially selected layer
81 form_name - (optional) Form name to copy values from. If not supplied, no
82 values will be copied.
84 form_action - Form action
86 form_text - (optional) Array reference of text (or hidden) form fields to copy
87 from the B<form_name> form.
89 form_checkbox - (optional) Array reference of checkbox form fields to copy from
90 the B<form_name> form.
92 form_radio - (optional) Array reference of radio form fields to copy from the
95 form_select - (optional) Array reference of select (not select multiple) form
96 fields to copy from the B<form_name> form.
98 fixup_callback - (optional) subroutine reference, returns supplimentary
99 JavaScript for the function described above under FORMS.
101 size - (optional) size of the E<lt>SELECTE<gt>, default 1.
103 unique_key - (optional) prepended to all JavaScript function/variable/object
104 names to avoid namespace collisions.
106 html_beween - (optional) HTML between the E<lt>SELECTE<gt> and the layers.
108 under_position - (optional) specifies the positioning of any HTML appearing after the widget. I<static>, the default, positions subsequent HTML underneath the current layer (or immediately under the select box if no layer has yet been selected), reflowing when layers are changed. I<absolute> calculates the size of the largest layer and keeps the subsequent HTML in a single position underneath it. Note that I<absolute> works by positioning subsequent HTML in a E<lt>DIVE<gt>, so you should probably close it yourself with a E<lt>/DIVE<gt> before your E<lt>/HTMLE<gt> end tag. I<absolute> is a bit experimental and might have some quirks with truncating the end of the page under IE; you might have better results by just making all your layers the exact same size at the moment.
113 my($proto, %options) = @_;
114 my $class = ref($proto) || $proto;
115 my $self = \%options;
116 bless($self, $class);
123 Returns HTML for the widget.
129 my $key = exists($self->{unique_key}) ? $self->{unique_key} : '';
130 my $between = exists($self->{html_between}) ? $self->{html_between} : '';
131 my $options = $self->{options};
132 my $form_action = exists($self->{form_action}) ? $self->{form_action} : '';
134 exists($self->{form_text}) ? $self->{form_text} : [];
136 exists($self->{form_checkbox}) ? $self->{form_checkbox} : [];
138 exists($self->{form_radio}) ? $self->{form_radio} : [];
140 exists($self->{form_select}) ? $self->{form_select} : [];
143 exists($self->{under_position}) ? $self->{under_position} : 'static';
144 my $hidden = lc($under_position) eq 'absolute'
145 ? 'visibility: hidden; position: absolute; z-index: 0'
146 : 'display: none; z-index: 0';
147 #my $show = lc($under_position) eq 'absolute'
148 # ? 'visibility: visible'
151 my $html = $self->_safeonload.
153 "<SCRIPT>SafeAddOnLoad(${key}visualize)</SCRIPT>".
156 $self->_select. $between. '</FORM>'.
157 "<SCRIPT>var ${key}maxHeight = 0;</SCRIPT>";
159 #foreach my $layer ( 'konq_kludge', keys %$options ) {
160 foreach my $layer ( keys %$options ) {
165 <DIV ID="${key}d$layer" STYLE="$hidden">
170 <FORM NAME="${key}$layer" ACTION="$form_action" METHOD=POST onSubmit="${key}fixup(this)" STYLE="margin-top: 0; margin-bottom: 0">
172 foreach my $f ( @$form_text, @$form_checkbox, @$form_radio, @$form_select )
175 <INPUT TYPE="hidden" NAME="$f" VALUE="">
180 $html .= &{$self->{layer_callback}}($layer);
187 if ( document.getElementById('${key}d$layer').offsetHeight > ${key}maxHeight )
188 ${key}maxHeight = document.getElementById('${key}d$layer').offsetHeight;
194 if ( $under_position eq 'absolute' ) {
197 //var max = ${key}maxHeight;
198 document.write("<DIV STYLE=\\\"position:relative; top: " + ${key}maxHeight + "px\\\">");
208 my $key = exists($self->{unique_key}) ? $self->{unique_key} : '';
209 my $form_name = $self->{form_name} or return '';
211 exists($self->{form_text}) ? $self->{form_text} : [];
213 exists($self->{form_checkbox}) ? $self->{form_checkbox} : [];
215 exists($self->{form_radio}) ? $self->{form_radio} : [];
217 exists($self->{form_select}) ? $self->{form_select} : [];
220 function ${key}fchanged(what) {
221 ${key}fixup(what.form);
223 function ${key}fixup(what) {\n";
225 foreach my $f ( @$form_text ) {
226 $html .= "what.$f.value = document.$form_name.$f.value;\n";
229 foreach my $f ( @$form_checkbox ) {
230 $html .= "if (document.$form_name.$f.checked)
231 what.$f.value = document.$form_name.$f.value;
233 what.$f.value = '';\n"
236 foreach my $f ( @$form_radio ) {
237 $html .= "what.$f.value = '';
238 for ( i=0; i< document.$form_name.$f.length; i++ )
239 if ( document.$form_name.$f\[i].checked )
240 what.$f.value = document.$form_name.$f\[i].value;\n";
243 foreach my $f ( @$form_select ) {
244 $html .= "what.$f.value = document.$form_name.$f.options[document.$form_name.$f.selectedIndex].value;\n";
247 $html .= &{$self->{fixup_callback}}() if exists($self->{fixup_callback});
249 $html .= "}\n</SCRIPT>";
257 my $key = exists($self->{unique_key}) ? $self->{unique_key} : '';
258 my $options = $self->{options};
259 my $selected = exists($self->{selected_layer}) ? $self->{selected_layer} : '';
260 my $size = exists($self->{size}) ? $self->{size} : 1;
262 <SELECT NAME=\"${key}select\" SIZE=$size onChange=\"${key}changed(this);\">
264 foreach my $option ( keys %$options ) {
265 $html .= qq(<OPTION VALUE="$option");
266 $html .= ' SELECTED' if $option eq $selected;
267 $html .= '>'. $options->{$option}. '</OPTION>';
269 $html .= '</SELECT>';
274 my $key = exists($self->{unique_key}) ? $self->{unique_key} : '';
275 my $options = $self->{options};
277 exists($self->{under_position}) ? $self->{under_position} : 'static';
281 var ${key}layer = null;
282 function ${key}changed(what) {
283 ${key}layer = what.options[what.selectedIndex].value;\n";
284 foreach my $layer ( keys %$options ) {
285 $html .= qq( if (${key}layer == "$layer" ) {\n);
286 foreach my $not ( grep { $_ ne $layer } keys %$options ) {
287 my $element_style = "document.getElementById('${key}d$not').style";
288 if ( $under_position eq 'absolute' ) {
289 $html .= qq( $element_style.visibility = "hidden";\n);
291 $html .= qq( $element_style.display = "none";\n);
293 $html .= qq( $element_style.zIndex = 0;\n);
295 my $element_style = "document.getElementById('${key}d$layer').style";
296 if ( $under_position eq 'absolute' ) {
297 $html .= qq( $element_style.visibility = "visible";\n);
299 $html .= qq( $element_style.display = "";\n);
301 $html .= qq( $element_style.zIndex = 1;\n);
304 $html .= "}\n</SCRIPT>";
310 my $key = exists($self->{unique_key}) ? $self->{unique_key} : '';
311 return '' unless exists($self->{selected_layer});
312 my $selected = $self->{selected_layer};
314 exists($self->{under_position}) ? $self->{under_position} : 'static';
315 my $display = ( $under_position eq 'absolute' )
316 ? 'visibility = "visible"'
320 function ${key}visualize() {
321 document.getElementById('${key}d$selected').style.$display;
322 document.getElementById('${key}d$selected').style.zIndex = 1;
331 var gSafeOnload = new Array();
332 function SafeAddOnLoad(f) {
334 if (window.onload != SafeOnload) {
335 gSafeOnload[0] = window.onload;
336 window.onload = SafeOnload;
338 gSafeOnload[gSafeOnload.length] = f;
343 function SafeOnload()
345 for (var i=0;i<gSafeOnload.length;i++)
356 Ivan Kohler E<lt>ivan-selectlayers@420.amE<gt>
360 Copyright (c) 2002-2005 Ivan Kohler
362 This program is free software; you can redistribute it and/or modify it under
363 the same terms as Perl itself.
369 All the different form_* options are unnecessary, could use .type to auto-sense
371 Could give you a function or something for copying variables out of the
376 L<perl>. L<Tie::IxHash>, http://www.xs4all.nl/~ppk/js/dom.html,
377 http://javascript.about.com/library/scripts/blsafeonload.htm