summaryrefslogtreecommitdiff
path: root/httemplate/search/elements/checkbox-foot.html
blob: f33a87467b4800de24f20867ae4d67e68c48442f (plain)
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
<%doc>
<& /elements/search.html,
  # options...
  html_foot => include('elements/checkbox-foot.html',
                  actions => [
                    { label   => 'Edit selected packages',
                      onclick => 'popup_package_edit()',
                    },
                    { submit  => 'Delete selected packages',
                      confirm => 'Really delete these packages?'
                    },
                  ],
                  filter        => '.name = "pkgpart"', # see below
                  minboxes      => 2, #will remove checkboxes if there aren't at least this many
               ),
&>

This creates a footer for a search page containing a column of checkboxes.
Typically this is used to select several items from the search result and 
apply some change to all of them at once.  The footer always provides 
"select all" and "unselect all" buttons.

"actions" is an arrayref of action buttons to show.  Each element of the
array is a hashref of either:

- "submit" and, optionally, "confirm".  Creates a submit button.  The value 
of "submit" becomes the "value" property of the button (and thus its label).
If "confirm" is specified, the button will have an onclick handler that 
displays the value of "confirm" in a popup message box and asks the user to 
confirm the choice.  The hashref may also have a "name" property, which 
sets the name of the submit button.

- "onclick" and "label".  Creates a non-submit button that executes the 
Javascript code in "onclick".  "label" is used as the text of the button.

If you want only a single action, you can forget the arrayref-of-hashrefs
business and just put "submit" and "confirm" (or "onclick" and "label") 
elements in the argument list.

"filter" is a javascript expression to limit which checkboxes are included in
the "select/unselect all" actions.  By default, any input with type="checkbox"
will be included.  If this option is given, it will be evaluated with the 
HTML node in a variable named "obj".  The expression should return true or
false.

</%doc>
<DIV ID="checkbox_footer" STYLE="display:block">
<INPUT TYPE="button" VALUE="<% emt('select all') %>" onclick="setAll(true)">
<INPUT TYPE="button" VALUE="<% emt('unselect all') %>" onclick="setAll(false)">
<BR>
% foreach my $action (@$actions) {
%   if ( $action->{onclick} ) {
<INPUT TYPE="button" <% $action->{name} %> onclick="<% $action->{onclick} %>"\
  VALUE="<% $action->{label} |h%>">
%   } elsif ( $action->{submit} ) {
<INPUT TYPE="submit" <% $action->{name} %> <% $action->{confirm} %>\
  VALUE="<% $action->{submit} |h%>">
%   } # else do nothing
% } #foreach
</DIV>
<SCRIPT>
var checkboxes = [];
var inputs = document.getElementsByTagName('input');
for (var i = 0; i < inputs.length; i++) {
  var obj = inputs[i];
  if ( obj.type == "checkbox" && <% $filter %> ) {
    checkboxes.push(obj);
  }
}
%# avoid the need for "$areboxes" late-evaluation hackery
% if ($opt{'minboxes'}) {
if ( checkboxes.length < <% $opt{'minboxes'} %> ) {
  for (i = 0; i < checkboxes.length; i++) {
    checkboxes[i].parentNode.removeChild(checkboxes[i]);
  }
  checkboxes = [];
}
% }
if ( checkboxes.length == 0 ) {
  document.getElementById('checkbox_footer').style.display = 'none';
}
function setAll(setTo) {
  for (var i = 0; i < checkboxes.length; i++) {
    checkboxes[i].checked = setTo;
  }
}
function toCGIString() {
  var out = '';
  for (var i = 0; i < checkboxes.length; i++) {
    if (checkboxes[i].checked) {
      out += '&' + checkboxes[i].name + '=' + checkboxes[i].value;
    }
  }
  return out;
}
</SCRIPT>
<%init>
my %opt = @_;
my $actions = $opt{'actions'} || [ \%opt ];
foreach (@$actions) {
  $_->{confirm} &&= qq!onclick="return confirm('! . $_->{confirm} . qq!')"!;
  $_->{name} &&= qq!NAME="! . $_->{name} . qq!"!;
}
my $filter = $opt{filter} || 'true';
</%init>