blib/lib/OpenThought.pm

filter: Statementstmt 8.3% 17 / 204 Branchbran 0.0% 0 / 72 Conditioncond 3.3% 1 / 30 MC/DCmcdc 0.0% 0 / 18 Subroutinesub 17.6% 3 / 17 Podpod 87.5% 7 / 8 totaltotal 8.0% 28 / 349 SCAR 79.6
CC
58
Cov
6%
_as_javascript
462.0
url
110.0
settings
56.0
CRAP 2862.6
« inc/ExtUtils/AutoInstall.pm ↑ Summary Makefile.PL »
1 # This file is Copyright (c) 2000-2007 Eric Andreychek. All rights reserved.
2 # For distribution terms, please see the included LICENSE file.
3  
4 package OpenThought;
5  
6 =head1 NAME
7  
8 OpenThought - An AJAX transport and helper library, making AJAX-based page updates trivial
9  
10 =head1 SYNOPSIS
11  
12 use OpenThought();
13 use CGI();
14  
15 my $OT = OpenThought->new();
16 my $q = CGI->new;
17  
18 # First, put everything you wish to give to the browser into a hash
19 my ($fields, $html, $image);
20 $fields->{'myTextBox'} = "Text Box Data";
21 $fields->{'myCheckbox'} = "true";
22 $fields->{'myRadioBtn'} = "RadioBtn2Value";
23 $fields->{'mySelectList'} = [
24 [ "text1", "value1" ],
25 [ "text2", "value2" ],
26 [ "text3", "value3" ],
27 ];
28  
29 $html->{'id_tagname'} = "<b>New HTML Code</b>";
30  
31 $image->{'image_name'} = "http://example.com/my_image.gif";
32  
33 # You can also execute JavaScript, just put it into a scalar
34 my $javascript_code = "alert('Howdy!')";
35  
36 # Then send it to the browser using:
37  
38 $OT->param( $fields );
39 $OT->param( $html );
40 $OT->param( $image );
41 $OT->focus( "myTextBox" );
42 $OT->javascript( $javascript_code );
43  
44 print $q->header:
45 print $OT->response();
46  
47 # Or use the utility method:
48 print $q->header;
49 print $OT->response( param => $fields,
50 param => $html,
51 param => $image,
52 focus => "myTextBox",
53 javascript => $javascript_code,
54 );
55  
56  
57 # In a seperate HTML file, you might have this (which is where you'd first
58 # point the browser, the HTML then calls the Perl when you click the button or
59 # select list)
60 <html>
61 <head>
62 <script src="OpenThought.js"></script>
63 </head>
64 <body>
65 <form name="my_form" onSubmit="return false">
66 <input type="text" name="myTextBox">
67 <input type="checkbox" name="myCheckbox">
68 <input type="radio" name="myRadioBtn" value="RadioBtn1value">
69 <input type="radio" name="myRadioBtn" value="RadioBtn2value">
70 <select name="mySelectList" onChange="OpenThought.CallUrl(
71 'http://example.com/my_openthought_app.pl', 'mySelectList')">
72 <span id="id_tag_name">HTML Code will go here</span>
73  
74 // Sends the current value of the textbox 'myTextBox', as well as the
75 // param 'this' with the value of 'that', to 'my_openthought_app.pl'.
76 <input type="button" onClick="OpenThought.CallUrl(
77 'http://example.com/my_openthought_app.pl', 'myTextBox', 'this=that')">
78 </form>
79 </body>
80 </html>
81  
82 =head1 DESCRIPTION
83  
84 OpenThought is a library which implements an API for AJAX communication and
85 updates. You can perform updates to form fields, HTML, call JavaScript
86 functions, and more with a trivial amount of code. OpenThought strives to
87 provide a simple yet powerful and flexible means for creating AJAX
88 applications.
89  
90 The interface is simple -- you just build a hash. Hash keys are mapped to
91 field names or id tags in the HTML. The value your hash keys contain is
92 dynamically inserted into the corresponding field (without reloading the page).
93  
94 ==head1 COMPATABILITY
95  
96 OpenThought is compatible with a wide range of browsers, including Internet
97 Explorer 4+, Netscape 4+, Mozilla/Firefox, Safari, Opera, Konqeueror, and
98 others. It detects the browsers capabilities; if the browser doesn't support
99 new functions such as XMLHttpRequest or XMLHTTP, it falls back to using
100 iframes.
101  
102 =head1 METHODS
103  
104 =cut
105  
106 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
107 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
108  
109 1 $OpenThought::VERSION="1.99.16";
110  
111 1 ▶$OpenThought::DEBUG ||= 0;
Condition50%1/2
$OpenThought::DEBUG ||= 0
l!l
01
MC/DC0%0/1
$OpenThought::DEBUG ||= 0
$OpenThought::DEBUG
Truth table50%1/2
A
A= $OpenThought::DEBUG
Aresult
11
00
112  
113 1 ▶use vars qw( $DEBUG );
Subroutine BEGIN: called CC=1 SCAR=0.0
114  
115  
116 #/-------------------------------------------------------------------------
117 # function: new
118 #
119  
120 =pod
121  
122 =over 4
123  
124 =item new()
125  
126 $OT = OpenThought->new();
127  
128 Creates a new OpenThought object.
129  
130 =item Return Value
131  
132 =over 4
133  
134 =item $OT
135  
136 OpenThought object.
137  
138 =back
139  
140 =back
141  
142 =cut
143  
144 # The main OpenThought constructor
145 sub new {
146 0 ▶ my ( $pkg, $args ) = @_;
Subroutine new: not called CC=4 SCAR=30.0
Pod documented
147  
148 0 ▶ $args ||= {};
Condition0%0/2
$args ||= {}
l!l
00
MC/DC0%0/1
$args ||= {}
$args
Truth table0%0/2
A
A= $args
Aresult
11
00
149  
150 0 ▶ my $class = ref $pkg || $pkg;
Condition0%0/3
ref $pkg || $pkg
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $pkg || $pkg
ref $pkg $pkg
Truth table0%0/3
A || B
A= ref $pkgB= $pkg
ABresult
1X1
011
000
151  
152 my $self = {
153  
154 0 ▶ %{ $args },
Condition0%0/2
$args->{'persist'} || 0
l!l
00
MC/DC0%0/1
$args->{'persist'} || 0
$args->{'persist'}
Truth table0%0/2
A
A= $args->{'persist'}
Aresult
11
00
155  
156 0 _persist => $args->{persist} || 0,
157  
158 };
159  
160 0 bless ($self, $class);
161  
162 0 $self->_init();
163  
164 0 return $self;
165 }
166  
167 sub _init {
168 0 ▶ my $self = shift;
Subroutine _init: not called CC=3 SCAR=24.8
169  
170 0 my @settings = qw(
171 log_enabled
172 log_level
173 require
174 channel_type
175 channel_visible
176 channel_url_replace
177 selectbox_max_width
178 selectbox_trim_string
179 selectbox_single_row_mode
180 selectbox_multi_row_mode
181 checkbox_true_value
182 checkbox_false_value
183 radio_null_selection_value
184 data_mode
185 );
186  
187 0 ▶ delete $self->{_settings} if exists $self->{_settings};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"_settings"} 0 0 0
188  
189 0 foreach my $setting ( @settings ) {
190 0 $self->{_settings}{$setting} = [];
191 }
192  
193 0 $self->{_response} = [];
194  
195 }
196  
197  
198 # Generate, in the proper order, the serialized params and settings
199 sub output {
200 0 ▶ my $self = shift;
Subroutine output: not called CC=6 SCAR=37.4
Pod undocumented
201  
202 0 my ( $save, $serialized_data, $restore );
203 0 $save = $serialized_data = $restore = "";
204  
205 0 my @settings;
206 0 foreach my $setting ( keys %{ $self->{_settings} } ) {
207 # There needs to be at least one passed in
208 0 ▶ next unless scalar @{ $self->{_settings}{$setting} } > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @{$self->{"_settings"}{$setting};} > 0 0 0 0
209  
210 0 $serialized_data .= join '', @{ $self->{_settings}{$setting} };
211 0 push @settings, $setting;
212 }
213  
214 # Grab all the response data (params, focus, url, javascript, etc)
215 0 $serialized_data .= join '', @{ $self->{_response} };
216  
217 # Save/restore the current settings unless told to make them
218 # persist
219 0 ▶ unless( $self->{settings_persist} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"settings_persist"}) 0 0 0
220 0 ▶ if (@settings) {
Branch: 1 branch
BranchTrueFalseTotal
if (@settings) 0 0 0
221 0 $save .= $self->_settings_save( @settings );
222 0 $restore .= $self->_settings_restore( @settings );
223 }
224 }
225  
226 # Hands JavaScript code to the browser. The browser processes the data
227 # automatically as we hand it over -- as far as the browser is concerned,
228 # it is simply loading a new page now (but in the hidden frame).
229 0 my $code = $self->_add_tags( "${save}${serialized_data}${restore}");
230  
231 0 ▶ $DEBUG && carp $code ;
Branch: 1 branch
BranchTrueFalseTotal
$OpenThought::DEBUG and carp($code) 0 0 0
232  
233 0 return $code;
234  
235 }
236  
237 1 *auto_param = \&param;
238 1 *fields = \&param;
239 1 *html = \&param;
240 1 *images = \&param;
241  
242 # The user has html, input fields, or images they want displayed in the browser
243 sub param {
244 0 ▶ my ( $self, $data, $options ) = @_;
Subroutine param: not called CC=5 SCAR=34.0
Pod documented
245  
246 0 ▶ if (ref $data eq "ARRAY") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $data eq "ARRAY") 0 0 0
247 0 ▶ $options = $data->[1] || {};
Condition0%0/2
$data->[1] || {}
l!l
00
MC/DC0%0/1
$data->[1] || {}
$data->[1]
Truth table0%0/2
A
A= $data->[1]
Aresult
11
00
248 0 ▶ $data = $data->[0] || {};
Condition0%0/2
$data->[0] || {}
l!l
00
MC/DC0%0/1
$data->[0] || {}
$data->[0]
Truth table0%0/2
A
A= $data->[0]
Aresult
11
00
249 }
250  
251 0 $data = $self->_as_javascript( $data );
252  
253 0 my $save = "";
254 0 my $restore = "";
255 0 ▶ if ($options) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options) 0 0 0
256 0 $save = $self->_settings_save( keys %{ $options } ) .
257 $self->settings($options, 1);
258 0 $restore = $self->_settings_restore( keys %{ $options } );
259 }
260  
261 0 $data = "${save}parent.OpenThought.ServerResponse(${data});${restore}";
262  
263 0 push @{ $self->{_response} }, $data;
264  
265 0 return $self->_add_tags($data);
266 }
267  
268 # Calls the Focus function within the browser, which in turn takes the
269 # cursor and puts it into a particular field
270 sub focus {
271 0 ▶ my ( $self, $field ) = @_;
Subroutine focus: not called CC=1 SCAR=6.9
Pod documented
272  
273 0 my $data = " parent.OpenThought.Focus('$field');";
274  
275 0 push @{ $self->{_response} }, $data;
276  
277 0 return $self->_add_tags($data);
278 }
279  
280 # Send Javascript code to be interpreted by the browser. This would often be
281 # used to call a user defined Javascript function.. an example application of
282 # this would be to use the dynapi Dynamic HTML API Library to create and
283 # manipulate DHTML objects from the server.
284 sub javascript {
285 0 ▶ my ( $self, $javascript_code ) = @_;
Subroutine javascript: not called CC=1 SCAR=6.9
Pod documented
286  
287 # NOTE: it really doesn't work to escape the JS! The developer needs to do
288 # it themselves...
289 0 my $data = " with (parent.document) { $javascript_code }";
290  
291 0 push @{ $self->{_response} }, $data;
292  
293 0 return $self->_add_tags($data);
294 }
295  
296 # Jump to a new page with this url
297 sub url {
298 0 ▶ my ( $self, $url ) = @_;
Subroutine url: not called CC=10 SCAR=47.0
Pod documented
299  
300 0 ▶ unless ( $url ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($url) 0 0 0
301 0 croak "You're missing the parameter to 'url'.";
302 }
303  
304 0 my $javascript_code;
305  
306 0 ▶ if ( ref $url eq "ARRAY" ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $url eq 'ARRAY') { } 0 0 0
elsif (not ref $url) { } 0 0 0
307 0 ▶ if ( $url->[0] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($url->[0]) 0 0 0
308 0 ▶ unless ( not ref $url->[0] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $url->[0]) 0 0 0
309 0 croak "The first element of the arrayref passed into 'url' should be a scalar containing the url.";
310 }
311  
312 0 $javascript_code = "parent.OpenThought.FetchHtml('$url->[0]'";
313 }
314 0 ▶ if ( $url->[1] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($url->[1]) 0 0 0
315 0 ▶ unless ( ref $url->[1] eq "HASH" ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $url->[1] eq "HASH") 0 0 0
316 0 croak "The second element of the arrayref passed into 'url' is optional, but if supplied, must be a hashref.";
317 }
318 0 foreach my $param ( keys %{ $url->[1] } ) {
319 0 ▶ if ( defined $url->[1]->{ $param } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $url->[1]{$param}) { } 0 0 0
320 0 $javascript_code .= ",'$param=$url->[1]{ $param }'";
321 }
322 else {
323 0 $javascript_code .= ",'$param'";
324 }
325 }
326 }
327 }
328 elsif ( not ref $url ) {
329 0 $javascript_code = "parent.OpenThought.FetchHtml('$url'";
330 }
331 else {
332 0 croak "The 'url' method takes either a scalar containing the url, or an an arrayref containing both the url and a hashref with url parameters.";
333 }
334  
335 0 $javascript_code .= ");";
336  
337 0 push @{ $self->{_response} }, $javascript_code;
338  
339 0 return $self->_add_tags($javascript_code);
340 }
341  
342 # Alter settings within the existing OpenThought Application
343 sub settings {
344 0 ▶ my ( $self, $settings, $return_only ) = @_;
Subroutine settings: not called CC=7 SCAR=40.3
Pod documented
345  
346 0 ▶ unless ( ref $settings eq "HASH" ){
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $settings eq "HASH") 0 0 0
347 0 croak "When you pass in settings, they need to be a hash " .
348 'reference. Either $OT->settings( \%settings ) or ' .
349 '$OT->response( settings => \%settings ).';
350 }
351  
352 0 my $data;
353 0 foreach my $name ( keys %{ $settings } ) {
354 # Persist is special as well. It defines whether the settings
355 # being sent are to remain for the life of this page, or just for this
356 # current request.
357 0 ▶ if( $name eq "settings_persist" ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($name eq 'settings_persist') { } 0 0 0
elsif ($self->{'_settings'}{$name}) { } 0 0 0
358 0 $self->{settings_persist} = $settings->{$name};
359 }
360  
361 # All other parameters are treated the same here
362 elsif ( $self->{_settings}{$name} ) {
363 my $setting = "parent.OpenThought.config.$name = \"" .
364 # $self->_escape_javascript( $settings->{$name} ) . "\");";
365 0 $self->_escape_javascript( $settings->{$name} ) . "\";";
366  
367 0 ▶ unless ($return_only) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($return_only) 0 0 0
368 0 push @{ $self->{_settings}{$name} }, $setting;
369 }
370  
371 0 $data .= $setting;
372  
373 }
374 else {
375 0 carp "No such setting [$name].";
376 }
377 }
378  
379 0 ▶ if ($return_only) {
Branch: 1 branch
BranchTrueFalseTotal
if ($return_only) { } 0 0 0
380 0 return $data;
381 }
382 else {
383 0 return $self->_add_tags($data);
384 }
385 }
386  
387  
388 =pod
389  
390 =over 4
391  
392 =item param()
393  
394 $OT->param( \%data, [ \%settings ] );
395  
396 Update input-type form field elements (text boxes, radio buttons, checkboxes,
397 select lists, text areas, etc), HTML elements, as well as images an image attributes.
398  
399 This method accepts a hash reference containing keys which map to field
400 names, html id's, and image names.
401  
402 The form element, html id, or image will be dynamically updated to contain the value found within the
403 hash key.
404  
405 B<Text, Textarea, Password fields>: These are very straight forward. The hash
406 values are inserted directly into the input fields matching the hash keys.
407  
408 B<Radio buttons>: The value for the hash key should match the C<value>
409 attribute of the radio button element in your HTML code. When the hash key and
410 value matches the radio button name and value, that radio button will become
411 checked.
412  
413 B<Select Lists>: Sending a single array reference to a select list will cause
414 that data to be appended to the select list. In contrast, sending data as a
415 reference to an array of arrays or an array of hashes will cause any values
416 within the select list to be overwritten with the new data.
417  
418 You can modify this select list behaviour by using the
419 C<selectbox_single_row_mode> and C<selectbox_multi_row_mode> options.
420  
421 When sending data to a select list (which, as we said, is done as an array),
422 the first element of the array is the text to be displayed, the second element
423 is the underlying value associated with that text.
424  
425 Sending a single scalar value to a select list highlights the corresponding
426 entry in the select list which contains that value.
427  
428 Sending undef, or a reference to an array with an empty string as it's only
429 element will cause the select list to be cleared.
430  
431 B<HTML elements>: It accepts a hash reference containing keys which map to HTML id attributes.
432 You can add id tags for nearly any HTML attribute. The hash values are inserted
433 within the html containing an id tag matching the hash key (using innerHtml). The data may contain HTML
434 tags, which will be correctly displayed.
435  
436 $OT->param({ "html_id_tag" => "<b>foo</b>" }).
437  
438 B<Images>: To change an image or image property, the hash key should be the image name. If you just want
439 to load a new image, the hash value should be a scalar containing the url of the new image.
440  
441 $OT->param({ foo => 'http://example.com/new_image.jpg' });
442  
443 $OT->param({ foo => { width => 100,
444 height => 150, });
445  
446 B<Optional Parameter: Settings>: The second optional parameter is a hash reference of settings that will
447 effect just the data passed into this call of C<param()>. See the C<settings()> method for a list of
448 available options.
449  
450 =item javascript()
451  
452 $OT->javascript( "alert('Howdy');" );
453  
454 This allows you to run JavaScript code, along with accessing JavaScript
455 functions and variables.
456  
457 It accepts a string containing the JavaScript code you wish to execute. There
458 is no need to add script tags, they will be added for you.
459  
460 =item focus()
461  
462 $OT->focus( "field_name" );
463  
464 This allows you to focus a given input field or anchor tag.
465  
466 It accepts a string containing the name of the field or anchor tag you wish to
467 focus. If it's a field, it will be given the cursor. If it's an anchor tag,
468 the browser will jump to it's position on the page.
469  
470 =item url()
471  
472 $OT->url( "http://example.com/my_openthought_app.pl" );
473  
474 $OT->url([ "http://example.com/my_openthought_app.pl" =>
475 { example_param => some_value,
476 param2 => another_value } ]);
477  
478 The C<url> method loads new page.
479  
480 This method can be used by passing in the url as a scalar, or by passing in the
481 url and url parameters within an arrayref. If you pass in an arrayref, the
482 first element of the array should be the url, the second element should be a
483 hash reference whose keys and values will be passed on as parameters to the new
484 url.
485  
486 =item settings
487  
488 $OT->settings({
489 settings_persist => boolean,
490 log_start => string,
491 log_level => string,
492 require => { ... },
493 http_request_type => string,
494 channel_type => string,
495 channel_visible => boolean,
496 channel_url_replace => boolean,
497 selectbox_max_width => size,
498 selectbox_trim_string => string,
499 selectbox_single_row_mode => string,
500 selectbox_multi_row_mode => string,
501 checkbox_true_value => string,
502 checkbox_false_value => string,
503 radio_null_selection_value => string,
504 data_mode => string,
505 });
506  
507 Alter settings in the OpenThought application running in the browser. Each
508 parameter is optional. Only pass in the option(s) you wish to change.
509  
510 For additional information on configuration, and for how/where to set the
511 defaults, please see the section labeled C<CONFIGURATION>.
512  
513 This method accepts a hash reference where the keys are names of OpenThought
514 options, and the values are the new option values.
515  
516 By default, these options will only be good for one request. You can change
517 that behaviour by either passing in the C<persist> option to this method.
518  
519 You can set the defaults for most of these settings at the top of the
520 OpenThought.js file.
521  
522 =over 4
523  
524 =item Parameters
525  
526 =over 4
527  
528 =item settings_persist()
529  
530 $OT->settings({ settings_persist => "true" });
531  
532 This specifies whether or not the settings being changed in the browser should
533 be just for this request, or whether they should persist as long as the current
534 page is loaded.
535  
536 The default is to not persist.
537  
538 If you use this parameter, only items you specify will be executed. That is,
539 if you fail to mention where C<fields> should be in the order, then C<fields>
540 will be completely ignored for that request.
541  
542 If C<url> is not last, everything sent after it will be lost when the page
543 changes.
544  
545 =item log_enabled
546  
547 $OT->settings({ log_enabled => "true" });
548  
549 Enable a log window so you can see what's going on behind the scenes. If
550 something in your app isn't working, try enabling this. This can be very
551 useful for debugging, but you probably want it disabled while your app is in
552 production. This, of course, won't work if your popup blocking software
553 doesn't allow popups from the site you're running your application from.
554  
555 =item log_level
556  
557 $OT->settings({ log_level => "info" });
558  
559 What log level to run at. You have the ability to enable lots of debugging
560 output, only serious errors, and various levels in between.
561  
562 Options are C<debug>, C<info>, C<warn>, C<error>, C<fatal>
563  
564 =item require
565  
566 $OT->settings({ require =>
567 { "40dom" => "http://example.com/no_40dom",
568 "xmlhttp" => "http://example.com/no_xmlhttp",
569 } })
570  
571 Define a set of browser requirements, and a page to go to if that requirement
572 is not met.
573  
574 Available requirements are C<40dom>, C<xmlhttp>, C<layer>, C<iframe>,
575 C<htmlrewrite>.
576  
577 =item http_request_type (EXPERIMENTAL)
578  
579 $OT->settings({ http_request_type => "POST" });
580  
581 The request type for communications with the server. This can be
582 overridden at any time by passing in either GET or POST as the first
583 parameter to CallUrl(). The default (and known to work) option is GET.
584  
585 Using POST has only been minimally tested. There have been problems noted when
586 using Firefox and POST, if the C<channel_type> option was changed from C<auto>
587 to C<iframes>. This appears to be a Firefox bug.
588  
589 Options are C<GET> or C<POST> (case sensitive).
590  
591 =item channel_type
592  
593 $OT->settings({ channel_type => "iframe" });
594  
595 The type of channel to use for communicating with the browser.
596  
597 By default, OpenThought will attempt to use the XMLHttpRequest or XMLHTTP
598 functions available in recent browsers, then fall back to iframes if the
599 browser doesn't support those newer options.
600  
601 However, XMLHttpRequest and XMLHTTP have one limitation -- for any given
602 request, the server can only respond once, and the response is all at the same
603 time.
604  
605 Iframes don't have that restriction, and the server can send a variety of
606 responses throughout the duration of the request.
607  
608 XMLHttpRequest/XMLHTTP are fine for most uses, but some applications may
609 benefit from being able to have the browser receive data a number of times
610 throughtout a single request (ie, irc and other realtime chat applications).
611  
612 Options are C<auto> or C<iframe>.
613  
614 =item channel_visible
615  
616 $OT->settings({ channel_visible => "true" });
617  
618 Normally, the channel used to communicate with the server is invisible. The
619 curious may wish to see whats going on inside it (or perhaps need it for
620 debugging). Enabling the following will make the channel visible. This only
621 works if the channel is an iframe (which means it's either an older browser, or
622 that you have C<channel_type> set to C<iframe>.
623  
624 =item url_replace
625  
626 $OT->settings({ url_replace => "true" });
627  
628 When using iframes and layers, the typical way to submit ajax requests to the
629 server involves using a 'document.location.replace()'. This means the requests
630 aren't being stored in the browser history. So, the back button will take you
631 to the previous *page*, not the previous AJAX request. This is often what
632 people want. This sometimes isn't what people want :-) Set to 'true' to not
633 add AJAX requests to the browser's history, set to 'false' to have them added
634 to the history.
635  
636 This option has no effect when using XMLHttpRequest/XMLHTTP.
637  
638 Options are C<true> or C<false>.
639  
640 =item url_prefix
641  
642 $OT->settings({ url_prefix => "include/" });
643  
644 During any call to the server (via CallUrl and FetchHtml), assume the script is
645 located in this directory (ie, the file/dir you pass in is relative to this
646 path). If there's no trailing slash, it will add one. This config option can
647 be overridden by beginning the url with 'http' or '/'.
648  
649 =item selectbox_single_row_mode
650  
651 $OT->settings({ selectbox_single_row_mode => "append" });
652  
653 This defines whether or not sending a new row (an arrayref) to the select list
654 overwrites the existing values, or adds to it. It can be set to C<append> or
655 C<overwrite>.
656  
657 The default behaviour for adding a row to select lists is to append itself to
658 the end of the selectlist. Setting C<selectbox_single_row_mode> to C<overwrite>
659 value is how you can alter that behavior. If C<selectbox_single_row_mode> is
660 C<overwrite>, the contents of a select list are overwritten by the new row.
661  
662 When C<selectbox_single_row_mode> is set to C<append>, you can still clear a
663 select list by passing in an empty string as a parameter to the select list.
664  
665 =item selectbox_multi_row_mode
666  
667 $OT->settings({ selectbox_single_row_mode => "overwrite" });
668  
669 This defines whether or not sending multiple rows (an array of arrays) to the
670 select list overwrites the existing values, or adds to it. It can be set to
671 C<append> or C<overwrite>.
672  
673 The default behaviour for adding multiple rows to select lists is to overwrite
674 the existing list. Setting C<selectbox_multi_row_mode> to C<append> value
675 is how you can alter that behavior. If C<selectbox_multi_row_mode> is
676 C<append>, the contents of a select list are preserved, and all new data is
677 appended to the end of the select list.
678  
679 When C<selectbox_multi_row_mode> is set to C<append>, you can still clear a
680 select list by passing in an empty string as a parameter to the select list.
681  
682 =item selectbox_max_width
683  
684 $OT->settings({ selectbox_max_width => "50" });
685  
686 Limit how many characters an entry in a select box can contain, 0 to not
687 constrain the size. The default is 30.
688  
689 Upon dynamically receiving select box content, most browsers resize the select
690 box to the width of the longest entry. This seems like a neat feature, but
691 resizing the select box will often adversely affect other parts of your visual
692 layout. This option allows you to modify the size of text going into a select
693 box, so the browser doesn't make the select box too big.
694  
695 Netscape 4 is the only browser known not to perform dynamic resizing. Instead,
696 it allows you to scroll side to side to view long text.
697  
698 See C<selectbox_trim_string> to learn what the trimmed text is replaced with.
699  
700 =item selectbox_trim_string
701  
702 $OT->settings({ selectbox_trim_string => "+" });
703  
704 Text to add to strings trimmed because of C<selectbox_max_width>.
705  
706 If the text being inserted into a selectbox needs to be resized to fit (due to
707 C<selectbox_max_width>), replace the removed text with the following string to
708 make it clear that the string was trimmed.
709  
710 The default is to use two periods: ..
711  
712 =item checkbox_true_value
713  
714 $OT->settings({ checkbox_true_value => "1" });
715  
716 The value a checkbox will return if it is checked, and no value is assigned to
717 the checkbox (via the value= attribute). The default is "1".
718  
719 =item checkbox_false_value
720  
721 $OT->settings({ checkbox_false_value => "0" });
722  
723 The value a checkbox will return if it isn't checked.
724  
725 =item radio_null_selection_value
726  
727 $OT->settings({ radio_null_selection_value => "0" });
728  
729 The value a group of radio buttons will return if none of them are selected.
730 The default is "0".
731  
732 =item data_mode
733  
734 $OT->settings({ data_mode => "append" });
735 $OT->param( $fields => { data_mode => "append" } );
736  
737 Define whether data should be overwritten or appended, for objects other than
738 select lists. It can be set to C<append> or C<overwrite>.
739  
740 By default, data sent from the server to the browser overwrites existing
741 content. This allows you to change that behaviour, and have it append.
742  
743 =back
744  
745 =back
746  
747 =item response
748  
749 print $OT->response();
750  
751 This returns the data gathered thus far, in a manner in which the browser will
752 understand (ie, JavaScript). Typically, you would just send this directly to
753 the browser, though you can modify it first if you desire.
754  
755 Calling C<response> clears all the data gathered so far on the internal stack.
756  
757 =cut
758  
759 1 *parse_and_output = \&response;
760  
761 sub response() {
762 0 ▶ my $self = shift;
Subroutine response: not called CC=6 SCAR=37.4
Pod documented
763  
764 0 ▶ if ( length(@_) and ref $_[0] eq "HASH" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length @_ and ref $_[0] eq 'HASH') { } 0 0 0
Condition0%0/3
length @_ and ref $_[0] eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/2
length @_ and ref $_[0] eq "HASH"
length @_ ref $_[0] eq "HASH"
Truth table0%0/3
A and B
A= length @_B= ref $_[0] eq "HASH"
ABresult
0X0
100
111
765 0 my $params = $_[0];
766  
767 0 foreach my $param ( keys %{ $params } ) {
768 0 $self->$param( $params->{$param} );
769 }
770 }
771 else {
772 0 my @params = @_;
773  
774 #for my $i ( 1 .. (length @params) / 2) {
775 0 ▶ while ( scalar @params > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @params > 0) 0 0 0
776  
777 0 my $method = shift @params;
778 0 my $method_params = shift @params;
779  
780 0 ▶ $self->$method( $method_params ) if $method ne "";
Branch: 1 branch
BranchTrueFalseTotal
if $method ne "" 0 0 0
781 }
782 }
783  
784 0 return $self->output();
785 }
786  
787 # Save the current value of a setting
788 sub _settings_save {
789 0 ▶ my ( $self, @settings ) = @_;
Subroutine _settings_save: not called CC=2 SCAR=17.9
790  
791 0 my $data;
792  
793 0 foreach my $setting ( @settings ) {
794 0 $data .= " var __$setting=parent.OpenThought.config.$setting;";
795 }
796  
797 0 return $data;
798 }
799  
800 # Restore the previous value of a setting
801 sub _settings_restore {
802 0 ▶ my ( $self, @settings ) = @_;
Subroutine _settings_restore: not called CC=2 SCAR=17.9
803  
804 0 my $data;
805  
806 0 foreach my $setting ( @settings ) {
807 0 $data .= " parent.OpenThought.config.$setting = __$setting;";
808 }
809  
810 0 return $data;
811 }
812  
813 # Convert a Perl hash into a JavaScript data structure. This has all been
814 # recently (7/24/05) modified to use JSON notation:
815 # http://www.crockford.com/JSON/index.html
816 sub _as_javascript {
817 0 ▶ my ($self, $data) = @_;
Subroutine _as_javascript: not called CC=21 SCAR=61.4
818 0 my $packet;
819  
820 0 ▶ unless ( ref $data eq "HASH" ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $data eq "HASH") 0 0 0
821 0 croak "Data sent to the serializer function must be a reference to a hash.";
822 }
823  
824 0 $packet = "{";
825  
826 # Loop through each element that needs filled
827 0 ▶ while ( my( $key, $val ) = each %{ $data } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$data;}) 0 0 0
828  
829 # In the case of a simple key=value assignment, do the following. This
830 # is used for text, password, textbox, uniquely named checkboxes, and
831 # radio buttons
832 # Convert: $hash->{key} = "value"
833 # To: key : value,
834 0 ▶ if( not ref $val) {
Branch: 5 branches
BranchTrueFalseTotal
if (not ref $val) { } 0 0 0
elsif (ref $val eq 'ARRAY' and not ref $val->[0]) { } 0 0 0
elsif (ref $val eq 'ARRAY' and ref $val->[0] eq 'HASH') { } 0 0 0
elsif (ref $val eq 'ARRAY' and ref $val->[0] eq 'ARRAY') { } 0 0 0
elsif (ref $val eq 'HASH') { } 0 0 0
835 0 ▶ if ( defined $val ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $val) { } 0 0 0
836 0 $val = $self->_escape_javascript( $val );
837 0 $packet .= qq("$key": "$val",);
838 }
839 else {
840 0 $packet .= qq("$key": null,);
841 }
842 }
843  
844 # In the case of adding one item to a select box, or clearing a select box
845 # Convert: $hash->{key} = [ $val1, $val2 ]
846 # To: key: [ val1, val2 ],
847 ▶ elsif ( ref $val eq "ARRAY" and not ref $val->[0] ) {
Condition0%0/3
ref $val eq "ARRAY" and not ref $val->[0]
!ll&&!rl&&r
000
MC/DC0%0/2
ref $val eq "ARRAY" and not ref $val->[0]
ref $val eq "ARRAY" not ref $val->[0]
Truth table0%0/3
A and not(B)
A= ref $val eq "ARRAY"B= not ref $val->[0]
ABresult
0X0
100
111
848  
849 # If we are sent something like:
850 # $field->{'selectbox_name'} = [ "" ];
851 # That means we wish to clear the selectbox
852 0 ▶ unless ( defined $val->[0] and $val->[0] ne "" ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $val->[0] and $val->[0] ne "") 0 0 0
Condition0%0/3
defined $val->[0] and $val->[0] ne ""
!ll&&!rl&&r
000
MC/DC0%0/2
defined $val->[0] and $val->[0] ne ""
defined $val->[0] $val->[0] ne ""
Truth table0%0/3
A and B
A= defined $val->[0]B= $val->[0] ne ""
ABresult
0X0
100
111
853  
854 0 $packet .= qq("$key": [ "" ],);
855 0 next;
856 }
857  
858 0 $packet .= qq("$key": [ );
859 $packet .= join '',
860 0 ▶ map { '"' . ($self->_escape_javascript($_) || "") . '",'}
Condition0%0/2
$self->_escape_javascript($_) || ''
l!l
00
MC/DC0%0/1
$self->_escape_javascript($_) || ''
$self->_escape_javascript($_)
Truth table0%0/2
A
A= $self->_escape_javascript($_)
Aresult
11
00
861 0 @{ $val };
862  
863 0 chop $packet;
864 0 $packet .= qq( ],);
865 }
866  
867 # For updating select lists using an array of hashes
868 # Convert: $hash->{key} = [ { val1 => val2 }, { val3 => val4 } ]
869 # To: key: [ { val1: val2 }, { val3: val4 } ],
870 ▶ elsif ( ref $val eq "ARRAY" and ref $val->[0] eq "HASH" ) {
Condition0%0/3
ref $val eq "ARRAY" and ref $val->[0] eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/2
ref $val eq "ARRAY" and ref $val->[0] eq "HASH"
ref $val eq "ARRAY" ref $val->[0] eq "HASH"
Truth table0%0/3
A and B
A= ref $val eq "ARRAY"B= ref $val->[0] eq "HASH"
ABresult
0X0
100
111
871 0 $packet .= qq("$key": [ );
872  
873 0 foreach my $hash ( @{ $val } ) {
874 0 ▶ while ( my ( $key1, $val1 ) = each %{ $hash } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$hash;}) 0 0 0
875 0 $val1 = $self->_escape_javascript( $val1 );
876 0 $packet .= qq({"$key1": "$val1"},)
877 }
878 0 chop $packet;
879 }
880 0 $packet .= qq( ],);
881 }
882  
883 # This is done for adding multiple items to select boxes
884 # Convert: $hash->{key} = [ [ val1, val2 ], [ val3, val4 ] ]
885 # To: key: [ [ val1, val2 ], [ val3, val4 ] ],
886 ▶ elsif ( ref $val eq "ARRAY" and ref $val->[0] eq "ARRAY" ) {
Condition0%0/3
ref $val eq "ARRAY" and ref $val->[0] eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
ref $val eq "ARRAY" and ref $val->[0] eq "ARRAY"
ref $val eq "ARRAY" ref $val->[0] eq "ARRAY"
Truth table0%0/3
A and B
A= ref $val eq "ARRAY"B= ref $val->[0] eq "ARRAY"
ABresult
0X0
100
111
887 0 $packet .= qq("$key": [ );
888 0 my $i=0;
889 0 foreach my $array ( @{ $val } ) {
890  
891 # If we are only sent text for the selectlist, and no value --
892 # define the value as empty. When it gets to the browser, the
893 # value will be made the same as the text
894 0 ▶ $array->[1] = "" unless defined($array->[1]);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $array->[1] 0 0 0
895  
896 0 $array->[0] = $self->_escape_javascript( $array->[0] );
897 0 $array->[1] = $self->_escape_javascript( $array->[1] );
898 0 $packet .= qq(["$array->[0]","$array->[1]"],);
899 0 $i++;
900 }
901 0 chop $packet;
902 0 $packet .= qq( ],);
903 }
904  
905 # This updates multiple checkboxes with the same name
906 # Convert: $hash->{key} = { key1 => val1, key2 = val2 }
907 # To: key : { key1 : val1, key2 : val2 },
908 elsif ( ref $val eq "HASH" ) {
909 0 $packet .= qq("$key": { );
910 0 foreach my $key2 ( keys %{ $val } ) {
911 0 ▶ $val->{$key2} = "" unless defined($val->{$key2});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val->{$key2} 0 0 0
912 0 $val->{$key2} = $self->_escape_javascript( $val->{$key2} );
913 0 $packet .= qq("$key2": "$val->{$key2}",);
914 }
915 0 chop $packet;
916 0 $packet .= qq(},);
917 }
918 else {
919 0 carp "I'm not sure what to do with the data structure you sent!";
920 }
921 }
922 0 chop $packet;
923 0 $packet .= "}";
924  
925 0 return $packet;
926 }
927  
928 # Adds the appropriate script tags to JavaScript code
929 sub _add_tags {
930 0 ▶ my ( $self, $code ) = @_;
Subroutine _add_tags: not called CC=1 SCAR=6.9
931  
932 # The <OT> tag is a bit of a unique ID... so that the JS can detect the
933 # difference between OT adding the tags, and tags added by the developer.
934 # The JS will strip all these tags -- everything but $code -- if the call
935 # is done through XmlHttpRequest or similar, but they're required with
936 # iframes.
937 #
938 # We're doing this instead of passing parameters from the browser to the
939 # server and testing on them to see if the tags should be added at all.
940 # Basically, this prevents the developer from having to pass params into OT
941 # that they don't understand, which I think makes things more complicated
942 # than they need to be. You have my email address, feel free to argue :-)
943 0 return qq{\r <OT><body onLoad="parent.OpenThought.ResponseComplete(self)"></body><script>${code}</script><OT> \r};
944 }
945  
946 sub _escape_javascript {
947 0 ▶ my ( $self, $code ) = @_;
Subroutine _escape_javascript: not called CC=2 SCAR=17.9
948  
949 0 ▶ return unless defined $code;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $code 0 0 0
950  
951 0 $code =~ s/\\/\\\\/g;
952 0 $code =~ s/\n/\\n/g;
953 0 $code =~ s/\r/\\r/g;
954 0 $code =~ s/\t/\\t/g;
955 0 $code =~ s/\"/\\"/g;
956 0 $code =~ s/([\x00-\x1F])/sprintf("\\%03o", ord($1))/ge;
957  
958 0 return $code;
959 }
960  
961 1 1;
962  
963 __END__
« inc/ExtUtils/AutoInstall.pm ↑ Summary Makefile.PL »