added the form functions from my latest complete version (rev 81)
[jquery.git] / form / form.js
1 /**\r
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
7  *\r
8  * Usage examples, when used with getForm().putForm():\r
9  *\r
10  * 1. Just eval the results returned from the backend.\r
11  *    $('#form-id').form();\r
12  *\r
13  * 2. Render backend results directly to target ID (expects (x)HTML).\r
14  *    $('#form-id').form('#target-id');\r
15  *\r
16  * 3. Submit to backend URL (form action) then call this function.\r
17  *    $('#form-id').form(post_callback);\r
18  *\r
19  * 4. Load target ID with backend results then call a function.\r
20  *    $('#form-id').form('#target-id', null, post_callback);\r
21  *\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
25  *\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
29  *\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
37  * @version        20060606\r
38  */\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
45                 } else {\r
46                         this.form.clicked_x = ev.pageX - this.offsetLeft;\r
47                         this.form.clicked_y = ev.pageY - this.offsetTop;\r
48                 }\r
49         });\r
50         this.submit(function(e){\r
51                 e.preventDefault();\r
52                 $(this).getForm().putForm(target, pre_cb, post_cb);\r
53                 return this;\r
54         });\r
55 };\r
56 \r
57 /**\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
62  *\r
63  * Standalone usage examples:\r
64  *\r
65  * 1. Gather form vars and return array to LHS variable.\r
66  *    var myform = $('#form-id').getForm();\r
67  *\r
68  * 2. Provide a serialized URL-ready string (after 1. above).\r
69  *    var mystring = $.param(myform.vars);\r
70  *\r
71  * 3. Gather form vars and send to RHS plugin via "this.vars".\r
72  *    $('#form-id').getForm().some_other_plugin();\r
73  *\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
78  * @version        20060606\r
79  */\r
80 $.fn.getForm = function() {\r
81         var a = [];\r
82         var ok = {INPUT:true, TEXTAREA:true, OPTION:true};\r
83 \r
84         $('*', this).each(function() {\r
85                 if (this.disabled || this.type == 'reset' || (this.type == 'checkbox' && !this.checked) || (this.type == 'radio' && !this.checked))\r
86                         return;\r
87 \r
88                 if (this.type == 'submit' || this.type == 'image') {\r
89                         if (this.form.clicked != this)\r
90                                 return;\r
91 \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
96                                                 return;\r
97                                         }\r
98                                 }\r
99                 }\r
100 \r
101                 if (!ok[this.nodeName.toUpperCase()])\r
102                         return;\r
103 \r
104                 var par = this.parentNode;\r
105                 var p = par.nodeName.toUpperCase();\r
106                 if ((p == 'SELECT' || p == 'OPTGROUP') && !this.selected)\r
107                         return;\r
108 \r
109                 var n = this.name || par.name;\r
110                 if (!n && p == 'OPTGROUP' && (par = par.parentNode))\r
111                         n = par.name;\r
112 \r
113                 if (n == undefined)\r
114                         return;\r
115 \r
116                 a.push({name: n, value: this.value});\r
117         });\r
118 \r
119         this.vars = a;\r
120 \r
121         return this;\r
122 }\r
123 \r
124 /**\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
131  *\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
141  *\r
142  * Usage example:\r
143  *\r
144  * $.fn.myvars = function() {\r
145  *   this.vars = [];\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
149  *   }\r
150  *   return this;\r
151  * }\r
152  *\r
153  * precb = function(vars) {\r
154  *   return confirm('Submit these values?\n\n'+$.param(vars));\r
155  * }\r
156  *\r
157  * $('*').myvars().putForm('#mytarget',precb,null,'myhandler.php');\r
158  *\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
169  */\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
173                         return;\r
174 \r
175         var f = this.get(0);\r
176         var url = url || f.action || '';\r
177         var mth = mth || f.method || 'POST';\r
178 \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
183         } else {\r
184                 this.vars.push({name: 'evaljs', value: 1});\r
185                 $.xml(mth, url, $.param(this.vars), function(r) { eval(r.responseText); });\r
186         }\r
187 \r
188         return this;\r
189 }\r