Merge branch 'master' of https://github.com/jgoodman/Freeside
[freeside.git] / rt / share / html / Widgets / SelectionBox
1 %# BEGIN BPS TAGGED BLOCK {{{
2 %#
3 %# COPYRIGHT:
4 %#
5 %# This software is Copyright (c) 1996-2014 Best Practical Solutions, LLC
6 %#                                          <sales@bestpractical.com>
7 %#
8 %# (Except where explicitly superseded by other copyright notices)
9 %#
10 %#
11 %# LICENSE:
12 %#
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
16 %# from www.gnu.org.
17 %#
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.
22 %#
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/licenses/old-licenses/gpl-2.0.html.
28 %#
29 %#
30 %# CONTRIBUTION SUBMISSION POLICY:
31 %#
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.)
37 %#
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.
46 %#
47 %# END BPS TAGGED BLOCK }}}
48 %# The SelectionBox Widget
49 %# 
50 %# SYNOPSIS
51 %#
52 %# include javascript:
53 %# <& /Widgets/SelectionBox:header &>
54 %#
55 %# <%init>:
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);
64 %#
65 %# $m->comp ('/Widgets/SelectionBox:process', %ARGS, self => $sel)
66 %#
67 %# where @items is an arrayref, each element is [value, label],
68 %# and @selected is an arrayref of selected values from @items.
69 %#
70 %# and in html:
71 %# <& /Widgets/SelectionBox:show, self => $sel &>
72 %#
73 %# if the SelectionBox is created with AutoSave option, OnSubmit will be called
74 %# on every button clicked in non-js mode.
75 <%method header>
76 % unless ($nojs) {
77 <script type="text/javascript" src="<%RT->Config->Get('WebPath')%>/NoAuth/js/class.js"></script>
78 <script type="text/javascript" src="<%RT->Config->Get('WebPath')%>/NoAuth/js/list.js"></script>
79 % }
80 <%ARGS>
81 $nojs => 0
82 </%ARGS>
83 </%method>
84
85 <%method new>
86 <%init>
87 $ARGS{_item_map} = {map {$_->[0] => $_->[1]} @{$ARGS{Available}}};
88 return \%ARGS;
89 </%init>
90 </%method>
91
92 <%method process>
93 <%init>
94 unless ($ARGS{$self->{Name}.'-Submit'}) {
95     # init
96     $self->{Current} = $self->{Selected};
97     $self->{Selected} = [];
98     return;
99 }
100
101 $self->{Selected} = $ARGS{$self->{Name}.'-Selected'};
102 if ($self->{Selected} && !ref($self->{Selected})) {
103     $self->{Selected} = [$self->{Selected}];
104 }
105
106 if ($ARGS{fromjs}) {
107     $self->{Current} = $self->{Selected};
108 }
109 else {
110     my $current = $self->{Current} = $ARGS{$self->{Name}.'-Current'};
111     if ($current && !ref ($current)) {
112         $current = [$current];
113     }
114
115     unless ($self->{ReadOnly}) {
116         ++$self->{Modified};
117         if ($ARGS{add}) {
118             my $choosed = $ARGS{$self->{Name}.'-Available'};
119             for my $add (ref($choosed) ? @$choosed : $choosed) {
120                 next if grep { $_ eq $add } @$current;
121                 push @$current, $add;
122             }
123         }
124
125         if ($ARGS{remove}) {
126             my $choosed = $ARGS{$self->{Name}.'-Selected'};
127             for my $del (ref($choosed) ? @$choosed : $choosed) {
128                 @$current = map { $_ eq $del ? () : $_ } @$current;
129             }
130         }
131
132         if ($ARGS{moveup} or $ARGS{movedown}) {
133             my $offset = $ARGS{moveup} ? 1 : 0;
134             my $choosed = $ARGS{$self->{Name}.'-Selected'};
135             $choosed = [$choosed] unless ref ($choosed);
136             my $canmove = 0; # not in the cornor
137             for my $i ($ARGS{moveup} ? 0..$#{$current} : reverse 0..$#{$current}) {
138                 if (grep {$_ eq $current->[$i]} @$choosed) {
139                 if ($canmove) {
140                     splice (@$current, $i-$offset, 2,
141                         @{$current}[$i+1-$offset,$i-$offset]);
142                 }
143                 }
144                 else {
145                 ++$canmove;
146                 }
147             }
148         }
149
150         if ($ARGS{clear}) {
151             $current = [];
152         }
153
154         $self->{Current} = $current;
155     }
156 }
157
158 @{$self->{Current}} = grep { exists $self->{_item_map}{$_} } @{$self->{Current}};
159
160 if ($self->{AutoSave} or $ARGS{$self->{Name}.'-Save'}) {
161     $self->{OnSubmit}->($self);
162     delete $self->{Modified};
163 }
164
165 </%init>
166 <%ARGS>
167 $self => undef
168 </%ARGS>
169
170 </%method>
171
172 <%method current>
173 % for (@{$self->{Current}}) {
174 <input type="hidden" class="hidden" name="<% $self->{Name} %>-Current" value="<%$_%>" />
175 % }
176 <%INIT>
177 </%INIT>
178 <%ARGS>
179 $self => undef
180 </%ARGS>
181
182 </%method>
183
184 <%method show>
185 <form method="post" action="<%$self->{Action}%>" name="SelectionBox-<% $name %>" id="SelectionBox-<% $name %>"
186 % unless ($nojs) {
187 onsubmit="list_<% $name %>.selectAll();"
188 % }
189 >
190 <input type="hidden" class="hidden" name="<% $self->{Name} %>-Submit" value="1" />
191 <& SelectionBox:current, self => $self &>
192 <input type="hidden" class="hidden" name="fromjs" value="0" />
193 <&|/l&>Available</&>:
194 <br />
195 <select name="<%$name%>-Available" id="<%$name%>-Available" size="<%$size%>" multiple="multiple">
196 % for (@{$self->{Available}}) {
197 <option value="<% $_->[0] %>"><% $_->[1] %></option>
198 % }
199 </select>
200
201 % unless ($self->{ReadOnly}) {
202 <input name="add" type="submit" class="button" value=" &rarr; " />
203 % }
204
205 <select name="<%$name%>-Selected" id="<%$name%>-Selected" size="<%$size%>" multiple="multiple">
206 % for (@{$self->{Current}}) {
207 <option value="<% $_ %>"
208 % if (exists $selected{$_}) {
209 selected="selected"
210 % }
211 ><% $self->{_item_map}{$_} ||'' %></option>
212 % }
213 </select>
214 % unless ($self->{'ReadOnly'}) {
215 % unless ($ARGS{'NoArrows'}) {
216  <input name="moveup" type="submit" class="button" value=" &uarr; " />
217  <input name="movedown" type="submit" class="button" value=" &darr; " />
218 % }
219  <input name="remove" type="submit" class="button" value="<&|/l&>Delete</&>" />
220 % if ($ARGS{'Clear'}) {
221  <input name="clear" type="submit" class="button" value="<&|/l&>Clear</&>" />
222 % }
223 % if ( $ARGS{'ShowUpdate'} ) {
224  <input name="update" type="submit" class="button" value="<&|/l&>Update</&>" />
225 % }
226 % }
227
228 % my $caption = "";
229 % unless ($self->{'AutoSave'}) {
230 % if ($self->{Modified}) {
231 % $caption = loc('Selections modified. Please save your changes');
232 % }
233 <& /Elements/Submit, Caption => loc($caption), Label => loc('Save'), Name => $name.'-Save' &>
234 % }
235 </form>
236
237 % unless ($nojs) {
238 <script type="text/javascript">
239 //<![CDATA[
240 var list_<%$name%> = new list(document.getElementById("SelectionBox-<% $name %>"), 0, "list_<%$name%>");
241 //]]>
242 </script>
243 % }
244 <%ARGS>
245 $self => undef
246 $size => 10
247 $nojs => 0
248 </%ARGS>
249 <%INIT>
250 my $name = $self->{Name};
251 my %selected = map {$_ => 1} @{$self->{Selected}};
252 </%INIT>
253
254 </%method>