Tabs/Heading.pm

filter: Statementstmt 26.1% 11 / 42 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 33.3% 3 / 9 Podpod 100% 6 / 6 totaltotal 26.3% 20 / 76 CC 9 SCAR 19.8
CC
9
Cov
19%
raw
12.0
text
12.0
new
6.0
CRAP 7.2
« Tabs.pm ↑ Summary Tabs/Style.pm »
1 =head1 NAME
2  
3 CGI::Widget::Tabs::Heading - Create OO tab headings for CGI::Widget::Tabs objects
4  
5  
6 =head1 SYNOPSIS
7  
8 None.
9  
10  
11 =head1 DESCRIPTION
12  
13 This module is designed to work with CGI::Widget::Tabs. You can not use this module
14 in a standalone fashion. Look at the CGI::Widget::Tabs documentation for more info.
15  
16 =head1 PUBLIC INTERFACE
17  
18 =head2 Public Class Interface
19  
20 =cut
21  
22  
23 package CGI::Widget::Tabs::Heading;
24  
25  
26 # pragmata
27 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 2 ▶use vars qw/$VERSION/;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 # Standard Perl Library and CPAN modules
30 2 ▶use HTML::Entities;
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32 2 $VERSION = "1.00";
33  
34 =head3 new
35  
36 new($proto)
37  
38 =cut
39  
40 0 ▶sub new {
Subroutine new: not called CC=2 SCAR=17.9
Pod documented
41 0 my $proto = shift;
42 0 ▶ my $class = ref($proto) || $proto;
Condition0%0/3
ref $proto || $proto
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table0%0/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
43 0 my $self = {};
44 0 bless ($self, $class);
45 0 $self->raw(0); # by default text is HTML escaped
46 0 return $self;
47 }
48  
49  
50 =head2 Public Object Interface
51  
52 These methods define the properties and behaviour of the object oriented
53 headings. Each OO heading can be tailored to specific requirements. Fresh new
54 OO headings are created by using the heading() method on a CGI::Widget::Tabs
55 object. Existing OO headings are returned by the headings() method. In the
56 tabs-demo.pl file OO headings are used as well. So look at that demo for a
57 real life example. Example:
58  
59 # create, append and return a new heading
60 my $h = $tab->heading();
61  
62 # focus on the third heading
63 my $h = ($tab->headings)[2];
64  
65  
66 The properties and behaviour of an OO heading can be set with the following
67 methods:
68  
69 =head3 class
70  
71 class(STRING)
72  
73 Overrides the widget's CSS class for this heading. This is useful if you have
74 a specific heading (e.g. "Maintenance") which always needs it's own private
75 mark up. If the optional argument STRING is given, the class for this heading
76 is set. Otherwise it is retrieved.
77  
78 =cut
79  
80 0 ▶sub class {
Subroutine class: not called CC=2 SCAR=17.9
Pod documented
81 #
82 # Specific heading class overriding the default widget class
83 #
84 0 my $self = shift;
85 0 ▶ if ( @_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
86 0 $self->{class} = shift;
87 }
88 0 return $self->{class};
89 }
90  
91 =head3 key
92  
93 key(STRING)
94  
95 Sets/returns the value to use for this heading in the CGI query param list.
96 This is similar to the use of keys in key/value pairs in the headings()
97 method. The goal is to simplify programming logic and shorten the URL's. (See
98 the headings() method elsewhere in this document for further explanation).
99 Example:
100  
101 # display the full heading...
102 # ...but use a small key as query param value
103 $h->text("Remote Configurations");
104 $h->key("rc");
105  
106 In contrast to the use of key/value pairs, CGI::Widget::Tabs knows that this
107 is a key and not a value. After all, you are using the key() method, right?
108 Consequently you don't need the prepend the key with a hyphen ("-"). You may
109 consider using a hyphen for your keys nevertheless. It will lead to more
110 transparent code. Observe how the snippet from above with a prepended "-"
111 will later on result in the following check:
112  
113 if ( $tab->active eq "-rc" ) { # clearly we are using keys ....
114  
115 Consider this a mild suggestion.
116  
117 =cut
118  
119 0 ▶sub key {
Subroutine key: not called CC=2 SCAR=17.9
Pod documented
120 #
121 # The key to identify this heading with
122 #
123 0 my $self = shift;
124 0 ▶ if ( @_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
125 0 $self->{key} = shift;
126 }
127 0 return $self->{key};
128 }
129  
130  
131 =head3 raw
132  
133 raw(BOOLEAN)
134  
135 The heading text will normally be HTML encoded. If you wish you can use
136 hard coded HTML. To avoid escaping this HTML, you need to set raw() to a
137 logical TRUE. This is usually a 1 (one). Setting it to FALSE (usually a 0)
138 will re-enable HTML encoding. The optional argument BOOLEAN can be any
139 argument evaluating to a logical value. Setting raw() will not take effect
140 until the widget is rendered. So it does not matter when you set it, as long
141 as you haven't rendered the widget. Examples:
142  
143 # HTML encoded
144 $h1->text("Names A > L");
145 $h2->text("Names M < Z");
146  
147 # Raw
148 $h1->text("Names A &gt; L");
149 $h1->raw(1);
150  
151 $h2->text("Names M &lt; Z");
152 $h2->raw(1);
153  
154 # get the encoding setting of the fourth element
155 my $h = ($tab->headings)[3];
156 my $raw = $h->raw;
157  
158  
159 =cut
160  
161 0 ▶sub raw {
Subroutine raw: not called CC=3 SCAR=24.8
Pod documented
162 #
163 # Raw or HTML escaped?
164 #
165 0 my $self = shift;
166 0 my $arg = shift;
167  
168 0 ▶ if ( defined $arg ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $arg) 0 0 0
169 0 ▶ $self->{raw} = $arg ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$arg ? : 0 0 0
170 }
171 0 return $self->{raw};
172 }
173  
174  
175 =head3 text
176  
177 text(STRING)
178  
179 Sets/returns the heading text. If the optional argument STRING is given, the
180 text will be set otherwise it will be returned. The heading text will be HTML
181 encoded unless explicitely told otherwise (see: raw()). Examples:
182  
183 # set heading text for the first two headings
184 ($tab->headings)[0]->text("Names A > L");
185 ($tab->headings)[1]->text("Names M < Z");
186  
187 # get the text of the 4th heading
188 my $text = ($tab->headings)[3]->text;
189  
190  
191 =cut
192  
193 0 ▶sub text {
Subroutine text: not called CC=3 SCAR=24.8
Pod documented
194 #
195 # Text to be displayed
196 #
197 0 my $self = shift;
198 0 my $text;
199  
200 0 ▶ if ( @_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
201 0 $self->{text} = shift;
202 }
203 0 ▶ if ( $self->raw ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->raw) { } 0 0 0
204 0 $text = $self->{text};
205 } else {
206 0 $text = HTML::Entities::encode_entities( $self->{text} );
207 }
208 0 return $text;
209 }
210  
211  
212 =head3 url
213  
214 url(STRING)
215  
216 Overrides the self referencing URL for this heading. If the optional argument
217 STRING is given the URL is set. Otherwise it is returned. The URL is used
218 exactly as given. This means that any query params and values need to be
219 added explicitely. If a URL is not set, the heading will get a default self
220 referencing URL. For trivial applications, you will mostly be using this one.
221 Note that generating the self referencing URL will be delayed until the tab
222 widget it rendered. This means it will not be returned by the url() method.
223 Example:
224  
225 $h->url("www.someremotesite.com"); # go somewhere else
226  
227 my $url = $h->url; # return the URL
228  
229 =cut
230  
231 0 ▶sub url {
Subroutine url: not called CC=2 SCAR=17.9
Pod documented
232 #
233 # The redirect URL where this tab heading points to
234 #
235 0 my $self = shift;
236 0 ▶ if ( @_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
237 0 $self->{url} = shift;
238 }
239 0 return $self->{url};
240 }
241  
242  
243  
244  
245  
246 2 1;
247  
248 __END__
« Tabs.pm ↑ Summary Tabs/Style.pm »