added the form functions from my latest complete version (rev 81)
authorGilles van den Hoven <gilles0181@gmail.com>
Wed, 21 Jun 2006 10:08:13 +0000 (10:08 +0000)
committerGilles van den Hoven <gilles0181@gmail.com>
Wed, 21 Jun 2006 10:08:13 +0000 (10:08 +0000)
form/form.js

index e69de29..80f0ba2 100644 (file)
@@ -0,0 +1,189 @@
+/**\r
+ * Initial frontend function to submit form variables. This function\r
+ * is for registering coordinates, in the case of an image being used\r
+ * as the submit element, and sets up an event to listen and wait for\r
+ * a form submit click. It then calls any following chained functions\r
+ * to actually gather the variables and submit them.\r
+ *\r
+ * Usage examples, when used with getForm().putForm():\r
+ *\r
+ * 1. Just eval the results returned from the backend.\r
+ *    $('#form-id').form();\r
+ *\r
+ * 2. Render backend results directly to target ID (expects (x)HTML).\r
+ *    $('#form-id').form('#target-id');\r
+ *\r
+ * 3. Submit to backend URL (form action) then call this function.\r
+ *    $('#form-id').form(post_callback);\r
+ *\r
+ * 4. Load target ID with backend results then call a function.\r
+ *    $('#form-id').form('#target-id', null, post_callback);\r
+ *\r
+ * 5. Call a browser function (for validation) and then (optionally)\r
+ *    load server results to target ID.\r
+ *    $('#form-id').form('#target-id', pre_callback);\r
+ *\r
+ * 6. Call validation function first then load server results to\r
+ *    target ID and then also call a browser function.\r
+ *    $('#form-id').form('#target-id', pre_callback, post_callback);\r
+ *\r
+ * @param target   arg for the target id element to render\r
+ * @param pre_cb   callback function before submission\r
+ * @param post_cb  callback after any results are returned\r
+ * @return         "this" object\r
+ * @see            getForm(), putForm()\r
+ * @author         Mark Constable (markc@renta.net)\r
+ * @author         G. vd Hoven, Mike Alsup, Sam Collett\r
+ * @version        20060606\r
+ */\r
+$.fn.form = function(target, pre_cb, post_cb) {\r
+       $('input[@type="submit"],input[@type="image"]', this).click(function(ev){\r
+               this.form.clicked = this;\r
+               if (ev.offsetX != undefined) {\r
+                       this.form.clicked_x = ev.offsetX;\r
+                       this.form.clicked_y = ev.offsetY;\r
+               } else {\r
+                       this.form.clicked_x = ev.pageX - this.offsetLeft;\r
+                       this.form.clicked_y = ev.pageY - this.offsetTop;\r
+               }\r
+       });\r
+       this.submit(function(e){\r
+               e.preventDefault();\r
+               $(this).getForm().putForm(target, pre_cb, post_cb);\r
+               return this;\r
+       });\r
+};\r
+\r
+/**\r
+ * This function gathers form element variables into an array that\r
+ * is embedded into the current "this" variable as "this.vars". It\r
+ * is normally used in conjunction with form() and putForm() but can\r
+ * be used standalone as long as an image is not used for submission.\r
+ *\r
+ * Standalone usage examples:\r
+ *\r
+ * 1. Gather form vars and return array to LHS variable.\r
+ *    var myform = $('#form-id').getForm();\r
+ *\r
+ * 2. Provide a serialized URL-ready string (after 1. above).\r
+ *    var mystring = $.param(myform.vars);\r
+ *\r
+ * 3. Gather form vars and send to RHS plugin via "this.vars".\r
+ *    $('#form-id').getForm().some_other_plugin();\r
+ *\r
+ * @return         "this" object\r
+ * @see            form(), putForm()\r
+ * @author         Mark Constable (markc@renta.net)\r
+ * @author         G. vd Hoven, Mike Alsup, Sam Collett\r
+ * @version        20060606\r
+ */\r
+$.fn.getForm = function() {\r
+       var a = [];\r
+       var ok = {INPUT:true, TEXTAREA:true, OPTION:true};\r
+\r
+       $('*', this).each(function() {\r
+               if (this.disabled || this.type == 'reset' || (this.type == 'checkbox' && !this.checked) || (this.type == 'radio' && !this.checked))\r
+                       return;\r
+\r
+               if (this.type == 'submit' || this.type == 'image') {\r
+                       if (this.form.clicked != this)\r
+                               return;\r
+\r
+                       if (this.type == 'image') {\r
+                               if (this.form.clicked_x) {\r
+                                               a.push({name: this.name+'_x', value: this.form.clicked_x});\r
+                                               a.push({name: this.name+'_y', value: this.form.clicked_y});\r
+                                               return;\r
+                                       }\r
+                               }\r
+               }\r
+\r
+               if (!ok[this.nodeName.toUpperCase()])\r
+                       return;\r
+\r
+               var par = this.parentNode;\r
+               var p = par.nodeName.toUpperCase();\r
+               if ((p == 'SELECT' || p == 'OPTGROUP') && !this.selected)\r
+                       return;\r
+\r
+               var n = this.name || par.name;\r
+               if (!n && p == 'OPTGROUP' && (par = par.parentNode))\r
+                       n = par.name;\r
+\r
+               if (n == undefined)\r
+                       return;\r
+\r
+               a.push({name: n, value: this.value});\r
+       });\r
+\r
+       this.vars = a;\r
+\r
+       return this;\r
+}\r
+\r
+/**\r
+ * Final form submission plugin usually used in conjunction with\r
+ * form() and getForm(). If a second argument is a valid function\r
+ * then it will be called before the form vars are sent to the\r
+ * backend. If this pre-submit function returns exactly "false"\r
+ * then it will abort further processing otherwise the process\r
+ * will continue according to the first and third arguments.\r
+ *\r
+ * If the first argument is a function, and it exists, then the form\r
+ * values will be submitted and that callback function called. If\r
+ * the first argument is a string value then the "load()" plugin\r
+ * will be called which will populate the innerHTML of the indicated\r
+ * element and a callback will be called if there is third argument.\r
+ * If there are no arguments then the form values are submitted with\r
+ * an additional variable (evaljs=1) which indicates to the backend\r
+ * to to prepare the returned results for evaluation, ie; the result\r
+ * needs to be valid javascript all on a single line.\r
+ *\r
+ * Usage example:\r
+ *\r
+ * $.fn.myvars = function() {\r
+ *   this.vars = [];\r
+ *   for (var i in this) {\r
+ *     if (this[i] instanceof Function || this[i] == null) continue;\r
+ *     this.vars.push({name: i, value: this[i].length});\r
+ *   }\r
+ *   return this;\r
+ * }\r
+ *\r
+ * precb = function(vars) {\r
+ *   return confirm('Submit these values?\n\n'+$.param(vars));\r
+ * }\r
+ *\r
+ * $('*').myvars().putForm('#mytarget',precb,null,'myhandler.php');\r
+ *\r
+ * @param target   arg for the target id element to render\r
+ * @param pre_cb   callback function before submission\r
+ * @param post_cb  callback after any results are returned\r
+ * @param url      form action override\r
+ * @param mth      form method override\r
+ * @return         "this" object\r
+ * @see            form(), getForm(), load(), xml()\r
+ * @author         Mark Constable (markc@renta.net)\r
+ * @author         G. vd Hoven, Mike Alsup, Sam Collett\r
+ * @version        20060606\r
+ */\r
+$.fn.putForm = function(target, pre_cb, post_cb, url, mth) {\r
+       if (pre_cb && pre_cb.constructor == Function)\r
+               if (pre_cb(this.vars) === false)\r
+                       return;\r
+\r
+       var f = this.get(0);\r
+       var url = url || f.action || '';\r
+       var mth = mth || f.method || 'POST';\r
+\r
+       if (target && target.constructor == Function) {\r
+               $.xml(mth, url, $.param(this.vars), target);\r
+       } else if (target && target.constructor == String) {\r
+               $(target).load(url, this.vars, post_cb);\r
+       } else {\r
+               this.vars.push({name: 'evaljs', value: 1});\r
+               $.xml(mth, url, $.param(this.vars), function(r) { eval(r.responseText); });\r
+       }\r
+\r
+       return this;\r
+}\r