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',
27 #new code auto-detects form types (radio not yet supported)
28 #'form_elements' => [ qw( textfield1 textfield2 checkbox1 radio1 select1 ) ],
29 'form_elements' => [ qw( textfield1 textfield2 checkbox1 radio1 select1 ) ],
31 #deprecated style still works for now
32 #'form_text' => [ qw( textfield1 textfield2 ) ],
33 #'form_checkbox' => [ qw( checkbox1 ) ],
34 #'form_radio' => [ qw( radio1 ) ],
35 #'form_select' => [ qw( select1 ) ],
37 'layer_callback' => sub {
39 my $html = qq!<INPUT TYPE="hidden" NAME="layer" VALUE="$layer">!;
40 $html .= $other_stuff;
45 print '<FORM NAME=dummy STYLE="margin-top: 0; margin-bottom: 0">'.
46 '<INPUT TYPE="text" NAME="textfield1">'.
47 '<INPUT TYPE="text" NAME="textfield2">'.
48 '<INPUT TYPE="checkbox" NAME="checkbox1" VALUE="Y">'.
53 This module implements an HTML widget with multiple layers. Only one layer
54 is visible at any given time, controlled by a E<lt>SELECTE<gt> box. For an
55 example see http://www.420.am/selectlayers/
57 This HTML generated by this module uses JavaScript, but nevertheless attempts
58 to be as cross-browser as possible. The 0.05 release drops Navigator 4
59 compatibility and has been tested under Mozilla Firefox 1.0.6, MSIE 6.0,
60 Konqueror 3.3.2, and Opera 8.0.2 (2006 note: still working under newer
61 browsers such as IE7, Firefox 2.0, etc.).
65 My understanding is that forms cannot span E<lt>DIVE<gt>s elements. The
66 generated HTML will have a E<lt>/FORME<gt> tag before the layers and will
67 generate E<lt>FORME<gt> and E<lt>/FORME<gt> tags for each layer. To facilitate
68 E<lt>SUBMITE<gt> buttons located within the layers, you can pass a form name
69 and element names, and the relevant values will be copied to the layer's form.
70 See the B<form_> options below.
76 =item new KEY, VALUE, KEY, VALUE...
78 Options are passed as name/value pairs:
80 options - Hash reference of layers and labels for the E<lt>SELECTE<gt>. See
81 L<Tie::IxHash> to control ordering.
82 In HTML: E<lt>OPTION VALUE="$layer"E<gt>$labelE<lt>/OPTIONE<gt>
84 layer_callback - subroutine reference to create each layer. The layer name
85 is passed as an option in I<@_>
87 selected_layer - (optional) initially selected layer
89 form_name - (optional) Form name to copy values from. If not supplied, no
90 values will be copied.
92 form_action - Form action
94 form_elements - (optional) Array reference of form fields to copy from the
95 B<form_name> form. Field type is autodetected; currently
96 text, hidden, checkbox, and select (single) fiels are
97 supported. Select (multiple) and radio fields are not yet
100 form_text - (optional) Array reference of text (or hidden) form fields to copy
101 from the B<form_name> form.
103 form_checkbox - (optional) Array reference of checkbox form fields to copy from
104 the B<form_name> form.
106 form_radio - (optional) Array reference of radio form fields to copy from the
109 form_select - (optional) Array reference of select (not select multiple) form
110 fields to copy from the B<form_name> form.
112 fixup_callback - (optional) subroutine reference, returns supplimentary
113 JavaScript for the function described above under FORMS.
115 size - (optional) size of the E<lt>SELECTE<gt>, default 1.
117 unique_key - (optional) prepended to all JavaScript function/variable/object
118 names to avoid namespace collisions.
120 html_beween - (optional) HTML between the E<lt>SELECTE<gt> and the layers.
122 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.
127 my($proto, %options) = @_;
128 my $class = ref($proto) || $proto;
129 my $self = \%options;
130 bless($self, $class);
137 Returns HTML for the widget.
143 my $key = exists($self->{unique_key}) ? $self->{unique_key} : '';
144 my $between = exists($self->{html_between}) ? $self->{html_between} : '';
145 my $options = $self->{options};
146 my $form_action = exists($self->{form_action}) ? $self->{form_action} : '';
149 exists($self->{form_elements}) ? $self->{form_elements} : [];
151 exists($self->{form_text}) ? $self->{form_text} : [];
153 exists($self->{form_checkbox}) ? $self->{form_checkbox} : [];
155 exists($self->{form_radio}) ? $self->{form_radio} : [];
157 exists($self->{form_select}) ? $self->{form_select} : [];
160 exists($self->{under_position}) ? $self->{under_position} : 'static';
161 my $hidden = lc($under_position) eq 'absolute'
162 ? 'visibility: hidden; position: absolute; z-index: 0'
163 : 'display: none; z-index: 0';
164 #my $show = lc($under_position) eq 'absolute'
165 # ? 'visibility: visible'
168 my $html = $self->_safeonload.
170 "<SCRIPT>SafeAddOnLoad(${key}visualize)</SCRIPT>".
173 $self->_select. $between. '</FORM>'.
174 "<SCRIPT>var ${key}maxHeight = 0;</SCRIPT>";
176 #foreach my $layer ( 'konq_kludge', keys %$options ) {
177 foreach my $layer ( keys %$options ) {
182 <DIV ID="${key}d$layer" STYLE="$hidden">
187 <FORM NAME="${key}$layer" ACTION="$form_action" METHOD=POST onSubmit="${key}fixup(this)" STYLE="margin-top: 0; margin-bottom: 0">
189 foreach my $f ( @$form_elements, @$form_text, @$form_checkbox, @$form_radio, @$form_select )
192 <INPUT TYPE="hidden" NAME="$f" VALUE="">
197 $html .= &{$self->{layer_callback}}($layer);
204 if ( document.getElementById('${key}d$layer').offsetHeight > ${key}maxHeight )
205 ${key}maxHeight = document.getElementById('${key}d$layer').offsetHeight;
211 if ( $under_position eq 'absolute' ) {
214 //var max = ${key}maxHeight;
215 document.write("<DIV STYLE=\\\"position:relative; top: " + ${key}maxHeight + "px\\\">");
225 my $key = exists($self->{unique_key}) ? $self->{unique_key} : '';
226 my $form_name = $self->{form_name} or return '';
229 exists($self->{form_elements}) ? $self->{form_elements} : [];
231 exists($self->{form_text}) ? $self->{form_text} : [];
233 exists($self->{form_checkbox}) ? $self->{form_checkbox} : [];
235 exists($self->{form_radio}) ? $self->{form_radio} : [];
237 exists($self->{form_select}) ? $self->{form_select} : [];
241 function copyelement(from, to) {
242 if ( from == undefined ) {
244 } else if ( from.type == 'select-one' ) {
245 to.value = from.options[from.selectedIndex].value;
246 //alert(from + " (" + from.type + "): " + to.name + " => (" + from.selectedIndex + ") " + to.value);
247 } else if ( from.type == 'checkbox' ) {
248 if ( from.checked ) {
249 to.value = from.value;
253 // } else if ( from.type == 'radio' ) {
255 if ( from.value == undefined ) {
258 to.value = from.value;
261 //alert(from + " (" + from.type + "): " + to.name + " => " + to.value);
266 function ${key}fchanged(what) {
267 ${key}fixup(what.form);
269 function ${key}fixup(what) {\n";
271 foreach my $f ( @$form_elements ) {
272 $html .= "copyelement( document.$form_name.elements['$f'],
277 foreach my $f ( @$form_text ) {
278 $html .= "what.$f.value = document.$form_name.$f.value;\n";
281 foreach my $f ( @$form_checkbox ) {
282 $html .= "if (document.$form_name.$f.checked)
283 what.$f.value = document.$form_name.$f.value;
285 what.$f.value = '';\n"
288 foreach my $f ( @$form_radio ) {
289 $html .= "what.$f.value = '';
290 for ( i=0; i< document.$form_name.$f.length; i++ )
291 if ( document.$form_name.$f\[i].checked )
292 what.$f.value = document.$form_name.$f\[i].value;\n";
295 foreach my $f ( @$form_select ) {
296 $html .= "what.$f.value = document.$form_name.$f.options[document.$form_name.$f.selectedIndex].value;\n";
299 $html .= &{$self->{fixup_callback}}() if exists($self->{fixup_callback});
301 $html .= "}\n</SCRIPT>";
309 my $key = exists($self->{unique_key}) ? $self->{unique_key} : '';
310 my $options = $self->{options};
311 my $selected = exists($self->{selected_layer}) ? $self->{selected_layer} : '';
312 my $size = exists($self->{size}) ? $self->{size} : 1;
314 <SELECT NAME=\"${key}select\" SIZE=$size onChange=\"${key}changed(this);\">
316 foreach my $option ( keys %$options ) {
317 $html .= qq(<OPTION VALUE="$option");
318 $html .= ' SELECTED' if $option eq $selected;
319 $html .= '>'. $options->{$option}. '</OPTION>';
321 $html .= '</SELECT>';
326 my $key = exists($self->{unique_key}) ? $self->{unique_key} : '';
327 my $options = $self->{options};
329 exists($self->{under_position}) ? $self->{under_position} : 'static';
333 var ${key}layer = null;
334 function ${key}changed(what) {
335 ${key}layer = what.options[what.selectedIndex].value;\n";
336 foreach my $layer ( keys %$options ) {
337 $html .= qq( if (${key}layer == "$layer" ) {\n);
338 foreach my $not ( grep { $_ ne $layer } keys %$options ) {
339 my $element_style = "document.getElementById('${key}d$not').style";
340 if ( $under_position eq 'absolute' ) {
341 $html .= qq( $element_style.visibility = "hidden";\n);
343 $html .= qq( $element_style.display = "none";\n);
345 $html .= qq( $element_style.zIndex = 0;\n);
347 my $element_style = "document.getElementById('${key}d$layer').style";
348 if ( $under_position eq 'absolute' ) {
349 $html .= qq( $element_style.visibility = "visible";\n);
351 $html .= qq( $element_style.display = "";\n);
353 $html .= qq( $element_style.zIndex = 1;\n);
356 $html .= "}\n</SCRIPT>";
362 my $key = exists($self->{unique_key}) ? $self->{unique_key} : '';
363 return '' unless exists($self->{selected_layer});
364 my $selected = $self->{selected_layer};
366 exists($self->{under_position}) ? $self->{under_position} : 'static';
367 my $display = ( $under_position eq 'absolute' )
368 ? 'visibility = "visible"'
372 function ${key}visualize() {
373 document.getElementById('${key}d$selected').style.$display;
374 document.getElementById('${key}d$selected').style.zIndex = 1;
383 var gSafeOnload = new Array();
384 function SafeAddOnLoad(f) {
386 if (window.onload != SafeOnload) {
387 gSafeOnload[0] = window.onload;
388 window.onload = SafeOnload;
390 gSafeOnload[gSafeOnload.length] = f;
395 function SafeOnload()
397 for (var i=0;i<gSafeOnload.length;i++)
408 Ivan Kohler E<lt>ivan-selectlayers@420.amE<gt>
412 Copyright (c) 2002-2005 Ivan Kohler
414 This program is free software; you can redistribute it and/or modify it under
415 the same terms as Perl itself.
421 All the different form_* options are unnecessary, could use .type to auto-sense
423 Could give you a function or something for copying variables out of the
428 L<perl>. L<Tie::IxHash>, http://www.xs4all.nl/~ppk/js/dom.html,
429 http://javascript.about.com/library/scripts/blsafeonload.htm