This series of classes creates a popup element that can contain a form that will be processed via an AJAX callback upon submission. The additional components can be used independently if desired.
Simply attach this to any elements (anchor tags usually) you would like to open the form when clicked:
<a class="popup_form" url="the_form.html">Click Here</a>
and in your javascript:
$$('.popup_form').each(function(popup) { var popup_form = new PopupForm(popup, { 'content_processor': function(html) { html.getElements('.ajax_form').each(function(ajax_form) { var ajax = new PopupForm.AjaxForm(ajax_form); }); } }); });
Note that you need to attach the AjaxForm class to the form within the response; it does not happen automatically.
When an element that has been attached to the PopupForm is clicked, a Mask is created, and an element with the class 'over_mask" is created as well. You will need to style the mask and over_mask so that the z-index of the over_mask places it above the mask, which will cover the entire screen. Any elements with a class 'cancel' will close the form, as well as if the user hits the escape key.
Here are the options available with the PopupForm:
When the form is submitted, a JSON data structure is expected in the response. It can contain:
Here are the options for the class: * submit_selector: The selector for the object within the form to execute the submit action. The class will also execute on the form's submit action. * target_selector: Default target, if one doesn't exist in the json response. * content_processor: A function to call on any HTML that is injected. Passed the parent dom element as a single argument. * highlight_color_start: The color for the highlight, leave null for default #ff8 * highlight_color_end: The color for the highlight to end, leave null for the default background color
The AjaxForm class changes the behavior of a form element so that it can be processed via AJAX rather than being submitted and a new page loading. The posting URL should be in the rel tag of the form container, or can be the action for the form. This form actually extends the JSONProcessor class.
The form will have an additional input, name set in options.ajax_form_field, with a value of "1". Use this to verify that the form was indeed submitted via the ajax form, or if the user had javascript turned off. It also posts a variable 'HTTP_REFERER' (options.referer_field) that contains the URL of the referer, for IE who doesn't pass that in the headers.
When the form is submitted, the resulting JSON is passed to the JSONProcessor. In addition, the following can be returned: * If a message is returned, the form will not execute the completion function unless a value for execute_complete is passed as well. * clear_form: Will reset the form. * keep_open: Do not execute the complete function * execute_complete: Execute the complete function, regardless of any decisions prior.
The complete function will normally be attached to the PopupForm class, but you can create your own if you want to use this outside of the PopupForm.
An optional spinner may be used during the form processing.
Store in the form 'onSubmit' a function to execute when the form is submitted. Store 'onComplete' a function to execute when the form submission gets returned, receives the json as the argument Store 'onFail' a function to execute if the form submission fails, receives the json as the argument
You can require confirmation by putting in the rel tag an object with key 'confirm' and the confirmation message.
If you want to upload a file, you can do that with an file input element. The form will automatically create an iframe to process the upload, and will also set the enctype correctly. Treat the upload like any other form submission.
Here are the options for the AjaxForm class:
PopupForm.StringFormatter includes an extendor for PopupForm.AjaxForm so that all of the hooks are automatically added. It forces formatting on fields based on a template. Several formatting options are bundled by default, but it is easy to add additional formatting options and override the default formatting if desired.
Simply assign the class to the fields to get it to hook into the formatter: * telephone: formats a phone number (XXX) XXX-XXXX * zip: formats a zip code XXXXX * format_date: forces a date format XX/XX/XXXX * numeric_only: requires numbers * cc_num: Credit card number (with spaces between number blocks) * cc_type: Optional, if a select element has this class it will change to the appropriate card type based on the number being entered * routing: Bank routing number (9 digits) XXXXXXXXX * ssn: Social Security Number XXX-XX-XXXX
Also included is a summing total based on fields within the form. As the individual fields are updated, the total value automatically updates. * total_count: assign this class to all fields to be used in the summation * total_field: the ID for the input element that will contain the calculated total * total_text: the ID for any dom element that should display the total as text (will have its html injected into)
Here is a sample to extend into other field types - use this in a file the requires PopupForm.StringFormatter
PopupForm.AjaxForm = new Class({ Extends: PopupForm.AjaxForm, options: { code_format: 'XX-XXX-XX', code_format_selector: '.email_code' }, init_hooks: function(form) { this.parent(form); // Code formatter form.getElements(this.options.code_format_selector).each(function(item) { item.store('ajax_form',this); item.addEvent('keyup',function() { this.format_other_field(item,this.options.code_format); }.bind(this)); },this); } });
A note on comments here: These comments are moderated. No comments will show up until they are approved. Comments that are not productive (i.e. inflammatory, rude, etc) will not be approved.
Found a bug in this plugin? Please report it this repository's Github Issues.
blog comments powered by Disqus