2 * Initial frontend function to submit form variables. This function
\r
3 * is for registering coordinates, in the case of an image being used
\r
4 * as the submit element, and sets up an event to listen and wait for
\r
5 * a form submit click. It then calls any following chained functions
\r
6 * to actually gather the variables and submit them.
\r
8 * Usage examples, when used with getForm().putForm():
\r
10 * 1. Just eval the results returned from the backend.
\r
11 * $('#form-id').form();
\r
13 * 2. Render backend results directly to target ID (expects (x)HTML).
\r
14 * $('#form-id').form('#target-id');
\r
16 * 3. Submit to backend URL (form action) then call this function.
\r
17 * $('#form-id').form(post_callback);
\r
19 * 4. Load target ID with backend results then call a function.
\r
20 * $('#form-id').form('#target-id', null, post_callback);
\r
22 * 5. Call a browser function (for validation) and then (optionally)
\r
23 * load server results to target ID.
\r
24 * $('#form-id').form('#target-id', pre_callback);
\r
26 * 6. Call validation function first then load server results to
\r
27 * target ID and then also call a browser function.
\r
28 * $('#form-id').form('#target-id', pre_callback, post_callback);
\r
30 * @param target arg for the target id element to render
\r
31 * @param pre_cb callback function before submission
\r
32 * @param post_cb callback after any results are returned
\r
33 * @return "this" object
\r
34 * @see getForm(), putForm()
\r
35 * @author Mark Constable (markc@renta.net)
\r
36 * @author G. vd Hoven, Mike Alsup, Sam Collett
\r
39 $.fn.form = function(target, pre_cb, post_cb) {
\r
40 $('input[@type="submit"],input[@type="image"]', this).click(function(ev){
\r
41 this.form.clicked = this;
\r
42 if (ev.offsetX != undefined) {
\r
43 this.form.clicked_x = ev.offsetX;
\r
44 this.form.clicked_y = ev.offsetY;
\r
46 this.form.clicked_x = ev.pageX - this.offsetLeft;
\r
47 this.form.clicked_y = ev.pageY - this.offsetTop;
\r
50 this.submit(function(e){
\r
52 $(this).getForm().putForm(target, pre_cb, post_cb);
\r
58 * This function gathers form element variables into an array that
\r
59 * is embedded into the current "this" variable as "this.vars". It
\r
60 * is normally used in conjunction with form() and putForm() but can
\r
61 * be used standalone as long as an image is not used for submission.
\r
63 * Standalone usage examples:
\r
65 * 1. Gather form vars and return array to LHS variable.
\r
66 * var myform = $('#form-id').getForm();
\r
68 * 2. Provide a serialized URL-ready string (after 1. above).
\r
69 * var mystring = $.param(myform.vars);
\r
71 * 3. Gather form vars and send to RHS plugin via "this.vars".
\r
72 * $('#form-id').getForm().some_other_plugin();
\r
74 * @return "this" object
\r
75 * @see form(), putForm()
\r
76 * @author Mark Constable (markc@renta.net)
\r
77 * @author G. vd Hoven, Mike Alsup, Sam Collett
\r
80 $.fn.getForm = function() {
\r
82 var ok = {INPUT:true, TEXTAREA:true, OPTION:true};
\r
84 $('*', this).each(function() {
\r
85 if (this.disabled || this.type == 'reset' || (this.type == 'checkbox' && !this.checked) || (this.type == 'radio' && !this.checked))
\r
88 if (this.type == 'submit' || this.type == 'image') {
\r
89 if (this.form.clicked != this)
\r
92 if (this.type == 'image') {
\r
93 if (this.form.clicked_x) {
\r
94 a.push({name: this.name+'_x', value: this.form.clicked_x});
\r
95 a.push({name: this.name+'_y', value: this.form.clicked_y});
\r
101 if (!ok[this.nodeName.toUpperCase()])
\r
104 var par = this.parentNode;
\r
105 var p = par.nodeName.toUpperCase();
\r
106 if ((p == 'SELECT' || p == 'OPTGROUP') && !this.selected)
\r
109 var n = this.name || par.name;
\r
110 if (!n && p == 'OPTGROUP' && (par = par.parentNode))
\r
113 if (n == undefined)
\r
116 a.push({name: n, value: this.value});
\r
125 * Final form submission plugin usually used in conjunction with
\r
126 * form() and getForm(). If a second argument is a valid function
\r
127 * then it will be called before the form vars are sent to the
\r
128 * backend. If this pre-submit function returns exactly "false"
\r
129 * then it will abort further processing otherwise the process
\r
130 * will continue according to the first and third arguments.
\r
132 * If the first argument is a function, and it exists, then the form
\r
133 * values will be submitted and that callback function called. If
\r
134 * the first argument is a string value then the "load()" plugin
\r
135 * will be called which will populate the innerHTML of the indicated
\r
136 * element and a callback will be called if there is third argument.
\r
137 * If there are no arguments then the form values are submitted with
\r
138 * an additional variable (evaljs=1) which indicates to the backend
\r
139 * to to prepare the returned results for evaluation, ie; the result
\r
140 * needs to be valid javascript all on a single line.
\r
144 * $.fn.myvars = function() {
\r
146 * for (var i in this) {
\r
147 * if (this[i] instanceof Function || this[i] == null) continue;
\r
148 * this.vars.push({name: i, value: this[i].length});
\r
153 * precb = function(vars) {
\r
154 * return confirm('Submit these values?\n\n'+$.param(vars));
\r
157 * $('*').myvars().putForm('#mytarget',precb,null,'myhandler.php');
\r
159 * @param target arg for the target id element to render
\r
160 * @param pre_cb callback function before submission
\r
161 * @param post_cb callback after any results are returned
\r
162 * @param url form action override
\r
163 * @param mth form method override
\r
164 * @return "this" object
\r
165 * @see form(), getForm(), load(), xml()
\r
166 * @author Mark Constable (markc@renta.net)
\r
167 * @author G. vd Hoven, Mike Alsup, Sam Collett
\r
168 * @version 20060606
\r
170 $.fn.putForm = function(target, pre_cb, post_cb, url, mth) {
\r
171 if (pre_cb && pre_cb.constructor == Function)
\r
172 if (pre_cb(this.vars) === false)
\r
175 var f = this.get(0);
\r
176 var url = url || f.action || '';
\r
177 var mth = mth || f.method || 'POST';
\r
179 if (target && target.constructor == Function) {
\r
180 $.xml(mth, url, $.param(this.vars), target);
\r
181 } else if (target && target.constructor == String) {
\r
182 $(target).load(url, this.vars, post_cb);
\r
184 this.vars.push({name: 'evaljs', value: 1});
\r
185 $.xml(mth, url, $.param(this.vars), function(r) { eval(r.responseText); });
\r