Bah i need some sleep...
[jquery.git] / form / form.js
index e69de29..5194d6f 100644 (file)
@@ -0,0 +1,210 @@
+/**\r
+ * A method for submitting an HTML form using AJAX, as opposed to the\r
+ * standard page-load way.\r
+ *\r
+ * This method attempts to mimic the functionality of the original form\r
+ * as best as possible (duplicating the method, action, and exact contents\r
+ * of the form).\r
+ *\r
+ * There are three different resulting operations that can occur, after\r
+ * your form has been submitted.\r
+ *\r
+ * 1. The form is submitted and a callback is fired, letting you know\r
+ *    when it's done:\r
+ *    $("form").ajaxSubmit(function(){\r
+ *      alert("all done!");\r
+ *    });\r
+ *\r
+ * 2. The form is submitted and the resulting HTML contents are injected\r
+ *    into the page, at your specified location.\r
+ *    $("form").ajaxSubmit("#destination");\r
+ *\r
+ * 3. The form is submitted and the results returned from the server are\r
+ *    automatically executed (useful for having the server return more\r
+ *    Javascript commands to execute).\r
+ *    $("form").ajaxSubmit();\r
+ *\r
+ * Additionally, an optional pre-submit callback can be provided. If it,\r
+ * when called with the contents of the form, returns false, the form will\r
+ * not be submitted.\r
+ *\r
+ * Finally, both the URL and method of the form submission can be\r
+ * overidden using the 'url' and 'mth' arguments.\r
+ *\r
+ * @param target   arg for the target id element to render\r
+ * @param post_cb  callback after any results are returned\r
+ * @param pre_cb   callback function before submission\r
+ * @param url      form action override\r
+ * @param mth      form method override\r
+ * @return         "this" object\r
+ * @see            ajaxForm(), serialize(), load(), $.ajax()\r
+ * @author         Mark Constable (markc@renta.net)\r
+ * @author         G. vd Hoven, Mike Alsup, Sam Collett, John Resig\r
+ */\r
+$.fn.ajaxSubmit = function(target, post_cb, pre_cb, url, mth) {\r
+       if ( !this.vars ) this.serialize();\r
+       \r
+       if (pre_cb && pre_cb.constructor == Function)\r
+               if (pre_cb(this.vars) === false) 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
+               $.ajax(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
+               $.ajax(mth, url, $.param(this.vars), function(r) {\r
+                       eval(r.responseText);\r
+               });\r
+       }\r
+\r
+       return this;\r
+};\r
+\r
+/**\r
+ * This function can be used to turn any HTML form into a form\r
+ * that submits using AJAX only.\r
+ *\r
+ * The purpose of using this method, instead of the ajaxSubmit()\r
+ * and submit() methods, is to make absolutely sure that the\r
+ * coordinates of <input type="image"/> elements are transmitted\r
+ * correctly OR figuring out exactly which <input type="submit"/>\r
+ * element was clicked to submit the form.\r
+ *\r
+ * If neither of the above points are important to you, then you'll\r
+ * probably just want to stick with the simpler ajaxSubmit() function.\r
+ *\r
+ * Usage examples, similar to ajaxSubmit():\r
+ *\r
+ * 1. Just eval the results returned from the backend.\r
+ *    $('#form-id').ajaxForm();\r
+ *\r
+ * 2. Render backend results directly to target ID (expects (x)HTML).\r
+ *    $('#form-id').ajaxForm('#target-id');\r
+ *\r
+ * 3. Submit to backend URL (form action) then call this function.\r
+ *    $('#form-id').ajaxForm(post_callback);\r
+ *\r
+ * 4. Load target ID with backend results then call a function.\r
+ *    $('#form-id').ajaxForm('#target-id', post_callback);\r
+ *\r
+ * 5. Call a browser function (for validation) and then (optionally)\r
+ *    load server results to target ID.\r
+ *    $('#form-id').ajaxForm('#target-id', null, 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').ajaxForm('#target-id', post_callback, pre_callback);\r
+ *\r
+ * @param target   arg for the target id element to render\r
+ * @param post_cb  callback after any results are returned\r
+ * @param pre_cb   callback function before submission\r
+ * @return         the jQuery Object\r
+ * @see            serialize(), ajaxSubmit()\r
+ * @author         Mark Constable (markc@renta.net)\r
+ * @author         G. vd Hoven, Mike Alsup, Sam Collett, John Resig\r
+ */\r
+$.fn.ajaxForm = function(target, post_cb, pre_cb) {\r
+       return this.each(function(){\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
+       }).submit(function(e){\r
+               e.preventDefault();\r
+               $(this).ajaxSubmit(target, post_cb, pre_cb);\r
+               return false;\r
+       });\r
+};\r
+\r
+/**\r
+ * A simple wrapper function that sits around the .serialize()\r
+ * method, allowing you to easily extract the data stored within\r
+ * a form.\r
+ *\r
+ * Usage examples:\r
+ *\r
+ * 1. Serialize the contents of a form to a & and = delmited string:\r
+ *    $.param( $("form").formdata() );\r
+ *\r
+ * @return         An array of name/value pairs representing the form\r
+ * @see            serialize()\r
+ # @author         John Resig\r
+ */\r
+$.fn.formdata = function(){\r
+       this.serialize();\r
+       return this.vars;\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 formdata() or ajaxSubmit() but can\r
+ * be used standalone as long as you don't need the x and y coordinates\r
+ * associated with an <input type="image"/> element..\r
+ *\r
+ * Standalone usage examples:\r
+ *\r
+ * 1. Gather form vars and return array to LHS variable.\r
+ *    var myform = $('#form-id').serialize();\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').serialize().some_other_plugin();\r
+ *\r
+ * @return         the jQuery Object\r
+ * @see            ajaxForm(), ajaxSubmit()\r
+ * @author         Mark Constable (markc@renta.net)\r
+ * @author         G. vd Hoven, Mike Alsup, Sam Collett, John Resig\r
+ */\r
+$.fn.serialize = 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' || \r
+                       (this.type == 'checkbox' && !this.checked) || \r
+                       (this.type == 'radio' && !this.checked)) return;\r
+\r
+               if (this.type == 'submit' || this.type == 'image') {\r
+                       if (this.form.clicked != this) 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) return;\r
+\r
+               var n = this.name;\r
+               if (!n) n = (p == 'OPTGROUP') ? par.parentNode.name : (p == 'SELECT') ? par.name : this.name;\r
+               if (n == undefined) return;\r
+\r
+               a.push({name: n, value: this.value});\r
+       });\r
+       \r
+       this.vars = a;\r
+\r
+       return this;\r
+};\r