Repository navigation
Expand file tree
/
Copy pathFilterSelect.pm
More file actions
415 lines (302 loc) · 10.9 KB
/
Copy pathFilterSelect.pm
File metadata and controls
415 lines (302 loc) · 10.9 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
package WebComponent::FilterSelect;
# FilterSelect - component for a list filtered by an infix search
# $Id: FilterSelect.pm,v 1.15 2011-06-13 09:44:01 paczian Exp $
use strict;
use warnings;
use base qw( WebComponent );
1;
=pod
=head1 NAME
FilterSelect - component for a list filtered by an infix search
=head1 DESCRIPTION
WebComponent for a list filtered by an infix search
=head1 METHODS
=over 4
=item * B<new> ()
Called when the object is initialized. Expands SUPER::new.
=cut
sub new {
my $self = shift->SUPER::new(@_);
$self->{html_id} = $self->id();
$self->{values} = [];
$self->{labels} = [];
$self->{name} = "filter_select";
$self->{attributes} = [];
$self->{size} = 8;
$self->{width} = 300;
$self->{multiple} = 0;
$self->{dropdown} = 0;
$self->{auto_place_attribute_boxes} = 1;
$self->{attribute_boxes} = {};
$self->{default} = undef;
$self->{initial_text} = '';
return $self;
}
=item * B<output> ()
Returns the html output of the FilterSelect component.
=cut
sub output {
my ($self) = @_;
# no values, no select box
unless ($self->values) {
die "Filter Select called without values";
}
# check for attributes
my $values_string = join("~", @{$self->values});
$values_string =~ s/'/#/g;
my $labels_string = join("~", @{$self->labels});
$labels_string =~ s/'/#/g;
my $attribute_values = [];
my $attribute_names = [];
my $attribute_types = [];
my $sort_attributes = [];
foreach my $attribute (@{$self->attributes}) {
push(@$attribute_values, join("~", map { defined($_) ? $_ : '' } @{$attribute->{values}}));
push(@$attribute_names, $attribute->{name});
if ($attribute->{sort_attribute}) {
push(@$sort_attributes, $attribute->{name});
push(@$attribute_types, 'sort');
} else {
push(@$attribute_types, 'filter');
}
}
$attribute_values = join("|", @$attribute_values);
$attribute_types = join("~", @$attribute_types);
$attribute_names = join("~", @$attribute_names);
$attribute_values =~ s/'//g;
# create hidden fields
my $default_value = "";
if (defined($self->default())) {
$default_value = $self->default();
}
my $select = "<input type='hidden' value='" . $values_string . "' id='filter_select_values_" . $self->html_id() . "'>\n";
$select .= "<input type='hidden' value='" . $labels_string . "' id='filter_select_labels_" . $self->html_id() . "'>\n";
$select .= "<input type='hidden' value='$default_value' id='filter_select_default_" . $self->html_id() . "'>\n";
# check if attribute fields are neccessary
if (scalar(@{$self->attributes})) {
$select .= "<input type='hidden' value='" . $attribute_names . "' id='filter_select_attribute_names_" . $self->html_id() . "'>\n";
$select .= "<input type='hidden' value='" . $attribute_values . "' id='filter_select_attribute_values_" . $self->html_id() . "'>\n";
$select .= "<input type='hidden' value='" . $attribute_types . "' id='filter_select_attribute_types_" . $self->html_id() . "'>\n";
}
my $default_text = "start typing to narrow selection";
if ($self->dropdown()) {
$select .= "<input type='hidden' value='yes' id='filter_select_dropdown_" . $self->html_id() . "'>\n";
$default_text = "click to view and type to filter";
}
if (defined($self->{initialtext})) {
$default_text = $self->{initialtext};
}
# create the input field and the select box
my $multiple = "";
if ($self->multiple) {
$multiple = " multiple=multiple";
}
$select .= "<input type='hidden' id='filter_initial_text_" . $self->html_id() . "' value='$default_text'>\n";
$select .= "<input type='text' style='width: " . $self->width() . "px;' name='" . $self->name() . "_text' onkeydown='textbox_key_down(event, \"" . $self->html_id() . "\");' onkeyup='textbox_key_up(event, \"" . $self->html_id() . "\");' id='filter_select_textbox_" . $self->html_id() . "' value='".$default_text."'><br/>\n";
# hide the select if dropdown
$select .= "<div id='filter_select_" . $self->html_id() . "_div'" . ($self->dropdown() ? " style='display:none; position:absolute;'" : "") . ">";
$select .= "<select style='width: " . $self->width() . "px;' name='" . $self->name() . "'$multiple id='filter_select_" . $self->html_id(). "' size='" . $self->size() . "'>";
$select .= "</select></div>";
# we have attributes, display the attribute filter
my $attribute_boxes = {};
if (scalar(@{$self->attributes})) {
foreach my $attribute (@{$self->attributes}) {
my $attribute_box = "";
# sort attributes are handled differently
next if $attribute->{sort_attribute};
# start the fieldset for this attribute
$attribute_box .= "<td><fieldset><legend>".$attribute->{name}."</legend><table>";
# check whether this is exclusive or not
my $type = "checkbox";
if ($attribute->{exclusive}) {
$type = "radio";
$attribute_box .= "<optgroup>";
}
# create options for every possible value of the attribute
foreach my $possible_value (@{$attribute->{possible_values}}) {
my $checked = " checked=checked";
if (ref($possible_value)) {
unless ($possible_value->[1]) {
$checked = "";
}
$possible_value = $possible_value->[0];
}
$attribute_box .= "<tr><td style='white-space: nowrap;'><input type='$type'$checked value='$possible_value' name='filter_select_" . $self->html_id() . "_" . $attribute->{name} . "' onchange='perform_attribute_filter(\"" . $self->html_id() . "\");'>$possible_value</td></tr>";
}
# close optgroup if this is exclusive
if ($attribute->{exclusive}) {
$attribute_box .= "</optgroup>";
}
# close attribute fieldset
$attribute_box .= "</table></fieldset>";
$attribute_boxes->{$attribute->{name}} = $attribute_box;
}
# check if we have any sort attributes
my $sort_box = "";
if (scalar(@$sort_attributes)) {
$sort_box .= "<fieldset><legend>sort</legend><table><optgroup>";
$sort_box .= "<tr><td style='white-space: nowrap;'><input type='radio' checked=checked value='alphabetical' name='filter_select_" . $self->html_id() . "_sort' onclick='perform_attribute_sort(\"alphabetical\", \"" . $self->html_id() . "\");'>alphabetical</td></tr>";
foreach my $attribute (@$sort_attributes) {
$sort_box .= "<tr><td><input type='radio' value='$attribute' name='filter_select_" . $self->html_id() . "_sort' onclick='perform_attribute_sort(\"" . $attribute . "\", \"" . $self->html_id() . "\");'>$attribute</td></tr>";
}
$sort_box .= "</optgroup></table></fieldset>";
$attribute_boxes->{sort} = $sort_box;
}
$self->{attribute_boxes} = $attribute_boxes;
}
if ($self->auto_place_attribute_boxes()) {
$select = "<table><tr><td>".$select."</td>";
foreach my $key (sort(keys(%$attribute_boxes))) {
$select .= "<td>".$attribute_boxes->{$key}."</td>";
}
$select .= "</tr></table>";
}
$select .= "<img src='$Conf::cgi_url/Html/clear.gif' onload='initialize_filter_select(\"" . $self->html_id() . "\");' />";
return $select;
}
=item * B<values> (I<values>)
Getter / Setter for the values attribute.
These correspond to the values in the list part of the component.
=cut
sub values {
my ($self, $values) = @_;
if (defined($values)) {
$self->{values} = $values;
}
return $self->{values};
}
=item * B<labels> (I<labels>)
Getter / Setter for the labels attribute.
These correspond to the labels in the list part of the component.
=cut
sub labels {
my ($self, $labels) = @_;
if (defined($labels)) {
$self->{labels} = $labels;
}
return $self->{labels};
}
=item * B<get_attribute_boxes> ()
Getter for the attribute_boxes attribute.
Returns a hash of the attribute boxes.
=cut
sub get_attribute_boxes {
my ($self) = @_;
return $self->{attribute_boxes};
}
=item * B<auto_place_attribute_boxes> (I<boolean>)
Getter / Setter for the auto_place_attribute_filters attribute.
If this is true (default) the attribute filters will be placed
automatically. Set this to false and call get_attribute_boxes to
place them manually.
=cut
sub auto_place_attribute_boxes {
my ($self, $value) = @_;
if (defined($value)) {
$self->{auto_place_attribute_boxes} = $value;
}
return $self->{auto_place_attribute_boxes};
}
=item * B<multiple> (I<multiple>)
Getter / Setter for the multiple attribute.
Determines whether this is a multiple select box or not.
=cut
sub multiple {
my ($self, $multiple) = @_;
if (defined($multiple)) {
$self->{multiple} = $multiple;
}
return $self->{multiple};
}
=item * B<attributes> (I<attributes>)
Getter for the attributes attribute.
This is an array of attributes for values. Each entry represents an attribute-
type and must consist of an array with an entry for every value of the select
box.
=cut
sub attributes {
my ($self, $attributes) = @_;
if (defined($attributes)) {
$self->{attributes} = $attributes;
}
return $self->{attributes};
}
=item * B<add_attribute> (I<name>, I<values>, I<possible_values>)
Adds an attribute to the attribute array. The name will be the text
displayed on the attribute filter box. The values must have one entry
for every item of the selet box. Possible values is an array that
determines the possible values of an attribute. If an entry in possible
values is an array, the first entry will determine the value, the second
a boolean whether this should be initially checked or not. Default is
checked.
=cut
sub add_attribute {
my ($self, $attribute) = @_;
push(@{$self->{attributes}}, $attribute);
return $self->{attributes};
}
=item * B<name> (I<name>)
Getter / Setter for the name attribute.
This corresponds to the name of the cgi paramter the selection of this
component will end up in.
=cut
sub name {
my ($self, $name) = @_;
if (defined($name)) {
$self->{name} = $name;
}
return $self->{name};
}
=item * B<size> (I<size>)
Getter / Setter for the size attribute.
This is the length of the list part of the component.
=cut
sub size {
my ($self, $size) = @_;
if (defined($size)) {
$self->{size} = $size;
}
return $self->{size};
}
=item * B<width> (I<width>)
Getter / Setter for the width attribute.
This determines the width of the component in pixels.
=cut
sub width {
my ($self, $width) = @_;
if (defined($width)) {
$self->{width} = $width;
}
return $self->{width};
}
sub default {
my ($self, $default) = @_;
if (defined($default)) {
$self->{default} = $default;
}
return $self->{default};
}
sub initial_text {
my ($self, $initialtext) = @_;
if (defined($initialtext)) {
$self->{initialtext} = $initialtext;
}
return $self->{initialtext};
}
sub dropdown {
my ($self, $dropdown) = @_;
if (defined($dropdown)) {
$self->{dropdown} = $dropdown;
}
return $self->{dropdown};
}
sub html_id {
my ($self, $value) = @_;
if($value) {
$self->{html_id} = $value;
}
return $self->{html_id};
}
sub require_javascript {
return ["$Conf::cgi_url/Html/FilterSelect.js"];
}