[Bio] / WebApplication / WebComponent.pm Repository:
ViewVC logotype

Annotation of /WebApplication/WebComponent.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.8 - (view) (download) (as text)

1 : paczian 1.1 package WebComponent;
2 :    
3 :     # WebComponent - abstract web component class
4 :    
5 : devoid 1.8 # $Id: WebComponent.pm,v 1.7 2010-09-29 16:21:51 paczian Exp $
6 : paczian 1.1
7 :     use strict;
8 :     use warnings;
9 : parrello 1.3 use FIG_Config;
10 : paczian 1.1
11 :     =pod
12 :    
13 :     =head1 NAME
14 :    
15 :     WebComponent - abstract web component class
16 :    
17 :     =head1 DESCRIPTION
18 :    
19 :     This module is the abstract WebComponent class used by the web application
20 :     framework. A web component is a reusable page element inherited from this
21 :     class. Each component is assigned an unique id (to support javascript
22 :     operations on the components).
23 :    
24 :     Using a web component is done by requesting the component from the application,
25 :     then setting parameters as supported by the specific component and finally
26 :     returning the html output of the component.
27 :    
28 :     =head1 METHODS
29 :    
30 :     =over 4
31 :    
32 :     =item * B<new> (I<application>, I<id>)
33 :    
34 :     Creates a new instance of the WebComponent object. The constructor requires
35 :     a reference to the web application object and an (unique) id. Unique ids
36 :     are used to manipulate html elements by javascript.
37 :    
38 :     =cut
39 :    
40 :     sub new {
41 : paczian 1.7 my ($class, $application, $id, $type) = @_;
42 : paczian 1.1
43 :     # check application
44 :     unless (ref $application and $application->isa("WebApplication")) {
45 : paarmann 1.2 die "Invalid application in __PACKAGE__ new.";
46 : paczian 1.1 }
47 :    
48 :     $id = '' unless (defined $id);
49 : parrello 1.4 my $sv_url = "$FIG_Config::cgi_url/seedviewer.cgi";
50 :     if (FIGRules::nmpdr_mode($application->cgi) && $FIG_Config::linkinSV) {
51 :     $sv_url = $FIG_Config::linkinSV;
52 :     }
53 : paczian 1.1 my $self = { 'application' => $application,
54 : devoid 1.8 'id' => $id,
55 :     'css' => undef,
56 : parrello 1.4 'svURL' => $sv_url,
57 : devoid 1.8 'sigleton' => 0,
58 :     '_type' => $type,
59 :     'childComponents' => [],
60 :     '_orderedCSS' => [],
61 :     '_orderedJS' => [],
62 : paczian 1.1 };
63 :     bless $self, $class;
64 :    
65 :     return $self;
66 :     }
67 :    
68 :    
69 :     =pod
70 :    
71 :     =item * B<application> ()
72 :    
73 :     Returns the reference to the application object
74 :    
75 :     =cut
76 :    
77 :     sub application {
78 :     return $_[0]->{'application'};
79 :     }
80 :    
81 :    
82 :     =pod
83 :    
84 : parrello 1.3 =item * B<svURL> ()
85 :    
86 :     Returns the Seed Viewer URL
87 :    
88 :     =cut
89 :    
90 :     sub svURL {
91 :     return $_[0]->{svURL};
92 :     }
93 :    
94 :     =pod
95 :    
96 : paczian 1.1 =item * B<id> ()
97 :    
98 :     Returns the numerical id of the web component
99 :    
100 :     =cut
101 :    
102 :     sub id {
103 : paczian 1.7 my ($self, $id) = @_;
104 :     if (defined($id)) {
105 :     $self->{'id'} = $id;
106 :     $self->{'application'}->{'components'}->{$self->{_type}}->[$id] = $self;
107 :     $self->{'application'}->{'component_index'}->{$self->{_id}} = $self;
108 :     }
109 :     return $self->{'id'};
110 : paczian 1.1 }
111 :    
112 :    
113 :     =pod
114 :    
115 :     =item * B<get_trigger> (I<action_name>)
116 :    
117 :     Returns a unique cgi action param based on the I<action_name>.
118 :    
119 :     =cut
120 :    
121 :     sub get_trigger {
122 :     return 'wac_'.$_[0]->id.'_'.$_[1];
123 :     }
124 :    
125 :     =pod
126 :    
127 :     =item * B<require_javascript> ()
128 :    
129 :     Returns a reference to an array of javascript files to include into the
130 :     output page. By default the method returns the reference to an empty
131 :     array. Overload in inherited web components as needed.
132 :    
133 :     =cut
134 :    
135 :     sub require_javascript {
136 :     return [ ];
137 :     }
138 :    
139 : devoid 1.8 =item * B<require_javascript_ordered> ([filenames]) || (filename)
140 :    
141 :     Adds a list of javascript files or a single file to an ordered list that
142 :     are required for this component. These files are always added after the
143 :     files in the component's child-components.
144 :    
145 :     =cut
146 :    
147 :     sub require_javascript_ordered {
148 :     my ($self, $files) = @_;
149 :     if (ref($files) eq 'ARRAY') {
150 :     push(@{$self->{"_orderedJS"}}, @$files);
151 :     } else {
152 :     push(@{$self->{"_orderedJS"}}, $files);
153 :     }
154 :     return $self->{"_orderedJS"};
155 :     }
156 : paczian 1.1
157 :     =pod
158 :    
159 :     =item * B<require_css> (I<css_file>)
160 :    
161 :     Returns the name of the css file to include into the the html of the web component.
162 :     If the optional parameter I<css_file> is given, the component will require that
163 :     css file instead of the default one.
164 :    
165 :     =cut
166 :    
167 :     sub require_css {
168 :     if ($_[1]) {
169 :     $_[0]->{'css'} = $_[1];
170 :     }
171 :     return $_[0]->{'css'};
172 :     }
173 :    
174 : devoid 1.8 =item * B<require_css_ordered> ([filenames]) || (filename)
175 :    
176 :     Adds a list of css files or a single file to an ordered list that
177 :     are required for this component. These files are always added after the
178 :     files in the component's child-components.
179 :    
180 :     =cut
181 :    
182 :     sub require_css_ordered {
183 :     my ($self, $files) = @_;
184 :     if (ref($files) eq 'ARRAY') {
185 :     push(@{$self->{"_orderedCSS"}}, @$files);
186 :     } else {
187 :     push(@{$self->{"_orderedCSS"}}, $files);
188 :     }
189 :     return $self->{"_orderedCSS"};
190 :     }
191 : paczian 1.1
192 :     =pod
193 :    
194 :     =item * B<output> ()
195 :    
196 :     Returns the html of the web component
197 :    
198 :     =cut
199 :    
200 :     sub output {
201 :     die "Abstract method 'output' must be implemented in __PACKAGE__.\n";
202 :     }
203 :    
204 : parrello 1.5 =head3 JavaCall
205 :    
206 :     my $string = $comp->JavaCall($name => @parms);
207 :    
208 :     Format a call to a java function. The parameters will all be converted to
209 :     string literals and assembled with the method name.
210 :    
211 :     =over 4
212 :    
213 :     =item name
214 :    
215 :     Name of the Java method to call.
216 :    
217 :     =item parms
218 :    
219 :     A list of strings. The strings will be converted to Javascript string literals
220 :     and specified as parameters on the method call.
221 :    
222 :     =item RETURN
223 :    
224 :     Returns a string that can be used as an event parameter to call the specified
225 :     Javascript method passing in the specified literal values.
226 :    
227 :     =back
228 :    
229 :     =cut
230 :    
231 :     sub JavaCall {
232 :     # Get the parameters.
233 :     my ($self, $name, @parms) = @_;
234 :     # Quote the strings.
235 :     my @literals;
236 :     for my $parm (@parms) {
237 :     $parm =~ s/\n/\\n/g;
238 :     $parm =~ s/'/\\'/g;
239 :     push @literals, "'$parm'";
240 :     }
241 :     # Assemble the call.
242 :     my $retVal = "$name(" . join(", ", @literals) . ")";
243 :     # Return the result.
244 :     return $retVal;
245 :     }
246 : paczian 1.1
247 : devoid 1.8 =pod
248 :    
249 :     =item * <register_component> (<CoponentName>, <id>)
250 :    
251 :     Calls register_component() of WebApplication, but also
252 :     remembers the scope of the component; useful when you need
253 :     to resolve dependencies, e.g. for js or css.
254 :    
255 :     =cut
256 :    
257 :     sub register_component {
258 :     my ($self, $component, $id) = @_;
259 :     my $web_component = $self->application()->register_component($component, $id);
260 :     push(@{$self->{"childComponents"}}, $web_component);
261 :     return $web_component;
262 :     }
263 :    
264 :    
265 : paczian 1.1 1;

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3