Repository navigation
Expand file tree
/
Copy pathWebComponent.pm
More file actions
263 lines (178 loc) · 5.5 KB
/
Copy pathWebComponent.pm
File metadata and controls
263 lines (178 loc) · 5.5 KB
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
package WebComponent;
# WebComponent - abstract web component class
# $Id: WebComponent.pm,v 1.8 2011-02-18 18:30:19 devoid Exp $
use strict;
use warnings;
use Conf;
=pod
=head1 NAME
WebComponent - abstract web component class
=head1 DESCRIPTION
This module is the abstract WebComponent class used by the web application
framework. A web component is a reusable page element inherited from this
class. Each component is assigned an unique id (to support javascript
operations on the components).
Using a web component is done by requesting the component from the application,
then setting parameters as supported by the specific component and finally
returning the html output of the component.
=head1 METHODS
=over 4
=item * B<new> (I<application>, I<id>)
Creates a new instance of the WebComponent object. The constructor requires
a reference to the web application object and an (unique) id. Unique ids
are used to manipulate html elements by javascript.
=cut
sub new {
my ($class, $application, $id, $type) = @_;
# check application
unless (ref $application and $application->isa("WebApplication")) {
die "Invalid application in __PACKAGE__ new.";
}
$id = '' unless (defined $id);
my $sv_url = "$Conf::cgi_url/seedviewer.cgi";
my $self = { 'application' => $application,
'id' => $id,
'css' => undef,
'svURL' => $sv_url,
'sigleton' => 0,
'_type' => $type,
'childComponents' => [],
'_orderedCSS' => [],
'_orderedJS' => [],
};
bless $self, $class;
return $self;
}
=pod
=item * B<application> ()
Returns the reference to the application object
=cut
sub application {
return $_[0]->{'application'};
}
=pod
=item * B<svURL> ()
Returns the Seed Viewer URL
=cut
sub svURL {
return $_[0]->{svURL};
}
=pod
=item * B<id> ()
Returns the numerical id of the web component
=cut
sub id {
my ($self, $id) = @_;
if (defined($id)) {
$self->{'id'} = $id;
$self->{'application'}->{'components'}->{$self->{_type}}->[$id] = $self;
$self->{'application'}->{'component_index'}->{$self->{_id}} = $self;
}
return $self->{'id'};
}
=pod
=item * B<get_trigger> (I<action_name>)
Returns a unique cgi action param based on the I<action_name>.
=cut
sub get_trigger {
return 'wac_'.$_[0]->id.'_'.$_[1];
}
=pod
=item * B<require_javascript> ()
Returns a reference to an array of javascript files to include into the
output page. By default the method returns the reference to an empty
array. Overload in inherited web components as needed.
=cut
sub require_javascript {
return [ ];
}
=item * B<require_javascript_ordered> ([filenames]) || (filename)
Adds a list of javascript files or a single file to an ordered list that
are required for this component. These files are always added after the
files in the component's child-components.
=cut
sub require_javascript_ordered {
my ($self, $files) = @_;
if (ref($files) eq 'ARRAY') {
push(@{$self->{"_orderedJS"}}, @$files);
} else {
push(@{$self->{"_orderedJS"}}, $files);
}
return $self->{"_orderedJS"};
}
=pod
=item * B<require_css> (I<css_file>)
Returns the name of the css file to include into the the html of the web component.
If the optional parameter I<css_file> is given, the component will require that
css file instead of the default one.
=cut
sub require_css {
if ($_[1]) {
$_[0]->{'css'} = $_[1];
}
return $_[0]->{'css'};
}
=item * B<require_css_ordered> ([filenames]) || (filename)
Adds a list of css files or a single file to an ordered list that
are required for this component. These files are always added after the
files in the component's child-components.
=cut
sub require_css_ordered {
my ($self, $files) = @_;
if (ref($files) eq 'ARRAY') {
push(@{$self->{"_orderedCSS"}}, @$files);
} else {
push(@{$self->{"_orderedCSS"}}, $files);
}
return $self->{"_orderedCSS"};
}
=pod
=item * B<output> ()
Returns the html of the web component
=cut
sub output {
die "Abstract method 'output' must be implemented in __PACKAGE__.\n";
}
=head3 JavaCall
my $string = $comp->JavaCall($name => @parms);
Format a call to a java function. The parameters will all be converted to
string literals and assembled with the method name.
=over 4
=item name
Name of the Java method to call.
=item parms
A list of strings. The strings will be converted to Javascript string literals
and specified as parameters on the method call.
=item RETURN
Returns a string that can be used as an event parameter to call the specified
Javascript method passing in the specified literal values.
=back
=cut
sub JavaCall {
# Get the parameters.
my ($self, $name, @parms) = @_;
# Quote the strings.
my @literals;
for my $parm (@parms) {
$parm =~ s/\n/\\n/g;
$parm =~ s/'/\\'/g;
push @literals, "'$parm'";
}
# Assemble the call.
my $retVal = "$name(" . join(", ", @literals) . ")";
# Return the result.
return $retVal;
}
=pod
=item * <register_component> (<CoponentName>, <id>)
Calls register_component() of WebApplication, but also
remembers the scope of the component; useful when you need
to resolve dependencies, e.g. for js or css.
=cut
sub register_component {
my ($self, $component, $id) = @_;
my $web_component = $self->application()->register_component($component, $id);
push(@{$self->{"childComponents"}}, $web_component);
return $web_component;
}
1;