Generate html and javascript for the prototype library
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(...);
The module contains some code generators for Prototype, the famous JavaScript \s-1OO\s0 library and the script.aculous extensions.
The Prototype library (http://prototype.conio.net/) is designed to make \s-1AJAX\s0 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.
Makes an \s-1HTML\s0 element specified by the \s-1DOM\s0 \s-1ID\s0 $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: \*(C`url\*(C': Specifies the url where the updated value should be sent after the user presses \*(L"ok\*(R". Addtional options are: \*(C`rows\*(C': Number of rows (more than 1 will use a \s-1TEXTAREA\s0) \*(C`cols\*(C': The number of columns the text area should span (works for both single line or multi line). \*(C`size\*(C': Synonym for XcolsX when using single-line (rows=1) input \*(C`cancel_text\*(C': The text on the cancel link. (default: \*(L"cancel\*(R") \*(C`form_class_name\*(C': \s-1CSS\s0 class used for the in place edit form. (default: \*(L"inplaceeditor-form\*(R") \*(C`save_text\*(C': The text on the save link. (default: \*(L"ok\*(R") \*(C`saving_class_name\*(C': \s-1CSS\s0 class added to the element while displaying \*(L"Saving...\*(R" (removed when server responds). (default: \*(L"inplaceeditor-saving\*(R") \*(C`load_text_url\*(C': Will cause the text to be loaded from the server (useful if your text is actually textile and formatted on the server) \*(C`loading_text\*(C': If the \*(C`load_text_url\*(C' option is specified then this text is displayed while the text is being loaded from the server. (default: \*(L"Loading...\*(R") \*(C`click_to_edit_text\*(C': The text on the click-to-edit link. (default: \*(L"click to edit\*(R") \*(C`external_control\*(C': The id of an external control used to enter edit mode. \*(C`ajax_options\*(C': Pass through options to the \s-1AJAX\s0 call (see prototype's Ajax.Updater) \*(C`with\*(C': JavaScript snippet that should return what is to be sent in the Ajax call, \*(C`form\*(C' and \*(C`value\*(C' are implicit parameters Renders the value of the specified object and method with in-place editing capabilities. Returns the in_place_editor stylesheet. Adds Ajax autocomplete functionality to the text input field with the \s-1DOM\s0 \s-1ID\s0 specified by $field_id. This function expects that the called action returns a \s-1HTML\s0 <ul> list, or nothing if no entries should be displayed for autocompletion. Required options are: \*(C`url\*(C': Specifies the \s-1URL\s0 to be used in the \s-1AJAX\s0 call. Addtional options are: \*(C`update\*(C': Specifies the \s-1DOM\s0 \s-1ID\s0 of the element whose innerHTML should be updated with the autocomplete entries returned by the Ajax request. Defaults to field_id + '_auto_complete'. \*(C`with\*(C': A Javascript expression specifying the parameters for the XMLHttpRequest. This defaults to 'value', which in the evaluated context refers to the new field value. \*(C`indicator\*(C': Specifies the \s-1DOM\s0 \s-1ID\s0 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=\*(L"display:none\*(R"' 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. \*(C`tokens\*(C': A string or an array of strings containing separator tokens for tokenized incremental autocompletion. Example: \*(C`<tokens =\*(C' ','>> would allow multiple autocompletion entries, separated by commas. \*(C`min_chars\*(C': The minimum number of characters that should be in the input field before an Ajax call is made to the server. \*(C`on_hide\*(C': A Javascript expression that is called when the autocompletion div is hidden. The expression should take two variables: element and update. Element is a \s-1DOM\s0 element for the field, update is a \s-1DOM\s0 element for the div from which the innerHTML is replaced. \*(C`on_show\*(C': Like on_hide, only now the expression is called then the div is shown. \*(C`select\*(C': 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 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) ); } 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 \s-1AJAX\s0 calls, Returns the auto_complete stylesheet. Returns a block with opening tag, content, and ending tag. Useful for autogenerating tags like <a href=\*(L"http://catalyst.perl.org\*(R"Catalyst Homepage</a>>. The first parameter is the tag name, i.e. 'a' or 'img'. Returns an input tag of the \*(L"text\*(R" 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. Returns the library of JavaScript functions and objects, in a script block. Notes for Catalyst users: You can use \*(C`script/myapp_create.pl Prototype\*(C' to generate a static JavaScript file which then can be included via remote \*(C`script\*(C' tag. Makes the element with the \s-1DOM\s0 \s-1ID\s0 specified by \*(C`element_id\*(C' draggable. Example: $prototype->draggable_element( 'my_image', { revert => 'true' } ); The available options are:
Default: none. Sets whether the element should only be draggable by an embedded handle. The value is a string referencing a \s-1CSS\s0 class. The first child/grandchild/etc. element found within the element that has this \s-1CSS\s0 class will be used as the handle.
Default: false. If set to true, the element returns to its original position when the drags ends.
Default: none. If set to 'horizontal' or 'vertical' the drag will be constrained to take place only horizontally or vertically.
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.
Makes the element with the \s-1DOM\s0 \s-1ID\s0 specified by \*(C`element_id\*(C' receive dropped draggable elements (created by draggable_element). And make an \s-1AJAX\s0 call. By default, the action called gets the \s-1DOM\s0 \s-1ID\s0 of the element as parameter. Example:
$prototype->drop_receiving_element( 'my_cart', { url => 'http://foo.bar/add' } );
Required options are:
The \s-1URL\s0 for the \s-1AJAX\s0 call.
Additional options are:
Default: none. Set accept to a string or an array of strings describing \s-1CSS\s0 classes. The Droppable will only accept Draggables that have one or more of these \s-1CSS\s0 classes.
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.
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:
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.
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. Returns a form tag that will submit in the background using XMLHttpRequest, instead of the regular reloading \s-1POST\s0 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 \*(C`url\*(C' and defining callbacks are the same as \*(C`link_to_remote\*(C'. Returns a javascript block with opening tag, content and ending tag. 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 => '' } ) Returns a link to a remote action defined by options \*(C`url\*(C' that's called in the background using XMLHttpRequest. The result of that request can then be inserted into a \s-1DOM\s0 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: \*(C`loading\*(C': Called when the remote document is being loaded with data by the browser. \*(C`loaded\*(C': Called when the browser has finished loading the remote document. \*(C`interactive\*(C': Called when the user can interact with the remote document, even though it has not finished loading. \*(C`complete\*(C': 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: \*(C`confirm\*(C': Adds confirmation dialog. \*(C`condition\*(C': Perform remote request conditionally by this expression. Use this to describe browser-side conditions when request should not be initiated. \*(C`before\*(C': Called before request is initiated. \*(C`after\*(C': Called immediately after request was initiated and before \*(C`loading\*(C'. Observes the field with the \s-1DOM\s0 \s-1ID\s0 specified by $id and makes an Ajax when its contents have changed. Required options are: \*(C`frequency\*(C': The frequency (in seconds) at which changes to this field will be detected. \*(C`url\*(C': url to be called when field content has changed. Additional options are: \*(C`update\*(C': Specifies the \s-1DOM\s0 \s-1ID\s0 of the element whose innerHTML should be updated with the XMLHttpRequest response text. \*(C`with\*(C': 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 \*(C`link_to_remote\*(C'. Example \s-1TT2\s0 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' } ) %] Like \*(C`observe_field\*(C', but operates on an entire form identified by the \s-1DOM\s0 \s-1ID\s0 $id. Options are the same as \*(C`observe_field\*(C', except the default value of the \*(C`with\*(C' option evaluates to the serialized (request string) value of the form. 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 \*(C`url\*(C' and defining callbacks is the same as \*(C`link_to_remote\*(C'. Makes the element with the \s-1DOM\s0 \s-1ID\s0 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 \*(L"my_list\*(R" 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. Returns a button input tag that will submit a form using XMLHttpRequest in the background instead of a typical reloading via \s-1POST\s0. \*(C`options\*(C' argument is the same as in \*(C`form_remote_tag\*(C' Returns a opening tag. Returns a Javascript function (or expression) that'll update a \s-1DOM\s0 element according to the options passed. \*(C`content\*(C': The content to use for updating. Can be left out if using block, see example. \*(C`action\*(C': Valid options are \*(C`update\*(C' (assumed by default), :empty, :remove \*(C`position\*(C': 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 => { \*(L"http://foo.bar/buy\*(R" }, 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!" } ); 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' } ) } );
Catalyst::Plugin::Prototype, Catalyst. <http://prototype.conio.net/>
Sascha Kiefer, \*(C`[email protected]\*(C' Sebastian Riedel, \*(C`[email protected]\*(C' Marcus Ramberg, \*(C`[email protected]\*(C'
Built around Prototype by Sam Stephenson. Much code is ported from Ruby on Rails javascript helpers.
Drew Taylor, Leon Brocard, Andreas Marienborg
This library is free software. You can redistribute it and/or modify it under the same terms as perl itself.