| 1 | Index: wp-includes/js/jquery/jquery.form.dev.js |
|---|
| 2 | =================================================================== |
|---|
| 3 | --- wp-includes/js/jquery/jquery.form.dev.js (revision 17936) |
|---|
| 4 | +++ wp-includes/js/jquery/jquery.form.dev.js (working copy) |
|---|
| 5 | @@ -1,382 +1,470 @@ |
|---|
| 6 | -/* |
|---|
| 7 | +/*! |
|---|
| 8 | * jQuery Form Plugin |
|---|
| 9 | - * version: 2.02 (12/16/2007) |
|---|
| 10 | - * @requires jQuery v1.1 or later |
|---|
| 11 | + * version: 2.73 (03-MAY-2011) |
|---|
| 12 | + * @requires jQuery v1.3.2 or later |
|---|
| 13 | * |
|---|
| 14 | - * Examples at: http://malsup.com/jquery/form/ |
|---|
| 15 | + * Examples and documentation at: http://malsup.com/jquery/form/ |
|---|
| 16 | * Dual licensed under the MIT and GPL licenses: |
|---|
| 17 | * http://www.opensource.org/licenses/mit-license.php |
|---|
| 18 | * http://www.gnu.org/licenses/gpl.html |
|---|
| 19 | - * |
|---|
| 20 | - * Revision: $Id$ |
|---|
| 21 | */ |
|---|
| 22 | - (function($) { |
|---|
| 23 | +;(function($) { |
|---|
| 24 | + |
|---|
| 25 | +/* |
|---|
| 26 | + Usage Note: |
|---|
| 27 | + ----------- |
|---|
| 28 | + Do not use both ajaxSubmit and ajaxForm on the same form. These |
|---|
| 29 | + functions are intended to be exclusive. Use ajaxSubmit if you want |
|---|
| 30 | + to bind your own submit handler to the form. For example, |
|---|
| 31 | + |
|---|
| 32 | + $(document).ready(function() { |
|---|
| 33 | + $('#myForm').bind('submit', function(e) { |
|---|
| 34 | + e.preventDefault(); // <-- important |
|---|
| 35 | + $(this).ajaxSubmit({ |
|---|
| 36 | + target: '#output' |
|---|
| 37 | + }); |
|---|
| 38 | + }); |
|---|
| 39 | + }); |
|---|
| 40 | + |
|---|
| 41 | + Use ajaxForm when you want the plugin to manage all the event binding |
|---|
| 42 | + for you. For example, |
|---|
| 43 | + |
|---|
| 44 | + $(document).ready(function() { |
|---|
| 45 | + $('#myForm').ajaxForm({ |
|---|
| 46 | + target: '#output' |
|---|
| 47 | + }); |
|---|
| 48 | + }); |
|---|
| 49 | + |
|---|
| 50 | + When using ajaxForm, the ajaxSubmit function will be invoked for you |
|---|
| 51 | + at the appropriate time. |
|---|
| 52 | +*/ |
|---|
| 53 | + |
|---|
| 54 | /** |
|---|
| 55 | - * ajaxSubmit() provides a mechanism for submitting an HTML form using AJAX. |
|---|
| 56 | - * |
|---|
| 57 | - * ajaxSubmit accepts a single argument which can be either a success callback function |
|---|
| 58 | - * or an options Object. If a function is provided it will be invoked upon successful |
|---|
| 59 | - * completion of the submit and will be passed the response from the server. |
|---|
| 60 | - * If an options Object is provided, the following attributes are supported: |
|---|
| 61 | - * |
|---|
| 62 | - * target: Identifies the element(s) in the page to be updated with the server response. |
|---|
| 63 | - * This value may be specified as a jQuery selection string, a jQuery object, |
|---|
| 64 | - * or a DOM element. |
|---|
| 65 | - * default value: null |
|---|
| 66 | - * |
|---|
| 67 | - * url: URL to which the form data will be submitted. |
|---|
| 68 | - * default value: value of form's 'action' attribute |
|---|
| 69 | - * |
|---|
| 70 | - * type: The method in which the form data should be submitted, 'GET' or 'POST'. |
|---|
| 71 | - * default value: value of form's 'method' attribute (or 'GET' if none found) |
|---|
| 72 | - * |
|---|
| 73 | - * data: Additional data to add to the request, specified as key/value pairs (see $.ajax). |
|---|
| 74 | - * |
|---|
| 75 | - * beforeSubmit: Callback method to be invoked before the form is submitted. |
|---|
| 76 | - * default value: null |
|---|
| 77 | - * |
|---|
| 78 | - * success: Callback method to be invoked after the form has been successfully submitted |
|---|
| 79 | - * and the response has been returned from the server |
|---|
| 80 | - * default value: null |
|---|
| 81 | - * |
|---|
| 82 | - * dataType: Expected dataType of the response. One of: null, 'xml', 'script', or 'json' |
|---|
| 83 | - * default value: null |
|---|
| 84 | - * |
|---|
| 85 | - * semantic: Boolean flag indicating whether data must be submitted in semantic order (slower). |
|---|
| 86 | - * default value: false |
|---|
| 87 | - * |
|---|
| 88 | - * resetForm: Boolean flag indicating whether the form should be reset if the submit is successful |
|---|
| 89 | - * |
|---|
| 90 | - * clearForm: Boolean flag indicating whether the form should be cleared if the submit is successful |
|---|
| 91 | - * |
|---|
| 92 | - * |
|---|
| 93 | - * The 'beforeSubmit' callback can be provided as a hook for running pre-submit logic or for |
|---|
| 94 | - * validating the form data. If the 'beforeSubmit' callback returns false then the form will |
|---|
| 95 | - * not be submitted. The 'beforeSubmit' callback is invoked with three arguments: the form data |
|---|
| 96 | - * in array format, the jQuery object, and the options object passed into ajaxSubmit. |
|---|
| 97 | - * The form data array takes the following form: |
|---|
| 98 | - * |
|---|
| 99 | - * [ { name: 'username', value: 'jresig' }, { name: 'password', value: 'secret' } ] |
|---|
| 100 | - * |
|---|
| 101 | - * If a 'success' callback method is provided it is invoked after the response has been returned |
|---|
| 102 | - * from the server. It is passed the responseText or responseXML value (depending on dataType). |
|---|
| 103 | - * See jQuery.ajax for further details. |
|---|
| 104 | - * |
|---|
| 105 | - * |
|---|
| 106 | - * The dataType option provides a means for specifying how the server response should be handled. |
|---|
| 107 | - * This maps directly to the jQuery.httpData method. The following values are supported: |
|---|
| 108 | - * |
|---|
| 109 | - * 'xml': if dataType == 'xml' the server response is treated as XML and the 'success' |
|---|
| 110 | - * callback method, if specified, will be passed the responseXML value |
|---|
| 111 | - * 'json': if dataType == 'json' the server response will be evaluted and passed to |
|---|
| 112 | - * the 'success' callback, if specified |
|---|
| 113 | - * 'script': if dataType == 'script' the server response is evaluated in the global context |
|---|
| 114 | - * |
|---|
| 115 | - * |
|---|
| 116 | - * Note that it does not make sense to use both the 'target' and 'dataType' options. If both |
|---|
| 117 | - * are provided the target will be ignored. |
|---|
| 118 | - * |
|---|
| 119 | - * The semantic argument can be used to force form serialization in semantic order. |
|---|
| 120 | - * This is normally true anyway, unless the form contains input elements of type='image'. |
|---|
| 121 | - * If your form must be submitted with name/value pairs in semantic order and your form |
|---|
| 122 | - * contains an input of type='image" then pass true for this arg, otherwise pass false |
|---|
| 123 | - * (or nothing) to avoid the overhead for this logic. |
|---|
| 124 | - * |
|---|
| 125 | - * |
|---|
| 126 | - * When used on its own, ajaxSubmit() is typically bound to a form's submit event like this: |
|---|
| 127 | - * |
|---|
| 128 | - * $("#form-id").submit(function() { |
|---|
| 129 | - * $(this).ajaxSubmit(options); |
|---|
| 130 | - * return false; // cancel conventional submit |
|---|
| 131 | - * }); |
|---|
| 132 | - * |
|---|
| 133 | - * When using ajaxForm(), however, this is done for you. |
|---|
| 134 | - * |
|---|
| 135 | - * @example |
|---|
| 136 | - * $('#myForm').ajaxSubmit(function(data) { |
|---|
| 137 | - * alert('Form submit succeeded! Server returned: ' + data); |
|---|
| 138 | - * }); |
|---|
| 139 | - * @desc Submit form and alert server response |
|---|
| 140 | - * |
|---|
| 141 | - * |
|---|
| 142 | - * @example |
|---|
| 143 | - * var options = { |
|---|
| 144 | - * target: '#myTargetDiv' |
|---|
| 145 | - * }; |
|---|
| 146 | - * $('#myForm').ajaxSubmit(options); |
|---|
| 147 | - * @desc Submit form and update page element with server response |
|---|
| 148 | - * |
|---|
| 149 | - * |
|---|
| 150 | - * @example |
|---|
| 151 | - * var options = { |
|---|
| 152 | - * success: function(responseText) { |
|---|
| 153 | - * alert(responseText); |
|---|
| 154 | - * } |
|---|
| 155 | - * }; |
|---|
| 156 | - * $('#myForm').ajaxSubmit(options); |
|---|
| 157 | - * @desc Submit form and alert the server response |
|---|
| 158 | - * |
|---|
| 159 | - * |
|---|
| 160 | - * @example |
|---|
| 161 | - * var options = { |
|---|
| 162 | - * beforeSubmit: function(formArray, jqForm) { |
|---|
| 163 | - * if (formArray.length == 0) { |
|---|
| 164 | - * alert('Please enter data.'); |
|---|
| 165 | - * return false; |
|---|
| 166 | - * } |
|---|
| 167 | - * } |
|---|
| 168 | - * }; |
|---|
| 169 | - * $('#myForm').ajaxSubmit(options); |
|---|
| 170 | - * @desc Pre-submit validation which aborts the submit operation if form data is empty |
|---|
| 171 | - * |
|---|
| 172 | - * |
|---|
| 173 | - * @example |
|---|
| 174 | - * var options = { |
|---|
| 175 | - * url: myJsonUrl.php, |
|---|
| 176 | - * dataType: 'json', |
|---|
| 177 | - * success: function(data) { |
|---|
| 178 | - * // 'data' is an object representing the the evaluated json data |
|---|
| 179 | - * } |
|---|
| 180 | - * }; |
|---|
| 181 | - * $('#myForm').ajaxSubmit(options); |
|---|
| 182 | - * @desc json data returned and evaluated |
|---|
| 183 | - * |
|---|
| 184 | - * |
|---|
| 185 | - * @example |
|---|
| 186 | - * var options = { |
|---|
| 187 | - * url: myXmlUrl.php, |
|---|
| 188 | - * dataType: 'xml', |
|---|
| 189 | - * success: function(responseXML) { |
|---|
| 190 | - * // responseXML is XML document object |
|---|
| 191 | - * var data = $('myElement', responseXML).text(); |
|---|
| 192 | - * } |
|---|
| 193 | - * }; |
|---|
| 194 | - * $('#myForm').ajaxSubmit(options); |
|---|
| 195 | - * @desc XML data returned from server |
|---|
| 196 | - * |
|---|
| 197 | - * |
|---|
| 198 | - * @example |
|---|
| 199 | - * var options = { |
|---|
| 200 | - * resetForm: true |
|---|
| 201 | - * }; |
|---|
| 202 | - * $('#myForm').ajaxSubmit(options); |
|---|
| 203 | - * @desc submit form and reset it if successful |
|---|
| 204 | - * |
|---|
| 205 | - * @example |
|---|
| 206 | - * $('#myForm).submit(function() { |
|---|
| 207 | - * $(this).ajaxSubmit(); |
|---|
| 208 | - * return false; |
|---|
| 209 | - * }); |
|---|
| 210 | - * @desc Bind form's submit event to use ajaxSubmit |
|---|
| 211 | - * |
|---|
| 212 | - * |
|---|
| 213 | - * @name ajaxSubmit |
|---|
| 214 | - * @type jQuery |
|---|
| 215 | - * @param options object literal containing options which control the form submission process |
|---|
| 216 | - * @cat Plugins/Form |
|---|
| 217 | - * @return jQuery |
|---|
| 218 | + * ajaxSubmit() provides a mechanism for immediately submitting |
|---|
| 219 | + * an HTML form using AJAX. |
|---|
| 220 | */ |
|---|
| 221 | $.fn.ajaxSubmit = function(options) { |
|---|
| 222 | - if (typeof options == 'function') |
|---|
| 223 | - options = { success: options }; |
|---|
| 224 | + // fast fail if nothing selected (http://dev.jquery.com/ticket/2752) |
|---|
| 225 | + if (!this.length) { |
|---|
| 226 | + log('ajaxSubmit: skipping submit process - no element selected'); |
|---|
| 227 | + return this; |
|---|
| 228 | + } |
|---|
| 229 | |
|---|
| 230 | - options = $.extend({ |
|---|
| 231 | - url: this.attr('action') || window.location.toString(), |
|---|
| 232 | - type: this.attr('method') || 'GET' |
|---|
| 233 | - }, options || {}); |
|---|
| 234 | + if (typeof options == 'function') { |
|---|
| 235 | + options = { success: options }; |
|---|
| 236 | + } |
|---|
| 237 | |
|---|
| 238 | - // hook for manipulating the form data before it is extracted; |
|---|
| 239 | - // convenient for use with rich editors like tinyMCE or FCKEditor |
|---|
| 240 | - var veto = {}; |
|---|
| 241 | - $.event.trigger('form.pre.serialize', [this, options, veto]); |
|---|
| 242 | - if (veto.veto) return this; |
|---|
| 243 | + var action = this.attr('action'); |
|---|
| 244 | + var url = (typeof action === 'string') ? $.trim(action) : ''; |
|---|
| 245 | + if (url) { |
|---|
| 246 | + // clean url (don't include hash vaue) |
|---|
| 247 | + url = (url.match(/^([^#]+)/)||[])[1]; |
|---|
| 248 | + } |
|---|
| 249 | + url = url || window.location.href || ''; |
|---|
| 250 | |
|---|
| 251 | - var a = this.formToArray(options.semantic); |
|---|
| 252 | + options = $.extend(true, { |
|---|
| 253 | + url: url, |
|---|
| 254 | + success: $.ajaxSettings.success, |
|---|
| 255 | + type: this[0].getAttribute('method') || 'GET', // IE7 massage (see issue 57) |
|---|
| 256 | + iframeSrc: /^https/i.test(window.location.href || '') ? 'javascript:false' : 'about:blank' |
|---|
| 257 | + }, options); |
|---|
| 258 | + |
|---|
| 259 | + // hook for manipulating the form data before it is extracted; |
|---|
| 260 | + // convenient for use with rich editors like tinyMCE or FCKEditor |
|---|
| 261 | + var veto = {}; |
|---|
| 262 | + this.trigger('form-pre-serialize', [this, options, veto]); |
|---|
| 263 | + if (veto.veto) { |
|---|
| 264 | + log('ajaxSubmit: submit vetoed via form-pre-serialize trigger'); |
|---|
| 265 | + return this; |
|---|
| 266 | + } |
|---|
| 267 | + |
|---|
| 268 | + // provide opportunity to alter form data before it is serialized |
|---|
| 269 | + if (options.beforeSerialize && options.beforeSerialize(this, options) === false) { |
|---|
| 270 | + log('ajaxSubmit: submit aborted via beforeSerialize callback'); |
|---|
| 271 | + return this; |
|---|
| 272 | + } |
|---|
| 273 | + |
|---|
| 274 | + var n,v,a = this.formToArray(options.semantic); |
|---|
| 275 | if (options.data) { |
|---|
| 276 | - for (var n in options.data) |
|---|
| 277 | - a.push( { name: n, value: options.data[n] } ); |
|---|
| 278 | + options.extraData = options.data; |
|---|
| 279 | + for (n in options.data) { |
|---|
| 280 | + if(options.data[n] instanceof Array) { |
|---|
| 281 | + for (var k in options.data[n]) { |
|---|
| 282 | + a.push( { name: n, value: options.data[n][k] } ); |
|---|
| 283 | + } |
|---|
| 284 | + } |
|---|
| 285 | + else { |
|---|
| 286 | + v = options.data[n]; |
|---|
| 287 | + v = $.isFunction(v) ? v() : v; // if value is fn, invoke it |
|---|
| 288 | + a.push( { name: n, value: v } ); |
|---|
| 289 | + } |
|---|
| 290 | + } |
|---|
| 291 | } |
|---|
| 292 | |
|---|
| 293 | - // give pre-submit callback an opportunity to abort the submit |
|---|
| 294 | - if (options.beforeSubmit && options.beforeSubmit(a, this, options) === false) return this; |
|---|
| 295 | + // give pre-submit callback an opportunity to abort the submit |
|---|
| 296 | + if (options.beforeSubmit && options.beforeSubmit(a, this, options) === false) { |
|---|
| 297 | + log('ajaxSubmit: submit aborted via beforeSubmit callback'); |
|---|
| 298 | + return this; |
|---|
| 299 | + } |
|---|
| 300 | |
|---|
| 301 | - // fire vetoable 'validate' event |
|---|
| 302 | - $.event.trigger('form.submit.validate', [a, this, options, veto]); |
|---|
| 303 | - if (veto.veto) return this; |
|---|
| 304 | + // fire vetoable 'validate' event |
|---|
| 305 | + this.trigger('form-submit-validate', [a, this, options, veto]); |
|---|
| 306 | + if (veto.veto) { |
|---|
| 307 | + log('ajaxSubmit: submit vetoed via form-submit-validate trigger'); |
|---|
| 308 | + return this; |
|---|
| 309 | + } |
|---|
| 310 | |
|---|
| 311 | - var q = $.param(a);//.replace(/%20/g,'+'); |
|---|
| 312 | + var q = $.param(a); |
|---|
| 313 | |
|---|
| 314 | - if (options.type.toUpperCase() == 'GET') { |
|---|
| 315 | - options.url += (options.url.indexOf('?') >= 0 ? '&' : '?') + q; |
|---|
| 316 | - options.data = null; // data is null for 'get' |
|---|
| 317 | - } |
|---|
| 318 | - else |
|---|
| 319 | - options.data = q; // data is the query string for 'post' |
|---|
| 320 | + if (options.type.toUpperCase() == 'GET') { |
|---|
| 321 | + options.url += (options.url.indexOf('?') >= 0 ? '&' : '?') + q; |
|---|
| 322 | + options.data = null; // data is null for 'get' |
|---|
| 323 | + } |
|---|
| 324 | + else { |
|---|
| 325 | + options.data = q; // data is the query string for 'post' |
|---|
| 326 | + } |
|---|
| 327 | |
|---|
| 328 | - var $form = this, callbacks = []; |
|---|
| 329 | - if (options.resetForm) callbacks.push(function() { $form.resetForm(); }); |
|---|
| 330 | - if (options.clearForm) callbacks.push(function() { $form.clearForm(); }); |
|---|
| 331 | + var $form = this, callbacks = []; |
|---|
| 332 | + if (options.resetForm) { |
|---|
| 333 | + callbacks.push(function() { $form.resetForm(); }); |
|---|
| 334 | + } |
|---|
| 335 | + if (options.clearForm) { |
|---|
| 336 | + callbacks.push(function() { $form.clearForm(); }); |
|---|
| 337 | + } |
|---|
| 338 | |
|---|
| 339 | - // perform a load on the target only if dataType is not provided |
|---|
| 340 | - if (!options.dataType && options.target) { |
|---|
| 341 | - var oldSuccess = options.success || function(){}; |
|---|
| 342 | - callbacks.push(function(data) { |
|---|
| 343 | - if (this.evalScripts) |
|---|
| 344 | - $(options.target).attr("innerHTML", data).evalScripts().each(oldSuccess, arguments); |
|---|
| 345 | - else // jQuery v1.1.4 |
|---|
| 346 | - $(options.target).html(data).each(oldSuccess, arguments); |
|---|
| 347 | - }); |
|---|
| 348 | - } |
|---|
| 349 | - else if (options.success) |
|---|
| 350 | - callbacks.push(options.success); |
|---|
| 351 | + // perform a load on the target only if dataType is not provided |
|---|
| 352 | + if (!options.dataType && options.target) { |
|---|
| 353 | + var oldSuccess = options.success || function(){}; |
|---|
| 354 | + callbacks.push(function(data) { |
|---|
| 355 | + var fn = options.replaceTarget ? 'replaceWith' : 'html'; |
|---|
| 356 | + $(options.target)[fn](data).each(oldSuccess, arguments); |
|---|
| 357 | + }); |
|---|
| 358 | + } |
|---|
| 359 | + else if (options.success) { |
|---|
| 360 | + callbacks.push(options.success); |
|---|
| 361 | + } |
|---|
| 362 | |
|---|
| 363 | - options.success = function(data, status) { |
|---|
| 364 | - for (var i=0, max=callbacks.length; i < max; i++) |
|---|
| 365 | - callbacks[i](data, status, $form); |
|---|
| 366 | - }; |
|---|
| 367 | + options.success = function(data, status, xhr) { // jQuery 1.4+ passes xhr as 3rd arg |
|---|
| 368 | + var context = options.context || options; // jQuery 1.4+ supports scope context |
|---|
| 369 | + for (var i=0, max=callbacks.length; i < max; i++) { |
|---|
| 370 | + callbacks[i].apply(context, [data, status, xhr || $form, $form]); |
|---|
| 371 | + } |
|---|
| 372 | + }; |
|---|
| 373 | |
|---|
| 374 | - // are there files to upload? |
|---|
| 375 | - var files = $('input:file', this).fieldValue(); |
|---|
| 376 | - var found = false; |
|---|
| 377 | - for (var j=0; j < files.length; j++) |
|---|
| 378 | - if (files[j]) |
|---|
| 379 | - found = true; |
|---|
| 380 | + // are there files to upload? |
|---|
| 381 | + var fileInputs = $('input:file', this).length > 0; |
|---|
| 382 | + var mp = 'multipart/form-data'; |
|---|
| 383 | + var multipart = ($form.attr('enctype') == mp || $form.attr('encoding') == mp); |
|---|
| 384 | |
|---|
| 385 | - // options.iframe allows user to force iframe mode |
|---|
| 386 | - if (options.iframe || found) { |
|---|
| 387 | - // hack to fix Safari hang (thanks to Tim Molendijk for this) |
|---|
| 388 | - // see: http://groups.google.com/group/jquery-dev/browse_thread/thread/36395b7ab510dd5d |
|---|
| 389 | - if ($.browser.safari && options.closeKeepAlive) |
|---|
| 390 | - $.get(options.closeKeepAlive, fileUpload); |
|---|
| 391 | - else |
|---|
| 392 | - fileUpload(); |
|---|
| 393 | - } |
|---|
| 394 | - else |
|---|
| 395 | - $.ajax(options); |
|---|
| 396 | + // options.iframe allows user to force iframe mode |
|---|
| 397 | + // 06-NOV-09: now defaulting to iframe mode if file input is detected |
|---|
| 398 | + if (options.iframe !== false && (fileInputs || options.iframe || multipart)) { |
|---|
| 399 | + // hack to fix Safari hang (thanks to Tim Molendijk for this) |
|---|
| 400 | + // see: http://groups.google.com/group/jquery-dev/browse_thread/thread/36395b7ab510dd5d |
|---|
| 401 | + if (options.closeKeepAlive) { |
|---|
| 402 | + $.get(options.closeKeepAlive, fileUpload); |
|---|
| 403 | + } |
|---|
| 404 | + else { |
|---|
| 405 | + fileUpload(); |
|---|
| 406 | + } |
|---|
| 407 | + } |
|---|
| 408 | + else { |
|---|
| 409 | + $.ajax(options); |
|---|
| 410 | + } |
|---|
| 411 | |
|---|
| 412 | - // fire 'notify' event |
|---|
| 413 | - $.event.trigger('form.submit.notify', [this, options]); |
|---|
| 414 | - return this; |
|---|
| 415 | + // fire 'notify' event |
|---|
| 416 | + this.trigger('form-submit-notify', [this, options]); |
|---|
| 417 | + return this; |
|---|
| 418 | |
|---|
| 419 | |
|---|
| 420 | - // private function for handling file uploads (hat tip to YAHOO!) |
|---|
| 421 | - function fileUpload() { |
|---|
| 422 | - var form = $form[0]; |
|---|
| 423 | - var opts = $.extend({}, $.ajaxSettings, options); |
|---|
| 424 | + // private function for handling file uploads (hat tip to YAHOO!) |
|---|
| 425 | + function fileUpload() { |
|---|
| 426 | + var form = $form[0]; |
|---|
| 427 | |
|---|
| 428 | - var id = 'jqFormIO' + $.fn.ajaxSubmit.counter++; |
|---|
| 429 | - var $io = $('<iframe id="' + id + '" name="' + id + '" />'); |
|---|
| 430 | - var io = $io[0]; |
|---|
| 431 | - var op8 = $.browser.opera && window.opera.version() < 9; |
|---|
| 432 | - if ($.browser.msie || op8) io.src = 'javascript:false;document.write("");'; |
|---|
| 433 | - $io.css({ position: 'absolute', top: '-1000px', left: '-1000px' }); |
|---|
| 434 | + if ($(':input[name=submit],:input[id=submit]', form).length) { |
|---|
| 435 | + // if there is an input with a name or id of 'submit' then we won't be |
|---|
| 436 | + // able to invoke the submit fn on the form (at least not x-browser) |
|---|
| 437 | + alert('Error: Form elements must not have name or id of "submit".'); |
|---|
| 438 | + return; |
|---|
| 439 | + } |
|---|
| 440 | + |
|---|
| 441 | + var s = $.extend(true, {}, $.ajaxSettings, options); |
|---|
| 442 | + s.context = s.context || s; |
|---|
| 443 | + var id = 'jqFormIO' + (new Date().getTime()), fn = '_'+id; |
|---|
| 444 | + var $io = $('<iframe id="' + id + '" name="' + id + '" src="'+ s.iframeSrc +'" />'); |
|---|
| 445 | + var io = $io[0]; |
|---|
| 446 | |
|---|
| 447 | - var xhr = { // mock object |
|---|
| 448 | - responseText: null, |
|---|
| 449 | - responseXML: null, |
|---|
| 450 | - status: 0, |
|---|
| 451 | - statusText: 'n/a', |
|---|
| 452 | - getAllResponseHeaders: function() {}, |
|---|
| 453 | - getResponseHeader: function() {}, |
|---|
| 454 | - setRequestHeader: function() {} |
|---|
| 455 | - }; |
|---|
| 456 | + $io.css({ position: 'absolute', top: '-1000px', left: '-1000px' }); |
|---|
| 457 | |
|---|
| 458 | - var g = opts.global; |
|---|
| 459 | - // trigger ajax global events so that activity/block indicators work like normal |
|---|
| 460 | - if (g && ! $.active++) $.event.trigger("ajaxStart"); |
|---|
| 461 | - if (g) $.event.trigger("ajaxSend", [xhr, opts]); |
|---|
| 462 | + var xhr = { // mock object |
|---|
| 463 | + aborted: 0, |
|---|
| 464 | + responseText: null, |
|---|
| 465 | + responseXML: null, |
|---|
| 466 | + status: 0, |
|---|
| 467 | + statusText: 'n/a', |
|---|
| 468 | + getAllResponseHeaders: function() {}, |
|---|
| 469 | + getResponseHeader: function() {}, |
|---|
| 470 | + setRequestHeader: function() {}, |
|---|
| 471 | + abort: function(status) { |
|---|
| 472 | + var e = (status === 'timeout' ? 'timeout' : 'aborted'); |
|---|
| 473 | + log('aborting upload... ' + e); |
|---|
| 474 | + this.aborted = 1; |
|---|
| 475 | + $io.attr('src', s.iframeSrc); // abort op in progress |
|---|
| 476 | + xhr.error = e; |
|---|
| 477 | + s.error && s.error.call(s.context, xhr, e, e); |
|---|
| 478 | + g && $.event.trigger("ajaxError", [xhr, s, e]); |
|---|
| 479 | + s.complete && s.complete.call(s.context, xhr, e); |
|---|
| 480 | + } |
|---|
| 481 | + }; |
|---|
| 482 | |
|---|
| 483 | - var cbInvoked = 0; |
|---|
| 484 | - var timedOut = 0; |
|---|
| 485 | + var g = s.global; |
|---|
| 486 | + // trigger ajax global events so that activity/block indicators work like normal |
|---|
| 487 | + if (g && ! $.active++) { |
|---|
| 488 | + $.event.trigger("ajaxStart"); |
|---|
| 489 | + } |
|---|
| 490 | + if (g) { |
|---|
| 491 | + $.event.trigger("ajaxSend", [xhr, s]); |
|---|
| 492 | + } |
|---|
| 493 | |
|---|
| 494 | - // take a breath so that pending repaints get some cpu time before the upload starts |
|---|
| 495 | - setTimeout(function() { |
|---|
| 496 | - // make sure form attrs are set |
|---|
| 497 | - var encAttr = form.encoding ? 'encoding' : 'enctype'; |
|---|
| 498 | - var t = $form.attr('target'); |
|---|
| 499 | - $form.attr({ |
|---|
| 500 | - target: id, |
|---|
| 501 | - method: 'POST', |
|---|
| 502 | - action: opts.url |
|---|
| 503 | - }); |
|---|
| 504 | - form[encAttr] = 'multipart/form-data'; |
|---|
| 505 | + if (s.beforeSend && s.beforeSend.call(s.context, xhr, s) === false) { |
|---|
| 506 | + if (s.global) { |
|---|
| 507 | + $.active--; |
|---|
| 508 | + } |
|---|
| 509 | + return; |
|---|
| 510 | + } |
|---|
| 511 | + if (xhr.aborted) { |
|---|
| 512 | + return; |
|---|
| 513 | + } |
|---|
| 514 | |
|---|
| 515 | - // support timout |
|---|
| 516 | - if (opts.timeout) |
|---|
| 517 | - setTimeout(function() { timedOut = true; cb(); }, opts.timeout); |
|---|
| 518 | + var timedOut = 0, timeoutHandle; |
|---|
| 519 | |
|---|
| 520 | - // add iframe to doc and submit the form |
|---|
| 521 | - $io.appendTo('body'); |
|---|
| 522 | - io.attachEvent ? io.attachEvent('onload', cb) : io.addEventListener('load', cb, false); |
|---|
| 523 | - form.submit(); |
|---|
| 524 | - $form.attr('target', t); // reset target |
|---|
| 525 | - }, 10); |
|---|
| 526 | + // add submitting element to data if we know it |
|---|
| 527 | + var sub = form.clk; |
|---|
| 528 | + if (sub) { |
|---|
| 529 | + var n = sub.name; |
|---|
| 530 | + if (n && !sub.disabled) { |
|---|
| 531 | + s.extraData = s.extraData || {}; |
|---|
| 532 | + s.extraData[n] = sub.value; |
|---|
| 533 | + if (sub.type == "image") { |
|---|
| 534 | + s.extraData[n+'.x'] = form.clk_x; |
|---|
| 535 | + s.extraData[n+'.y'] = form.clk_y; |
|---|
| 536 | + } |
|---|
| 537 | + } |
|---|
| 538 | + } |
|---|
| 539 | |
|---|
| 540 | - function cb() { |
|---|
| 541 | - if (cbInvoked++) return; |
|---|
| 542 | + // take a breath so that pending repaints get some cpu time before the upload starts |
|---|
| 543 | + function doSubmit() { |
|---|
| 544 | + // make sure form attrs are set |
|---|
| 545 | + var t = $form.attr('target'), a = $form.attr('action'); |
|---|
| 546 | |
|---|
| 547 | + // update form attrs in IE friendly way |
|---|
| 548 | + form.setAttribute('target',id); |
|---|
| 549 | + if (form.getAttribute('method') != 'POST') { |
|---|
| 550 | + form.setAttribute('method', 'POST'); |
|---|
| 551 | + } |
|---|
| 552 | + if (form.getAttribute('action') != s.url) { |
|---|
| 553 | + form.setAttribute('action', s.url); |
|---|
| 554 | + } |
|---|
| 555 | + |
|---|
| 556 | + // ie borks in some cases when setting encoding |
|---|
| 557 | + if (! s.skipEncodingOverride) { |
|---|
| 558 | + $form.attr({ |
|---|
| 559 | + encoding: 'multipart/form-data', |
|---|
| 560 | + enctype: 'multipart/form-data' |
|---|
| 561 | + }); |
|---|
| 562 | + } |
|---|
| 563 | + |
|---|
| 564 | + // support timout |
|---|
| 565 | + if (s.timeout) { |
|---|
| 566 | + timeoutHandle = setTimeout(function() { timedOut = true; cb(true); }, s.timeout); |
|---|
| 567 | + } |
|---|
| 568 | + |
|---|
| 569 | + // add "extra" data to form if provided in options |
|---|
| 570 | + var extraInputs = []; |
|---|
| 571 | + try { |
|---|
| 572 | + if (s.extraData) { |
|---|
| 573 | + for (var n in s.extraData) { |
|---|
| 574 | + extraInputs.push( |
|---|
| 575 | + $('<input type="hidden" name="'+n+'" value="'+s.extraData[n]+'" />') |
|---|
| 576 | + .appendTo(form)[0]); |
|---|
| 577 | + } |
|---|
| 578 | + } |
|---|
| 579 | + |
|---|
| 580 | + // add iframe to doc and submit the form |
|---|
| 581 | + $io.appendTo('body'); |
|---|
| 582 | + io.attachEvent ? io.attachEvent('onload', cb) : io.addEventListener('load', cb, false); |
|---|
| 583 | + form.submit(); |
|---|
| 584 | + } |
|---|
| 585 | + finally { |
|---|
| 586 | + // reset attrs and remove "extra" input elements |
|---|
| 587 | + form.setAttribute('action',a); |
|---|
| 588 | + if(t) { |
|---|
| 589 | + form.setAttribute('target', t); |
|---|
| 590 | + } else { |
|---|
| 591 | + $form.removeAttr('target'); |
|---|
| 592 | + } |
|---|
| 593 | + $(extraInputs).remove(); |
|---|
| 594 | + } |
|---|
| 595 | + } |
|---|
| 596 | + |
|---|
| 597 | + if (s.forceSync) { |
|---|
| 598 | + doSubmit(); |
|---|
| 599 | + } |
|---|
| 600 | + else { |
|---|
| 601 | + setTimeout(doSubmit, 10); // this lets dom updates render |
|---|
| 602 | + } |
|---|
| 603 | + |
|---|
| 604 | + var data, doc, domCheckCount = 50, callbackProcessed; |
|---|
| 605 | + |
|---|
| 606 | + function cb(e) { |
|---|
| 607 | + if (xhr.aborted || callbackProcessed) { |
|---|
| 608 | + return; |
|---|
| 609 | + } |
|---|
| 610 | + if (e === true && xhr) { |
|---|
| 611 | + xhr.abort('timeout'); |
|---|
| 612 | + return; |
|---|
| 613 | + } |
|---|
| 614 | + |
|---|
| 615 | + var doc = io.contentWindow ? io.contentWindow.document : io.contentDocument ? io.contentDocument : io.document; |
|---|
| 616 | + if (!doc || doc.location.href == s.iframeSrc) { |
|---|
| 617 | + // response not received yet |
|---|
| 618 | + if (!timedOut) |
|---|
| 619 | + return; |
|---|
| 620 | + } |
|---|
| 621 | io.detachEvent ? io.detachEvent('onload', cb) : io.removeEventListener('load', cb, false); |
|---|
| 622 | |
|---|
| 623 | - var ok = true; |
|---|
| 624 | - try { |
|---|
| 625 | - if (timedOut) throw 'timeout'; |
|---|
| 626 | - // extract the server response from the iframe |
|---|
| 627 | - var data, doc; |
|---|
| 628 | - doc = io.contentWindow ? io.contentWindow.document : io.contentDocument ? io.contentDocument : io.document; |
|---|
| 629 | - xhr.responseText = doc.body ? doc.body.innerHTML : null; |
|---|
| 630 | - xhr.responseXML = doc.XMLDocument ? doc.XMLDocument : doc; |
|---|
| 631 | + var ok = true; |
|---|
| 632 | + try { |
|---|
| 633 | + if (timedOut) { |
|---|
| 634 | + throw 'timeout'; |
|---|
| 635 | + } |
|---|
| 636 | |
|---|
| 637 | - if (opts.dataType == 'json' || opts.dataType == 'script') { |
|---|
| 638 | - var ta = doc.getElementsByTagName('textarea')[0]; |
|---|
| 639 | - data = ta ? ta.value : xhr.responseText; |
|---|
| 640 | - if (opts.dataType == 'json') |
|---|
| 641 | - eval("data = " + data); |
|---|
| 642 | - else |
|---|
| 643 | - $.globalEval(data); |
|---|
| 644 | - } |
|---|
| 645 | - else if (opts.dataType == 'xml') { |
|---|
| 646 | - data = xhr.responseXML; |
|---|
| 647 | - if (!data && xhr.responseText != null) |
|---|
| 648 | - data = toXml(xhr.responseText); |
|---|
| 649 | - } |
|---|
| 650 | - else { |
|---|
| 651 | - data = xhr.responseText; |
|---|
| 652 | - } |
|---|
| 653 | - } |
|---|
| 654 | - catch(e){ |
|---|
| 655 | - ok = false; |
|---|
| 656 | - $.handleError(opts, xhr, 'error', e); |
|---|
| 657 | - } |
|---|
| 658 | + var isXml = s.dataType == 'xml' || doc.XMLDocument || $.isXMLDoc(doc); |
|---|
| 659 | + log('isXml='+isXml); |
|---|
| 660 | + if (!isXml && window.opera && (doc.body == null || doc.body.innerHTML == '')) { |
|---|
| 661 | + if (--domCheckCount) { |
|---|
| 662 | + // in some browsers (Opera) the iframe DOM is not always traversable when |
|---|
| 663 | + // the onload callback fires, so we loop a bit to accommodate |
|---|
| 664 | + log('requeing onLoad callback, DOM not available'); |
|---|
| 665 | + setTimeout(cb, 250); |
|---|
| 666 | + return; |
|---|
| 667 | + } |
|---|
| 668 | + // let this fall through because server response could be an empty document |
|---|
| 669 | + //log('Could not access iframe DOM after mutiple tries.'); |
|---|
| 670 | + //throw 'DOMException: not available'; |
|---|
| 671 | + } |
|---|
| 672 | |
|---|
| 673 | - // ordering of these callbacks/triggers is odd, but that's how $.ajax does it |
|---|
| 674 | - if (ok) { |
|---|
| 675 | - opts.success(data, 'success'); |
|---|
| 676 | - if (g) $.event.trigger("ajaxSuccess", [xhr, opts]); |
|---|
| 677 | - } |
|---|
| 678 | - if (g) $.event.trigger("ajaxComplete", [xhr, opts]); |
|---|
| 679 | - if (g && ! --$.active) $.event.trigger("ajaxStop"); |
|---|
| 680 | - if (opts.complete) opts.complete(xhr, ok ? 'success' : 'error'); |
|---|
| 681 | + //log('response detected'); |
|---|
| 682 | + xhr.responseText = doc.body ? doc.body.innerHTML : doc.documentElement ? doc.documentElement.innerHTML : null; |
|---|
| 683 | + xhr.responseXML = doc.XMLDocument ? doc.XMLDocument : doc; |
|---|
| 684 | + if (isXml) |
|---|
| 685 | + s.dataType = 'xml'; |
|---|
| 686 | + xhr.getResponseHeader = function(header){ |
|---|
| 687 | + var headers = {'content-type': s.dataType}; |
|---|
| 688 | + return headers[header]; |
|---|
| 689 | + }; |
|---|
| 690 | |
|---|
| 691 | - // clean up |
|---|
| 692 | - setTimeout(function() { |
|---|
| 693 | - $io.remove(); |
|---|
| 694 | - xhr.responseXML = null; |
|---|
| 695 | - }, 100); |
|---|
| 696 | - }; |
|---|
| 697 | + var scr = /(json|script|text)/.test(s.dataType); |
|---|
| 698 | + if (scr || s.textarea) { |
|---|
| 699 | + // see if user embedded response in textarea |
|---|
| 700 | + var ta = doc.getElementsByTagName('textarea')[0]; |
|---|
| 701 | + if (ta) { |
|---|
| 702 | + xhr.responseText = ta.value; |
|---|
| 703 | + } |
|---|
| 704 | + else if (scr) { |
|---|
| 705 | + // account for browsers injecting pre around json response |
|---|
| 706 | + var pre = doc.getElementsByTagName('pre')[0]; |
|---|
| 707 | + var b = doc.getElementsByTagName('body')[0]; |
|---|
| 708 | + if (pre) { |
|---|
| 709 | + xhr.responseText = pre.textContent; |
|---|
| 710 | + } |
|---|
| 711 | + else if (b) { |
|---|
| 712 | + xhr.responseText = b.innerHTML; |
|---|
| 713 | + } |
|---|
| 714 | + } |
|---|
| 715 | + } |
|---|
| 716 | + else if (s.dataType == 'xml' && !xhr.responseXML && xhr.responseText != null) { |
|---|
| 717 | + xhr.responseXML = toXml(xhr.responseText); |
|---|
| 718 | + } |
|---|
| 719 | + |
|---|
| 720 | + data = httpData(xhr, s.dataType, s); |
|---|
| 721 | + } |
|---|
| 722 | + catch(e){ |
|---|
| 723 | + log('error caught:',e); |
|---|
| 724 | + ok = false; |
|---|
| 725 | + xhr.error = e; |
|---|
| 726 | + s.error && s.error.call(s.context, xhr, 'error', e); |
|---|
| 727 | + g && $.event.trigger("ajaxError", [xhr, s, e]); |
|---|
| 728 | + } |
|---|
| 729 | + |
|---|
| 730 | + if (xhr.aborted) { |
|---|
| 731 | + log('upload aborted'); |
|---|
| 732 | + ok = false; |
|---|
| 733 | + } |
|---|
| 734 | |
|---|
| 735 | - function toXml(s, doc) { |
|---|
| 736 | - if (window.ActiveXObject) { |
|---|
| 737 | - doc = new ActiveXObject('Microsoft.XMLDOM'); |
|---|
| 738 | - doc.async = 'false'; |
|---|
| 739 | - doc.loadXML(s); |
|---|
| 740 | - } |
|---|
| 741 | - else |
|---|
| 742 | - doc = (new DOMParser()).parseFromString(s, 'text/xml'); |
|---|
| 743 | - return (doc && doc.documentElement && doc.documentElement.tagName != 'parsererror') ? doc : null; |
|---|
| 744 | - }; |
|---|
| 745 | - }; |
|---|
| 746 | + // ordering of these callbacks/triggers is odd, but that's how $.ajax does it |
|---|
| 747 | + if (ok) { |
|---|
| 748 | + s.success && s.success.call(s.context, data, 'success', xhr); |
|---|
| 749 | + g && $.event.trigger("ajaxSuccess", [xhr, s]); |
|---|
| 750 | + } |
|---|
| 751 | + |
|---|
| 752 | + g && $.event.trigger("ajaxComplete", [xhr, s]); |
|---|
| 753 | + |
|---|
| 754 | + if (g && ! --$.active) { |
|---|
| 755 | + $.event.trigger("ajaxStop"); |
|---|
| 756 | + } |
|---|
| 757 | + |
|---|
| 758 | + s.complete && s.complete.call(s.context, xhr, ok ? 'success' : 'error'); |
|---|
| 759 | + |
|---|
| 760 | + callbackProcessed = true; |
|---|
| 761 | + if (s.timeout) |
|---|
| 762 | + clearTimeout(timeoutHandle); |
|---|
| 763 | + |
|---|
| 764 | + // clean up |
|---|
| 765 | + setTimeout(function() { |
|---|
| 766 | + $io.removeData('form-plugin-onload'); |
|---|
| 767 | + $io.remove(); |
|---|
| 768 | + xhr.responseXML = null; |
|---|
| 769 | + }, 100); |
|---|
| 770 | + } |
|---|
| 771 | + |
|---|
| 772 | + var toXml = $.parseXML || function(s, doc) { // use parseXML if available (jQuery 1.5+) |
|---|
| 773 | + if (window.ActiveXObject) { |
|---|
| 774 | + doc = new ActiveXObject('Microsoft.XMLDOM'); |
|---|
| 775 | + doc.async = 'false'; |
|---|
| 776 | + doc.loadXML(s); |
|---|
| 777 | + } |
|---|
| 778 | + else { |
|---|
| 779 | + doc = (new DOMParser()).parseFromString(s, 'text/xml'); |
|---|
| 780 | + } |
|---|
| 781 | + return (doc && doc.documentElement && doc.documentElement.nodeName != 'parsererror') ? doc : null; |
|---|
| 782 | + }; |
|---|
| 783 | + var parseJSON = $.parseJSON || function(s) { |
|---|
| 784 | + return window['eval']('(' + s + ')'); |
|---|
| 785 | + }; |
|---|
| 786 | + |
|---|
| 787 | + var httpData = function( xhr, type, s ) { // mostly lifted from jq1.4.4 |
|---|
| 788 | + var ct = xhr.getResponseHeader('content-type') || '', |
|---|
| 789 | + xml = type === 'xml' || !type && ct.indexOf('xml') >= 0, |
|---|
| 790 | + data = xml ? xhr.responseXML : xhr.responseText; |
|---|
| 791 | + |
|---|
| 792 | + if (xml && data.documentElement.nodeName === 'parsererror') { |
|---|
| 793 | + $.error && $.error('parsererror'); |
|---|
| 794 | + } |
|---|
| 795 | + if (s && s.dataFilter) { |
|---|
| 796 | + data = s.dataFilter(data, type); |
|---|
| 797 | + } |
|---|
| 798 | + if (typeof data === 'string') { |
|---|
| 799 | + if (type === 'json' || !type && ct.indexOf('json') >= 0) { |
|---|
| 800 | + data = parseJSON(data); |
|---|
| 801 | + } else if (type === "script" || !type && ct.indexOf("javascript") >= 0) { |
|---|
| 802 | + $.globalEval(data); |
|---|
| 803 | + } |
|---|
| 804 | + } |
|---|
| 805 | + return data; |
|---|
| 806 | + }; |
|---|
| 807 | + } |
|---|
| 808 | }; |
|---|
| 809 | -$.fn.ajaxSubmit.counter = 0; // used to create unique iframe ids |
|---|
| 810 | |
|---|
| 811 | /** |
|---|
| 812 | * ajaxForm() provides a mechanism for fully automating form submission. |
|---|
| 813 | @@ -384,112 +472,70 @@ |
|---|
| 814 | * The advantages of using this method instead of ajaxSubmit() are: |
|---|
| 815 | * |
|---|
| 816 | * 1: This method will include coordinates for <input type="image" /> elements (if the element |
|---|
| 817 | - * is used to submit the form). |
|---|
| 818 | + * is used to submit the form). |
|---|
| 819 | * 2. This method will include the submit element's name/value data (for the element that was |
|---|
| 820 | - * used to submit the form). |
|---|
| 821 | + * used to submit the form). |
|---|
| 822 | * 3. This method binds the submit() method to the form for you. |
|---|
| 823 | * |
|---|
| 824 | - * Note that for accurate x/y coordinates of image submit elements in all browsers |
|---|
| 825 | - * you need to also use the "dimensions" plugin (this method will auto-detect its presence). |
|---|
| 826 | - * |
|---|
| 827 | * The options argument for ajaxForm works exactly as it does for ajaxSubmit. ajaxForm merely |
|---|
| 828 | * passes the options argument along after properly binding events for submit elements and |
|---|
| 829 | - * the form itself. See ajaxSubmit for a full description of the options argument. |
|---|
| 830 | - * |
|---|
| 831 | - * |
|---|
| 832 | - * @example |
|---|
| 833 | - * var options = { |
|---|
| 834 | - * target: '#myTargetDiv' |
|---|
| 835 | - * }; |
|---|
| 836 | - * $('#myForm').ajaxSForm(options); |
|---|
| 837 | - * @desc Bind form's submit event so that 'myTargetDiv' is updated with the server response |
|---|
| 838 | - * when the form is submitted. |
|---|
| 839 | - * |
|---|
| 840 | - * |
|---|
| 841 | - * @example |
|---|
| 842 | - * var options = { |
|---|
| 843 | - * success: function(responseText) { |
|---|
| 844 | - * alert(responseText); |
|---|
| 845 | - * } |
|---|
| 846 | - * }; |
|---|
| 847 | - * $('#myForm').ajaxSubmit(options); |
|---|
| 848 | - * @desc Bind form's submit event so that server response is alerted after the form is submitted. |
|---|
| 849 | - * |
|---|
| 850 | - * |
|---|
| 851 | - * @example |
|---|
| 852 | - * var options = { |
|---|
| 853 | - * beforeSubmit: function(formArray, jqForm) { |
|---|
| 854 | - * if (formArray.length == 0) { |
|---|
| 855 | - * alert('Please enter data.'); |
|---|
| 856 | - * return false; |
|---|
| 857 | - * } |
|---|
| 858 | - * } |
|---|
| 859 | - * }; |
|---|
| 860 | - * $('#myForm').ajaxSubmit(options); |
|---|
| 861 | - * @desc Bind form's submit event so that pre-submit callback is invoked before the form |
|---|
| 862 | - * is submitted. |
|---|
| 863 | - * |
|---|
| 864 | - * |
|---|
| 865 | - * @name ajaxForm |
|---|
| 866 | - * @param options object literal containing options which control the form submission process |
|---|
| 867 | - * @return jQuery |
|---|
| 868 | - * @cat Plugins/Form |
|---|
| 869 | - * @type jQuery |
|---|
| 870 | + * the form itself. |
|---|
| 871 | */ |
|---|
| 872 | $.fn.ajaxForm = function(options) { |
|---|
| 873 | - return this.ajaxFormUnbind().submit(submitHandler).each(function() { |
|---|
| 874 | - // store options in hash |
|---|
| 875 | - this.formPluginId = $.fn.ajaxForm.counter++; |
|---|
| 876 | - $.fn.ajaxForm.optionHash[this.formPluginId] = options; |
|---|
| 877 | - $(":submit,input:image", this).click(clickHandler); |
|---|
| 878 | - }); |
|---|
| 879 | + // in jQuery 1.3+ we can fix mistakes with the ready state |
|---|
| 880 | + if (this.length === 0) { |
|---|
| 881 | + var o = { s: this.selector, c: this.context }; |
|---|
| 882 | + if (!$.isReady && o.s) { |
|---|
| 883 | + log('DOM not ready, queuing ajaxForm'); |
|---|
| 884 | + $(function() { |
|---|
| 885 | + $(o.s,o.c).ajaxForm(options); |
|---|
| 886 | + }); |
|---|
| 887 | + return this; |
|---|
| 888 | + } |
|---|
| 889 | + // is your DOM ready? http://docs.jquery.com/Tutorials:Introducing_$(document).ready() |
|---|
| 890 | + log('terminating; zero elements found by selector' + ($.isReady ? '' : ' (DOM not ready)')); |
|---|
| 891 | + return this; |
|---|
| 892 | + } |
|---|
| 893 | + |
|---|
| 894 | + return this.ajaxFormUnbind().bind('submit.form-plugin', function(e) { |
|---|
| 895 | + if (!e.isDefaultPrevented()) { // if event has been canceled, don't proceed |
|---|
| 896 | + e.preventDefault(); |
|---|
| 897 | + $(this).ajaxSubmit(options); |
|---|
| 898 | + } |
|---|
| 899 | + }).bind('click.form-plugin', function(e) { |
|---|
| 900 | + var target = e.target; |
|---|
| 901 | + var $el = $(target); |
|---|
| 902 | + if (!($el.is(":submit,input:image"))) { |
|---|
| 903 | + // is this a child element of the submit el? (ex: a span within a button) |
|---|
| 904 | + var t = $el.closest(':submit'); |
|---|
| 905 | + if (t.length == 0) { |
|---|
| 906 | + return; |
|---|
| 907 | + } |
|---|
| 908 | + target = t[0]; |
|---|
| 909 | + } |
|---|
| 910 | + var form = this; |
|---|
| 911 | + form.clk = target; |
|---|
| 912 | + if (target.type == 'image') { |
|---|
| 913 | + if (e.offsetX != undefined) { |
|---|
| 914 | + form.clk_x = e.offsetX; |
|---|
| 915 | + form.clk_y = e.offsetY; |
|---|
| 916 | + } else if (typeof $.fn.offset == 'function') { // try to use dimensions plugin |
|---|
| 917 | + var offset = $el.offset(); |
|---|
| 918 | + form.clk_x = e.pageX - offset.left; |
|---|
| 919 | + form.clk_y = e.pageY - offset.top; |
|---|
| 920 | + } else { |
|---|
| 921 | + form.clk_x = e.pageX - target.offsetLeft; |
|---|
| 922 | + form.clk_y = e.pageY - target.offsetTop; |
|---|
| 923 | + } |
|---|
| 924 | + } |
|---|
| 925 | + // clear form vars |
|---|
| 926 | + setTimeout(function() { form.clk = form.clk_x = form.clk_y = null; }, 100); |
|---|
| 927 | + }); |
|---|
| 928 | }; |
|---|
| 929 | |
|---|
| 930 | -$.fn.ajaxForm.counter = 1; |
|---|
| 931 | -$.fn.ajaxForm.optionHash = {}; |
|---|
| 932 | - |
|---|
| 933 | -function clickHandler(e) { |
|---|
| 934 | - var $form = this.form; |
|---|
| 935 | - $form.clk = this; |
|---|
| 936 | - if (this.type == 'image') { |
|---|
| 937 | - if (e.offsetX != undefined) { |
|---|
| 938 | - $form.clk_x = e.offsetX; |
|---|
| 939 | - $form.clk_y = e.offsetY; |
|---|
| 940 | - } else if (typeof $.fn.offset == 'function') { // try to use dimensions plugin |
|---|
| 941 | - var offset = $(this).offset(); |
|---|
| 942 | - $form.clk_x = e.pageX - offset.left; |
|---|
| 943 | - $form.clk_y = e.pageY - offset.top; |
|---|
| 944 | - } else { |
|---|
| 945 | - $form.clk_x = e.pageX - this.offsetLeft; |
|---|
| 946 | - $form.clk_y = e.pageY - this.offsetTop; |
|---|
| 947 | - } |
|---|
| 948 | - } |
|---|
| 949 | - // clear form vars |
|---|
| 950 | - setTimeout(function() { $form.clk = $form.clk_x = $form.clk_y = null; }, 10); |
|---|
| 951 | -}; |
|---|
| 952 | - |
|---|
| 953 | -function submitHandler() { |
|---|
| 954 | - // retrieve options from hash |
|---|
| 955 | - var id = this.formPluginId; |
|---|
| 956 | - var options = $.fn.ajaxForm.optionHash[id]; |
|---|
| 957 | - $(this).ajaxSubmit(options); |
|---|
| 958 | - return false; |
|---|
| 959 | -}; |
|---|
| 960 | - |
|---|
| 961 | -/** |
|---|
| 962 | - * ajaxFormUnbind unbinds the event handlers that were bound by ajaxForm |
|---|
| 963 | - * |
|---|
| 964 | - * @name ajaxFormUnbind |
|---|
| 965 | - * @return jQuery |
|---|
| 966 | - * @cat Plugins/Form |
|---|
| 967 | - * @type jQuery |
|---|
| 968 | - */ |
|---|
| 969 | +// ajaxFormUnbind unbinds the event handlers that were bound by ajaxForm |
|---|
| 970 | $.fn.ajaxFormUnbind = function() { |
|---|
| 971 | - this.unbind('submit', submitHandler); |
|---|
| 972 | - return this.each(function() { |
|---|
| 973 | - $(":submit,input:image", this).unbind('click', clickHandler); |
|---|
| 974 | - }); |
|---|
| 975 | - |
|---|
| 976 | + return this.unbind('submit.form-plugin click.form-plugin'); |
|---|
| 977 | }; |
|---|
| 978 | |
|---|
| 979 | /** |
|---|
| 980 | @@ -502,144 +548,103 @@ |
|---|
| 981 | * |
|---|
| 982 | * It is this array that is passed to pre-submit callback functions provided to the |
|---|
| 983 | * ajaxSubmit() and ajaxForm() methods. |
|---|
| 984 | - * |
|---|
| 985 | - * The semantic argument can be used to force form serialization in semantic order. |
|---|
| 986 | - * This is normally true anyway, unless the form contains input elements of type='image'. |
|---|
| 987 | - * If your form must be submitted with name/value pairs in semantic order and your form |
|---|
| 988 | - * contains an input of type='image" then pass true for this arg, otherwise pass false |
|---|
| 989 | - * (or nothing) to avoid the overhead for this logic. |
|---|
| 990 | - * |
|---|
| 991 | - * @example var data = $("#myForm").formToArray(); |
|---|
| 992 | - * $.post( "myscript.cgi", data ); |
|---|
| 993 | - * @desc Collect all the data from a form and submit it to the server. |
|---|
| 994 | - * |
|---|
| 995 | - * @name formToArray |
|---|
| 996 | - * @param semantic true if serialization must maintain strict semantic ordering of elements (slower) |
|---|
| 997 | - * @type Array<Object> |
|---|
| 998 | - * @cat Plugins/Form |
|---|
| 999 | */ |
|---|
| 1000 | $.fn.formToArray = function(semantic) { |
|---|
| 1001 | - var a = []; |
|---|
| 1002 | - if (this.length == 0) return a; |
|---|
| 1003 | + var a = []; |
|---|
| 1004 | + if (this.length === 0) { |
|---|
| 1005 | + return a; |
|---|
| 1006 | + } |
|---|
| 1007 | |
|---|
| 1008 | - var form = this[0]; |
|---|
| 1009 | - var els = semantic ? form.getElementsByTagName('*') : form.elements; |
|---|
| 1010 | - if (!els) return a; |
|---|
| 1011 | - for(var i=0, max=els.length; i < max; i++) { |
|---|
| 1012 | - var el = els[i]; |
|---|
| 1013 | - var n = el.name; |
|---|
| 1014 | - if (!n) continue; |
|---|
| 1015 | + var form = this[0]; |
|---|
| 1016 | + var els = semantic ? form.getElementsByTagName('*') : form.elements; |
|---|
| 1017 | + if (!els) { |
|---|
| 1018 | + return a; |
|---|
| 1019 | + } |
|---|
| 1020 | + |
|---|
| 1021 | + var i,j,n,v,el,max,jmax; |
|---|
| 1022 | + for(i=0, max=els.length; i < max; i++) { |
|---|
| 1023 | + el = els[i]; |
|---|
| 1024 | + n = el.name; |
|---|
| 1025 | + if (!n) { |
|---|
| 1026 | + continue; |
|---|
| 1027 | + } |
|---|
| 1028 | |
|---|
| 1029 | - if (semantic && form.clk && el.type == "image") { |
|---|
| 1030 | - // handle image inputs on the fly when semantic == true |
|---|
| 1031 | - if(!el.disabled && form.clk == el) |
|---|
| 1032 | - a.push({name: n+'.x', value: form.clk_x}, {name: n+'.y', value: form.clk_y}); |
|---|
| 1033 | - continue; |
|---|
| 1034 | - } |
|---|
| 1035 | + if (semantic && form.clk && el.type == "image") { |
|---|
| 1036 | + // handle image inputs on the fly when semantic == true |
|---|
| 1037 | + if(!el.disabled && form.clk == el) { |
|---|
| 1038 | + a.push({name: n, value: $(el).val()}); |
|---|
| 1039 | + a.push({name: n+'.x', value: form.clk_x}, {name: n+'.y', value: form.clk_y}); |
|---|
| 1040 | + } |
|---|
| 1041 | + continue; |
|---|
| 1042 | + } |
|---|
| 1043 | |
|---|
| 1044 | - var v = $.fieldValue(el, true); |
|---|
| 1045 | - if (v && v.constructor == Array) { |
|---|
| 1046 | - for(var j=0, jmax=v.length; j < jmax; j++) |
|---|
| 1047 | - a.push({name: n, value: v[j]}); |
|---|
| 1048 | - } |
|---|
| 1049 | - else if (v !== null && typeof v != 'undefined') |
|---|
| 1050 | - a.push({name: n, value: v}); |
|---|
| 1051 | - } |
|---|
| 1052 | + v = $.fieldValue(el, true); |
|---|
| 1053 | + if (v && v.constructor == Array) { |
|---|
| 1054 | + for(j=0, jmax=v.length; j < jmax; j++) { |
|---|
| 1055 | + a.push({name: n, value: v[j]}); |
|---|
| 1056 | + } |
|---|
| 1057 | + } |
|---|
| 1058 | + else if (v !== null && typeof v != 'undefined') { |
|---|
| 1059 | + a.push({name: n, value: v}); |
|---|
| 1060 | + } |
|---|
| 1061 | + } |
|---|
| 1062 | |
|---|
| 1063 | - if (!semantic && form.clk) { |
|---|
| 1064 | - // input type=='image' are not found in elements array! handle them here |
|---|
| 1065 | - var inputs = form.getElementsByTagName("input"); |
|---|
| 1066 | - for(var i=0, max=inputs.length; i < max; i++) { |
|---|
| 1067 | - var input = inputs[i]; |
|---|
| 1068 | - var n = input.name; |
|---|
| 1069 | - if(n && !input.disabled && input.type == "image" && form.clk == input) |
|---|
| 1070 | - a.push({name: n+'.x', value: form.clk_x}, {name: n+'.y', value: form.clk_y}); |
|---|
| 1071 | - } |
|---|
| 1072 | - } |
|---|
| 1073 | - return a; |
|---|
| 1074 | + if (!semantic && form.clk) { |
|---|
| 1075 | + // input type=='image' are not found in elements array! handle it here |
|---|
| 1076 | + var $input = $(form.clk), input = $input[0]; |
|---|
| 1077 | + n = input.name; |
|---|
| 1078 | + if (n && !input.disabled && input.type == 'image') { |
|---|
| 1079 | + a.push({name: n, value: $input.val()}); |
|---|
| 1080 | + a.push({name: n+'.x', value: form.clk_x}, {name: n+'.y', value: form.clk_y}); |
|---|
| 1081 | + } |
|---|
| 1082 | + } |
|---|
| 1083 | + return a; |
|---|
| 1084 | }; |
|---|
| 1085 | |
|---|
| 1086 | - |
|---|
| 1087 | /** |
|---|
| 1088 | * Serializes form data into a 'submittable' string. This method will return a string |
|---|
| 1089 | * in the format: name1=value1&name2=value2 |
|---|
| 1090 | - * |
|---|
| 1091 | - * The semantic argument can be used to force form serialization in semantic order. |
|---|
| 1092 | - * If your form must be submitted with name/value pairs in semantic order then pass |
|---|
| 1093 | - * true for this arg, otherwise pass false (or nothing) to avoid the overhead for |
|---|
| 1094 | - * this logic (which can be significant for very large forms). |
|---|
| 1095 | - * |
|---|
| 1096 | - * @example var data = $("#myForm").formSerialize(); |
|---|
| 1097 | - * $.ajax('POST', "myscript.cgi", data); |
|---|
| 1098 | - * @desc Collect all the data from a form into a single string |
|---|
| 1099 | - * |
|---|
| 1100 | - * @name formSerialize |
|---|
| 1101 | - * @param semantic true if serialization must maintain strict semantic ordering of elements (slower) |
|---|
| 1102 | - * @type String |
|---|
| 1103 | - * @cat Plugins/Form |
|---|
| 1104 | */ |
|---|
| 1105 | $.fn.formSerialize = function(semantic) { |
|---|
| 1106 | - //hand off to jQuery.param for proper encoding |
|---|
| 1107 | - return $.param(this.formToArray(semantic)); |
|---|
| 1108 | + //hand off to jQuery.param for proper encoding |
|---|
| 1109 | + return $.param(this.formToArray(semantic)); |
|---|
| 1110 | }; |
|---|
| 1111 | |
|---|
| 1112 | - |
|---|
| 1113 | /** |
|---|
| 1114 | * Serializes all field elements in the jQuery object into a query string. |
|---|
| 1115 | * This method will return a string in the format: name1=value1&name2=value2 |
|---|
| 1116 | - * |
|---|
| 1117 | - * The successful argument controls whether or not serialization is limited to |
|---|
| 1118 | - * 'successful' controls (per http://www.w3.org/TR/html4/interact/forms.html#successful-controls). |
|---|
| 1119 | - * The default value of the successful argument is true. |
|---|
| 1120 | - * |
|---|
| 1121 | - * @example var data = $("input").formSerialize(); |
|---|
| 1122 | - * @desc Collect the data from all successful input elements into a query string |
|---|
| 1123 | - * |
|---|
| 1124 | - * @example var data = $(":radio").formSerialize(); |
|---|
| 1125 | - * @desc Collect the data from all successful radio input elements into a query string |
|---|
| 1126 | - * |
|---|
| 1127 | - * @example var data = $("#myForm :checkbox").formSerialize(); |
|---|
| 1128 | - * @desc Collect the data from all successful checkbox input elements in myForm into a query string |
|---|
| 1129 | - * |
|---|
| 1130 | - * @example var data = $("#myForm :checkbox").formSerialize(false); |
|---|
| 1131 | - * @desc Collect the data from all checkbox elements in myForm (even the unchecked ones) into a query string |
|---|
| 1132 | - * |
|---|
| 1133 | - * @example var data = $(":input").formSerialize(); |
|---|
| 1134 | - * @desc Collect the data from all successful input, select, textarea and button elements into a query string |
|---|
| 1135 | - * |
|---|
| 1136 | - * @name fieldSerialize |
|---|
| 1137 | - * @param successful true if only successful controls should be serialized (default is true) |
|---|
| 1138 | - * @type String |
|---|
| 1139 | - * @cat Plugins/Form |
|---|
| 1140 | */ |
|---|
| 1141 | $.fn.fieldSerialize = function(successful) { |
|---|
| 1142 | - var a = []; |
|---|
| 1143 | - this.each(function() { |
|---|
| 1144 | - var n = this.name; |
|---|
| 1145 | - if (!n) return; |
|---|
| 1146 | - var v = $.fieldValue(this, successful); |
|---|
| 1147 | - if (v && v.constructor == Array) { |
|---|
| 1148 | - for (var i=0,max=v.length; i < max; i++) |
|---|
| 1149 | - a.push({name: n, value: v[i]}); |
|---|
| 1150 | - } |
|---|
| 1151 | - else if (v !== null && typeof v != 'undefined') |
|---|
| 1152 | - a.push({name: this.name, value: v}); |
|---|
| 1153 | - }); |
|---|
| 1154 | - //hand off to jQuery.param for proper encoding |
|---|
| 1155 | - return $.param(a); |
|---|
| 1156 | + var a = []; |
|---|
| 1157 | + this.each(function() { |
|---|
| 1158 | + var n = this.name; |
|---|
| 1159 | + if (!n) { |
|---|
| 1160 | + return; |
|---|
| 1161 | + } |
|---|
| 1162 | + var v = $.fieldValue(this, successful); |
|---|
| 1163 | + if (v && v.constructor == Array) { |
|---|
| 1164 | + for (var i=0,max=v.length; i < max; i++) { |
|---|
| 1165 | + a.push({name: n, value: v[i]}); |
|---|
| 1166 | + } |
|---|
| 1167 | + } |
|---|
| 1168 | + else if (v !== null && typeof v != 'undefined') { |
|---|
| 1169 | + a.push({name: this.name, value: v}); |
|---|
| 1170 | + } |
|---|
| 1171 | + }); |
|---|
| 1172 | + //hand off to jQuery.param for proper encoding |
|---|
| 1173 | + return $.param(a); |
|---|
| 1174 | }; |
|---|
| 1175 | |
|---|
| 1176 | - |
|---|
| 1177 | /** |
|---|
| 1178 | * Returns the value(s) of the element in the matched set. For example, consider the following form: |
|---|
| 1179 | * |
|---|
| 1180 | * <form><fieldset> |
|---|
| 1181 | - * <input name="A" type="text" /> |
|---|
| 1182 | - * <input name="A" type="text" /> |
|---|
| 1183 | - * <input name="B" type="checkbox" value="B1" /> |
|---|
| 1184 | - * <input name="B" type="checkbox" value="B2"/> |
|---|
| 1185 | - * <input name="C" type="radio" value="C1" /> |
|---|
| 1186 | - * <input name="C" type="radio" value="C2" /> |
|---|
| 1187 | + * <input name="A" type="text" /> |
|---|
| 1188 | + * <input name="A" type="text" /> |
|---|
| 1189 | + * <input name="B" type="checkbox" value="B1" /> |
|---|
| 1190 | + * <input name="B" type="checkbox" value="B2"/> |
|---|
| 1191 | + * <input name="C" type="radio" value="C1" /> |
|---|
| 1192 | + * <input name="C" type="radio" value="C2" /> |
|---|
| 1193 | * </fieldset></form> |
|---|
| 1194 | * |
|---|
| 1195 | * var v = $(':text').fieldValue(); |
|---|
| 1196 | @@ -666,95 +671,62 @@ |
|---|
| 1197 | * for each element is returned. |
|---|
| 1198 | * |
|---|
| 1199 | * Note: This method *always* returns an array. If no valid value can be determined the |
|---|
| 1200 | - * array will be empty, otherwise it will contain one or more values. |
|---|
| 1201 | - * |
|---|
| 1202 | - * @example var data = $("#myPasswordElement").fieldValue(); |
|---|
| 1203 | - * alert(data[0]); |
|---|
| 1204 | - * @desc Alerts the current value of the myPasswordElement element |
|---|
| 1205 | - * |
|---|
| 1206 | - * @example var data = $("#myForm :input").fieldValue(); |
|---|
| 1207 | - * @desc Get the value(s) of the form elements in myForm |
|---|
| 1208 | - * |
|---|
| 1209 | - * @example var data = $("#myForm :checkbox").fieldValue(); |
|---|
| 1210 | - * @desc Get the value(s) for the successful checkbox element(s) in the jQuery object. |
|---|
| 1211 | - * |
|---|
| 1212 | - * @example var data = $("#mySingleSelect").fieldValue(); |
|---|
| 1213 | - * @desc Get the value(s) of the select control |
|---|
| 1214 | - * |
|---|
| 1215 | - * @example var data = $(':text').fieldValue(); |
|---|
| 1216 | - * @desc Get the value(s) of the text input or textarea elements |
|---|
| 1217 | - * |
|---|
| 1218 | - * @example var data = $("#myMultiSelect").fieldValue(); |
|---|
| 1219 | - * @desc Get the values for the select-multiple control |
|---|
| 1220 | - * |
|---|
| 1221 | - * @name fieldValue |
|---|
| 1222 | - * @param Boolean successful true if only the values for successful controls should be returned (default is true) |
|---|
| 1223 | - * @type Array<String> |
|---|
| 1224 | - * @cat Plugins/Form |
|---|
| 1225 | + * array will be empty, otherwise it will contain one or more values. |
|---|
| 1226 | */ |
|---|
| 1227 | $.fn.fieldValue = function(successful) { |
|---|
| 1228 | - for (var val=[], i=0, max=this.length; i < max; i++) { |
|---|
| 1229 | - var el = this[i]; |
|---|
| 1230 | - var v = $.fieldValue(el, successful); |
|---|
| 1231 | - if (v === null || typeof v == 'undefined' || (v.constructor == Array && !v.length)) |
|---|
| 1232 | - continue; |
|---|
| 1233 | - v.constructor == Array ? $.merge(val, v) : val.push(v); |
|---|
| 1234 | - } |
|---|
| 1235 | - return val; |
|---|
| 1236 | + for (var val=[], i=0, max=this.length; i < max; i++) { |
|---|
| 1237 | + var el = this[i]; |
|---|
| 1238 | + var v = $.fieldValue(el, successful); |
|---|
| 1239 | + if (v === null || typeof v == 'undefined' || (v.constructor == Array && !v.length)) { |
|---|
| 1240 | + continue; |
|---|
| 1241 | + } |
|---|
| 1242 | + v.constructor == Array ? $.merge(val, v) : val.push(v); |
|---|
| 1243 | + } |
|---|
| 1244 | + return val; |
|---|
| 1245 | }; |
|---|
| 1246 | |
|---|
| 1247 | /** |
|---|
| 1248 | * Returns the value of the field element. |
|---|
| 1249 | - * |
|---|
| 1250 | - * The successful argument controls whether or not the field element must be 'successful' |
|---|
| 1251 | - * (per http://www.w3.org/TR/html4/interact/forms.html#successful-controls). |
|---|
| 1252 | - * The default value of the successful argument is true. If the given element is not |
|---|
| 1253 | - * successful and the successful arg is not false then the returned value will be null. |
|---|
| 1254 | - * |
|---|
| 1255 | - * Note: If the successful flag is true (default) but the element is not successful, the return will be null |
|---|
| 1256 | - * Note: The value returned for a successful select-multiple element will always be an array. |
|---|
| 1257 | - * Note: If the element has no value the return value will be undefined. |
|---|
| 1258 | - * |
|---|
| 1259 | - * @example var data = jQuery.fieldValue($("#myPasswordElement")[0]); |
|---|
| 1260 | - * @desc Gets the current value of the myPasswordElement element |
|---|
| 1261 | - * |
|---|
| 1262 | - * @name fieldValue |
|---|
| 1263 | - * @param Element el The DOM element for which the value will be returned |
|---|
| 1264 | - * @param Boolean successful true if value returned must be for a successful controls (default is true) |
|---|
| 1265 | - * @type String or Array<String> or null or undefined |
|---|
| 1266 | - * @cat Plugins/Form |
|---|
| 1267 | */ |
|---|
| 1268 | $.fieldValue = function(el, successful) { |
|---|
| 1269 | - var n = el.name, t = el.type, tag = el.tagName.toLowerCase(); |
|---|
| 1270 | - if (typeof successful == 'undefined') successful = true; |
|---|
| 1271 | + var n = el.name, t = el.type, tag = el.tagName.toLowerCase(); |
|---|
| 1272 | + if (successful === undefined) { |
|---|
| 1273 | + successful = true; |
|---|
| 1274 | + } |
|---|
| 1275 | |
|---|
| 1276 | - if (successful && (!n || el.disabled || t == 'reset' || t == 'button' || |
|---|
| 1277 | - (t == 'checkbox' || t == 'radio') && !el.checked || |
|---|
| 1278 | - (t == 'submit' || t == 'image') && el.form && el.form.clk != el || |
|---|
| 1279 | - tag == 'select' && el.selectedIndex == -1)) |
|---|
| 1280 | - return null; |
|---|
| 1281 | + if (successful && (!n || el.disabled || t == 'reset' || t == 'button' || |
|---|
| 1282 | + (t == 'checkbox' || t == 'radio') && !el.checked || |
|---|
| 1283 | + (t == 'submit' || t == 'image') && el.form && el.form.clk != el || |
|---|
| 1284 | + tag == 'select' && el.selectedIndex == -1)) { |
|---|
| 1285 | + return null; |
|---|
| 1286 | + } |
|---|
| 1287 | |
|---|
| 1288 | - if (tag == 'select') { |
|---|
| 1289 | - var index = el.selectedIndex; |
|---|
| 1290 | - if (index < 0) return null; |
|---|
| 1291 | - var a = [], ops = el.options; |
|---|
| 1292 | - var one = (t == 'select-one'); |
|---|
| 1293 | - var max = (one ? index+1 : ops.length); |
|---|
| 1294 | - for(var i=(one ? index : 0); i < max; i++) { |
|---|
| 1295 | - var op = ops[i]; |
|---|
| 1296 | - if (op.selected) { |
|---|
| 1297 | - // extra pain for IE... |
|---|
| 1298 | - var v = $.browser.msie && !(op.attributes['value'].specified) ? op.text : op.value; |
|---|
| 1299 | - if (one) return v; |
|---|
| 1300 | - a.push(v); |
|---|
| 1301 | - } |
|---|
| 1302 | - } |
|---|
| 1303 | - return a; |
|---|
| 1304 | - } |
|---|
| 1305 | - return el.value; |
|---|
| 1306 | + if (tag == 'select') { |
|---|
| 1307 | + var index = el.selectedIndex; |
|---|
| 1308 | + if (index < 0) { |
|---|
| 1309 | + return null; |
|---|
| 1310 | + } |
|---|
| 1311 | + var a = [], ops = el.options; |
|---|
| 1312 | + var one = (t == 'select-one'); |
|---|
| 1313 | + var max = (one ? index+1 : ops.length); |
|---|
| 1314 | + for(var i=(one ? index : 0); i < max; i++) { |
|---|
| 1315 | + var op = ops[i]; |
|---|
| 1316 | + if (op.selected) { |
|---|
| 1317 | + var v = op.value; |
|---|
| 1318 | + if (!v) { // extra pain for IE... |
|---|
| 1319 | + v = (op.attributes && op.attributes['value'] && !(op.attributes['value'].specified)) ? op.text : op.value; |
|---|
| 1320 | + } |
|---|
| 1321 | + if (one) { |
|---|
| 1322 | + return v; |
|---|
| 1323 | + } |
|---|
| 1324 | + a.push(v); |
|---|
| 1325 | + } |
|---|
| 1326 | + } |
|---|
| 1327 | + return a; |
|---|
| 1328 | + } |
|---|
| 1329 | + return $(el).val(); |
|---|
| 1330 | }; |
|---|
| 1331 | |
|---|
| 1332 | - |
|---|
| 1333 | /** |
|---|
| 1334 | * Clears the form data. Takes the following actions on the form's input fields: |
|---|
| 1335 | * - input text fields will have their 'value' property set to the empty string |
|---|
| 1336 | @@ -762,111 +734,92 @@ |
|---|
| 1337 | * - checkbox and radio inputs will have their 'checked' property set to false |
|---|
| 1338 | * - inputs of type submit, button, reset, and hidden will *not* be effected |
|---|
| 1339 | * - button elements will *not* be effected |
|---|
| 1340 | - * |
|---|
| 1341 | - * @example $('form').clearForm(); |
|---|
| 1342 | - * @desc Clears all forms on the page. |
|---|
| 1343 | - * |
|---|
| 1344 | - * @name clearForm |
|---|
| 1345 | - * @type jQuery |
|---|
| 1346 | - * @cat Plugins/Form |
|---|
| 1347 | */ |
|---|
| 1348 | $.fn.clearForm = function() { |
|---|
| 1349 | - return this.each(function() { |
|---|
| 1350 | - $('input,select,textarea', this).clearFields(); |
|---|
| 1351 | - }); |
|---|
| 1352 | + return this.each(function() { |
|---|
| 1353 | + $('input,select,textarea', this).clearFields(); |
|---|
| 1354 | + }); |
|---|
| 1355 | }; |
|---|
| 1356 | |
|---|
| 1357 | /** |
|---|
| 1358 | - * Clears the selected form elements. Takes the following actions on the matched elements: |
|---|
| 1359 | - * - input text fields will have their 'value' property set to the empty string |
|---|
| 1360 | - * - select elements will have their 'selectedIndex' property set to -1 |
|---|
| 1361 | - * - checkbox and radio inputs will have their 'checked' property set to false |
|---|
| 1362 | - * - inputs of type submit, button, reset, and hidden will *not* be effected |
|---|
| 1363 | - * - button elements will *not* be effected |
|---|
| 1364 | - * |
|---|
| 1365 | - * @example $('.myInputs').clearFields(); |
|---|
| 1366 | - * @desc Clears all inputs with class myInputs |
|---|
| 1367 | - * |
|---|
| 1368 | - * @name clearFields |
|---|
| 1369 | - * @type jQuery |
|---|
| 1370 | - * @cat Plugins/Form |
|---|
| 1371 | + * Clears the selected form elements. |
|---|
| 1372 | */ |
|---|
| 1373 | $.fn.clearFields = $.fn.clearInputs = function() { |
|---|
| 1374 | - return this.each(function() { |
|---|
| 1375 | - var t = this.type, tag = this.tagName.toLowerCase(); |
|---|
| 1376 | - if (t == 'text' || t == 'password' || tag == 'textarea') |
|---|
| 1377 | - this.value = ''; |
|---|
| 1378 | - else if (t == 'checkbox' || t == 'radio') |
|---|
| 1379 | - this.checked = false; |
|---|
| 1380 | - else if (tag == 'select') |
|---|
| 1381 | - this.selectedIndex = -1; |
|---|
| 1382 | - }); |
|---|
| 1383 | + return this.each(function() { |
|---|
| 1384 | + var t = this.type, tag = this.tagName.toLowerCase(); |
|---|
| 1385 | + if (t == 'text' || t == 'password' || tag == 'textarea') { |
|---|
| 1386 | + this.value = ''; |
|---|
| 1387 | + } |
|---|
| 1388 | + else if (t == 'checkbox' || t == 'radio') { |
|---|
| 1389 | + this.checked = false; |
|---|
| 1390 | + } |
|---|
| 1391 | + else if (tag == 'select') { |
|---|
| 1392 | + this.selectedIndex = -1; |
|---|
| 1393 | + } |
|---|
| 1394 | + }); |
|---|
| 1395 | }; |
|---|
| 1396 | |
|---|
| 1397 | - |
|---|
| 1398 | /** |
|---|
| 1399 | * Resets the form data. Causes all form elements to be reset to their original value. |
|---|
| 1400 | - * |
|---|
| 1401 | - * @example $('form').resetForm(); |
|---|
| 1402 | - * @desc Resets all forms on the page. |
|---|
| 1403 | - * |
|---|
| 1404 | - * @name resetForm |
|---|
| 1405 | - * @type jQuery |
|---|
| 1406 | - * @cat Plugins/Form |
|---|
| 1407 | */ |
|---|
| 1408 | $.fn.resetForm = function() { |
|---|
| 1409 | - return this.each(function() { |
|---|
| 1410 | - // guard against an input with the name of 'reset' |
|---|
| 1411 | - // note that IE reports the reset function as an 'object' |
|---|
| 1412 | - if (typeof this.reset == 'function' || (typeof this.reset == 'object' && !this.reset.nodeType)) |
|---|
| 1413 | - this.reset(); |
|---|
| 1414 | - }); |
|---|
| 1415 | + return this.each(function() { |
|---|
| 1416 | + // guard against an input with the name of 'reset' |
|---|
| 1417 | + // note that IE reports the reset function as an 'object' |
|---|
| 1418 | + if (typeof this.reset == 'function' || (typeof this.reset == 'object' && !this.reset.nodeType)) { |
|---|
| 1419 | + this.reset(); |
|---|
| 1420 | + } |
|---|
| 1421 | + }); |
|---|
| 1422 | }; |
|---|
| 1423 | |
|---|
| 1424 | - |
|---|
| 1425 | /** |
|---|
| 1426 | * Enables or disables any matching elements. |
|---|
| 1427 | - * |
|---|
| 1428 | - * @example $(':radio').enabled(false); |
|---|
| 1429 | - * @desc Disables all radio buttons |
|---|
| 1430 | - * |
|---|
| 1431 | - * @name select |
|---|
| 1432 | - * @type jQuery |
|---|
| 1433 | - * @cat Plugins/Form |
|---|
| 1434 | */ |
|---|
| 1435 | -$.fn.enable = function(b) { |
|---|
| 1436 | - if (b == undefined) b = true; |
|---|
| 1437 | - return this.each(function() { |
|---|
| 1438 | - this.disabled = !b |
|---|
| 1439 | - }); |
|---|
| 1440 | +$.fn.enable = function(b) { |
|---|
| 1441 | + if (b === undefined) { |
|---|
| 1442 | + b = true; |
|---|
| 1443 | + } |
|---|
| 1444 | + return this.each(function() { |
|---|
| 1445 | + this.disabled = !b; |
|---|
| 1446 | + }); |
|---|
| 1447 | }; |
|---|
| 1448 | |
|---|
| 1449 | /** |
|---|
| 1450 | * Checks/unchecks any matching checkboxes or radio buttons and |
|---|
| 1451 | * selects/deselects and matching option elements. |
|---|
| 1452 | - * |
|---|
| 1453 | - * @example $(':checkbox').selected(); |
|---|
| 1454 | - * @desc Checks all checkboxes |
|---|
| 1455 | - * |
|---|
| 1456 | - * @name select |
|---|
| 1457 | - * @type jQuery |
|---|
| 1458 | - * @cat Plugins/Form |
|---|
| 1459 | */ |
|---|
| 1460 | -$.fn.select = function(select) { |
|---|
| 1461 | - if (select == undefined) select = true; |
|---|
| 1462 | - return this.each(function() { |
|---|
| 1463 | - var t = this.type; |
|---|
| 1464 | - if (t == 'checkbox' || t == 'radio') |
|---|
| 1465 | - this.checked = select; |
|---|
| 1466 | - else if (this.tagName.toLowerCase() == 'option') { |
|---|
| 1467 | - var $sel = $(this).parent('select'); |
|---|
| 1468 | - if (select && $sel[0] && $sel[0].type == 'select-one') { |
|---|
| 1469 | - // deselect all other options |
|---|
| 1470 | - $sel.find('option').select(false); |
|---|
| 1471 | - } |
|---|
| 1472 | - this.selected = select; |
|---|
| 1473 | - } |
|---|
| 1474 | - }); |
|---|
| 1475 | +$.fn.selected = function(select) { |
|---|
| 1476 | + if (select === undefined) { |
|---|
| 1477 | + select = true; |
|---|
| 1478 | + } |
|---|
| 1479 | + return this.each(function() { |
|---|
| 1480 | + var t = this.type; |
|---|
| 1481 | + if (t == 'checkbox' || t == 'radio') { |
|---|
| 1482 | + this.checked = select; |
|---|
| 1483 | + } |
|---|
| 1484 | + else if (this.tagName.toLowerCase() == 'option') { |
|---|
| 1485 | + var $sel = $(this).parent('select'); |
|---|
| 1486 | + if (select && $sel[0] && $sel[0].type == 'select-one') { |
|---|
| 1487 | + // deselect all other options |
|---|
| 1488 | + $sel.find('option').selected(false); |
|---|
| 1489 | + } |
|---|
| 1490 | + this.selected = select; |
|---|
| 1491 | + } |
|---|
| 1492 | + }); |
|---|
| 1493 | }; |
|---|
| 1494 | |
|---|
| 1495 | +// helper fn for console logging |
|---|
| 1496 | +// set $.fn.ajaxSubmit.debug to true to enable debug logging |
|---|
| 1497 | +function log() { |
|---|
| 1498 | + if ($.fn.ajaxSubmit.debug) { |
|---|
| 1499 | + var msg = '[jquery.form] ' + Array.prototype.join.call(arguments,''); |
|---|
| 1500 | + if (window.console && window.console.log) { |
|---|
| 1501 | + window.console.log(msg); |
|---|
| 1502 | + } |
|---|
| 1503 | + else if (window.opera && window.opera.postError) { |
|---|
| 1504 | + window.opera.postError(msg); |
|---|
| 1505 | + } |
|---|
| 1506 | + } |
|---|
| 1507 | +}; |
|---|
| 1508 | + |
|---|
| 1509 | })(jQuery); |
|---|