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 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543
|
NAME
HTML::Prototype - Generate HTML and Javascript for the Prototype library
SYNOPSIS
use HTML::Prototype;
my $prototype = HTML::Prototype->new;
print $prototype->auto_complete_field(...);
print $prototype->auto_complete_result(...);
print $prototype->auto_complete_stylesheet(...);
print $prototype->content_tag(...);
print $prototype->define_javascript_functions;
print $prototype->draggable_element(...);
print $prototype->drop_receiving_element(...);
print $prototype->evaluate_remote_response(...);
print $prototype->form_remote_tag(...);
print $prototype->in_place_editor(...);
print $prototype->in_place_editor_field(...);
print $prototype->in_place_editor_stylesheet(...);
print $prototype->javascript_tag(...);
print $prototype->link_to_function(...);
print $prototype->link_to_remote(...);
print $prototype->observe_field(...);
print $prototype->observe_form(...);
print $prototype->periodically_call_remote(...);
print $prototype->sortable_element(...);
print $prototype->submit_to_remote(...);
print $prototype->tag(...);
print $prototype->text_field_with_auto_complete(...);
print $prototype->update_element_function(...);
print $prototype->visual_effect(...);
DESCRIPTION
The module contains some code generators for Prototype, the famous
JavaScript OO library and the script.aculous extensions.
The Prototype library (http://prototype.conio.net/) is designed to make
AJAX easy. Catalyst::Plugin::Prototype makes it easy to connect to the
Prototype library.
This is mostly a port of the Ruby on Rails helper tags for JavaScript
for use in Catalyst.
METHODS
$prototype->in_place_editor( $field_id, \%options )
Makes an HTML element specified by the DOM ID $field_id become an
in-place editor of a property.
A form is automatically created and displayed when the user clicks
the element, something like this:
<form id="myElement-in-place-edit-form" target="specified url">
<input name="value" text="The content of myElement"/>
<input type="submit" value="ok"/>
<a onClick="javascript to cancel the editing">cancel</a>
</form>
The form is serialized and sent to the server using an Ajax call,
the action on the server should process the value and return the
updated value in the body of the reponse. The element will
automatically be updated with the changed value (as returned from
the server).
Required options are:
"url": Specifies the url where the updated value should be sent
after the user presses "ok".
Addtional options are:
"rows": Number of rows (more than 1 will use a TEXTAREA)
"cols": The number of columns the text area should span (works for
both single line or multi line).
"size": Synonym for cols when using single-line (rows=1) input
"cancel_text": The text on the cancel link. (default: "cancel")
"form_class_name": CSS class used for the in place edit form.
(default: "inplaceeditor-form")
"save_text": The text on the save link. (default: "ok")
"saving_class_name": CSS class added to the element while displaying
"Saving..." (removed when server responds). (default:
"inplaceeditor-saving")
"load_text_url": Will cause the text to be loaded from the server
(useful if your text is actually textile and formatted on the
server)
"loading_text": If the "load_text_url" option is specified then this
text is displayed while the text is being loaded from the server.
(default: "Loading...")
"click_to_edit_text": The text on the click-to-edit link. (default:
"click to edit")
"external_control": The id of an external control used to enter edit
mode.
"ajax_options": Pass through options to the AJAX call (see
prototype's Ajax.Updater)
"with": JavaScript snippet that should return what is to be sent in
the Ajax call, "form" and "value" are implicit parameters
$prototype->in_place_editor_field( $object, $method, \%tag_options,
\%in_place_editor_options )
Renders the value of the specified object and method with in-place
editing capabilities.
$prototype->in_place_editor_stylesheet
Returns the in_place_editor stylesheet.
$prototype->auto_complete_field( $field_id, \%options )
Adds Ajax autocomplete functionality to the text input field with
the DOM ID specified by $field_id.
This function expects that the called action returns a HTML <ul>
list, or nothing if no entries should be displayed for
autocompletion.
Required options are:
"url": Specifies the URL to be used in the AJAX call.
Addtional options are:
"update": Specifies the DOM ID of the element whose innerHTML should
be updated with the autocomplete entries returned by the Ajax
request. Defaults to field_id + '_auto_complete'.
"with": A Javascript expression specifying the parameters for the
XMLHttpRequest. This defaults to 'value', which in the evaluated
context refers to the new field value.
"indicator": Specifies the DOM ID of an elment which will be
displayed Here's an example using Catalyst::View::Mason with an
indicator against the auto_complete_result example below on the
server side. Notice the 'style="display:none"' in the indicator
<span>.
<% $c->prototype->define_javascript_functions %>
<form action="/bar" method="post" id="baz">
<fieldset>
<legend>Type search terms</legend>
<label for="acomp"><span class="field">Search:</span></label>
<input type="text" name="acomp" id="acomp"/>
<span style="display:none" id="acomp_stat">Searching...</span><br />
</fieldset>
</form>
<span id="acomp_auto_complete"></span><br/>
<% $c->prototype->auto_complete_field( 'acomp', { url => '/autocomplete', indicator => 'acomp_stat' } ) %>
while autocomplete is running.
"tokens": A string or an array of strings containing separator
tokens for tokenized incremental autocompletion. Example: "<tokens
=" ','>> would allow multiple autocompletion entries, separated by
commas.
"min_chars": The minimum number of characters that should be in the
input field before an Ajax call is made to the server.
"on_hide": A Javascript expression that is called when the
autocompletion div is hidden. The expression should take two
variables: element and update. Element is a DOM element for the
field, update is a DOM element for the div from which the innerHTML
is replaced.
"on_show": Like on_hide, only now the expression is called then the
div is shown.
"select": Pick the class of the element from which the value for
insertion should be extracted. If this is not specified, the entire
element is used
$prototype->auto_complete_result(\@items, $fieldname, [$phrase])
Returns a list, to communcate with the Autocompleter.
Here's an example for Catalyst:
sub autocomplete : Global {
my ( $self, $c ) = @_;
my @items = qw/foo bar baz/;
$c->res->body( $c->prototype->auto_complete_result(\@items) );
}
$prototype->text_field_with_auto_complete($method, [\%tag_options],
[\%completion_options])
Wrapper for text_field with added Ajax autocompletion functionality.
In your controller, you'll need to define an action called
auto_complete_for_object_method to respond the AJAX calls,
$prototype->auto_complete_stylesheet
Returns the auto_complete stylesheet.
$prototype->content_tag( $name, $content, \%html_options )
Returns a block with opening tag, content, and ending tag. Useful
for autogenerating tags like <a
href="http://catalyst.perl.org"Catalyst Homepage</a>>. The first
parameter is the tag name, i.e. 'a' or 'img'.
$prototype->text_field( $name, $method, $html_options )
Returns an input tag of the "text" type tailored for accessing a
specified attribute (identified by *$method*) on an object assigned
to the template (identified by *$object*). Additional options on the
input tag can be passed as a hash ref with *$html_options*.
$prototype->define_javascript_functions
Returns the library of JavaScript functions and objects, in a script
block.
Notes for Catalyst users:
You can use "script/myapp_create.pl Prototype" to generate a static
JavaScript file which then can be included via remote "script" tag.
$prototype->draggable_element( $element_id, \%options )
Makes the element with the DOM ID specified by "element_id"
draggable.
Example:
$prototype->draggable_element( 'my_image', { revert => 'true' } );
The available options are:
handle
Default: none. Sets whether the element should only be draggable
by an embedded handle. The value is a string referencing a CSS
class. The first child/grandchild/etc. element found within the
element that has this CSS class will be used as the handle.
revert
Default: false. If set to true, the element returns to its
original position when the drags ends.
constraint
Default: none. If set to 'horizontal' or 'vertical' the drag
will be constrained to take place only horizontally or
vertically.
change
Javascript callback function called whenever the Draggable is
moved by dragging. It should be a string whose contents is a
valid JavaScript function definition. The called function gets
the Draggable instance as its parameter. It might look something
like this:
'function (element) { // do something with dragged element }'
See http://script.aculo.us for more documentation.
$prototype->drop_receiving_element( $element_id, \%options )
Makes the element with the DOM ID specified by "element_id" receive
dropped draggable elements (created by draggable_element).
And make an AJAX call.
By default, the action called gets the DOM ID of the element as
parameter.
Example: $prototype->drop_receiving_element( 'my_cart', { url =>
'http://foo.bar/add' } );
Required options are:
url The URL for the AJAX call.
Additional options are:
accept
Default: none. Set accept to a string or an array of strings
describing CSS classes. The Droppable will only accept
Draggables that have one or more of these CSS classes.
containment
Default: none. The droppable will only accept the Draggable if
the Draggable is contained in the given elements (or element
ids). Can be a single element or an array of elements. This is
option is used by Sortables to control Drag-and-Drop between
Sortables.
overlap
Default: none. If set to 'horizontal' or 'vertical' the
droppable will only react to a Draggable if it overlaps by more
than 50% in the given direction. Used by Sortables.
Additionally, the following JavaScript callback functions can be
used in the option parameter:
onHover
Javascript function called whenever a Draggable is moved over
the Droppable and the Droppable is affected (would accept it).
The callback gets three parameters: the Draggable, the Droppable
element, and the percentage of overlapping as defined by the
overlap option. Used by Sortables. The function might look
something like this:
'function (draggable, droppable, pcnt) { // do something }'
See http://script.aculo.us for more documentation.
$prototype->evaluate_remote_response
Returns 'eval(request.responseText)' which is the Javascript
function that form_remote_tag can call in :complete to evaluate a
multiple update return document using update_element_function calls.
$prototype->form_remote_tag(\%options)
Returns a form tag that will submit in the background using
XMLHttpRequest, instead of the regular reloading POST arrangement.
Even though it is using JavaScript to serialize the form elements,
the form submission will work just like a regular submission as
viewed by the receiving side.
The options for specifying the target with "url" and defining
callbacks are the same as "link_to_remote".
$prototype->javascript_tag( $content, \%html_options )
Returns a javascript block with opening tag, content and ending tag.
$prototype->link_to_function( $name, $function, \%html_options )
Returns a link that will trigger a JavaScript function using the
onClick handler and return false after the fact.
Examples:
$prototype->link_to_function( "Greeting", "alert('Hello world!') )
$prototype->link_to_function( '<img src="really.png"/>', 'do_delete()', { entities => '' } )
$prototype->link_to_remote( $content, \%options, \%html_options )
Returns a link to a remote action defined by options "url" that's
called in the background using XMLHttpRequest.
The result of that request can then be inserted into a DOM object
whose id can be specified with options->{update}.
Examples:
$prototype->link_to_remote( 'Delete', {
update => 'posts',
url => 'http://localhost/posts/'
} )
$prototype->link_to_remote( '<img src="refresh.png"/>', {
update => 'emails',
url => 'http://localhost/refresh/'
} )
By default, these remote requests are processed asynchronously,
during which various callbacks can be triggered (e.g. for progress
indicators and the like).
Example:
$prototype->link_to_remote( 'count', {
url => 'http://localhost/count/',
complete => 'doStuff(request)'
} )
The callbacks that may be specified are:
"loading": Called when the remote document is being loaded with data
by the browser.
"loaded": Called when the browser has finished loading the remote
document.
"interactive": Called when the user can interact with the remote
document, even though it has not finished loading.
"complete": Called when the XMLHttpRequest is complete.
If you do need synchronous processing (this will block the browser
while the request is happening), you can specify $options->{type} =
'synchronous'.
You can customize further browser side call logic by passing in
Javascript code snippets via some optional parameters. In their
order of use these are:
"confirm": Adds confirmation dialog.
"condition": Perform remote request conditionally by this
expression. Use this to describe browser-side conditions when
request should not be initiated.
"before": Called before request is initiated.
"after": Called immediately after request was initiated and before
"loading".
$prototype->observe_field( $id, \%options)
Observes the field with the DOM ID specified by $id and makes an
Ajax when its contents have changed.
Required options are:
"frequency": The frequency (in seconds) at which changes to this
field will be detected.
"url": url to be called when field content has changed.
Additional options are:
"update": Specifies the DOM ID of the element whose innerHTML should
be updated with the XMLHttpRequest response text.
"with": A JavaScript expression specifying the parameters for the
XMLHttpRequest. This defaults to value, which in the evaluated
context refers to the new field value.
Additionally, you may specify any of the options documented in
"link_to_remote".
Example TT2 template in Catalyst:
[% c.prototype.define_javascript_functions %]
<h1>[% page.title %]</h1>
<div id="view"></div>
<textarea id="editor" rows="24" cols="80">[% page.body %]</textarea>
[% url = base _ 'edit/' _ page.title %]
[% c.prototype.observe_field( 'editor', {
url => url,
with => "'body='+value",
update => 'view'
} ) %]
$prototype->observe_form( $id, \%options )
Like "observe_field", but operates on an entire form identified by
the DOM ID $id.
Options are the same as "observe_field", except the default value of
the "with" option evaluates to the serialized (request string) value
of the form.
$prototype->periodically_call_remote( \%options )
Periodically calls the specified url $options->{url} every
$options->{frequency} seconds (default is 10).
Usually used to update a specified div $options->{update} with the
results of the remote call.
The options for specifying the target with "url" and defining
callbacks is the same as "link_to_remote".
$prototype->sortable_element( $element_id, \%options )
Makes the element with the DOM ID specified by $element_id sortable
by drag-and-drop and make an Ajax call whenever the sort order has
changed. By default, the action called gets the serialized sortable
element as parameters.
Example: $prototype->sortable_element( 'my_list', { url =>
'http://foo.bar/baz' } );
In the example, the action gets a "my_list" array parameter
containing the values of the ids of elements the sortable consists
of, in the current order.
You can change the behaviour with various options, see
http://script.aculo.us for more documentation.
$prototype->submit_to_remote( $name, $value, \%options )
Returns a button input tag that will submit a form using
XMLHttpRequest in the background instead of a typical reloading via
POST.
"options" argument is the same as in "form_remote_tag"
$prototype->tag( $name, \%options, $starttag );
Returns a opening tag.
$prototype->update_element_function( $element_id, \%options, \&code )
Returns a Javascript function (or expression) that'll update a DOM
element according to the options passed.
"content": The content to use for updating. Can be left out if using
block, see example.
"action": Valid options are "update" (assumed by default), :empty,
:remove
"position": If the :action is :update, you can optionally specify
one of the following positions: :before, :top, :bottom, :after.
Example: $prototype->javascript_tag(
$prototype->update_element_function( 'products', { position =>
'bottom', content => '<p>New product!</p>' ) );
This method can also be used in combination with remote method call
where the result is evaluated afterwards to cause multiple updates
on a page.
Example: # View $prototype->form_remote_tag( { url => {
"http://foo.bar/buy" }, complete =>
$prototype->evaluate_remote_response } );
# Returning view
$prototype->update_element_function( 'cart', {
action => 'update',
position => 'bottom',
content => "<p>New Product: $product_name</p>"
} );
$prototype->update_element_function( 'status',
{ binding => "You've bought a new product!" } );
$prototype->visual_effect( $name, $element_id, \%js_options )
Returns a JavaScript snippet to be used on the Ajax callbacks for
starting visual effects.
$prototype->link_to_remote( 'Reload', {
update => 'posts',
url => 'http://foo.bar/baz',
complete => $prototype->visual_effect( 'highlight', 'posts', {
duration => '0.5'
} )
} );
SEE ALSO
Catalyst::Plugin::Prototype, Catalyst. <http://prototype.conio.net/>
AUTHOR
Sascha Kiefer, "esskar@cpan.org" Sebastian Riedel, "sri@oook.de" Marcus
Ramberg, "mramberg@cpan.org"
Built around Prototype by Sam Stephenson. Much code is ported from Ruby
on Rails javascript helpers.
THANK YOU
Drew Taylor, Leon Brocard, Andreas Marienborg
LICENSE
This library is free software. You can redistribute it and/or modify it
under the same terms as perl itself.
|