1 %# BEGIN BPS TAGGED BLOCK {{{
5 %# This software is Copyright (c) 1996-2007 Best Practical Solutions, LLC
6 %# <jesse@bestpractical.com>
8 %# (Except where explicitly superseded by other copyright notices)
13 %# This work is made available to you under the terms of Version 2 of
14 %# the GNU General Public License. A copy of that license should have
15 %# been provided with this software, but in any event can be snarfed
18 %# This work is distributed in the hope that it will be useful, but
19 %# WITHOUT ANY WARRANTY; without even the implied warranty of
20 %# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 %# General Public License for more details.
23 %# You should have received a copy of the GNU General Public License
24 %# along with this program; if not, write to the Free Software
25 %# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
26 %# 02110-1301 or visit their web page on the internet at
27 %# http://www.gnu.org/copyleft/gpl.html.
30 %# CONTRIBUTION SUBMISSION POLICY:
32 %# (The following paragraph is not intended to limit the rights granted
33 %# to you to modify and distribute this software under the terms of
34 %# the GNU General Public License and is only of importance to you if
35 %# you choose to contribute your changes and enhancements to the
36 %# community by submitting them to Best Practical Solutions, LLC.)
38 %# By intentionally submitting any modifications, corrections or
39 %# derivatives to this work, or any other work intended for use with
40 %# Request Tracker, to Best Practical Solutions, LLC, you confirm that
41 %# you are the copyright holder for those contributions and you grant
42 %# Best Practical Solutions, LLC a nonexclusive, worldwide, irrevocable,
43 %# royalty-free, perpetual, license to use, copy, create derivative
44 %# works based on those contributions, and sublicense and distribute
45 %# those contributions and any derivatives thereof.
47 %# END BPS TAGGED BLOCK }}}
48 %# The SelectionBox Widget
52 %# include javascript:
53 %# <& /Widgets/SelectionBox:header &>
56 %# my $sel = $m->comp ('/Widgets/SelectionBox:new',
57 %# Action => me.html',
58 %# Name => 'my-selection',
59 %# Available => \@items,
60 %# # you can do things with @{$sel->{Current}} in the
61 %# # OnSubmit callback
62 %# OnSubmit => sub { my $sel = shift; },
63 %# Selected => \@selected);
65 %# $m->comp ('/Widgets/SelectionBox:process', %ARGS, self => $sel)
67 %# where @items is an arrayref, each element is [value, label],
68 %# and @selected is an arrayref of selected values from @items.
71 %# <& /Widgets/SelectionBox:sow, self => $sel &>
73 %# if the SelectionBox is created with AutoSave option, OnSubmit will be called
74 %# on every button clicked in non-js mode.
77 <script type="text/javascript" src="<%$RT::WebPath%>/NoAuth/js/class.js"></script>
78 <script type="text/javascript" src="<%$RT::WebPath%>/NoAuth/js/list.js"></script>
87 $ARGS{_item_map} = {map {$_->[0] => $_->[1]} @{$ARGS{Available}}};
94 unless ($ARGS{$self->{Name}.'-Submit'}) {
96 $self->{Current} = $self->{Selected};
97 $self->{Selected} = [];
101 $self->{Selected} = $ARGS{$self->{Name}.'-Selected'};
102 if ($self->{Selected} && !ref($self->{Selected})) {
103 $self->{Selected} = [$self->{Selected}];
107 $self->{Current} = $self->{Selected};
110 my $current = $self->{Current} = $ARGS{$self->{Name}.'-Current'};
112 if ($current && !ref ($current)) {
113 $current = [$current];
117 my $choosed = $ARGS{$self->{Name}.'-Available'};
118 for my $add (ref($choosed) ? @$choosed : $choosed) {
119 next if grep { $_ eq $add } @$current;
120 push @$current, $add;
125 my $choosed = $ARGS{$self->{Name}.'-Selected'};
126 for my $del (ref($choosed) ? @$choosed : $choosed) {
127 @$current = map { $_ eq $del ? () : $_ } @$current;
131 if ($ARGS{moveup} or $ARGS{movedown}) {
132 my $offset = $ARGS{moveup} ? 1 : 0;
133 my $choosed = $ARGS{$self->{Name}.'-Selected'};
134 $choosed = [$choosed] unless ref ($choosed);
135 my $canmove = 0; # not in the cornor
136 for my $i ($ARGS{moveup} ? 0..$#{$current} : reverse 0..$#{$current}) {
137 if (grep {$_ eq $current->[$i]} @$choosed) {
139 splice (@$current, $i-$offset, 2,
140 @{$current}[$i+1-$offset,$i-$offset]);
153 $self->{Current} = $current;
156 @{$self->{Current}} = grep { exists $self->{_item_map}{$_} } @{$self->{Current}};
158 if ($self->{AutoSave} or $ARGS{$self->{Name}.'-Save'}) {
159 $self->{OnSubmit}->($self);
160 delete $self->{Modified};
171 % for (@{$self->{Current}}) {
172 <input type="hidden" class="hidden" name="<% $self->{Name} %>-Current" value="<%$_%>" />
183 <form method="post" action="<%$self->{Action}%>" name="SelectionBox-<% $name %>" id="SelectionBox-<% $name %>"
185 onsubmit="list_<% $name %>.selectAll();"
188 <input type="hidden" class="hidden" name="<% $self->{Name} %>-Submit" value="1" />
189 <& SelectionBox:current, self => $self &>
190 <input type="hidden" class="hidden" name="fromjs" value="0" />
191 <&|/l&>Available</&>:
193 <select name="<%$name%>-Available" id="<%$name%>-Available" size="<%$size%>" multiple="multiple">
194 % for (@{$self->{Available}}) {
195 <option value="<% $_->[0] %>"><% $_->[1] %></option>
198 <input name="add" type="submit" class="button" value=" → " />
199 <select name="<%$name%>-Selected" id="<%$name%>-Selected" size="<%$size%>" multiple="multiple">
200 % for (@{$self->{Current}}) {
201 <option value="<% $_ %>"
202 % if (exists $selected{$_}) {
205 ><% $self->{_item_map}{$_} %></option>
208 % unless ($ARGS{'NoArrows'}) {
209 <input name="moveup" type="submit" class="button" value=" ↑ " />
210 <input name="movedown" type="submit" class="button" value=" ↓ " />
212 <input name="remove" type="submit" class="button" value="<&|/l&>Delete</&>" />
213 % if ($ARGS{'Clear'}) {
214 <input name="clear" type="submit" class="button" value="<&|/l&>Clear</&>" />
218 % unless ($self->{'AutoSave'}) {
219 % if ($self->{Modified}) {
220 % $caption = loc('Selections modified. Please save your changes');
222 <& /Elements/Submit, Caption => loc($caption), Label => loc('Save'), Name => $name.'-Save' &>
227 <script type="text/javascript">
229 var list_<%$name%> = new list(document.getElementById("SelectionBox-<% $name %>"), 0, "list_<%$name%>");
239 my $name = $self->{Name};
240 my %selected = map {$_ => 1} @{$self->{Selected}};