Révision 224efd63
Ajouté par Jérôme Schneider il y a presque 10 ans
calebasse/static/js/jquery.form.js | ||
---|---|---|
1 | 1 |
/*! |
2 | 2 |
* jQuery Form Plugin |
3 |
* version: 3.18 (28-SEP-2012)
|
|
4 |
* @requires jQuery v1.5 or later
|
|
5 |
* |
|
3 |
* version: 3.50.0-2014.02.05
|
|
4 |
* Requires jQuery v1.5 or later
|
|
5 |
* Copyright (c) 2013 M. Alsup
|
|
6 | 6 |
* Examples and documentation at: http://malsup.com/jquery/form/ |
7 | 7 |
* Project repository: https://github.com/malsup/form |
8 |
* Dual licensed under the MIT and GPL licenses: |
|
9 |
* http://malsup.github.com/mit-license.txt |
|
10 |
* http://malsup.github.com/gpl-license-v2.txt |
|
8 |
* Dual licensed under the MIT and GPL licenses. |
|
9 |
* https://github.com/malsup/form#copyright-and-license |
|
11 | 10 |
*/ |
12 |
/*global ActiveXObject alert */ |
|
13 |
;(function($) { |
|
11 |
/*global ActiveXObject */ |
|
12 |
|
|
13 |
// AMD support |
|
14 |
(function (factory) { |
|
15 |
"use strict"; |
|
16 |
if (typeof define === 'function' && define.amd) { |
|
17 |
// using AMD; register as anon module |
|
18 |
define(['jquery'], factory); |
|
19 |
} else { |
|
20 |
// no AMD; invoke directly |
|
21 |
factory( (typeof(jQuery) != 'undefined') ? jQuery : window.Zepto ); |
|
22 |
} |
|
23 |
} |
|
24 |
|
|
25 |
(function($) { |
|
14 | 26 |
"use strict"; |
15 | 27 |
|
16 | 28 |
/* |
... | ... | |
37 | 49 |
target: '#output' |
38 | 50 |
}); |
39 | 51 |
}); |
40 |
|
|
52 |
|
|
41 | 53 |
You can also use ajaxForm with delegation (requires jQuery v1.7+), so the |
42 | 54 |
form does not have to exist when you invoke ajaxForm: |
43 | 55 |
|
... | ... | |
45 | 57 |
delegation: true, |
46 | 58 |
target: '#output' |
47 | 59 |
}); |
48 |
|
|
60 |
|
|
49 | 61 |
When using ajaxForm, the ajaxSubmit function will be invoked for you |
50 | 62 |
at the appropriate time. |
51 | 63 |
*/ |
... | ... | |
57 | 69 |
feature.fileapi = $("<input type='file'/>").get(0).files !== undefined; |
58 | 70 |
feature.formdata = window.FormData !== undefined; |
59 | 71 |
|
72 |
var hasProp = !!$.fn.prop; |
|
73 |
|
|
74 |
// attr2 uses prop when it can but checks the return type for |
|
75 |
// an expected string. this accounts for the case where a form |
|
76 |
// contains inputs with names like "action" or "method"; in those |
|
77 |
// cases "prop" returns the element |
|
78 |
$.fn.attr2 = function() { |
|
79 |
if ( ! hasProp ) { |
|
80 |
return this.attr.apply(this, arguments); |
|
81 |
} |
|
82 |
var val = this.prop.apply(this, arguments); |
|
83 |
if ( ( val && val.jquery ) || typeof val === 'string' ) { |
|
84 |
return val; |
|
85 |
} |
|
86 |
return this.attr.apply(this, arguments); |
|
87 |
}; |
|
88 |
|
|
60 | 89 |
/** |
61 | 90 |
* ajaxSubmit() provides a mechanism for immediately submitting |
62 | 91 |
* an HTML form using AJAX. |
... | ... | |
69 | 98 |
log('ajaxSubmit: skipping submit process - no element selected'); |
70 | 99 |
return this; |
71 | 100 |
} |
72 |
|
|
101 |
|
|
73 | 102 |
var method, action, url, $form = this; |
74 | 103 |
|
75 | 104 |
if (typeof options == 'function') { |
76 | 105 |
options = { success: options }; |
77 | 106 |
} |
107 |
else if ( options === undefined ) { |
|
108 |
options = {}; |
|
109 |
} |
|
110 |
|
|
111 |
method = options.type || this.attr2('method'); |
|
112 |
action = options.url || this.attr2('action'); |
|
78 | 113 |
|
79 |
method = this.attr('method'); |
|
80 |
action = this.attr('action'); |
|
81 | 114 |
url = (typeof action === 'string') ? $.trim(action) : ''; |
82 | 115 |
url = url || window.location.href || ''; |
83 | 116 |
if (url) { |
... | ... | |
88 | 121 |
options = $.extend(true, { |
89 | 122 |
url: url, |
90 | 123 |
success: $.ajaxSettings.success, |
91 |
type: method || 'GET',
|
|
124 |
type: method || $.ajaxSettings.type,
|
|
92 | 125 |
iframeSrc: /^https/i.test(window.location.href || '') ? 'javascript:false' : 'about:blank' |
93 | 126 |
}, options); |
94 | 127 |
|
... | ... | |
111 | 144 |
if ( traditional === undefined ) { |
112 | 145 |
traditional = $.ajaxSettings.traditional; |
113 | 146 |
} |
114 |
|
|
147 |
|
|
115 | 148 |
var elements = []; |
116 | 149 |
var qx, a = this.formToArray(options.semantic, elements); |
117 | 150 |
if (options.data) { |
... | ... | |
135 | 168 |
var q = $.param(a, traditional); |
136 | 169 |
if (qx) { |
137 | 170 |
q = ( q ? (q + '&' + qx) : qx ); |
138 |
}
|
|
171 |
} |
|
139 | 172 |
if (options.type.toUpperCase() == 'GET') { |
140 | 173 |
options.url += (options.url.indexOf('?') >= 0 ? '&' : '?') + q; |
141 | 174 |
options.data = null; // data is null for 'get' |
... | ... | |
165 | 198 |
} |
166 | 199 |
|
167 | 200 |
options.success = function(data, status, xhr) { // jQuery 1.4+ passes xhr as 3rd arg |
168 |
var context = options.context || this ; // jQuery 1.4+ supports scope context
|
|
201 |
var context = options.context || this ; // jQuery 1.4+ supports scope context |
|
169 | 202 |
for (var i=0, max=callbacks.length; i < max; i++) { |
170 | 203 |
callbacks[i].apply(context, [data, status, xhr || $form, $form]); |
171 | 204 |
} |
172 | 205 |
}; |
173 | 206 |
|
207 |
if (options.error) { |
|
208 |
var oldError = options.error; |
|
209 |
options.error = function(xhr, status, error) { |
|
210 |
var context = options.context || this; |
|
211 |
oldError.apply(context, [xhr, status, error, $form]); |
|
212 |
}; |
|
213 |
} |
|
214 |
|
|
215 |
if (options.complete) { |
|
216 |
var oldComplete = options.complete; |
|
217 |
options.complete = function(xhr, status) { |
|
218 |
var context = options.context || this; |
|
219 |
oldComplete.apply(context, [xhr, status, $form]); |
|
220 |
}; |
|
221 |
} |
|
222 |
|
|
174 | 223 |
// are there files to upload? |
175 |
var fileInputs = $('input:file:enabled[value]', this); // [value] (issue #113) |
|
224 |
|
|
225 |
// [value] (issue #113), also see comment: |
|
226 |
// https://github.com/malsup/form/commit/588306aedba1de01388032d5f42a60159eea9228#commitcomment-2180219 |
|
227 |
var fileInputs = $('input[type=file]:enabled', this).filter(function() { return $(this).val() !== ''; }); |
|
228 |
|
|
176 | 229 |
var hasFileInputs = fileInputs.length > 0; |
177 | 230 |
var mp = 'multipart/form-data'; |
178 | 231 |
var multipart = ($form.attr('enctype') == mp || $form.attr('encoding') == mp); |
... | ... | |
207 | 260 |
$form.removeData('jqxhr').data('jqxhr', jqxhr); |
208 | 261 |
|
209 | 262 |
// clear element array |
210 |
for (var k=0; k < elements.length; k++) |
|
263 |
for (var k=0; k < elements.length; k++) {
|
|
211 | 264 |
elements[k] = null; |
265 |
} |
|
212 | 266 |
|
213 | 267 |
// fire 'notify' event |
214 | 268 |
this.trigger('form-submit-notify', [this, options]); |
... | ... | |
216 | 270 |
|
217 | 271 |
// utility fn for deep serialization |
218 | 272 |
function deepSerialize(extraData){ |
219 |
var serialized = $.param(extraData).split('&'); |
|
273 |
var serialized = $.param(extraData, options.traditional).split('&');
|
|
220 | 274 |
var len = serialized.length; |
221 |
var result = {};
|
|
275 |
var result = [];
|
|
222 | 276 |
var i, part; |
223 | 277 |
for (i=0; i < len; i++) { |
278 |
// #252; undo param space replacement |
|
279 |
serialized[i] = serialized[i].replace(/\+/g,' '); |
|
224 | 280 |
part = serialized[i].split('='); |
225 |
result[decodeURIComponent(part[0])] = decodeURIComponent(part[1]); |
|
281 |
// #278; use array instead of object storage, favoring array serializations |
|
282 |
result.push([decodeURIComponent(part[0]), decodeURIComponent(part[1])]); |
|
226 | 283 |
} |
227 | 284 |
return result; |
228 | 285 |
} |
... | ... | |
237 | 294 |
|
238 | 295 |
if (options.extraData) { |
239 | 296 |
var serializedData = deepSerialize(options.extraData); |
240 |
for (var p in serializedData) |
|
241 |
if (serializedData.hasOwnProperty(p)) |
|
242 |
formdata.append(p, serializedData[p]); |
|
297 |
for (i=0; i < serializedData.length; i++) { |
|
298 |
if (serializedData[i]) { |
|
299 |
formdata.append(serializedData[i][0], serializedData[i][1]); |
|
300 |
} |
|
301 |
} |
|
243 | 302 |
} |
244 | 303 |
|
245 | 304 |
options.data = null; |
... | ... | |
250 | 309 |
cache: false, |
251 | 310 |
type: method || 'POST' |
252 | 311 |
}); |
253 |
|
|
312 |
|
|
254 | 313 |
if (options.uploadProgress) { |
255 | 314 |
// workaround because jqXHR does not expose upload property |
256 | 315 |
s.xhr = function() { |
257 |
var xhr = jQuery.ajaxSettings.xhr();
|
|
316 |
var xhr = $.ajaxSettings.xhr();
|
|
258 | 317 |
if (xhr.upload) { |
259 |
xhr.upload.onprogress = function(event) {
|
|
318 |
xhr.upload.addEventListener('progress', function(event) {
|
|
260 | 319 |
var percent = 0; |
261 | 320 |
var position = event.loaded || event.position; /*event.position is deprecated*/ |
262 | 321 |
var total = event.total; |
... | ... | |
264 | 323 |
percent = Math.ceil(position / total * 100); |
265 | 324 |
} |
266 | 325 |
options.uploadProgress(event, position, total, percent); |
267 |
}; |
|
326 |
}, false);
|
|
268 | 327 |
} |
269 | 328 |
return xhr; |
270 | 329 |
}; |
271 | 330 |
} |
272 | 331 |
|
273 | 332 |
s.data = null; |
274 |
var beforeSend = s.beforeSend; |
|
275 |
s.beforeSend = function(xhr, o) { |
|
333 |
var beforeSend = s.beforeSend; |
|
334 |
s.beforeSend = function(xhr, o) { |
|
335 |
//Send FormData() provided by user |
|
336 |
if (options.formData) { |
|
337 |
o.data = options.formData; |
|
338 |
} |
|
339 |
else { |
|
276 | 340 |
o.data = formdata; |
277 |
if(beforeSend) |
|
278 |
beforeSend.call(this, xhr, o); |
|
341 |
} |
|
342 |
if(beforeSend) { |
|
343 |
beforeSend.call(this, xhr, o); |
|
344 |
} |
|
279 | 345 |
}; |
280 | 346 |
return $.ajax(s); |
281 | 347 |
} |
... | ... | |
283 | 349 |
// private function for handling file uploads (hat tip to YAHOO!) |
284 | 350 |
function fileUploadIframe(a) { |
285 | 351 |
var form = $form[0], el, i, s, g, id, $io, io, xhr, sub, n, timedOut, timeoutHandle; |
286 |
var useProp = !!$.fn.prop; |
|
287 | 352 |
var deferred = $.Deferred(); |
288 | 353 |
|
289 |
if ($(':input[name=submit],:input[id=submit]', form).length) { |
|
290 |
// if there is an input with a name or id of 'submit' then we won't be |
|
291 |
// able to invoke the submit fn on the form (at least not x-browser) |
|
292 |
alert('Error: Form elements must not have name or id of "submit".'); |
|
293 |
deferred.reject(); |
|
294 |
return deferred; |
|
295 |
} |
|
296 |
|
|
354 |
// #341 |
|
355 |
deferred.abort = function(status) { |
|
356 |
xhr.abort(status); |
|
357 |
}; |
|
358 |
|
|
297 | 359 |
if (a) { |
298 | 360 |
// ensure that every serialized input is still enabled |
299 | 361 |
for (i=0; i < elements.length; i++) { |
300 | 362 |
el = $(elements[i]); |
301 |
if ( useProp )
|
|
363 |
if ( hasProp ) {
|
|
302 | 364 |
el.prop('disabled', false); |
303 |
else |
|
365 |
} |
|
366 |
else { |
|
304 | 367 |
el.removeAttr('disabled'); |
368 |
} |
|
305 | 369 |
} |
306 | 370 |
} |
307 | 371 |
|
... | ... | |
310 | 374 |
id = 'jqFormIO' + (new Date().getTime()); |
311 | 375 |
if (s.iframeTarget) { |
312 | 376 |
$io = $(s.iframeTarget); |
313 |
n = $io.attr('name'); |
|
314 |
if (!n) |
|
315 |
$io.attr('name', id); |
|
316 |
else |
|
377 |
n = $io.attr2('name'); |
|
378 |
if (!n) { |
|
379 |
$io.attr2('name', id); |
|
380 |
} |
|
381 |
else { |
|
317 | 382 |
id = n; |
383 |
} |
|
318 | 384 |
} |
319 | 385 |
else { |
320 | 386 |
$io = $('<iframe name="' + id + '" src="'+ s.iframeSrc +'" />'); |
... | ... | |
336 | 402 |
var e = (status === 'timeout' ? 'timeout' : 'aborted'); |
337 | 403 |
log('aborting upload... ' + e); |
338 | 404 |
this.aborted = 1; |
339 |
// #214 |
|
340 |
if (io.contentWindow.document.execCommand) {
|
|
341 |
try { // #214
|
|
405 |
|
|
406 |
try { // #214, #257
|
|
407 |
if (io.contentWindow.document.execCommand) {
|
|
342 | 408 |
io.contentWindow.document.execCommand('Stop'); |
343 |
} catch(ignore) {}
|
|
409 |
} |
|
344 | 410 |
} |
411 |
catch(ignore) {} |
|
412 |
|
|
345 | 413 |
$io.attr('src', s.iframeSrc); // abort op in progress |
346 | 414 |
xhr.error = e; |
347 |
if (s.error) |
|
415 |
if (s.error) {
|
|
348 | 416 |
s.error.call(s.context, xhr, e, status); |
349 |
if (g) |
|
417 |
} |
|
418 |
if (g) { |
|
350 | 419 |
$.event.trigger("ajaxError", [xhr, s, e]); |
351 |
if (s.complete) |
|
420 |
} |
|
421 |
if (s.complete) { |
|
352 | 422 |
s.complete.call(s.context, xhr, e); |
423 |
} |
|
353 | 424 |
} |
354 | 425 |
}; |
355 | 426 |
|
... | ... | |
387 | 458 |
} |
388 | 459 |
} |
389 | 460 |
} |
390 |
|
|
461 |
|
|
391 | 462 |
var CLIENT_TIMEOUT_ABORT = 1; |
392 | 463 |
var SERVER_ABORT = 2; |
393 |
|
|
464 |
|
|
394 | 465 |
function getDoc(frame) { |
395 |
var doc = frame.contentWindow ? frame.contentWindow.document : frame.contentDocument ? frame.contentDocument : frame.document; |
|
466 |
/* it looks like contentWindow or contentDocument do not |
|
467 |
* carry the protocol property in ie8, when running under ssl |
|
468 |
* frame.document is the only valid response document, since |
|
469 |
* the protocol is know but not on the other two objects. strange? |
|
470 |
* "Same origin policy" http://en.wikipedia.org/wiki/Same_origin_policy |
|
471 |
*/ |
|
472 |
|
|
473 |
var doc = null; |
|
474 |
|
|
475 |
// IE8 cascading access check |
|
476 |
try { |
|
477 |
if (frame.contentWindow) { |
|
478 |
doc = frame.contentWindow.document; |
|
479 |
} |
|
480 |
} catch(err) { |
|
481 |
// IE8 access denied under ssl & missing protocol |
|
482 |
log('cannot get iframe.contentWindow document: ' + err); |
|
483 |
} |
|
484 |
|
|
485 |
if (doc) { // successful getting content |
|
486 |
return doc; |
|
487 |
} |
|
488 |
|
|
489 |
try { // simply checking may throw in ie8 under ssl or mismatched protocol |
|
490 |
doc = frame.contentDocument ? frame.contentDocument : frame.document; |
|
491 |
} catch(err) { |
|
492 |
// last attempt |
|
493 |
log('cannot get iframe.contentDocument: ' + err); |
|
494 |
doc = frame.document; |
|
495 |
} |
|
396 | 496 |
return doc; |
397 | 497 |
} |
398 |
|
|
498 |
|
|
399 | 499 |
// Rails CSRF hack (thanks to Yvan Barthelemy) |
400 | 500 |
var csrf_token = $('meta[name=csrf-token]').attr('content'); |
401 | 501 |
var csrf_param = $('meta[name=csrf-param]').attr('content'); |
... | ... | |
407 | 507 |
// take a breath so that pending repaints get some cpu time before the upload starts |
408 | 508 |
function doSubmit() { |
409 | 509 |
// make sure form attrs are set |
410 |
var t = $form.attr('target'), a = $form.attr('action'); |
|
510 |
var t = $form.attr2('target'), |
|
511 |
a = $form.attr2('action'), |
|
512 |
mp = 'multipart/form-data', |
|
513 |
et = $form.attr('enctype') || $form.attr('encoding') || mp; |
|
411 | 514 |
|
412 | 515 |
// update form attrs in IE friendly way |
413 | 516 |
form.setAttribute('target',id); |
414 |
if (!method) { |
|
517 |
if (!method || /post/i.test(method) ) {
|
|
415 | 518 |
form.setAttribute('method', 'POST'); |
416 | 519 |
} |
417 | 520 |
if (a != s.url) { |
... | ... | |
430 | 533 |
if (s.timeout) { |
431 | 534 |
timeoutHandle = setTimeout(function() { timedOut = true; cb(CLIENT_TIMEOUT_ABORT); }, s.timeout); |
432 | 535 |
} |
433 |
|
|
536 |
|
|
434 | 537 |
// look for server aborts |
435 | 538 |
function checkState() { |
436 | 539 |
try { |
437 | 540 |
var state = getDoc(io).readyState; |
438 | 541 |
log('state = ' + state); |
439 |
if (state && state.toLowerCase() == 'uninitialized') |
|
542 |
if (state && state.toLowerCase() == 'uninitialized') {
|
|
440 | 543 |
setTimeout(checkState,50); |
544 |
} |
|
441 | 545 |
} |
442 | 546 |
catch(e) { |
443 | 547 |
log('Server abort: ' , e, ' (', e.name, ')'); |
444 | 548 |
cb(SERVER_ABORT); |
445 |
if (timeoutHandle) |
|
549 |
if (timeoutHandle) {
|
|
446 | 550 |
clearTimeout(timeoutHandle); |
551 |
} |
|
447 | 552 |
timeoutHandle = undefined; |
448 | 553 |
} |
449 | 554 |
} |
... | ... | |
457 | 562 |
// if using the $.param format that allows for multiple values with the same name |
458 | 563 |
if($.isPlainObject(s.extraData[n]) && s.extraData[n].hasOwnProperty('name') && s.extraData[n].hasOwnProperty('value')) { |
459 | 564 |
extraInputs.push( |
460 |
$('<input type="hidden" name="'+s.extraData[n].name+'">').attr('value',s.extraData[n].value)
|
|
565 |
$('<input type="hidden" name="'+s.extraData[n].name+'">').val(s.extraData[n].value)
|
|
461 | 566 |
.appendTo(form)[0]); |
462 | 567 |
} else { |
463 | 568 |
extraInputs.push( |
464 |
$('<input type="hidden" name="'+n+'">').attr('value',s.extraData[n])
|
|
569 |
$('<input type="hidden" name="'+n+'">').val(s.extraData[n])
|
|
465 | 570 |
.appendTo(form)[0]); |
466 | 571 |
} |
467 | 572 |
} |
... | ... | |
471 | 576 |
if (!s.iframeTarget) { |
472 | 577 |
// add iframe to doc and submit the form |
473 | 578 |
$io.appendTo('body'); |
474 |
if (io.attachEvent) |
|
475 |
io.attachEvent('onload', cb); |
|
476 |
else |
|
477 |
io.addEventListener('load', cb, false); |
|
579 |
} |
|
580 |
if (io.attachEvent) { |
|
581 |
io.attachEvent('onload', cb); |
|
582 |
} |
|
583 |
else { |
|
584 |
io.addEventListener('load', cb, false); |
|
478 | 585 |
} |
479 | 586 |
setTimeout(checkState,15); |
480 |
form.submit(); |
|
587 |
|
|
588 |
try { |
|
589 |
form.submit(); |
|
590 |
} catch(err) { |
|
591 |
// just in case form has element with name/id of 'submit' |
|
592 |
var submitFn = document.createElement('form').submit; |
|
593 |
submitFn.apply(form); |
|
594 |
} |
|
481 | 595 |
} |
482 | 596 |
finally { |
483 | 597 |
// reset attrs and remove "extra" input elements |
484 | 598 |
form.setAttribute('action',a); |
599 |
form.setAttribute('enctype', et); // #380 |
|
485 | 600 |
if(t) { |
486 | 601 |
form.setAttribute('target', t); |
487 | 602 |
} else { |
... | ... | |
504 | 619 |
if (xhr.aborted || callbackProcessed) { |
505 | 620 |
return; |
506 | 621 |
} |
507 |
try { |
|
508 |
doc = getDoc(io); |
|
509 |
} |
|
510 |
catch(ex) { |
|
511 |
log('cannot access response document: ', ex); |
|
622 |
|
|
623 |
doc = getDoc(io); |
|
624 |
if(!doc) { |
|
625 |
log('cannot access response document'); |
|
512 | 626 |
e = SERVER_ABORT; |
513 | 627 |
} |
514 | 628 |
if (e === CLIENT_TIMEOUT_ABORT && xhr) { |
... | ... | |
524 | 638 |
|
525 | 639 |
if (!doc || doc.location.href == s.iframeSrc) { |
526 | 640 |
// response not received yet |
527 |
if (!timedOut) |
|
641 |
if (!timedOut) {
|
|
528 | 642 |
return; |
643 |
} |
|
529 | 644 |
} |
530 |
if (io.detachEvent) |
|
645 |
if (io.detachEvent) {
|
|
531 | 646 |
io.detachEvent('onload', cb); |
532 |
else |
|
647 |
} |
|
648 |
else { |
|
533 | 649 |
io.removeEventListener('load', cb, false); |
650 |
} |
|
534 | 651 |
|
535 | 652 |
var status = 'success', errMsg; |
536 | 653 |
try { |
... | ... | |
557 | 674 |
var docRoot = doc.body ? doc.body : doc.documentElement; |
558 | 675 |
xhr.responseText = docRoot ? docRoot.innerHTML : null; |
559 | 676 |
xhr.responseXML = doc.XMLDocument ? doc.XMLDocument : doc; |
560 |
if (isXml) |
|
677 |
if (isXml) {
|
|
561 | 678 |
s.dataType = 'xml'; |
679 |
} |
|
562 | 680 |
xhr.getResponseHeader = function(header){ |
563 | 681 |
var headers = {'content-type': s.dataType}; |
564 |
return headers[header]; |
|
682 |
return headers[header.toLowerCase()];
|
|
565 | 683 |
}; |
566 | 684 |
// support for XHR 'status' & 'statusText' emulation : |
567 | 685 |
if (docRoot) { |
... | ... | |
599 | 717 |
try { |
600 | 718 |
data = httpData(xhr, dt, s); |
601 | 719 |
} |
602 |
catch (e) { |
|
720 |
catch (err) {
|
|
603 | 721 |
status = 'parsererror'; |
604 |
xhr.error = errMsg = (e || status); |
|
722 |
xhr.error = errMsg = (err || status);
|
|
605 | 723 |
} |
606 | 724 |
} |
607 |
catch (e) { |
|
608 |
log('error caught: ',e); |
|
725 |
catch (err) {
|
|
726 |
log('error caught: ',err);
|
|
609 | 727 |
status = 'error'; |
610 |
xhr.error = errMsg = (e || status); |
|
728 |
xhr.error = errMsg = (err || status);
|
|
611 | 729 |
} |
612 | 730 |
|
613 | 731 |
if (xhr.aborted) { |
... | ... | |
621 | 739 |
|
622 | 740 |
// ordering of these callbacks/triggers is odd, but that's how $.ajax does it |
623 | 741 |
if (status === 'success') { |
624 |
if (s.success) |
|
742 |
if (s.success) {
|
|
625 | 743 |
s.success.call(s.context, data, 'success', xhr); |
744 |
} |
|
626 | 745 |
deferred.resolve(xhr.responseText, 'success', xhr); |
627 |
if (g) |
|
746 |
if (g) {
|
|
628 | 747 |
$.event.trigger("ajaxSuccess", [xhr, s]); |
748 |
} |
|
629 | 749 |
} |
630 | 750 |
else if (status) { |
631 |
if (errMsg === undefined) |
|
751 |
if (errMsg === undefined) {
|
|
632 | 752 |
errMsg = xhr.statusText; |
633 |
if (s.error) |
|
753 |
} |
|
754 |
if (s.error) { |
|
634 | 755 |
s.error.call(s.context, xhr, status, errMsg); |
756 |
} |
|
635 | 757 |
deferred.reject(xhr, 'error', errMsg); |
636 |
if (g) |
|
758 |
if (g) {
|
|
637 | 759 |
$.event.trigger("ajaxError", [xhr, s, errMsg]); |
760 |
} |
|
638 | 761 |
} |
639 | 762 |
|
640 |
if (g) |
|
763 |
if (g) {
|
|
641 | 764 |
$.event.trigger("ajaxComplete", [xhr, s]); |
765 |
} |
|
642 | 766 |
|
643 | 767 |
if (g && ! --$.active) { |
644 | 768 |
$.event.trigger("ajaxStop"); |
645 | 769 |
} |
646 | 770 |
|
647 |
if (s.complete) |
|
771 |
if (s.complete) {
|
|
648 | 772 |
s.complete.call(s.context, xhr, status); |
773 |
} |
|
649 | 774 |
|
650 | 775 |
callbackProcessed = true; |
651 |
if (s.timeout) |
|
776 |
if (s.timeout) {
|
|
652 | 777 |
clearTimeout(timeoutHandle); |
778 |
} |
|
653 | 779 |
|
654 | 780 |
// clean up |
655 | 781 |
setTimeout(function() { |
656 |
if (!s.iframeTarget) |
|
782 |
if (!s.iframeTarget) {
|
|
657 | 783 |
$io.remove(); |
784 |
} |
|
785 |
else { //adding else to clean up existing iframe response. |
|
786 |
$io.attr('src', s.iframeSrc); |
|
787 |
} |
|
658 | 788 |
xhr.responseXML = null; |
659 | 789 |
}, 100); |
660 | 790 |
} |
... | ... | |
682 | 812 |
data = xml ? xhr.responseXML : xhr.responseText; |
683 | 813 |
|
684 | 814 |
if (xml && data.documentElement.nodeName === 'parsererror') { |
685 |
if ($.error) |
|
815 |
if ($.error) {
|
|
686 | 816 |
$.error('parsererror'); |
817 |
} |
|
687 | 818 |
} |
688 | 819 |
if (s && s.dataFilter) { |
689 | 820 |
data = s.dataFilter(data, type); |
... | ... | |
720 | 851 |
$.fn.ajaxForm = function(options) { |
721 | 852 |
options = options || {}; |
722 | 853 |
options.delegation = options.delegation && $.isFunction($.fn.on); |
723 |
|
|
854 |
|
|
724 | 855 |
// in jQuery 1.3+ we can fix mistakes with the ready state |
725 | 856 |
if (!options.delegation && this.length === 0) { |
726 | 857 |
var o = { s: this.selector, c: this.context }; |
... | ... | |
750 | 881 |
.bind('click.form-plugin', options, captureSubmittingElement); |
751 | 882 |
}; |
752 | 883 |
|
753 |
// private event handlers
|
|
884 |
// private event handlers |
|
754 | 885 |
function doAjaxSubmit(e) { |
755 | 886 |
/*jshint validthis:true */ |
756 | 887 |
var options = e.data; |
757 | 888 |
if (!e.isDefaultPrevented()) { // if event has been canceled, don't proceed |
758 | 889 |
e.preventDefault(); |
759 |
$(this).ajaxSubmit(options);
|
|
890 |
$(e.target).ajaxSubmit(options); // #365
|
|
760 | 891 |
} |
761 | 892 |
} |
762 |
|
|
893 |
|
|
763 | 894 |
function captureSubmittingElement(e) { |
764 | 895 |
/*jshint validthis:true */ |
765 | 896 |
var target = e.target; |
766 | 897 |
var $el = $(target); |
767 |
if (!($el.is(":submit,input:image"))) {
|
|
898 |
if (!($el.is("[type=submit],[type=image]"))) {
|
|
768 | 899 |
// is this a child element of the submit el? (ex: a span within a button) |
769 |
var t = $el.closest(':submit');
|
|
900 |
var t = $el.closest('[type=submit]');
|
|
770 | 901 |
if (t.length === 0) { |
771 | 902 |
return; |
772 | 903 |
} |
... | ... | |
815 | 946 |
} |
816 | 947 |
|
817 | 948 |
var form = this[0]; |
949 |
var formId = this.attr('id'); |
|
818 | 950 |
var els = semantic ? form.getElementsByTagName('*') : form.elements; |
819 |
if (!els) { |
|
951 |
var els2; |
|
952 |
|
|
953 |
if (els && !/MSIE [678]/.test(navigator.userAgent)) { // #390 |
|
954 |
els = $(els).get(); // convert to standard array |
|
955 |
} |
|
956 |
|
|
957 |
// #386; account for inputs outside the form which use the 'form' attribute |
|
958 |
if ( formId ) { |
|
959 |
els2 = $(':input[form=' + formId + ']').get(); |
|
960 |
if ( els2.length ) { |
|
961 |
els = (els || []).concat(els2); |
|
962 |
} |
|
963 |
} |
|
964 |
|
|
965 |
if (!els || !els.length) { |
|
820 | 966 |
return a; |
821 | 967 |
} |
822 | 968 |
|
... | ... | |
824 | 970 |
for(i=0, max=els.length; i < max; i++) { |
825 | 971 |
el = els[i]; |
826 | 972 |
n = el.name; |
827 |
if (!n) { |
|
973 |
if (!n || el.disabled) {
|
|
828 | 974 |
continue; |
829 | 975 |
} |
830 | 976 |
|
831 | 977 |
if (semantic && form.clk && el.type == "image") { |
832 | 978 |
// handle image inputs on the fly when semantic == true |
833 |
if(!el.disabled && form.clk == el) {
|
|
979 |
if(form.clk == el) { |
|
834 | 980 |
a.push({name: n, value: $(el).val(), type: el.type }); |
835 | 981 |
a.push({name: n+'.x', value: form.clk_x}, {name: n+'.y', value: form.clk_y}); |
836 | 982 |
} |
... | ... | |
839 | 985 |
|
840 | 986 |
v = $.fieldValue(el, true); |
841 | 987 |
if (v && v.constructor == Array) { |
842 |
if (elements) |
|
988 |
if (elements) {
|
|
843 | 989 |
elements.push(el); |
990 |
} |
|
844 | 991 |
for(j=0, jmax=v.length; j < jmax; j++) { |
845 | 992 |
a.push({name: n, value: v[j]}); |
846 | 993 |
} |
847 | 994 |
} |
848 |
else if (feature.fileapi && el.type == 'file' && !el.disabled) {
|
|
849 |
if (elements) |
|
995 |
else if (feature.fileapi && el.type == 'file') { |
|
996 |
if (elements) {
|
|
850 | 997 |
elements.push(el); |
998 |
} |
|
851 | 999 |
var files = el.files; |
852 | 1000 |
if (files.length) { |
853 | 1001 |
for (j=0; j < files.length; j++) { |
... | ... | |
860 | 1008 |
} |
861 | 1009 |
} |
862 | 1010 |
else if (v !== null && typeof v != 'undefined') { |
863 |
if (elements) |
|
1011 |
if (elements) {
|
|
864 | 1012 |
elements.push(el); |
1013 |
} |
|
865 | 1014 |
a.push({name: n, value: v, type: el.type, required: el.required}); |
866 | 1015 |
} |
867 | 1016 |
} |
... | ... | |
924 | 1073 |
* <input name="C" type="radio" value="C2" /> |
925 | 1074 |
* </fieldset></form> |
926 | 1075 |
* |
927 |
* var v = $(':text').fieldValue();
|
|
1076 |
* var v = $('input[type=text]').fieldValue();
|
|
928 | 1077 |
* // if no values are entered into the text inputs |
929 | 1078 |
* v == ['',''] |
930 | 1079 |
* // if values entered into the text inputs are 'foo' and 'bar' |
931 | 1080 |
* v == ['foo','bar'] |
932 | 1081 |
* |
933 |
* var v = $(':checkbox').fieldValue();
|
|
1082 |
* var v = $('input[type=checkbox]').fieldValue();
|
|
934 | 1083 |
* // if neither checkbox is checked |
935 | 1084 |
* v === undefined |
936 | 1085 |
* // if both checkboxes are checked |
937 | 1086 |
* v == ['B1', 'B2'] |
938 | 1087 |
* |
939 |
* var v = $(':radio').fieldValue();
|
|
1088 |
* var v = $('input[type=radio]').fieldValue();
|
|
940 | 1089 |
* // if neither radio is checked |
941 | 1090 |
* v === undefined |
942 | 1091 |
* // if first radio is checked |
... | ... | |
957 | 1106 |
if (v === null || typeof v == 'undefined' || (v.constructor == Array && !v.length)) { |
958 | 1107 |
continue; |
959 | 1108 |
} |
960 |
if (v.constructor == Array) |
|
1109 |
if (v.constructor == Array) {
|
|
961 | 1110 |
$.merge(val, v); |
962 |
else |
|
1111 |
} |
|
1112 |
else { |
|
963 | 1113 |
val.push(v); |
1114 |
} |
|
964 | 1115 |
} |
965 | 1116 |
return val; |
966 | 1117 |
}; |
... | ... | |
994 | 1145 |
if (op.selected) { |
995 | 1146 |
var v = op.value; |
996 | 1147 |
if (!v) { // extra pain for IE... |
997 |
v = (op.attributes && op.attributes['value'] && !(op.attributes['value'].specified)) ? op.text : op.value;
|
|
1148 |
v = (op.attributes && op.attributes.value && !(op.attributes.value.specified)) ? op.text : op.value;
|
|
998 | 1149 |
} |
999 | 1150 |
if (one) { |
1000 | 1151 |
return v; |
... | ... | |
1037 | 1188 |
else if (tag == 'select') { |
1038 | 1189 |
this.selectedIndex = -1; |
1039 | 1190 |
} |
1191 |
else if (t == "file") { |
|
1192 |
if (/MSIE/.test(navigator.userAgent)) { |
|
1193 |
$(this).replaceWith($(this).clone(true)); |
|
1194 |
} else { |
|
1195 |
$(this).val(''); |
|
1196 |
} |
|
1197 |
} |
|
1040 | 1198 |
else if (includeHidden) { |
1041 | 1199 |
// includeHidden can be the value true, or it can be a selector string |
1042 | 1200 |
// indicating a special test; for example: |
1043 | 1201 |
// $('#myForm').clearForm('.special:hidden') |
1044 | 1202 |
// the above would clean hidden inputs that have the class of 'special' |
1045 | 1203 |
if ( (includeHidden === true && /hidden/.test(t)) || |
1046 |
(typeof includeHidden == 'string' && $(this).is(includeHidden)) ) |
|
1204 |
(typeof includeHidden == 'string' && $(this).is(includeHidden)) ) {
|
|
1047 | 1205 |
this.value = ''; |
1206 |
} |
|
1048 | 1207 |
} |
1049 | 1208 |
}); |
1050 | 1209 |
}; |
... | ... | |
1103 | 1262 |
|
1104 | 1263 |
// helper fn for console logging |
1105 | 1264 |
function log() { |
1106 |
if (!$.fn.ajaxSubmit.debug) |
|
1265 |
if (!$.fn.ajaxSubmit.debug) {
|
|
1107 | 1266 |
return; |
1267 |
} |
|
1108 | 1268 |
var msg = '[jquery.form] ' + Array.prototype.join.call(arguments,''); |
1109 | 1269 |
if (window.console && window.console.log) { |
1110 | 1270 |
window.console.log(msg); |
... | ... | |
1114 | 1274 |
} |
1115 | 1275 |
} |
1116 | 1276 |
|
1117 |
})(jQuery); |
|
1277 |
})); |
|
1278 |
|
Formats disponibles : Unified diff
calebasse/static/js/jquery.form.js: update to version 3.18