SelectionBox 6.56 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
%# BEGIN BPS TAGGED BLOCK {{{
%# 
%# COPYRIGHT:
%#  
%# This software is Copyright (c) 1996-2005 Best Practical Solutions, LLC 
%#                                          <jesse@bestpractical.com>
%# 
%# (Except where explicitly superseded by other copyright notices)
%# 
%# 
%# LICENSE:
%# 
%# This work is made available to you under the terms of Version 2 of
%# the GNU General Public License. A copy of that license should have
%# been provided with this software, but in any event can be snarfed
%# from www.gnu.org.
%# 
%# This work is distributed in the hope that it will be useful, but
%# WITHOUT ANY WARRANTY; without even the implied warranty of
%# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
%# General Public License for more details.
%# 
%# You should have received a copy of the GNU General Public License
%# along with this program; if not, write to the Free Software
%# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
%# 
%# 
%# CONTRIBUTION SUBMISSION POLICY:
%# 
%# (The following paragraph is not intended to limit the rights granted
%# to you to modify and distribute this software under the terms of
%# the GNU General Public License and is only of importance to you if
%# you choose to contribute your changes and enhancements to the
%# community by submitting them to Best Practical Solutions, LLC.)
%# 
%# By intentionally submitting any modifications, corrections or
%# derivatives to this work, or any other work intended for use with
%# Request Tracker, to Best Practical Solutions, LLC, you confirm that
%# you are the copyright holder for those contributions and you grant
%# Best Practical Solutions,  LLC a nonexclusive, worldwide, irrevocable,
%# royalty-free, perpetual, license to use, copy, create derivative
%# works based on those contributions, and sublicense and distribute
%# those contributions and any derivatives thereof.
%# 
%# END BPS TAGGED BLOCK }}}

%# The SelectionBox Widget
%# 
%# SYNOPSIS
%#
%# include javascript:
%# <& /Widgets/SelectionBox:header &>
%#
%# <%init>:
%# my $sel = $m->comp ('/Widgets/SelectionBox:new',
%#		  Action => me.html',
%#		  Name => 'my-selection',
%#		  Available => \@items,
%#                # you can do things with @{$sel->{Current}} in the 
%#                # OnSubmit callback
%#		  OnSubmit => sub { my $sel = shift; },
%#		  Selected => \@selected);
%#
%# $m->comp ('/Widgets/SelectionBox:process', %ARGS, self => $sel)
%#
%# where @items is an arrayref, each element is [value, label],
67
%# and @selected is an arrayref of selected values from @items.
68
69
%#
%# and in html:
70
%# <& /Widgets/SelectionBox:sow, self => $sel &>
71
72
73
%#
%# if the SelectionBox is created with AutoSave option, OnSubmit will be called
%# on every button clicked in non-js mode.
74
<%method header>
75
% unless ($nojs) {
76
77
<script type="text/javascript" src="<%$RT::WebPath%>/NoAuth/js/class.js"></script>
<script type="text/javascript" src="<%$RT::WebPath%>/NoAuth/js/list.js"></script>
78
% }
79
80
81
<%ARGS>
$nojs => 0
</%ARGS>
82
83
84
85
</%method>

<%method new>
<%init>
86
$ARGS{_item_map} = {map {$_->[0] => $_->[1]} @{$ARGS{Available}}};
87
88
89
90
91
92
93
94
95
96
97
98
99
100
return \%ARGS;
</%init>
</%method>

<%method process>
<%init>
unless ($ARGS{$self->{Name}.'-Submit'}) {
    # init
    $self->{Current} = $self->{Selected};
    $self->{Selected} = [];
    return;
}

$self->{Selected} = $ARGS{$self->{Name}.'-Selected'};
101
102
103
if ($self->{Selected} && !ref($self->{Selected})) {
    $self->{Selected} = [$self->{Selected}];
}
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150

