package HTML::Widget::Element::DoubleSelect; use warnings; use strict; use base 'HTML::Widget::Element::Select'; use Data::Dumper; __PACKAGE__->mk_accessors(qw/js_options/); =head1 NAME HTML::Widget::Element::DoubleSelect - JSAN Widget.Select Element =head1 SYNOPSIS my $e = $widget->element( 'JSAN::Widget::Select', 'foo' ); $e->comment('(Required)'); $e->label('Foo'); =head1 DESCRIPTION JSAN Widget.Select Element. If the users' JavaScript is disabled, they will see a plain Select element. If a C is set, the Textfield will contain that, otherwise it will contain the C string, but formatted like C instead of C<%m/%d/%Y>. This Element inherits from L, so it's methods are available. =head1 METHODS =head2 js_options This returns a hash-ref of options that will be passed to the js widget constructor. Options that can be set are: =over =back =head2 $self->prepare() =cut # override prepare. We do not want constraints here. sub prepare {} =head2 $self->containerize Containerize the element, label and error for later rendering. Uses HTML::Widget::Container by default, but this can be over-ridden on a class or instance basis via L. =cut sub containerize { my ( $self, $w, $value, $errors ) = @_; my $o = $self->options; my $opt = $self->js_options || {}; my $name = $self->name; #$opt->{new_right_options} ||= $name; $opt->{delimiter} ||= ','; my @options = ref $o eq 'ARRAY' ? @$o : (); my @values; if ($value) { @values = ref $value eq 'ARRAY' ? @$value : split($opt->{delimiter}, $value); } else { @values = ref $self->selected eq 'ARRAY' ? @{ $self->selected } : split($opt->{delimiter}, $self->selected || ''); } my %selected = map { $_ => 1 } @values; my @temp_options = @options; my @o = ([], []); while ( scalar @temp_options ) { my $key = shift @temp_options; my $value = shift @temp_options; my $option = HTML::Element->new( 'option', value => $key ); $option->push_content($value); push @{$o[$selected{$key} ? 1 : 0]}, $option; } my $label = $self->mk_label( $w, $self->label, $self->comment, $errors ); my $selectelm = $self->mk_tag($w, 'select', { _suffix => 'left' }); $selectelm->push_content(@{$o[0]}); $selectelm->attr( multiple => 'multiple' ) if $self->multiple; my $selectelm2 = $self->mk_tag($w, 'select', { _suffix => 'right' }); $selectelm2->push_content(@{$o[1]}); $selectelm2->attr( name => $name ); $selectelm2->attr( multiple => 'multiple' ) if $self->multiple; my $class = $self->attributes->{class} ||= 'doubleselect'; my $button_left = $self->mk_input( $w, { type => 'button', value => '<', _suffix => 'button_left', } ); $button_left->attr( class => "$class\_button_left"); $button_left->attr( name => "$name\_move_left"); my $button_right = $self->mk_input( $w, { type => 'button', value => '>', _suffix => 'button_right', } ); $button_right->attr( name => "$name\_move_right"); $button_right->attr( class => "$class\_button_right"); my $buttons = HTML::Element->new('span'); $buttons->push_content($button_left, $button_right); $buttons->attr( class => "$class\_buttons" ); my %hidden; for (grep { exists $opt->{$_} && $opt->{$_} } qw/removed_left_options removed_right_options added_left_options added_right_options new_left_options new_right_options/) { $hidden{$_} = $self->mk_input( $w, { type => 'hidden', _suffix => $_ } ); $hidden{$_}->attr( class => 'hidden' ); $hidden{$_}->attr( name => $opt->{$_} ); } my $dselect = HTML::Element->new('span'); $dselect->push_content($selectelm, $buttons, $selectelm2); $dselect->push_content($_) for values %hidden; $dselect->attr( class => $class ); $dselect->attr( id => $self->id($w) ); my $e = $self->mk_error( $w, $errors ); my $js = "var opt_$name = new OptionTransfer('$name\_left', '$name');\n"; $js .= sprintf "opt_$name.setAutoSort('%s');\n", $opt->{auto_sort} ? 'true' : 'false'; $js .= "opt_$name.setDelimiter('$opt->{delimiter}');\n"; $js .= "opt_$name.setStatiOptionRegex('$opt->{static_option_regex}');\n" if $opt->{static_option_regex}; $js .= "opt_$name.saveRemovedLeftOptions('$opt->{removed_left_options}');\n" if $opt->{removed_left_options}; $js .= "opt_$name.saveRemovedRightOptions('$opt->{removed_right_options}');\n" if $opt->{removed_right_options}; $js .= "opt_$name.saveAddedLeftOptions('$opt->{added_left_options}');\n" if $opt->{added_left_options}; $js .= "opt_$name.saveAddedRightOptions('$opt->{added_right_options}');\n" if $opt->{added_right_options}; $js .= "opt_$name.saveNewLeftOptions('$opt->{new_left_options}');\n" if $opt->{new_left_options}; $js .= "opt_$name.saveNewRightOptions('$opt->{new_right_options}');\n" if $opt->{new_right_options}; $js .= "opt_$name.setTransferLeft('$name\_move_left');\n"; $js .= "opt_$name.setTransferRight('$name\_move_right');\n"; $js .= "opt_$name.init(document.forms[0]);"; return $self->container( { element => $dselect, error => $e, label => $label, javascript => $js, } ); } =head2 $self->mk_tag( $w, $tagtype, $attrs, $errors ) Creates a new tag. =cut sub mk_tag { my ( $self, $w, $tag, $attrs, $errors ) = @_; my $suffix = delete $attrs->{_suffix}; my $e = $self->SUPER::mk_tag($w, $tag, $attrs, $errors); if ($suffix) { $e->attr( id => $e->attr('id') . "_$suffix" ); $e->attr( name => $e->attr('name') . "_$suffix" ); $e->attr( class => $e->attr('class') . "_$suffix" ); } return $e; } =head2 $self->js_lib() Returns the javascript library =cut sub js_lib { return <<'END'; function addEvent(elm, evType, fn, useCapture) // addEvent and removeEvent // cross-browser event handling for IE5+, NS6 and Mozilla // By Scott Andrew { if (elm.addEventListener){ elm.addEventListener(evType, fn, useCapture); return true; } else if (elm.attachEvent){ var r = elm.attachEvent("on"+evType, fn); return r; } else { alert("Handler could not be removed"); } } // =================================================================== // Author: Matt Kruse // WWW: http://www.mattkruse.com/ // // NOTICE: You may use this code for any purpose, commercial or // private, without any further permission from the author. You may // remove this notice from your final code if you wish, however it is // appreciated by the author if at least my web site address is kept. // // You may *NOT* re-distribute this code in any way except through its // use. That means, you can include it in your product, or your web // site, or any other form where the code is actually being used. You // may not put the plain javascript up on your site for download or // include it in your javascript libraries for download. // If you wish to share this code with others, please just point them // to the URL instead. // Please DO NOT link directly to my .js files from your site. Copy // the files to your server and use them there. Thank you. // =================================================================== /* SOURCE FILE: selectbox.js */ // HISTORY // ------------------------------------------------------------------ // June 12, 2003: Modified up and down functions to support more than // one selected option /* DESCRIPTION: These are general functions to deal with and manipulate select boxes. Also see the OptionTransfer library to more easily handle transferring options between two lists COMPATABILITY: These are fairly basic functions - they should work on all browsers that support Javascript. */ // ------------------------------------------------------------------- // hasOptions(obj) // Utility function to determine if a select object has an options array // ------------------------------------------------------------------- function hasOptions(obj) { if (obj!=null && obj.options!=null) { return true; } return false; } // ------------------------------------------------------------------- // selectUnselectMatchingOptions(select_object,regex,select/unselect,true/false) // This is a general function used by the select functions below, to // avoid code duplication // ------------------------------------------------------------------- function selectUnselectMatchingOptions(obj,regex,which,only) { if (window.RegExp) { if (which == "select") { var selected1=true; var selected2=false; } else if (which == "unselect") { var selected1=false; var selected2=true; } else { return; } var re = new RegExp(regex); if (!hasOptions(obj)) { return; } for (var i=0; i (b.text+"")) { return 1; } return 0; } ); for (var i=0; i object as follows: // onDblClick="moveSelectedOptions(this,this.form.target) // This way, when the user double-clicks on a value in one box, it // will be transferred to the other (in browsers that support the // onDblClick() event handler). // ------------------------------------------------------------------- function moveSelectedOptions(from,to) { // Unselect matching options, if required if (arguments.length>3) { var regex = arguments[3]; if (regex != "") { unSelectMatchingOptions(from,regex); } } // Move them over if (!hasOptions(from)) { return; } for (var i=0; i=0; i--) { var o = from.options[i]; if (o.selected) { from.options[i] = null; } } if ((arguments.length<3) || (arguments[2]==true)) { sortSelect(from); sortSelect(to); } from.selectedIndex = -1; to.selectedIndex = -1; } // ------------------------------------------------------------------- // copySelectedOptions(select_object,select_object[,autosort(true/false)]) // This function copies options between select boxes instead of // moving items. Duplicates in the target list are not allowed. // ------------------------------------------------------------------- function copySelectedOptions(from,to) { var options = new Object(); if (hasOptions(to)) { for (var i=0; i=0; i--) { if (obj.options[i].selected) { if (i != (obj.options.length-1) && ! obj.options[i+1].selected) { swapOptions(obj,i,i+1); obj.options[i+1].selected = true; } } } } // ------------------------------------------------------------------- // removeSelectedOptions(select_object) // Remove all selected options from a list // (Thanks to Gene Ninestein) // ------------------------------------------------------------------- function removeSelectedOptions(from) { if (!hasOptions(from)) { return; } for (var i=(from.options.length-1); i>=0; i--) { var o=from.options[i]; if (o.selected) { from.options[i] = null; } } from.selectedIndex = -1; } // ------------------------------------------------------------------- // removeAllOptions(select_object) // Remove all options from a list // ------------------------------------------------------------------- function removeAllOptions(from) { if (!hasOptions(from)) { return; } for (var i=(from.options.length-1); i>=0; i--) { from.options[i] = null; } from.selectedIndex = -1; } // ------------------------------------------------------------------- // addOption(select_object,display_text,value,selected) // Add an option to a list // ------------------------------------------------------------------- function addOption(obj,text,value,selected) { if (obj!=null && obj.options!=null) { obj.options[obj.options.length] = new Option(text, value, false, selected); } } /* SOURCE FILE: OptionTransfer.js */ /* OptionTransfer.js Last Modified: 7/12/2004 DESCRIPTION: This widget is used to easily and quickly create an interface where the user can transfer choices from one select box to another. For example, when selecting which columns to show or hide in search results. This object adds value by automatically storing the values that were added or removed from each list, as well as the state of the final list. COMPATABILITY: Should work on all Javascript-compliant browsers. USAGE: // Create a new OptionTransfer object. Pass it the field names of the left // select box and the right select box. var ot = new OptionTransfer("from","to"); // Optionally tell the lists whether or not to auto-sort when options are // moved. By default, the lists will be sorted. ot.setAutoSort(true); // Optionally set the delimiter to be used to separate values that are // stored in hidden fields for the added and removed options, as well as // final state of the lists. Defaults to a comma. ot.setDelimiter("|"); // You can set a regular expression for option texts which are _not_ allowed to // be transferred in either direction ot.setStaticOptionRegex("static"); // These functions assign the form fields which will store the state of // the lists. Each one is optional, so you can pick to only store the // new options which were transferred to the right list, for example. // Each function takes the name of a HIDDEN or TEXT input field. // Store list of options removed from left list into an input field ot.saveRemovedLeftOptions("removedLeft"); // Store list of options removed from right list into an input field ot.saveRemovedRightOptions("removedRight"); // Store list of options added to left list into an input field ot.saveAddedLeftOptions("addedLeft"); // Store list of options radded to right list into an input field ot.saveAddedRightOptions("addedRight"); // Store all options existing in the left list into an input field ot.saveNewLeftOptions("newLeft"); // Store all options existing in the right list into an input field ot.saveNewRightOptions("newRight"); // IMPORTANT: This step is required for the OptionTransfer object to work // correctly. // Add a call to the BODY onLoad="" tag of the page, and pass a reference to // the form which contains the select boxes and input fields. BODY onLoad="ot.init(document.forms[0])" // ADDING ACTIONS INTO YOUR PAGE // Finally, add calls to the object to move options back and forth, either // from links in your page or from double-clicking the options themselves. // See example page, and use the following methods: ot.transferRight(); ot.transferAllRight(); ot.transferLeft(); ot.transferAllLeft(); NOTES: 1) Requires the functions in selectbox.js */ function OT_transferLeft() { moveSelectedOptions(this.right,this.left,this.autoSort,this.staticOptionRegex); this.update(); } function OT_transferRight() { moveSelectedOptions(this.left,this.right,this.autoSort,this.staticOptionRegex); this.update(); } function OT_transferAllLeft() { moveAllOptions(this.right,this.left,this.autoSort,this.staticOptionRegex); this.update(); } function OT_transferAllRight() { moveAllOptions(this.left,this.right,this.autoSort,this.staticOptionRegex); this.update(); } function OT_saveRemovedLeftOptions(f) { this.removedLeftField = f; } function OT_saveRemovedRightOptions(f) { this.removedRightField = f; } function OT_saveAddedLeftOptions(f) { this.addedLeftField = f; } function OT_saveAddedRightOptions(f) { this.addedRightField = f; } function OT_saveNewLeftOptions(f) { this.newLeftField = f; } function OT_saveNewRightOptions(f) { this.newRightField = f; } function OT_update() { var removedLeft = new Object(); var removedRight = new Object(); var addedLeft = new Object(); var addedRight = new Object(); var newLeft = new Object(); var newRight = new Object(); for (var i=0;i0) { str=str+delimiter; } str=str+val; } return str; } function OT_setDelimiter(val) { this.delimiter=val; } function OT_setAutoSort(val) { this.autoSort=val; } function OT_setStaticOptionRegex(val) { this.staticOptionRegex=val; } function OT_setTransferLeft(val) { this.transferLeftButton=val; } function OT_setTransferRight(val) { this.transferRightButton=val; } function OT_init(theform) { this.form = theform; if(!theform[this.left]){alert("OptionTransfer init(): Left select list does not exist in form!");return false;} if(!theform[this.right]){alert("OptionTransfer init(): Right select list does not exist in form!");return false;} this.left=theform[this.left]; this.right=theform[this.right]; for(var i=0;i, L, L, L L =head1 AUTHOR Jonas Alves, C =head1 LICENSE Copyright 2006, Jonas Alves. All rights reserved. This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself (L, L). =cut 1;