blib/lib/CGI/Toggle.pm untested

filter: Statementstmt 0.0% 0 / 71 Branchbran 0.0% 0 / 34 Conditioncond 0.0% 0 / 12 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 127 CC 18 SCAR 45.1
CC
18
Cov
0%
start_html
156.0
toggle_section
56.0
BEGIN
2.0
CRAP 91.0
1 package CGI::Toggle;
2  
3 use strict;
4 use base 'Exporter';
5 use CGI 'div','span','img','url';
6  
7 our @EXPORT = ('toggle_section',
8 'start_html');
9  
10 use constant PLUS => 'plus.png';
11 use constant MINUS => 'minus.png';
12 use constant JS => 'toggle.js';
13  
14 my $image_dir = '/gbrowse2/images/buttons';
15 my $js_dir = '/gbrowse2/js';
16  
17 my $style = <<'END';
18 .el_hidden {display:none}
19 .el_visible {display:inline;cursor:pointer}
20 .ctl_hidden {
21 cursor:pointer;
22 display:none;
23 }
24 .ctl_visible {
25 cursor:pointer;
26 display:inline;
27 }
28 .tctl { text-decoration:underline; }
29 END
30  
31 sub start_html {
32 my %args = @_ == 1 ? (-title=>shift) : @_;
33  
34 $image_dir = $args{-gbrowse_images} if defined $args{-gbrowse_images};
35 $js_dir = $args{-gbrowse_js} if defined $args{-gbrowse_js};
36  
37 delete $args{-gbrowse_images};
38 delete $args{-gbrowse_js};
39  
40 # earlier versions of CGI.pm don't support multiple -style and -script args.
41 if ($CGI::VERSION >= 3.05) {
42 if ($args{-style}) {
43 $args{-style}= [{src => $args{-style}}] if !ref $args{-style};
44 $args{-style}= [$args{-style}] if ref $args{-style} && ref $args{-style} ne 'ARRAY';
45 }
46 push @{$args{-style}},{code=>$style};
47  
48 if ($args{-script}) {
49 $args{-script} = [{src => $args{-script}}] if !ref $args{-script};
50 $args{-script} = [$args{-script}] if ref $args{-script} && ref $args{-script} ne 'ARRAY';
51 }
52  
53 push @{$args{-script}},{src=>"$js_dir/".JS};
54 }
55  
56 my $result = CGI::start_html(%args);
57  
58 if ($CGI::VERSION < 3.05) {
59 my $style_section = join '',$CGI::Q->_style({code=>$style});
60 my $script_section = join '',$CGI::Q->_script({src=>"$js_dir/".JS});
61 $result =~ s/<\/head>/$style_section\n$script_section\n<\/head>/i;
62 }
63  
64 return $result;
65 }
66  
67 sub toggle_section {
68 my %config = ref $_[0] eq 'HASH' ? %{shift()} : ();
69 my ($name,$section_title,@section_body) = @_;
70 my $visible = $config{on};
71  
72 my $plus = $config{plus_img} || "$image_dir/".PLUS;
73 my $minus = $config{minus_img} || "$image_dir/".MINUS;
74 my $class = $config{class} || 'tctl';
75  
76 my $show_ctl = div({-id=>"${name}_show",
77 -class=>'ctl_hidden',
78 -style=>$visible ? 'display:none' : 'display:inline;cursor:pointer',
79 -onClick=>"visibility('$name',1)"
80 },
81 img({-src=>$plus,-alt=>'+'}).'&nbsp;'.span({-class=>$class},$section_title));
82 my $hide_ctl = div({-id=>"${name}_hide",
83 -class=>'ctl_visible',
84 -style=>$visible ? 'display:inline;cursor:pointer' : 'display:none',
85 -onClick=>"visibility('$name',0)"
86 },
87 img({-src=>$minus,-alt=>'-'}).'&nbsp;'.span({-class=>$class},$section_title));
88 my $content = div({-id => $name,
89 -style=>$visible ? 'display:inline;cursor:pointer'
90 : 'display:none',
91 -class => 'el_visible'},
92 @section_body);
93 my @result = $config{nodiv} ? ($show_ctl.$hide_ctl,$content)
94 : div({-class=>'cusor:pointer'},$show_ctl.$hide_ctl,$content);
95 return wantarray ? @result : "@result";
96 }
97  
98 1;
99  
100 __END__