if ($ARGS{fromjs}) {
    $self->{Current} = $self->{Selected};
}
else {
    my $current = $self->{Current} = $ARGS{$self->{Name}.'-Current'};
    ++$self->{Modified};
    if ($current && !ref ($current)) {
	$current = [$current];
    }

    if ($ARGS{add}) {
	my $choosed = $ARGS{$self->{Name}.'-Available'};
	for my $add (ref($choosed) ? @$choosed : $choosed) {
	    next if grep { $_ eq $add } @$current;
	    push @$current, $add;
	}
    }

    if ($ARGS{remove}) {
	my $choosed = $ARGS{$self->{Name}.'-Selected'};
	for my $del (ref($choosed) ? @$choosed : $choosed) {
	    @$current = map { $_ eq $del ? () : $_ } @$current;
	}
    }

    if ($ARGS{moveup} or $ARGS{movedown}) {
	my $offset = $ARGS{moveup} ? 1 : 0;
	my $choosed = $ARGS{$self->{Name}.'-Selected'};
	$choosed = [$choosed] unless ref ($choosed);
	my $canmove = 0; # not in the cornor
	for my $i ($ARGS{moveup} ? 0..$#{$current} : reverse 0..$#{$current}) {
	    if (grep {$_ eq $current->[$i]} @$choosed) {
		if ($canmove) {
		    splice (@$current, $i-$offset, 2,
			    @{$current}[$i+1-$offset,$i-$offset]);
		}
	    }
	    else {
		++$canmove;
	    }
	}
    }

    $self->{Current} = $current;
}

151
@{$self->{Current}} = grep { exists $self->{_item_map}{$_} } @{$self->{Current}};
152

153
if ($self->{AutoSave} or $ARGS{$self->{Name}.'-Save'}) {
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
    $self->{OnSubmit}->($self);
    delete $self->{Modified};
}

</%init>
<%ARGS>
$self => undef
</%ARGS>

</%method>

<%method current>
% for (@{$self->{Current}}) {
<input type="hidden" name="<% $self->{Name} %>-Current" value="<%$_%>">
% }
<%INIT>
</%INIT>
<%ARGS>
$self => undef
</%ARGS>

</%method>

<%method show>
178
<form method="post" action="<%$self->{Action}%>" name="SelectionBox-<% $name %>" id="SelectionBox-<% $name %>"
179
180
181
182
% unless ($nojs) {
onSubmit="list_<% $name %>.selectAll();"
% }
>
183
184
185
<input type="hidden" name="<% $self->{Name} %>-Submit" value="1">
<& SelectionBox:current, self => $self &>
<input type="hidden" name="fromjs" value="0">
Chia-liang Kao's avatar
Chia-liang Kao committed
186
<&|/l&>Available</&>:
187
<br>
Chia-liang Kao's avatar
Chia-liang Kao committed
188
<select name="<%$name%>-Available" id="<%$name%>-Available" size="<%$size%>" multiple="multiple">
189
190
191
192
193
% for (@{$self->{Available}}) {
<option value="<% $_->[0] %>"><% $_->[1] %></option>
% }
</select>
<input name="add" type="submit" value="->"/>
Chia-liang Kao's avatar
Chia-liang Kao committed
194
<select name="<%$name%>-Selected" id="<%$name%>-Selected" size="<%$size%>" multiple="multiple">
195
196
197
% for (@{$self->{Current}}) {
<option value="<% $_ %>"
% if (exists $selected{$_}) {
Jesse Vincent's avatar
Jesse Vincent committed
198
selected="selected"
199
% }
200
><% $self->{_item_map}{$_} %></option>
201
202
203
204
% }
</select>
 <input name="moveup" type="submit" value="^"/>
 <input name="movedown" type="submit" value="v"/>
Chia-liang Kao's avatar
Chia-liang Kao committed
205
 <input name="remove" type="submit" value="<&|/l&>Delete</&>"/>
206

207
% my $caption = "";
208
% unless ($self->{'AutoSave'}) {
209
210
211
212
% if ($self->{Modified}) {
% $caption = loc('Selections modified. Please save your changes');
% }
<& /Elements/Submit, Caption => loc($caption), Label => loc('Save'), Name => $name.'-Save' &>
213
% }
214
215
</form>

216
% unless ($nojs) {
217
<script type="text/javascript">
Jesse Vincent's avatar
Jesse Vincent committed
218
//<![CDATA[
219
var list_<%$name%> = new list(document.getElementById("SelectionBox-<% $name %>"), 0, "list_<%$name%>");
Jesse Vincent's avatar
Jesse Vincent committed
220
//]]>
221
</script>
222
% }
223
224
<%ARGS>
$self => undef
Chia-liang Kao's avatar
Chia-liang Kao committed
225
$size => 10
226
$nojs => 0
227
228
229
230
231
232
233
</%ARGS>
<%INIT>
my $name = $self->{Name};
my %selected = map {$_ => 1} @{$self->{Selected}};
</%INIT>

</%method>