forked from webmin/webmin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathui-lib.pl
executable file
·2624 lines (2103 loc) · 77 KB
/
ui-lib.pl
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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
use vars qw($theme_no_table $ui_radio_selector_donejs $module_name
$ui_multi_select_donejs, $ui_formcount);
=head1 ui-lib.pl
Common functions for generating HTML for Webmin user interface elements.
Some example code :
use WebminCore;
init_config();
ui_print_header(undef, 'My Module', '');
print ui_form_start('save.cgi');
print ui_table_start('My form', undef, 2);
print ui_table_row('Enter your name',
ui_textbox('name', undef, 40));
print ui_table_end();
print ui_form_end([ [ undef, 'Save' ] ]);
ui_print_footer('/', 'Webmin index');
=cut
####################### utility functions
=head2 ui_link(href, text, [class], [tags])
Returns HTML for an <a href>.
=item href - Link
=item text - Text to display for link
=item class - Optional additional CSS classes to include
=item tags - Additional HTML attributes for the <a> tag.
=cut
sub ui_link
{
return &theme_ui_link(@_) if (defined(&theme_ui_link));
my ($href, $text, $class, $tags) = @_;
return ("<a class='ui_link".($class ? " ".$class : "")."' href='$href'".($tags ? " ".$tags : "").">$text</a>");
}
=head2 ui_img(src, alt, title, [class], [tags])
Returns HTML for an <img src>.
=item src - Image path and filename
=item alt - Alt text for screen readers, etc.
=item title - Element title, and tooltip when user hovers over image
=item class - Optional additional CSS classes to include
=item tags - Additional HTML attributes for the <img> tag
=cut
sub ui_img
{
return &theme_ui_img(@_) if (defined(&theme_ui_img));
my ($src, $alt, $title, $class, $tags) = @_;
return ("<img src='".$src."' class='ui_img".($class ? " ".$class : "")."' alt='$alt' ".($title ? "title='$title'" : "").($tags ? " ".$tags : "").">");
}
####################### table generation functions
=head2 ui_table_start(heading, [tabletags], [cols], [&default-tds], [right-heading])
Returns HTML for the start of a form block into which labelled inputs can
be placed. By default this is implemented as a table with another table inside
it, but themes may override this with their own layout.
The parameters are :
=item heading - Text to show at the top of the form.
=item tabletags - HTML attributes to put in the outer <table>, typically something like width=100%.
=item cols - Desired number of columns for labels and fields. Defaults to 4, but can be 2 for forms with lots of wide inputs.
=item default-tds - An optional array reference of HTML attributes for the <td> tags in each row of the table.
=item right-heading - HTML to appear in the heading, aligned to the right.
=cut
sub ui_table_start
{
return &theme_ui_table_start(@_) if (defined(&theme_ui_table_start));
my ($heading, $tabletags, $cols, $tds, $rightheading) = @_;
if (defined($main::ui_table_cols)) {
# Push on stack, for nested call
push(@main::ui_table_cols_stack, $main::ui_table_cols);
push(@main::ui_table_pos_stack, $main::ui_table_pos);
push(@main::ui_table_default_tds_stack, $main::ui_table_default_tds);
}
my $colspan = 1;
my $rv;
$rv .= "<table class='ui_table' border $tabletags>\n";
if (defined($heading) || defined($rightheading)) {
$rv .= "<tr".($tb ? " ".$tb : "")." class='ui_table_head'>";
if (defined($heading)) {
$rv .= "<td><b>$heading</b></td>"
}
if (defined($rightheading)) {
$rv .= "<td align='right'>$rightheading</td>";
$colspan++;
}
$rv .= "</tr>\n";
}
$rv .= "<tr".($cb ? " ".$cb : "")." class='ui_table_body'> <td colspan='$colspan'>".
"<table width='100%'>\n";
$main::ui_table_cols = $cols || 4;
$main::ui_table_pos = 0;
$main::ui_table_default_tds = $tds;
return $rv;
}
=head2 ui_table_end
Returns HTML for the end of a block started by ui_table_start.
=cut
sub ui_table_end
{
return &theme_ui_table_end(@_) if (defined(&theme_ui_table_end));
my $rv;
if ($main::ui_table_cols == 4 && $main::ui_table_pos) {
# Add an empty block to balance the table
$rv .= &ui_table_row(" ", " ");
}
if (@main::ui_table_cols_stack) {
$main::ui_table_cols = pop(@main::ui_table_cols_stack);
$main::ui_table_pos = pop(@main::ui_table_pos_stack);
$main::ui_table_default_tds = pop(@main::ui_table_default_tds_stack);
}
else {
$main::ui_table_cols = undef;
$main::ui_table_pos = undef;
$main::ui_table_default_tds = undef;
}
$rv .= "</table></td></tr></table>\n";
return $rv;
}
=head2 ui_table_row(label, value, [cols], [&td-tags])
Returns HTML for a row in a table started by ui_table_start, with a 1-column
label and 1+ column value. The parameters are :
=item label - Label for the input field. If this is undef, no label is displayed.
=item value - HTML for the input part of the row.
=item cols - Number of columns the value should take up, defaulting to 1.
=item td-tags - Array reference of HTML attributes for the <td> tags in this row.
=cut
sub ui_table_row
{
return &theme_ui_table_row(@_) if (defined(&theme_ui_table_row));
my ($label, $value, $cols, $tds) = @_;
$cols ||= 1;
$tds ||= $main::ui_table_default_tds;
my $rv;
if ($main::ui_table_pos+$cols+1 > $main::ui_table_cols &&
$main::ui_table_pos != 0) {
# If the requested number of cols won't fit in the number
# remaining, start a new row
my $leftover = $main::ui_table_cols - $main::ui_table_pos;
$rv .= "<td colspan='$leftover'></td>\n";
$rv .= "</tr>\n";
$main::ui_table_pos = 0;
}
$rv .= "<tr class='ui_table_row'>\n"
if ($main::ui_table_pos%$main::ui_table_cols == 0);
if (defined($label) &&
($value =~ /id="([^"]+)"/ || $value =~ /id='([^']+)'/ ||
$value =~ /id=([^>\s]+)/)) {
# Value contains an input with an ID
my $id = $1;
$label = "<label for=\""."e_escape($id)."\">$label</label>";
}
$rv .= "<td valign='top' $tds->[0] class='ui_label'><b>$label</b></td>\n"
if (defined($label));
$rv .= "<td valign='top' colspan='$cols' $tds->[1] class='ui_value'>$value</td>\n";
$main::ui_table_pos += $cols+(defined($label) ? 1 : 0);
if ($main::ui_table_pos%$main::ui_table_cols == 0) {
$rv .= "</tr>\n";
$main::ui_table_pos = 0;
}
return $rv;
}
=head2 ui_table_hr
Returns HTML for a row in a block started by ui_table_row, with a horizontal
line inside it to separate sections.
=cut
sub ui_table_hr
{
return &theme_ui_table_hr(@_) if (defined(&theme_ui_table_hr));
my $rv;
if ($ui_table_pos) {
$rv .= "</tr>\n";
$ui_table_pos = 0;
}
$rv .= "<tr class='ui_table_hr'> ".
"<td colspan='$main::ui_table_cols'><hr></td> </tr>\n";
return $rv;
}
=head2 ui_table_span(text)
Outputs a table row that spans the whole table, and contains the given text.
=cut
sub ui_table_span
{
my ($text) = @_;
return &theme_ui_table_span(@_) if (defined(&theme_ui_table_span));
my $rv;
if ($ui_table_pos) {
$rv .= "</tr>\n";
$ui_table_pos = 0;
}
$rv .= "<tr class='ui_table_span'> ".
"<td colspan='$main::ui_table_cols'>$text</td> </tr>\n";
return $rv;
}
=head2 ui_columns_start(&headings, [width-percent], [noborder], [&tdtags], [heading])
Returns HTML for the start of a multi-column table, with the given headings.
The parameters are :
=item headings - An array reference of headers for the table's columns.
=item width-percent - Desired width as a percentage, or undef to let the browser decide.
=item noborder - Set to 1 if the table should not have a border.
=item tdtags - An optional reference to an array of HTML attributes for the table's <td> tags.
=item heading - An optional heading to put above the table.
=cut
sub ui_columns_start
{
return &theme_ui_columns_start(@_) if (defined(&theme_ui_columns_start));
my ($heads, $width, $noborder, $tdtags, $title) = @_;
my $rv;
$rv .= "<table".($noborder ? "" : " border").
(defined($width) ? " width='$width%'" : "")." class='ui_columns'>\n";
if ($title) {
$rv .= "<tr".($tb ? " ".$tb : "")." class='ui_columns_heading'>".
"<td colspan='".scalar(@$heads)."'><b>$title</b></td></tr>\n";
}
$rv .= "<tr".($tb ? " ".$tb : "")." class='ui_columns_heads'>\n";
my $i;
for($i=0; $i<@$heads; $i++) {
$rv .= "<td ".$tdtags->[$i]."><b>".
($heads->[$i] eq "" ? "<br>" : $heads->[$i])."</b></td>\n";
}
$rv .= "</tr>\n";
return $rv;
}
=head2 ui_columns_row(&columns, &tdtags)
Returns HTML for a row in a multi-column table. The parameters are :
=item columns - Reference to an array containing the HTML to show in the columns for this row.
=item tdtags - An optional array reference containing HTML attributes for the row's <td> tags.
=cut
sub ui_columns_row
{
return &theme_ui_columns_row(@_) if (defined(&theme_ui_columns_row));
my ($cols, $tdtags) = @_;
my $rv;
$rv .= "<tr".($cb ? " ".$cb : "")." class='ui_columns_row'>\n";
my $i;
for($i=0; $i<@$cols; $i++) {
$rv .= "<td ".$tdtags->[$i].">".
($cols->[$i] !~ /\S/ ? "<br>" : $cols->[$i])."</td>\n";
}
$rv .= "</tr>\n";
return $rv;
}
=head2 ui_columns_header(&columns, &tdtags)
Returns HTML for a row in a multi-column table, styled as a header. Parameters
are the same as ui_columns_row.
=cut
sub ui_columns_header
{
return &theme_ui_columns_header(@_) if (defined(&theme_ui_columns_header));
my ($cols, $tdtags) = @_;
my $rv;
$rv .= "<tr".($tb ? " ".$tb : "")." class='ui_columns_header'>\n";
my $i;
for($i=0; $i<@$cols; $i++) {
$rv .= "<td ".$tdtags->[$i]."><b>".
($cols->[$i] eq "" ? "<br>" : $cols->[$i])."</b></td>\n";
}
$rv .= "</tr>\n";
return $rv;
}
=head2 ui_checked_columns_row(&columns, &tdtags, checkname, checkvalue, [checked?], [disabled], [tags])
Returns HTML for a row in a multi-column table, in which the first column
contains a checkbox. The parameters are :
=item columns - Reference to an array containing the HTML to show in the columns for this row.
=item tdtags - An optional array reference containing HTML attributes for the row's <td> tags.
=item checkname - Name for the checkbox input. Should be the same for all rows.
=item checkvalue - Value for this checkbox input.
=item checked - Set to 1 if it should be checked by default.
=item disabled - Set to 1 if the checkbox should be disabled and thus un-clickable.
=item tags - Extra HTML tags to include in the radio button.
=cut
sub ui_checked_columns_row
{
return &theme_ui_checked_columns_row(@_) if (defined(&theme_ui_checked_columns_row));
my ($cols, $tdtags, $checkname, $checkvalue, $checked, $disabled, $tags) = @_;
my $rv;
$rv .= "<tr".($cb ? " ".$cb : "")." class='ui_checked_columns'>\n";
$rv .= "<td class='ui_checked_checkbox' ".$tdtags->[0].">".
&ui_checkbox($checkname, $checkvalue, undef, $checked, $tags, $disabled).
"</td>\n";
my $i;
for($i=0; $i<@$cols; $i++) {
$rv .= "<td ".$tdtags->[$i+1].">";
if ($cols->[$i] !~ /<a\s+href|<input|<select|<textarea/) {
$rv .= "<label for=\"".
"e_escape("${checkname}_${checkvalue}")."\">";
}
$rv .= ($cols->[$i] !~ /\S/ ? "<br>" : $cols->[$i]);
if ($cols->[$i] !~ /<a\s+href|<input|<select|<textarea/) {
$rv .= "</label>";
}
$rv .= "</td>\n";
}
$rv .= "</tr>\n";
return $rv;
}
=head2 ui_radio_columns_row(&columns, &tdtags, checkname, checkvalue, [checked], [disabled], [tags])
Returns HTML for a row in a multi-column table, in which the first
column is a radio button. The parameters are :
=item columns - Reference to an array containing the HTML to show in the columns for this row.
=item tdtags - An optional array reference containing HTML attributes for the row's <td> tags.
=item checkname - Name for the radio button input. Should be the same for all rows.
=item checkvalue - Value for this radio button option.
=item checked - Set to 1 if it should be checked by default.
=item disabled - Set to 1 if the radio button should be disabled and thus un-clickable.
=item tags - Extra HTML tags to include in the radio button.
=cut
sub ui_radio_columns_row
{
return &theme_ui_radio_columns_row(@_) if (defined(&theme_ui_radio_columns_row));
my ($cols, $tdtags, $checkname, $checkvalue, $checked, $dis, $tags) = @_;
my $rv;
$rv .= "<tr".($cb ? " ".$cb : "")." class='ui_radio_columns'>\n";
$rv .= "<td class='ui_radio_radio' ".$tdtags->[0].">".
&ui_oneradio($checkname, $checkvalue, "", $checked, undef, $dis)."</td>\n";
my $i;
for($i=0; $i<@$cols; $i++) {
$rv .= "<td ".$tdtags->[$i+1].">";
if ($cols->[$i] !~ /<a\s+href|<input|<select|<textarea/) {
$rv .= "<label for=\"".
"e_escape("${checkname}_${checkvalue}")."\">";
}
$rv .= ($cols->[$i] !~ /\S/ ? "<br>" : $cols->[$i]);
if ($cols->[$i] !~ /<a\s+href|<input|<select|<textarea/) {
$rv .= "</label>";
}
$rv .= "</td>\n";
}
$rv .= "</tr>\n";
return $rv;
}
=head2 ui_columns_end
Returns HTML to end a table started by ui_columns_start.
=cut
sub ui_columns_end
{
return &theme_ui_columns_end(@_) if (defined(&theme_ui_columns_end));
return "</table>\n";
}
=head2 ui_columns_table(&headings, width-percent, &data, &types, no-sort, title, empty-msg)
Returns HTML for a complete table, typically generated internally by
ui_columns_start, ui_columns_row and ui_columns_end. The parameters are :
=item headings - An array ref of heading HTML.
=item width-percent - Preferred total width
=item data - A 2x2 array ref of table contents. Each can either be a simple string, or a hash ref like :
{ 'type' => 'group', 'desc' => 'Some section title' }
{ 'type' => 'string', 'value' => 'Foo', 'colums' => 3,
'nowrap' => 1 }
{ 'type' => 'checkbox', 'name' => 'd', 'value' => 'foo',
'label' => 'Yes', 'checked' => 1, 'disabled' => 1 }
{ 'type' => 'radio', 'name' => 'd', 'value' => 'foo', ... }
=item types - An array ref of data types, such as 'string', 'number', 'bytes' or 'date'
=item no-sort - Set to 1 to disable sorting by theme.
=item title - Text to appear above the table.
=item empty-msg - Message to display if no data.
=cut
sub ui_columns_table
{
return &theme_ui_columns_table(@_) if (defined(&theme_ui_columns_table));
my ($heads, $width, $data, $types, $nosort, $title, $emptymsg) = @_;
my $rv;
# Just show empty message if no data
if ($emptymsg && !@$data) {
$rv .= &ui_subheading($title) if ($title);
$rv .= "<span class='ui_emptymsg'><b>$emptymsg</b></span><p>\n";
return $rv;
}
# Are there any checkboxes in each column? If so, make those columns narrow
my @tds = map { "valign='top'" } @$heads;
my $maxwidth = 0;
foreach my $r (@$data) {
my $cc = 0;
foreach my $c (@$r) {
if (ref($c) &&
($c->{'type'} eq 'checkbox' || $c->{'type'} eq 'radio')) {
$tds[$cc] .= " width='5'" if ($tds[$cc] !~ /width=/);
}
$cc++;
}
$maxwidth = $cc if ($cc > $maxwidth);
}
$rv .= &ui_columns_start($heads, $width, 0, \@tds, $title);
# Add the data rows
foreach my $r (@$data) {
my $c0;
if (ref($r->[0]) && ($r->[0]->{'type'} eq 'checkbox' ||
$r->[0]->{'type'} eq 'radio')) {
# First column is special
$c0 = $r->[0];
$r = [ @$r[1..(@$r-1)] ];
}
# Turn data into HTML
my @rtds = @tds;
my @cols;
my $cn = 0;
$cn++ if ($c0);
foreach my $c (@$r) {
if (!ref($c)) {
# Plain old string
push(@cols, $c);
}
elsif ($c->{'type'} eq 'checkbox') {
# Checkbox in non-first column
push(@cols, &ui_checkbox($c->{'name'}, $c->{'value'},
$c->{'label'}, $c->{'checked'},
$c->{'tags'},
$c->{'disabled'}));
}
elsif ($c->{'type'} eq 'radio') {
# Radio button in non-first column
push(@cols, &ui_oneradio($c->{'name'}, $c->{'value'},
$c->{'label'}, $c->{'checked'},
$c->{'tags'},
$c->{'disabled'}));
}
elsif ($c->{'type'} eq 'group') {
# Header row that spans whole table
$rv .= &ui_columns_header([ $c->{'desc'} ],
[ "colspan=$width" ]);
next;
}
elsif ($c->{'type'} eq 'string') {
# A string, which might be special
push(@cols, $c->{'value'});
if ($c->{'columns'} > 1) {
splice(@rtds, $cn, $c->{'columns'},
"colspan=".$c->{'columns'});
}
if ($c->{'nowrap'}) {
$rtds[$cn] .= " nowrap";
}
$rtds[$cn] .= " ".$c->{'td'} if ($c->{'td'});
}
$cn++;
}
# Add the row
if (!$c0) {
$rv .= &ui_columns_row(\@cols, \@rtds);
}
elsif ($c0->{'type'} eq 'checkbox') {
$rv .= &ui_checked_columns_row(\@cols, \@rtds, $c0->{'name'},
$c0->{'value'}, $c0->{'checked'},
$c0->{'disabled'},
$c0->{'tags'});
}
elsif ($c0->{'type'} eq 'radio') {
$rv .= &ui_radio_columns_row(\@cols, \@rtds, $c0->{'name'},
$c0->{'value'}, $c0->{'checked'},
$c0->{'disabled'},
$c0->{'tags'});
}
}
$rv .= &ui_columns_end();
return $rv;
}
=head2 ui_form_columns_table(cgi, &buttons, select-all, &otherlinks, &hiddens, &headings, width-percent, &data, &types, no-sort, title, empty-msg, form-no)
Similar to ui_columns_table, but wrapped in a form. Parameters are :
=item cgi - URL to submit the form to.
=item buttons - An array ref of buttons at the end of the form, similar to that taken by ui_form_end.
=item select-all - If set to 1, include select all / invert links.
=item otherslinks - An array ref of other links to put at the top of the table, each of which is a 3-element hash ref of url, text and alignment (left or right).
=item hiddens - An array ref of hidden fields, each of which is a 2-element array ref containing the name and value.
=item formno - Index of this form on the page. Defaults to 0, but should be set if there is more than one form on the page.
All other parameters are the same as ui_columns_table.
=cut
sub ui_form_columns_table
{
return &theme_ui_form_columns_table(@_)
if (defined(&theme_ui_form_columns_table));
my ($cgi, $buttons, $selectall, $others, $hiddens,
$heads, $width, $data, $types, $nosort, $title, $emptymsg, $formno) = @_;
my $rv;
# Build links
my @leftlinks = map { "<a href='$_->[0]'>$_->[1]</a>" }
grep { $_->[2] ne 'right' } @$others;
my @rightlinks = map { "<a href='$_->[0]'>$_->[1]</a>" }
grep { $_->[2] eq 'right' } @$others;
my $links;
# Add select links
if (@$data) {
if ($selectall) {
my $cbname;
foreach my $r (@$data) {
foreach my $c (@$r) {
if (ref($c) && $c->{'type'} eq 'checkbox') {
$cbname = $c->{'name'};
last;
}
}
}
if ($cbname) {
unshift(@leftlinks, &select_all_link($cbname, $formno),
&select_invert_link($cbname, $formno));
}
}
}
# Turn to HTML
if (@rightlinks) {
$links = &ui_grid_table([ &ui_links_row(\@leftlinks),
&ui_links_row(\@rightlinks) ], 2, 100,
[ undef, "align='right'" ]);
}
elsif (@leftlinks) {
$links = &ui_links_row(\@leftlinks);
}
# Start the form, if we need one
if (@$data) {
$rv .= &ui_form_start($cgi, "post");
foreach my $h (@$hiddens) {
$rv .= &ui_hidden(@$h);
}
$rv .= $links;
}
# Add the table
$rv .= &ui_columns_table($heads, $width, $data, $types, $nosort, $title,
$emptymsg);
# Add form end
$rv .= $links;
if (@$data) {
$rv .= &ui_form_end($buttons);
}
return $rv;
}
####################### form generation functions
=head2 ui_form_start(script, method, [target], [tags])
Returns HTML for the start of a a form that submits to some script. The
parameters are :
=item script - CGI script to submit to, like save.cgi.
=item method - HTTP method, which must be one of 'get', 'post' or 'form-data'. If form-data is used, the target CGI must call ReadParseMime to parse parameters.
=item target - Optional target window or frame for the form.
=item tags - Additional HTML attributes for the form tag.
=cut
sub ui_form_start
{
$ui_formcount ||= 0;
return &theme_ui_form_start(@_) if (defined(&theme_ui_form_start));
my ($script, $method, $target, $tags) = @_;
my $rv;
$rv .= "<form class='ui_form' action='".&html_escape($script)."' ".
($method eq "post" ? "method='post'" :
$method eq "form-data" ?
"method='post' enctype='multipart/form-data'" :
"method='get'").
($target ? " target='$target'" : "").
($tags ? " ".$tags : "").">\n";
return $rv;
}
=head2 ui_form_end([&buttons], [width], [nojs])
Returns HTML for the end of a form, optionally with a row of submit buttons.
These are specified by the buttons parameter, which is an array reference
of array refs, with the following elements :
=item HTML value for the submit input for the button, or undef for none.
=item Text to appear on the button.
=item HTML or other inputs to appear after the button.
=item Set to 1 if the button should be disabled.
=item Additional HTML attributes to appear inside the button's input tag.
=item Don't include generated javascript for ui_opt_textbox
=cut
sub ui_form_end
{
$ui_formcount++;
return &theme_ui_form_end(@_) if (defined(&theme_ui_form_end));
my ($buttons, $width, $nojs) = @_;
my $rv;
if ($buttons && @$buttons) {
$rv .= "<table class='ui_form_end_buttons' ".($width ? " width='$width'" : "")."><tr>\n";
my $b;
foreach $b (@$buttons) {
if (ref($b)) {
$rv .= "<td".(!$width ? "" :
$b eq $buttons->[0] ? " align='left'" :
$b eq $buttons->[@$buttons-1] ?
" align='right'" : " align='center'").">".
&ui_submit($b->[1], $b->[0], $b->[3], $b->[4]).
($b->[2] ? " ".$b->[2] : "")."</td>\n";
}
elsif ($b) {
$rv .= "<td>$b</td>\n";
}
else {
$rv .= "<td> </td>\n";
}
}
$rv .= "</tr></table>\n";
}
$rv .= "</form>\n";
if ( !$nojs ) {
# When going back to a form, re-enable any text fields generated by
# ui_opt_textbox that aren't in the default state.
$rv .= "<script type='text/javascript'>\n";
$rv .= "var opts = document.getElementsByClassName('ui_opt_textbox');\n";
$rv .= "for(var i=0; i<opts.length; i++) {\n";
$rv .= " opts[i].disabled = document.getElementsByName(opts[i].name+'_def')[0].checked;\n";
$rv .= "}\n";
$rv .= "</script>\n";
}
return $rv;
}
=head2 ui_textbox(name, value, size, [disabled?], [maxlength], [tags])
Returns HTML for a text input box. The parameters are :
=item name - Name for this input.
=item value - Initial contents for the text box.
=item size - Desired width in characters.
=item disabled - Set to 1 if this text box should be disabled by default.
=item maxlength - Maximum length of the string the user is allowed to input.
=item tags - Additional HTML attributes for the <input> tag.
=cut
sub ui_textbox
{
return &theme_ui_textbox(@_) if (defined(&theme_ui_textbox));
my ($name, $value, $size, $dis, $max, $tags) = @_;
$size = &ui_max_text_width($size);
return "<input class='ui_textbox' type='text' ".
"name=\"".&html_escape($name)."\" ".
"id=\"".&html_escape($name)."\" ".
"value=\"".&html_escape($value)."\" ".
"size=$size".($dis ? " disabled='true'" : "").
($max ? " maxlength='$max'" : "").
($tags ? " ".$tags : "").">";
}
=head2 ui_filebox(name, value, size, [disabled?], [maxlength], [tags], [dir-only])
Returns HTML for a text box for choosing a file. Parameters are the same
as ui_textbox, except for the extra dir-only option which limits the chooser
to directories.
=cut
sub ui_filebox
{
return &theme_ui_filebox(@_) if (defined(&theme_ui_filebox));
my ($name, $value, $size, $dis, $max, $tags, $dironly) = @_;
return &ui_textbox($name, $value, $size, $dis, $max, $tags)." ".
&file_chooser_button($name, $dironly);
}
=head2 ui_bytesbox(name, bytes, [size], [disabled?])
Returns HTML for entering a number of bytes, but with friendly kB/MB/GB
options. May truncate values to 2 decimal points! The parameters are :
=item name - Name for this input.
=item bytes - Initial number of bytes to show.
=item size - Desired width of the text box part.
=item disabled - Set to 1 if this text box should be disabled by default.
=item tags - Additional HTML attributes for the <input> tag.
=item defaultunits - Units mode selected by default
=cut
sub ui_bytesbox
{
my ($name, $bytes, $size, $dis, $tags, $defaultunits) = @_;
my $units = 1;
if ($bytes eq '' && $defaultunits) {
$units = $defaultunits;
}
elsif ($bytes >= 10*1024*1024*1024*1024) {
$units = 1024*1024*1024*1024;
}
elsif ($bytes >= 10*1024*1024*1024) {
$units = 1024*1024*1024;
}
elsif ($bytes >= 10*1024*1024) {
$units = 1024*1024;
}
elsif ($bytes >= 10*1024) {
$units = 1024;
}
else {
$units = 1;
}
if ($bytes ne "") {
$bytes = sprintf("%.2f", ($bytes*1.0)/$units);
$bytes =~ s/\.00$//;
}
$size = &ui_max_text_width($size || 8);
return &ui_textbox($name, $bytes, $size, $dis, undef, $tags)." ".
&ui_select($name."_units", $units,
[ [ 1, "bytes" ],
[ 1024, "kB" ],
[ 1024*1024, "MB" ],
[ 1024*1024*1024, "GB" ],
[ 1024*1024*1024*1024, "TB" ] ], undef, undef, undef, $dis);
}
=head2 ui_upload(name, size, [disabled?], [tags])
Returns HTML for a file upload input, for use in a form with the form-data
method. The parameters are :
=item name - Name for this input.
=item size - Desired width in characters.
=item disabled - Set to 1 if this text box should be disabled by default.
=item tags - Additional HTML attributes for the <input> tag.
=item multiple - Set to 1 to allow uploading of multiple files
=cut
sub ui_upload
{
return &theme_ui_upload(@_) if (defined(&theme_ui_upload));
my ($name, $size, $dis, $tags, $multiple) = @_;
$size = &ui_max_text_width($size);
return "<input class='ui_upload' type='file' ".
"name=\""."e_escape($name)."\" ".
"id=\""."e_escape($name)."\" ".
"size='$size'".
($dis ? " disabled='true'" : "").
($multiple ? " multiple" : "").
($tags ? " ".$tags : "").">";
}
=head2 ui_password(name, value, size, [disabled?], [maxlength], [tags])
Returns HTML for a password text input. Parameters are the same as ui_textbox,
and behaviour is identical except that the user's input is not visible.
=cut
sub ui_password
{
return &theme_ui_password(@_) if (defined(&theme_ui_password));
my ($name, $value, $size, $dis, $max, $tags) = @_;
$size = &ui_max_text_width($size);
return "<input class='ui_password' type='password' ".
"name=\""."e_escape($name)."\" ".
"id=\""."e_escape($name)."\" ".
($value ne "" ? "value=\""."e_escape($value)."\" " : "").
"size='$size'".($dis ? " disabled='true'" : "").
($max ? " maxlength='$max'" : "").
($tags ? " ".$tags : "").">";
}
=head2 ui_hidden(name, value)
Returns HTML for a hidden field with the given name and value.
=cut
sub ui_hidden
{
return &theme_ui_hidden(@_) if (defined(&theme_ui_hidden));
my ($name, $value) = @_;
return "<input class='ui_hidden' type='hidden' ".
"name=\""."e_escape($name)."\" ".
"id=\""."e_escape($name)."\" ".
"value=\""."e_escape($value)."\">\n";
}
=head2 ui_select(name, value|&values, &options, [size], [multiple], [add-if-missing], [disabled?], [tags])
Returns HTML for a drop-down menu or multiple selection list. The parameters
are :
=item name - Name for this input.
=item value - Either a single initial value, or an array reference of values if this is a multi-select list.
=item options - An array reference of possible options. Each element can either be a scalar, or a two-element array ref containing a submitted value and displayed text.
=item size - Desired vertical size in rows, which defaults to 1. For multi-select lists, this must be set to something larger.
=item multiple - Set to 1 for a multi-select list, 0 for single.
=item add-if-missing - If set to 1, any value that is not in the list of options will be automatically added (and selected).
=item disabled - Set to 1 to disable this input.
=item tags - Additional HTML attributes for the <select> input.
=cut
sub ui_select
{
return &theme_ui_select(@_) if (defined(&theme_ui_select));
my ($name, $value, $opts, $size, $multiple, $missing, $dis, $tags) = @_;
my $rv;
$rv .= "<select class='ui_select' ".
"name=\""."e_escape($name)."\" ".
"id=\""."e_escape($name)."\" ".
($size ? " size='$size'" : "").
($multiple ? " multiple" : "").
($dis ? " disabled=true" : "").($tags ? " ".$tags : "").">\n";
my ($o, %opt, $s);
my %sel = ref($value) ? ( map { $_, 1 } @$value ) : ( $value, 1 );
foreach $o (@$opts) {
$o = [ $o ] if (!ref($o));
$rv .= "<option value=\""."e_escape($o->[0])."\"".
($sel{$o->[0]} ? " selected" : "").($o->[2] ne '' ? " ".$o->[2] : "").">".
($o->[1] || $o->[0])."</option>\n";
$opt{$o->[0]}++;
}
foreach $s (keys %sel) {
if (!$opt{$s} && $missing) {
$rv .= "<option value=\""."e_escape($s)."\"".
" selected>".($s eq "" ? " " : $s)."</option>\n";
}
}
$rv .= "</select>\n";
return $rv;
}
=head2 ui_multi_select(name, &values, &options, size, [add-if-missing], [disabled?], [options-title, values-title], [width])
Returns HTML for selecting many of many from a list. By default, this is
implemented using two <select> lists and Javascript buttons to move elements
between them. The resulting input value is \n separated.
Parameters are :
=item name - HTML name for this input.
=item values - An array reference of two-element array refs, containing the submitted values and descriptions of items that are selected by default.
=item options - An array reference of two-element array refs, containing the submitted values and descriptions of items that the user can select from.
=item size - Vertical size in rows.
=item add-if-missing - If set to 1, any entries that are in values but not in options will be added automatically.
=item disabled - Set to 1 to disable this input by default.
=item options-title - Optional text to appear above the list of options.
=item values-title - Optional text to appear above the list of selected values.
=item width - Optional width of the two lists in pixels.
=cut
sub ui_multi_select
{
return &theme_ui_multi_select(@_) if (defined(&theme_ui_multi_select));
my ($name, $values, $opts, $size, $missing, $dis,
$opts_title, $vals_title, $width) = @_;
my $rv;
my %already = map { $_->[0], $_ } @$values;
my $leftover = [ grep { !$already{$_->[0]} } @$opts ];
if ($missing) {
my %optsalready = map { $_->[0], $_ } @$opts;
push(@$opts, grep { !$optsalready{$_->[0]} } @$values);
}
if (!defined($width)) {
$width = "200";
}
my $wstyle = $width ? "style='width:$width'" : "";
if (!$main::ui_multi_select_donejs++) {
$rv .= &ui_multi_select_javascript();
}
$rv .= "<table cellpadding=0 cellspacing=0 class='ui_multi_select'>";
if (defined($opts_title)) {
$rv .= "<tr class='ui_multi_select_heads'>".
"<td><b>$opts_title</b></td> ".
"<td></td><td><b>$vals_title</b></td></tr>";