Mercurial > hgbook
annotate web/javascript/form.js @ 751:751ee9bf2e8d
ch00-preface.xml: Typo fix
author | Dongsheng Song <dongsheng.song@gmail.com> |
---|---|
date | Fri, 20 Mar 2009 16:59:07 +0800 |
parents | ad304b606163 |
children |
rev | line source |
---|---|
673
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
1 /* |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
2 * jQuery Form Plugin |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
3 * @requires jQuery v1.1 or later |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
4 * |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
5 * Examples at: http://malsup.com/jquery/form/ |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
6 * Dual licensed under the MIT and GPL licenses: |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
7 * http://www.opensource.org/licenses/mit-license.php |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
8 * http://www.gnu.org/licenses/gpl.html |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
9 * |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
10 * Revision: $Id$ |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
11 */ |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
12 (function($) { |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
13 /** |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
14 * ajaxSubmit() provides a mechanism for submitting an HTML form using AJAX. |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
15 * |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
16 * ajaxSubmit accepts a single argument which can be either a success callback function |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
17 * or an options Object. If a function is provided it will be invoked upon successful |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
18 * completion of the submit and will be passed the response from the server. |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
19 * If an options Object is provided, the following attributes are supported: |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
20 * |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
21 * target: Identifies the element(s) in the page to be updated with the server response. |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
22 * This value may be specified as a jQuery selection string, a jQuery object, |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
23 * or a DOM element. |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
24 * default value: null |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
25 * |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
26 * url: URL to which the form data will be submitted. |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
27 * default value: value of form's 'action' attribute |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
28 * |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
29 * type: The method in which the form data should be submitted, 'GET' or 'POST'. |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
30 * default value: value of form's 'method' attribute (or 'GET' if none found) |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
31 * |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
32 * data: Additional data to add to the request, specified as key/value pairs (see $.ajax). |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
33 * |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
34 * beforeSubmit: Callback method to be invoked before the form is submitted. |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
35 * default value: null |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
36 * |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
37 * success: Callback method to be invoked after the form has been successfully submitted |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
38 * and the response has been returned from the server |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
39 * default value: null |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
40 * |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
41 * dataType: Expected dataType of the response. One of: null, 'xml', 'script', or 'json' |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
42 * default value: null |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
43 * |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
44 * semantic: Boolean flag indicating whether data must be submitted in semantic order (slower). |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
45 * default value: false |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
46 * |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
47 * resetForm: Boolean flag indicating whether the form should be reset if the submit is successful |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
48 * |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
49 * clearForm: Boolean flag indicating whether the form should be cleared if the submit is successful |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
50 * |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
51 * |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
52 * The 'beforeSubmit' callback can be provided as a hook for running pre-submit logic or for |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
53 * validating the form data. If the 'beforeSubmit' callback returns false then the form will |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
54 * not be submitted. The 'beforeSubmit' callback is invoked with three arguments: the form data |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
55 * in array format, the jQuery object, and the options object passed into ajaxSubmit. |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
56 * The form data array takes the following form: |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
57 * |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
58 * [ { name: 'username', value: 'jresig' }, { name: 'password', value: 'secret' } ] |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
59 * |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
60 * If a 'success' callback method is provided it is invoked after the response has been returned |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
61 * from the server. It is passed the responseText or responseXML value (depending on dataType). |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
62 * See jQuery.ajax for further details. |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
63 * |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
64 * |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
65 * The dataType option provides a means for specifying how the server response should be handled. |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
66 * This maps directly to the jQuery.httpData method. The following values are supported: |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
67 * |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
68 * 'xml': if dataType == 'xml' the server response is treated as XML and the 'success' |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
69 * callback method, if specified, will be passed the responseXML value |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
70 * 'json': if dataType == 'json' the server response will be evaluted and passed to |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
71 * the 'success' callback, if specified |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
72 * 'script': if dataType == 'script' the server response is evaluated in the global context |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
73 * |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
74 * |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
75 * Note that it does not make sense to use both the 'target' and 'dataType' options. If both |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
76 * are provided the target will be ignored. |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
77 * |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
78 * The semantic argument can be used to force form serialization in semantic order. |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
79 * This is normally true anyway, unless the form contains input elements of type='image'. |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
80 * If your form must be submitted with name/value pairs in semantic order and your form |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
81 * contains an input of type='image" then pass true for this arg, otherwise pass false |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
82 * (or nothing) to avoid the overhead for this logic. |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
83 * |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
84 * |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
85 * When used on its own, ajaxSubmit() is typically bound to a form's submit event like this: |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
86 * |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
87 * $("#form-id").submit(function() { |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
88 * $(this).ajaxSubmit(options); |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
89 * return false; // cancel conventional submit |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
90 * }); |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
91 * |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
92 * When using ajaxForm(), however, this is done for you. |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
93 * |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
94 * @example |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
95 * $('#myForm').ajaxSubmit(function(data) { |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
96 * alert('Form submit succeeded! Server returned: ' + data); |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
97 * }); |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
98 * @desc Submit form and alert server response |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
99 * |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
100 * |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
101 * @example |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
102 * var options = { |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
103 * target: '#myTargetDiv' |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
104 * }; |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
105 * $('#myForm').ajaxSubmit(options); |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
106 * @desc Submit form and update page element with server response |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
107 * |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
108 * |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
109 * @example |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
110 * var options = { |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
111 * success: function(responseText) { |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
112 * alert(responseText); |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
113 * } |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
114 * }; |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
115 * $('#myForm').ajaxSubmit(options); |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
116 * @desc Submit form and alert the server response |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
117 * |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
118 * |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
119 * @example |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
120 * var options = { |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
121 * beforeSubmit: function(formArray, jqForm) { |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
122 * if (formArray.length == 0) { |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
123 * alert('Please enter data.'); |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
124 * return false; |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
125 * } |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
126 * } |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
127 * }; |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
128 * $('#myForm').ajaxSubmit(options); |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
129 * @desc Pre-submit validation which aborts the submit operation if form data is empty |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
130 * |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
131 * |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
132 * @example |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
133 * var options = { |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
134 * url: myJsonUrl.php, |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
135 * dataType: 'json', |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
136 * success: function(data) { |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
137 * // 'data' is an object representing the the evaluated json data |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
138 * } |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
139 * }; |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
140 * $('#myForm').ajaxSubmit(options); |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
141 * @desc json data returned and evaluated |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
142 * |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
143 * |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
144 * @example |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
145 * var options = { |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
146 * url: myXmlUrl.php, |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
147 * dataType: 'xml', |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
148 * success: function(responseXML) { |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
149 * // responseXML is XML document object |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
150 * var data = $('myElement', responseXML).text(); |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
151 * } |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
152 * }; |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
153 * $('#myForm').ajaxSubmit(options); |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
154 * @desc XML data returned from server |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
155 * |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
156 * |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
157 * @example |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
158 * var options = { |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
159 * resetForm: true |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
160 * }; |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
161 * $('#myForm').ajaxSubmit(options); |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
162 * @desc submit form and reset it if successful |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
163 * |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
164 * @example |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
165 * $('#myForm).submit(function() { |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
166 * $(this).ajaxSubmit(); |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
167 * return false; |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
168 * }); |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
169 * @desc Bind form's submit event to use ajaxSubmit |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
170 * |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
171 * |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
172 * @name ajaxSubmit |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
173 * @type jQuery |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
174 * @param options object literal containing options which control the form submission process |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
175 * @cat Plugins/Form |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
176 * @return jQuery |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
177 */ |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
178 $.fn.ajaxSubmit = function(options) { |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
179 if (typeof options == 'function') |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
180 options = { success: options }; |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
181 |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
182 options = $.extend({ |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
183 url: this.attr('action') || window.location, |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
184 type: this.attr('method') || 'GET' |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
185 }, options || {}); |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
186 |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
187 // hook for manipulating the form data before it is extracted; |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
188 // convenient for use with rich editors like tinyMCE or FCKEditor |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
189 var veto = {}; |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
190 $.event.trigger('form.pre.serialize', [this, options, veto]); |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
191 if (veto.veto) return this; |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
192 |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
193 var a = this.formToArray(options.semantic); |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
194 if (options.data) { |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
195 for (var n in options.data) |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
196 a.push( { name: n, value: options.data[n] } ); |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
197 } |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
198 |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
199 // give pre-submit callback an opportunity to abort the submit |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
200 if (options.beforeSubmit && options.beforeSubmit(a, this, options) === false) return this; |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
201 |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
202 // fire vetoable 'validate' event |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
203 $.event.trigger('form.submit.validate', [a, this, options, veto]); |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
204 if (veto.veto) return this; |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
205 |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
206 var q = $.param(a);//.replace(/%20/g,'+'); |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
207 |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
208 if (options.type.toUpperCase() == 'GET') { |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
209 options.url += (options.url.indexOf('?') >= 0 ? '&' : '?') + q; |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
210 options.data = null; // data is null for 'get' |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
211 } |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
212 else |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
213 options.data = q; // data is the query string for 'post' |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
214 |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
215 var $form = this, callbacks = []; |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
216 if (options.resetForm) callbacks.push(function() { $form.resetForm(); }); |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
217 if (options.clearForm) callbacks.push(function() { $form.clearForm(); }); |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
218 |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
219 // perform a load on the target only if dataType is not provided |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
220 if (!options.dataType && options.target) { |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
221 var oldSuccess = options.success || function(){}; |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
222 callbacks.push(function(data) { |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
223 if (this.evalScripts) |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
224 $(options.target).attr("innerHTML", data).evalScripts().each(oldSuccess, arguments); |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
225 else // jQuery v1.1.4 |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
226 $(options.target).html(data).each(oldSuccess, arguments); |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
227 }); |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
228 } |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
229 else if (options.success) |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
230 callbacks.push(options.success); |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
231 |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
232 options.success = function(data, status) { |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
233 for (var i=0, max=callbacks.length; i < max; i++) |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
234 callbacks[i](data, status, $form); |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
235 }; |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
236 |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
237 // are there files to upload? |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
238 var files = $('input:file', this).fieldValue(); |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
239 var found = false; |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
240 for (var j=0; j < files.length; j++) |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
241 if (files[j]) |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
242 found = true; |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
243 |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
244 if (options.iframe || found) // options.iframe allows user to force iframe mode |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
245 fileUpload(); |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
246 else |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
247 $.ajax(options); |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
248 |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
249 // fire 'notify' event |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
250 $.event.trigger('form.submit.notify', [this, options]); |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
251 return this; |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
252 |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
253 |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
254 // private function for handling file uploads (hat tip to YAHOO!) |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
255 function fileUpload() { |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
256 var form = $form[0]; |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
257 var opts = $.extend({}, $.ajaxSettings, options); |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
258 |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
259 var id = 'jqFormIO' + $.fn.ajaxSubmit.counter++; |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
260 var $io = $('<iframe id="' + id + '" name="' + id + '" />'); |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
261 var io = $io[0]; |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
262 var op8 = $.browser.opera && window.opera.version() < 9; |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
263 if ($.browser.msie || op8) io.src = 'javascript:false;document.write("");'; |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
264 $io.css({ position: 'absolute', top: '-1000px', left: '-1000px' }); |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
265 |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
266 var xhr = { // mock object |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
267 responseText: null, |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
268 responseXML: null, |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
269 status: 0, |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
270 statusText: 'n/a', |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
271 getAllResponseHeaders: function() {}, |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
272 getResponseHeader: function() {}, |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
273 setRequestHeader: function() {} |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
274 }; |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
275 |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
276 var g = opts.global; |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
277 // trigger ajax global events so that activity/block indicators work like normal |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
278 if (g && ! $.active++) $.event.trigger("ajaxStart"); |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
279 if (g) $.event.trigger("ajaxSend", [xhr, opts]); |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
280 |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
281 var cbInvoked = 0; |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
282 var timedOut = 0; |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
283 |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
284 // take a breath so that pending repaints get some cpu time before the upload starts |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
285 setTimeout(function() { |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
286 $io.appendTo('body'); |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
287 // jQuery's event binding doesn't work for iframe events in IE |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
288 io.attachEvent ? io.attachEvent('onload', cb) : io.addEventListener('load', cb, false); |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
289 |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
290 // make sure form attrs are set |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
291 var encAttr = form.encoding ? 'encoding' : 'enctype'; |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
292 var t = $form.attr('target'); |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
293 $form.attr({ |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
294 target: id, |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
295 method: 'POST', |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
296 action: opts.url |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
297 }); |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
298 form[encAttr] = 'multipart/form-data'; |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
299 |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
300 // support timout |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
301 if (opts.timeout) |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
302 setTimeout(function() { timedOut = true; cb(); }, opts.timeout); |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
303 |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
304 form.submit(); |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
305 $form.attr('target', t); // reset target |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
306 }, 10); |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
307 |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
308 function cb() { |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
309 if (cbInvoked++) return; |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
310 |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
311 io.detachEvent ? io.detachEvent('onload', cb) : io.removeEventListener('load', cb, false); |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
312 |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
313 var ok = true; |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
314 try { |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
315 if (timedOut) throw 'timeout'; |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
316 // extract the server response from the iframe |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
317 var data, doc; |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
318 doc = io.contentWindow ? io.contentWindow.document : io.contentDocument ? io.contentDocument : io.document; |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
319 xhr.responseText = doc.body ? doc.body.innerHTML : null; |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
320 xhr.responseXML = doc.XMLDocument ? doc.XMLDocument : doc; |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
321 |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
322 if (opts.dataType == 'json' || opts.dataType == 'script') { |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
323 var ta = doc.getElementsByTagName('textarea')[0]; |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
324 data = ta ? ta.value : xhr.responseText; |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
325 if (opts.dataType == 'json') |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
326 eval("data = " + data); |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
327 else |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
328 $.globalEval(data); |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
329 } |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
330 else if (opts.dataType == 'xml') { |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
331 data = xhr.responseXML; |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
332 if (!data && xhr.responseText != null) |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
333 data = toXml(xhr.responseText); |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
334 } |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
335 else { |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
336 data = xhr.responseText; |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
337 } |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
338 } |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
339 catch(e){ |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
340 ok = false; |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
341 $.handleError(opts, xhr, 'error', e); |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
342 } |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
343 |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
344 // ordering of these callbacks/triggers is odd, but that's how $.ajax does it |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
345 if (ok) { |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
346 opts.success(data, 'success'); |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
347 if (g) $.event.trigger("ajaxSuccess", [xhr, opts]); |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
348 } |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
349 if (g) $.event.trigger("ajaxComplete", [xhr, opts]); |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
350 if (g && ! --$.active) $.event.trigger("ajaxStop"); |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
351 if (opts.complete) opts.complete(xhr, ok ? 'success' : 'error'); |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
352 |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
353 // clean up |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
354 setTimeout(function() { |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
355 $io.remove(); |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
356 xhr.responseXML = null; |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
357 }, 100); |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
358 }; |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
359 |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
360 function toXml(s, doc) { |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
361 if (window.ActiveXObject) { |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
362 doc = new ActiveXObject('Microsoft.XMLDOM'); |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
363 doc.async = 'false'; |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
364 doc.loadXML(s); |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
365 } |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
366 else |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
367 doc = (new DOMParser()).parseFromString(s, 'text/xml'); |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
368 return (doc && doc.documentElement && doc.documentElement.tagName != 'parsererror') ? doc : null; |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
369 }; |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
370 }; |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
371 }; |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
372 $.fn.ajaxSubmit.counter = 0; // used to create unique iframe ids |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
373 |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
374 /** |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
375 * ajaxForm() provides a mechanism for fully automating form submission. |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
376 * |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
377 * The advantages of using this method instead of ajaxSubmit() are: |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
378 * |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
379 * 1: This method will include coordinates for <input type="image" /> elements (if the element |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
380 * is used to submit the form). |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
381 * 2. This method will include the submit element's name/value data (for the element that was |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
382 * used to submit the form). |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
383 * 3. This method binds the submit() method to the form for you. |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
384 * |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
385 * Note that for accurate x/y coordinates of image submit elements in all browsers |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
386 * you need to also use the "dimensions" plugin (this method will auto-detect its presence). |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
387 * |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
388 * The options argument for ajaxForm works exactly as it does for ajaxSubmit. ajaxForm merely |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
389 * passes the options argument along after properly binding events for submit elements and |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
390 * the form itself. See ajaxSubmit for a full description of the options argument. |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
391 * |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
392 * |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
393 * @example |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
394 * var options = { |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
395 * target: '#myTargetDiv' |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
396 * }; |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
397 * $('#myForm').ajaxSForm(options); |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
398 * @desc Bind form's submit event so that 'myTargetDiv' is updated with the server response |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
399 * when the form is submitted. |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
400 * |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
401 * |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
402 * @example |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
403 * var options = { |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
404 * success: function(responseText) { |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
405 * alert(responseText); |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
406 * } |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
407 * }; |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
408 * $('#myForm').ajaxSubmit(options); |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
409 * @desc Bind form's submit event so that server response is alerted after the form is submitted. |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
410 * |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
411 * |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
412 * @example |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
413 * var options = { |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
414 * beforeSubmit: function(formArray, jqForm) { |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
415 * if (formArray.length == 0) { |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
416 * alert('Please enter data.'); |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
417 * return false; |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
418 * } |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
419 * } |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
420 * }; |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
421 * $('#myForm').ajaxSubmit(options); |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
422 * @desc Bind form's submit event so that pre-submit callback is invoked before the form |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
423 * is submitted. |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
424 * |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
425 * |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
426 * @name ajaxForm |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
427 * @param options object literal containing options which control the form submission process |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
428 * @return jQuery |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
429 * @cat Plugins/Form |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
430 * @type jQuery |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
431 */ |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
432 $.fn.ajaxForm = function(options) { |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
433 return this.ajaxFormUnbind().submit(submitHandler).each(function() { |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
434 // store options in hash |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
435 this.formPluginId = $.fn.ajaxForm.counter++; |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
436 $.fn.ajaxForm.optionHash[this.formPluginId] = options; |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
437 $(":submit,input:image", this).click(clickHandler); |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
438 }); |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
439 }; |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
440 |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
441 $.fn.ajaxForm.counter = 1; |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
442 $.fn.ajaxForm.optionHash = {}; |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
443 |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
444 function clickHandler(e) { |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
445 var $form = this.form; |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
446 $form.clk = this; |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
447 if (this.type == 'image') { |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
448 if (e.offsetX != undefined) { |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
449 $form.clk_x = e.offsetX; |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
450 $form.clk_y = e.offsetY; |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
451 } else if (typeof $.fn.offset == 'function') { // try to use dimensions plugin |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
452 var offset = $(this).offset(); |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
453 $form.clk_x = e.pageX - offset.left; |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
454 $form.clk_y = e.pageY - offset.top; |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
455 } else { |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
456 $form.clk_x = e.pageX - this.offsetLeft; |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
457 $form.clk_y = e.pageY - this.offsetTop; |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
458 } |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
459 } |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
460 // clear form vars |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
461 setTimeout(function() { $form.clk = $form.clk_x = $form.clk_y = null; }, 10); |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
462 }; |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
463 |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
464 function submitHandler() { |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
465 // retrieve options from hash |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
466 var id = this.formPluginId; |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
467 var options = $.fn.ajaxForm.optionHash[id]; |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
468 $(this).ajaxSubmit(options); |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
469 return false; |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
470 }; |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
471 |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
472 /** |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
473 * ajaxFormUnbind unbinds the event handlers that were bound by ajaxForm |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
474 * |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
475 * @name ajaxFormUnbind |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
476 * @return jQuery |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
477 * @cat Plugins/Form |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
478 * @type jQuery |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
479 */ |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
480 $.fn.ajaxFormUnbind = function() { |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
481 this.unbind('submit', submitHandler); |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
482 return this.each(function() { |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
483 $(":submit,input:image", this).unbind('click', clickHandler); |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
484 }); |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
485 |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
486 }; |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
487 |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
488 /** |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
489 * formToArray() gathers form element data into an array of objects that can |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
490 * be passed to any of the following ajax functions: $.get, $.post, or load. |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
491 * Each object in the array has both a 'name' and 'value' property. An example of |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
492 * an array for a simple login form might be: |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
493 * |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
494 * [ { name: 'username', value: 'jresig' }, { name: 'password', value: 'secret' } ] |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
495 * |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
496 * It is this array that is passed to pre-submit callback functions provided to the |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
497 * ajaxSubmit() and ajaxForm() methods. |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
498 * |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
499 * The semantic argument can be used to force form serialization in semantic order. |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
500 * This is normally true anyway, unless the form contains input elements of type='image'. |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
501 * If your form must be submitted with name/value pairs in semantic order and your form |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
502 * contains an input of type='image" then pass true for this arg, otherwise pass false |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
503 * (or nothing) to avoid the overhead for this logic. |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
504 * |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
505 * @example var data = $("#myForm").formToArray(); |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
506 * $.post( "myscript.cgi", data ); |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
507 * @desc Collect all the data from a form and submit it to the server. |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
508 * |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
509 * @name formToArray |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
510 * @param semantic true if serialization must maintain strict semantic ordering of elements (slower) |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
511 * @type Array<Object> |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
512 * @cat Plugins/Form |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
513 */ |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
514 $.fn.formToArray = function(semantic) { |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
515 var a = []; |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
516 if (this.length == 0) return a; |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
517 |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
518 var form = this[0]; |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
519 var els = semantic ? form.getElementsByTagName('*') : form.elements; |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
520 if (!els) return a; |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
521 for(var i=0, max=els.length; i < max; i++) { |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
522 var el = els[i]; |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
523 var n = el.name; |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
524 if (!n) continue; |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
525 |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
526 if (semantic && form.clk && el.type == "image") { |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
527 // handle image inputs on the fly when semantic == true |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
528 if(!el.disabled && form.clk == el) |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
529 a.push({name: n+'.x', value: form.clk_x}, {name: n+'.y', value: form.clk_y}); |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
530 continue; |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
531 } |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
532 |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
533 var v = $.fieldValue(el, true); |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
534 if (v && v.constructor == Array) { |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
535 for(var j=0, jmax=v.length; j < jmax; j++) |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
536 a.push({name: n, value: v[j]}); |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
537 } |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
538 else if (v !== null && typeof v != 'undefined') |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
539 a.push({name: n, value: v}); |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
540 } |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
541 |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
542 if (!semantic && form.clk) { |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
543 // input type=='image' are not found in elements array! handle them here |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
544 var inputs = form.getElementsByTagName("input"); |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
545 for(var i=0, max=inputs.length; i < max; i++) { |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
546 var input = inputs[i]; |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
547 var n = input.name; |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
548 if(n && !input.disabled && input.type == "image" && form.clk == input) |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
549 a.push({name: n+'.x', value: form.clk_x}, {name: n+'.y', value: form.clk_y}); |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
550 } |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
551 } |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
552 return a; |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
553 }; |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
554 |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
555 |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
556 /** |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
557 * Serializes form data into a 'submittable' string. This method will return a string |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
558 * in the format: name1=value1&name2=value2 |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
559 * |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
560 * The semantic argument can be used to force form serialization in semantic order. |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
561 * If your form must be submitted with name/value pairs in semantic order then pass |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
562 * true for this arg, otherwise pass false (or nothing) to avoid the overhead for |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
563 * this logic (which can be significant for very large forms). |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
564 * |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
565 * @example var data = $("#myForm").formSerialize(); |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
566 * $.ajax('POST', "myscript.cgi", data); |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
567 * @desc Collect all the data from a form into a single string |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
568 * |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
569 * @name formSerialize |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
570 * @param semantic true if serialization must maintain strict semantic ordering of elements (slower) |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
571 * @type String |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
572 * @cat Plugins/Form |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
573 */ |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
574 $.fn.formSerialize = function(semantic) { |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
575 //hand off to jQuery.param for proper encoding |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
576 return $.param(this.formToArray(semantic)); |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
577 }; |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
578 |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
579 |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
580 /** |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
581 * Serializes all field elements in the jQuery object into a query string. |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
582 * This method will return a string in the format: name1=value1&name2=value2 |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
583 * |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
584 * The successful argument controls whether or not serialization is limited to |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
585 * 'successful' controls (per http://www.w3.org/TR/html4/interact/forms.html#successful-controls). |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
586 * The default value of the successful argument is true. |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
587 * |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
588 * @example var data = $("input").formSerialize(); |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
589 * @desc Collect the data from all successful input elements into a query string |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
590 * |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
591 * @example var data = $(":radio").formSerialize(); |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
592 * @desc Collect the data from all successful radio input elements into a query string |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
593 * |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
594 * @example var data = $("#myForm :checkbox").formSerialize(); |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
595 * @desc Collect the data from all successful checkbox input elements in myForm into a query string |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
596 * |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
597 * @example var data = $("#myForm :checkbox").formSerialize(false); |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
598 * @desc Collect the data from all checkbox elements in myForm (even the unchecked ones) into a query string |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
599 * |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
600 * @example var data = $(":input").formSerialize(); |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
601 * @desc Collect the data from all successful input, select, textarea and button elements into a query string |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
602 * |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
603 * @name fieldSerialize |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
604 * @param successful true if only successful controls should be serialized (default is true) |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
605 * @type String |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
606 * @cat Plugins/Form |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
607 */ |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
608 $.fn.fieldSerialize = function(successful) { |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
609 var a = []; |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
610 this.each(function() { |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
611 var n = this.name; |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
612 if (!n) return; |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
613 var v = $.fieldValue(this, successful); |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
614 if (v && v.constructor == Array) { |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
615 for (var i=0,max=v.length; i < max; i++) |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
616 a.push({name: n, value: v[i]}); |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
617 } |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
618 else if (v !== null && typeof v != 'undefined') |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
619 a.push({name: this.name, value: v}); |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
620 }); |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
621 //hand off to jQuery.param for proper encoding |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
622 return $.param(a); |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
623 }; |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
624 |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
625 |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
626 /** |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
627 * Returns the value(s) of the element in the matched set. For example, consider the following form: |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
628 * |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
629 * <form><fieldset> |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
630 * <input name="A" type="text" /> |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
631 * <input name="A" type="text" /> |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
632 * <input name="B" type="checkbox" value="B1" /> |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
633 * <input name="B" type="checkbox" value="B2"/> |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
634 * <input name="C" type="radio" value="C1" /> |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
635 * <input name="C" type="radio" value="C2" /> |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
636 * </fieldset></form> |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
637 * |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
638 * var v = $(':text').fieldValue(); |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
639 * // if no values are entered into the text inputs |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
640 * v == ['',''] |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
641 * // if values entered into the text inputs are 'foo' and 'bar' |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
642 * v == ['foo','bar'] |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
643 * |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
644 * var v = $(':checkbox').fieldValue(); |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
645 * // if neither checkbox is checked |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
646 * v === undefined |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
647 * // if both checkboxes are checked |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
648 * v == ['B1', 'B2'] |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
649 * |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
650 * var v = $(':radio').fieldValue(); |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
651 * // if neither radio is checked |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
652 * v === undefined |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
653 * // if first radio is checked |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
654 * v == ['C1'] |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
655 * |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
656 * The successful argument controls whether or not the field element must be 'successful' |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
657 * (per http://www.w3.org/TR/html4/interact/forms.html#successful-controls). |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
658 * The default value of the successful argument is true. If this value is false the value(s) |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
659 * for each element is returned. |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
660 * |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
661 * Note: This method *always* returns an array. If no valid value can be determined the |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
662 * array will be empty, otherwise it will contain one or more values. |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
663 * |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
664 * @example var data = $("#myPasswordElement").fieldValue(); |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
665 * alert(data[0]); |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
666 * @desc Alerts the current value of the myPasswordElement element |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
667 * |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
668 * @example var data = $("#myForm :input").fieldValue(); |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
669 * @desc Get the value(s) of the form elements in myForm |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
670 * |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
671 * @example var data = $("#myForm :checkbox").fieldValue(); |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
672 * @desc Get the value(s) for the successful checkbox element(s) in the jQuery object. |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
673 * |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
674 * @example var data = $("#mySingleSelect").fieldValue(); |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
675 * @desc Get the value(s) of the select control |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
676 * |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
677 * @example var data = $(':text').fieldValue(); |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
678 * @desc Get the value(s) of the text input or textarea elements |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
679 * |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
680 * @example var data = $("#myMultiSelect").fieldValue(); |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
681 * @desc Get the values for the select-multiple control |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
682 * |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
683 * @name fieldValue |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
684 * @param Boolean successful true if only the values for successful controls should be returned (default is true) |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
685 * @type Array<String> |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
686 * @cat Plugins/Form |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
687 */ |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
688 $.fn.fieldValue = function(successful) { |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
689 for (var val=[], i=0, max=this.length; i < max; i++) { |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
690 var el = this[i]; |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
691 var v = $.fieldValue(el, successful); |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
692 if (v === null || typeof v == 'undefined' || (v.constructor == Array && !v.length)) |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
693 continue; |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
694 v.constructor == Array ? $.merge(val, v) : val.push(v); |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
695 } |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
696 return val; |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
697 }; |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
698 |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
699 /** |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
700 * Returns the value of the field element. |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
701 * |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
702 * The successful argument controls whether or not the field element must be 'successful' |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
703 * (per http://www.w3.org/TR/html4/interact/forms.html#successful-controls). |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
704 * The default value of the successful argument is true. If the given element is not |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
705 * successful and the successful arg is not false then the returned value will be null. |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
706 * |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
707 * Note: If the successful flag is true (default) but the element is not successful, the return will be null |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
708 * Note: The value returned for a successful select-multiple element will always be an array. |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
709 * Note: If the element has no value the return value will be undefined. |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
710 * |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
711 * @example var data = jQuery.fieldValue($("#myPasswordElement")[0]); |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
712 * @desc Gets the current value of the myPasswordElement element |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
713 * |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
714 * @name fieldValue |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
715 * @param Element el The DOM element for which the value will be returned |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
716 * @param Boolean successful true if value returned must be for a successful controls (default is true) |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
717 * @type String or Array<String> or null or undefined |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
718 * @cat Plugins/Form |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
719 */ |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
720 $.fieldValue = function(el, successful) { |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
721 var n = el.name, t = el.type, tag = el.tagName.toLowerCase(); |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
722 if (typeof successful == 'undefined') successful = true; |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
723 |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
724 if (successful && (!n || el.disabled || t == 'reset' || t == 'button' || |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
725 (t == 'checkbox' || t == 'radio') && !el.checked || |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
726 (t == 'submit' || t == 'image') && el.form && el.form.clk != el || |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
727 tag == 'select' && el.selectedIndex == -1)) |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
728 return null; |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
729 |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
730 if (tag == 'select') { |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
731 var index = el.selectedIndex; |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
732 if (index < 0) return null; |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
733 var a = [], ops = el.options; |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
734 var one = (t == 'select-one'); |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
735 var max = (one ? index+1 : ops.length); |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
736 for(var i=(one ? index : 0); i < max; i++) { |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
737 var op = ops[i]; |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
738 if (op.selected) { |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
739 // extra pain for IE... |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
740 var v = $.browser.msie && !(op.attributes['value'].specified) ? op.text : op.value; |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
741 if (one) return v; |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
742 a.push(v); |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
743 } |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
744 } |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
745 return a; |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
746 } |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
747 return el.value; |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
748 }; |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
749 |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
750 |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
751 /** |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
752 * Clears the form data. Takes the following actions on the form's input fields: |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
753 * - input text fields will have their 'value' property set to the empty string |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
754 * - select elements will have their 'selectedIndex' property set to -1 |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
755 * - checkbox and radio inputs will have their 'checked' property set to false |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
756 * - inputs of type submit, button, reset, and hidden will *not* be effected |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
757 * - button elements will *not* be effected |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
758 * |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
759 * @example $('form').clearForm(); |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
760 * @desc Clears all forms on the page. |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
761 * |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
762 * @name clearForm |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
763 * @type jQuery |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
764 * @cat Plugins/Form |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
765 */ |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
766 $.fn.clearForm = function() { |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
767 return this.each(function() { |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
768 $('input,select,textarea', this).clearFields(); |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
769 }); |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
770 }; |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
771 |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
772 /** |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
773 * Clears the selected form elements. Takes the following actions on the matched elements: |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
774 * - input text fields will have their 'value' property set to the empty string |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
775 * - select elements will have their 'selectedIndex' property set to -1 |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
776 * - checkbox and radio inputs will have their 'checked' property set to false |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
777 * - inputs of type submit, button, reset, and hidden will *not* be effected |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
778 * - button elements will *not* be effected |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
779 * |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
780 * @example $('.myInputs').clearFields(); |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
781 * @desc Clears all inputs with class myInputs |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
782 * |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
783 * @name clearFields |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
784 * @type jQuery |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
785 * @cat Plugins/Form |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
786 */ |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
787 $.fn.clearFields = $.fn.clearInputs = function() { |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
788 return this.each(function() { |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
789 var t = this.type, tag = this.tagName.toLowerCase(); |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
790 if (t == 'text' || t == 'password' || tag == 'textarea') |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
791 this.value = ''; |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
792 else if (t == 'checkbox' || t == 'radio') |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
793 this.checked = false; |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
794 else if (tag == 'select') |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
795 this.selectedIndex = -1; |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
796 }); |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
797 }; |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
798 |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
799 |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
800 /** |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
801 * Resets the form data. Causes all form elements to be reset to their original value. |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
802 * |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
803 * @example $('form').resetForm(); |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
804 * @desc Resets all forms on the page. |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
805 * |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
806 * @name resetForm |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
807 * @type jQuery |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
808 * @cat Plugins/Form |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
809 */ |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
810 $.fn.resetForm = function() { |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
811 return this.each(function() { |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
812 // guard against an input with the name of 'reset' |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
813 // note that IE reports the reset function as an 'object' |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
814 if (typeof this.reset == 'function' || (typeof this.reset == 'object' && !this.reset.nodeType)) |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
815 this.reset(); |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
816 }); |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
817 }; |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
818 |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
819 })(jQuery); |