001.
002.
003.
004.
005.
006.
007.
008.
009.
010.
011.
;(
function
($) {
012.
013.
014.
015.
016.
017.
018.
019.
020.
021.
022.
023.
024.
025.
026.
027.
028.
029.
030.
031.
032.
033.
034.
035.
036.
037.
038.
039.
040.
041.
042.
043.
044.
045.
046.
$.fn.ajaxSubmit =
function
(options) {
047.
048.
if
(!this.length) {
049.
log(
'ajaxSubmit: skipping submit process - no element selected'
);
050.
return
this;
051.
}
052.
053.
if
(typeof options ==
'function'
) {
054.
options = { success: options };
055.
}
056.
057.
var
url = $.trim(this.attr(
'action'
));
058.
if
(url) {
059.
060.
url = (url.match(/^([^#]+)/)||[])[1];
061.
}
062.
url = url || window.location.href ||
''
;
063.
064.
options = $.extend(true, {
065.
url: url,
066.
type: this.attr(
'method'
) ||
'GET'
,
067.
iframeSrc: /^https/i.test(window.location.href ||
''
) ?
'javascript:false'
:
'about:blank'
068.
}, options);
069.
070.
071.
072.
var
veto = {};
073.
this.trigger(
'form-pre-serialize'
, [this, options, veto]);
074.
if
(veto.veto) {
075.
log(
'ajaxSubmit: submit vetoed via form-pre-serialize trigger'
);
076.
return
this;
077.
}
078.
079.
080.
if
(options.beforeSerialize && options.beforeSerialize(this, options) === false) {
081.
log(
'ajaxSubmit: submit aborted via beforeSerialize callback'
);
082.
return
this;
083.
}
084.
085.
var
n,v,a = this.formToArray(options.semantic);
086.
if
(options.data) {
087.
options.extraData = options.data;
088.
for
(n in options.data) {
089.
if
(options.data[n] instanceof Array) {
090.
for
(
var
k in options.data[n]) {
091.
a.push( { name: n, value: options.data[n][k] } );
092.
}
093.
}
094.
else
{
095.
v = options.data[n];
096.
v = $.isFunction(v) ? v() : v;
097.
a.push( { name: n, value: v } );
098.
}
099.
}
100.
}
101.
102.
103.
if
(options.beforeSubmit && options.beforeSubmit(a, this, options) === false) {
104.
log(
'ajaxSubmit: submit aborted via beforeSubmit callback'
);
105.
return
this;
106.
}
107.
108.
109.
this.trigger(
'form-submit-validate'
, [a, this, options, veto]);
110.
if
(veto.veto) {
111.
log(
'ajaxSubmit: submit vetoed via form-submit-validate trigger'
);
112.
return
this;
113.
}
114.
115.
var
q = $.param(a);
116.
117.
if
(options.type.toUpperCase() ==
'GET'
) {
118.
options.url += (options.url.indexOf(
'?'
) >= 0 ?
'&'
:
'?'
) + q;
119.
options.data = null;
120.
}
121.
else
{
122.
options.data = q;
123.
}
124.
125.
var
$form
= this, callbacks = [];
126.
if
(options.resetForm) {
127.
callbacks.push(
function
() {
$form
.resetForm(); });
128.
}
129.
if
(options.clearForm) {
130.
callbacks.push(
function
() {
$form
.clearForm(); });
131.
}
132.
133.
134.
if
(!options.dataType && options.target) {
135.
var
oldSuccess = options.success ||
function
(){};
136.
callbacks.push(
function
(data) {
137.
var
fn = options.replaceTarget ?
'replaceWith'
:
'html'
;
138.
$(options.target)[fn](data).each(oldSuccess, arguments);
139.
});
140.
}
141.
else
if
(options.success) {
142.
callbacks.push(options.success);
143.
}
144.
145.
options.success =
function
(data, status, xhr) {
146.
var
context = options.context || options;
147.
for
(
var
i=0, max=callbacks.length; i < max; i++) {
148.
callbacks[i].apply(context, [data, status, xhr ||
$form
,
$form
]);
149.
}
150.
};
151.
152.
153.
var
fileInputs = $(
'input:file'
, this).length > 0;
154.
var
mp =
'multipart/form-data'
;
155.
var
multipart = (
$form
.attr(
'enctype'
) == mp ||
$form
.attr(
'encoding'
) == mp);
156.
157.
158.
159.
if
(options.iframe !== false && (fileInputs || options.iframe || multipart)) {
160.
161.
162.
if
(options.closeKeepAlive) {
163.
$.get(options.closeKeepAlive, fileUpload);
164.
}
165.
else
{
166.
fileUpload();
167.
}
168.
}
169.
else
{
170.
$.ajax(options);
171.
}
172.
173.
174.
this.trigger(
'form-submit-notify'
, [this, options]);
175.
return
this;
176.
177.
178.
179.
function
fileUpload() {
180.
var
form =
$form
[0];
181.
182.
if
($(
':input[name=submit],:input[id=submit]'
, form).length) {
183.
184.
185.
alert(
'Error: Form elements must not have name or id of "submit".'
);
186.
return
;
187.
}
188.
189.
var
s = $.extend(true, {}, $.ajaxSettings, options);
190.
s.context = s.context || s;
191.
var
id =
'jqFormIO'
+ (
new
Date
().getTime());
192.
var
$io
= $(
'<iframe id="'
+ id +
'" name="'
+ id +
'" src="'
+ s.iframeSrc +
'" onload="var f = jQuery(this).data(\'form-plugin-onload\'); if (f) f();" />'
);
193.
var
io =
$io
[0];
194.
195.
$io
.css({ position:
'absolute'
, top:
'-1000px'
, left:
'-1000px'
});
196.
197.
var
xhr = {
198.
aborted: 0,
199.
responseText: null,
200.
responseXML: null,
201.
status: 0,
202.
statusText:
'n/a'
,
203.
getAllResponseHeaders:
function
() {},
204.
getResponseHeader:
function
() {},
205.
setRequestHeader:
function
() {},
206.
abort:
function
() {
207.
this.aborted = 1;
208.
$io
.attr(
'src'
, s.iframeSrc);
209.
}
210.
};
211.
212.
var
g = s.
global
;
213.
214.
if
(g && ! $.active++) {
215.
$.event.trigger(
"ajaxStart"
);
216.
}
217.
if
(g) {
218.
$.event.trigger(
"ajaxSend"
, [xhr, s]);
219.
}
220.
221.
if
(s.beforeSend && s.beforeSend.call(s.context, xhr, s) === false) {
222.
if
(s.
global
) {
223.
$.active--;
224.
}
225.
return
;
226.
}
227.
if
(xhr.aborted) {
228.
return
;
229.
}
230.
231.
var
cbInvoked = false;
232.
var
timedOut = 0;
233.
234.
235.
var
sub = form.clk;
236.
if
(sub) {
237.
var
n = sub.name;
238.
if
(n && !sub.disabled) {
239.
s.extraData = s.extraData || {};
240.
s.extraData[n] = sub.value;
241.
if
(sub.type ==
"image"
) {
242.
s.extraData[n+
'.x'
] = form.clk_x;
243.
s.extraData[n+
'.y'
] = form.clk_y;
244.
}
245.
}
246.
}
247.
248.
249.
function
doSubmit() {
250.
251.
var
t =
$form
.attr(
'target'
), a =
$form
.attr(
'action'
);
252.
253.
254.
form.setAttribute(
'target'
,id);
255.
if
(form.getAttribute(
'method'
) !=
'POST'
) {
256.
form.setAttribute(
'method'
,
'POST'
);
257.
}
258.
if
(form.getAttribute(
'action'
) != s.url) {
259.
form.setAttribute(
'action'
, s.url);
260.
}
261.
262.
263.
if
(! s.skipEncodingOverride) {
264.
$form
.attr({
265.
encoding:
'multipart/form-data'
,
266.
enctype:
'multipart/form-data'
267.
});
268.
}
269.
270.
271.
if
(s.timeout) {
272.
setTimeout(
function
() { timedOut = true; cb(); }, s.timeout);
273.
}
274.
275.
276.
var
extraInputs = [];
277.
try {
278.
if
(s.extraData) {
279.
for
(
var
n in s.extraData) {
280.
extraInputs.push(
281.
$(
'<input type="hidden" name="'
+n+
'" value="'
+s.extraData[n]+
'" />'
)
282.
.appendTo(form)[0]);
283.
}
284.
}
285.
286.
287.
$io
.appendTo(
'body'
);
288.
$io
.data(
'form-plugin-onload'
, cb);
289.
form.submit();
290.
}
291.
finally {
292.
293.
form.setAttribute(
'action'
,a);
294.
if
(t) {
295.
form.setAttribute(
'target'
, t);
296.
}
else
{
297.
$form
.removeAttr(
'target'
);
298.
}
299.
$(extraInputs).remove();
300.
}
301.
}
302.
303.
if
(s.forceSync) {
304.
doSubmit();
305.
}
306.
else
{
307.
setTimeout(doSubmit, 10);
308.
}
309.
310.
var
data, doc, domCheckCount = 100;
311.
312.
function
cb() {
313.
if
(cbInvoked) {
314.
return
;
315.
}
316.
317.
$io
.removeData(
'form-plugin-onload'
);
318.
319.
var
ok = true;
320.
try {
321.
if
(timedOut) {
322.
throw
'timeout'
;
323.
}
324.
325.
doc = io.contentWindow ? io.contentWindow.document : io.contentDocument ? io.contentDocument : io.document;
326.
327.
var
isXml = s.dataType ==
'xml'
|| doc.XMLDocument || $.isXMLDoc(doc);
328.
log(
'isXml='
+isXml);
329.
if
(!isXml && (doc.body == null || doc.body.innerHTML ==
''
)) {
330.
if
(--domCheckCount) {
331.
332.
333.
log(
'requeing onLoad callback, DOM not available'
);
334.
setTimeout(cb, 250);
335.
return
;
336.
}
337.
log(
'Could not access iframe DOM after 100 tries.'
);
338.
throw
'DOMException: not available'
;
339.
}
340.
341.
log(
'response detected'
);
342.
cbInvoked = true;
343.
xhr.responseText = doc.documentElement ? doc.documentElement.innerHTML : null;
344.
xhr.responseXML = doc.XMLDocument ? doc.XMLDocument : doc;
345.
xhr.getResponseHeader =
function
(header){
346.
var
headers = {
'content-type'
: s.dataType};
347.
return
headers[header];
348.
};
349.
350.
var
scr = /(json|script)/.test(s.dataType);
351.
if
(scr || s.textarea) {
352.
353.
var
ta = doc.getElementsByTagName(
'textarea'
)[0];
354.
if
(ta) {
355.
xhr.responseText = ta.value;
356.
}
357.
else
if
(scr) {
358.
359.
var
pre = doc.getElementsByTagName(
'pre'
)[0];
360.
if
(pre) {
361.
xhr.responseText = pre.innerHTML;
362.
}
363.
}
364.
}
365.
else
if
(s.dataType ==
'xml'
&& !xhr.responseXML && xhr.responseText != null) {
366.
xhr.responseXML = toXml(xhr.responseText);
367.
}
368.
data = $.httpData(xhr, s.dataType);
369.
}
370.
catch(e){
371.
log(
'error caught:'
,e);
372.
ok = false;
373.
xhr.error = e;
374.
$.handleError(s, xhr,
'error'
, e);
375.
}
376.
377.
378.
if
(ok) {
379.
s.success.call(s.context, data,
'success'
);
380.
if
(g) {
381.
$.event.trigger(
"ajaxSuccess"
, [xhr, s]);
382.
}
383.
}
384.
if
(g) {
385.
$.event.trigger(
"ajaxComplete"
, [xhr, s]);
386.
}
387.
if
(g && ! --$.active) {
388.
$.event.trigger(
"ajaxStop"
);
389.
}
390.
if
(s.complete) {
391.
s.complete.call(s.context, xhr, ok ?
'success'
:
'error'
);
392.
}
393.
394.
395.
setTimeout(
function
() {
396.
$io
.removeData(
'form-plugin-onload'
);
397.
$io
.remove();
398.
xhr.responseXML = null;
399.
}, 100);
400.
}
401.
402.
function
toXml(s, doc) {
403.
if
(window.ActiveXObject) {
404.
doc =
new
ActiveXObject(
'Microsoft.XMLDOM'
);
405.
doc.async =
'false'
;
406.
doc.loadXML(s);
407.
}
408.
else
{
409.
doc = (
new
DOMParser()).parseFromString(s,
'text/xml'
);
410.
}
411.
return
(doc && doc.documentElement && doc.documentElement.tagName !=
'parsererror'
) ? doc : null;
412.
}
413.
}
414.
};
415.
416.
417.
418.
419.
420.
421.
422.
423.
424.
425.
426.
427.
428.
429.
430.
431.
$.fn.ajaxForm =
function
(options) {
432.
433.
if
(this.length === 0) {
434.
var
o = { s: this.selector, c: this.context };
435.
if
(!$.isReady && o.s) {
436.
log(
'DOM not ready, queuing ajaxForm'
);
437.
$(
function
() {
438.
$(o.s,o.c).ajaxForm(options);
439.
});
440.
return
this;
441.
}
442.
443.
log(
'terminating; zero elements found by selector'
+ ($.isReady ?
''
:
' (DOM not ready)'
));
444.
return
this;
445.
}
446.
447.
return
this.ajaxFormUnbind().bind(
'submit.form-plugin'
,
function
(e) {
448.
if
(!e.isDefaultPrevented()) {
449.
e.preventDefault();
450.
$(this).ajaxSubmit(options);
451.
}
452.
}).bind(
'click.form-plugin'
,
function
(e) {
453.
var
target = e.target;
454.
var
$el
= $(target);
455.
if
(!(
$el
.is(
":submit,input:image"
))) {
456.
457.
var
t =
$el
.closest(
':submit'
);
458.
if
(t.length == 0) {
459.
return
;
460.
}
461.
target = t[0];
462.
}
463.
var
form = this;
464.
form.clk = target;
465.
if
(target.type ==
'image'
) {
466.
if
(e.offsetX != undefined) {
467.
form.clk_x = e.offsetX;
468.
form.clk_y = e.offsetY;
469.
}
else
if
(typeof $.fn.offset ==
'function'
) {
470.
var
offset =
$el
.offset();
471.
form.clk_x = e.pageX - offset.left;
472.
form.clk_y = e.pageY - offset.top;
473.
}
else
{
474.
form.clk_x = e.pageX - target.offsetLeft;
475.
form.clk_y = e.pageY - target.offsetTop;
476.
}
477.
}
478.
479.
setTimeout(
function
() { form.clk = form.clk_x = form.clk_y = null; }, 100);
480.
});
481.
};
482.
483.
484.
$.fn.ajaxFormUnbind =
function
() {
485.
return
this.unbind(
'submit.form-plugin click.form-plugin'
);
486.
};
487.
488.
489.
490.
491.
492.
493.
494.
495.
496.
497.
498.
499.
$.fn.formToArray =
function
(semantic) {
500.
var
a = [];
501.
if
(this.length === 0) {
502.
return
a;
503.
}
504.
505.
var
form = this[0];
506.
var
els = semantic ? form.getElementsByTagName(
'*'
) : form.elements;
507.
if
(!els) {
508.
return
a;
509.
}
510.
511.
var
i,j,n,v,el;
512.
for
(i=0, max=els.length; i < max; i++) {
513.
el = els[i];
514.
n = el.name;
515.
if
(!n) {
516.
continue
;
517.
}
518.
519.
if
(semantic && form.clk && el.type ==
"image"
) {
520.
521.
if
(!el.disabled && form.clk == el) {
522.
a.push({name: n, value: $(el).val()});
523.
a.push({name: n+
'.x'
, value: form.clk_x}, {name: n+
'.y'
, value: form.clk_y});
524.
}
525.
continue
;
526.
}
527.
528.
v = $.fieldValue(el, true);
529.
if
(v && v.constructor == Array) {
530.
for
(j=0, jmax=v.length; j < jmax; j++) {
531.
a.push({name: n, value: v[j]});
532.
}
533.
}
534.
else
if
(v !== null && typeof v !=
'undefined'
) {
535.
a.push({name: n, value: v});
536.
}
537.
}
538.
539.
if
(!semantic && form.clk) {
540.
541.
var
$input
= $(form.clk), input =
$input
[0];
542.
n = input.name;
543.
if
(n && !input.disabled && input.type ==
'image'
) {
544.
a.push({name: n, value:
$input
.val()});
545.
a.push({name: n+
'.x'
, value: form.clk_x}, {name: n+
'.y'
, value: form.clk_y});
546.
}
547.
}
548.
return
a;
549.
};
550.
551.
552.
553.
554.
555.
$.fn.formSerialize =
function
(semantic) {
556.
557.
return
$.param(this.formToArray(semantic));
558.
};
559.
560.
561.
562.
563.
564.
$.fn.fieldSerialize =
function
(successful) {
565.
var
a = [];
566.
this.each(
function
() {
567.
var
n = this.name;
568.
if
(!n) {
569.
return
;
570.
}
571.
var
v = $.fieldValue(this, successful);
572.
if
(v && v.constructor == Array) {
573.
for
(
var
i=0,max=v.length; i < max; i++) {
574.
a.push({name: n, value: v[i]});
575.
}
576.
}
577.
else
if
(v !== null && typeof v !=
'undefined'
) {
578.
a.push({name: this.name, value: v});
579.
}
580.
});
581.
582.
return
$.param(a);
583.
};
584.
585.
586.
587.
588.
589.
590.
591.
592.
593.
594.
595.
596.
597.
598.
599.
600.
601.
602.
603.
604.
605.
606.
607.
608.
609.
610.
611.
612.
613.
614.
615.
616.
617.
618.
619.
620.
621.
622.
623.
$.fn.fieldValue =
function
(successful) {
624.
for
(
var
val=[], i=0, max=this.length; i < max; i++) {
625.
var
el = this[i];
626.
var
v = $.fieldValue(el, successful);
627.
if
(v === null || typeof v ==
'undefined'
|| (v.constructor == Array && !v.length)) {
628.
continue
;
629.
}
630.
v.constructor == Array ? $.merge(val, v) : val.push(v);
631.
}
632.
return
val;
633.
};
634.
635.
636.
637.
638.
$.fieldValue =
function
(el, successful) {
639.
var
n = el.name, t = el.type, tag = el.tagName.toLowerCase();
640.
if
(successful === undefined) {
641.
successful = true;
642.
}
643.
644.
if
(successful && (!n || el.disabled || t ==
'reset'
|| t ==
'button'
||
645.
(t ==
'checkbox'
|| t ==
'radio'
) && !el.checked ||
646.
(t ==
'submit'
|| t ==
'image'
) && el.form && el.form.clk != el ||
647.
tag ==
'select'
&& el.selectedIndex == -1)) {
648.
return
null;
649.
}
650.
651.
if
(tag ==
'select'
) {
652.
var
index = el.selectedIndex;
653.
if
(index < 0) {
654.
return
null;
655.
}
656.
var
a = [], ops = el.options;
657.
var
one = (t ==
'select-one'
);
658.
var
max = (one ? index+1 : ops.length);
659.
for
(
var
i=(one ? index : 0); i < max; i++) {
660.
var
op = ops[i];
661.
if
(op.selected) {
662.
var
v = op.value;
663.
if
(!v) {
664.
v = (op.attributes && op.attributes[
'value'
] && !(op.attributes[
'value'
].specified)) ? op.text : op.value;
665.
}
666.
if
(one) {
667.
return
v;
668.
}
669.
a.push(v);
670.
}
671.
}
672.
return
a;
673.
}
674.
return
$(el).val();
675.
};
676.
677.
678.
679.
680.
681.
682.
683.
684.
685.
$.fn.clearForm =
function
() {
686.
return
this.each(
function
() {
687.
$(
'input,select,textarea'
, this).clearFields();
688.
});
689.
};
690.
691.
692.
693.
694.
$.fn.clearFields = $.fn.clearInputs =
function
() {
695.
return
this.each(
function
() {
696.
var
t = this.type, tag = this.tagName.toLowerCase();
697.
if
(t ==
'text'
|| t ==
'password'
|| tag ==
'textarea'
) {
698.
this.value =
''
;
699.
}
700.
else
if
(t ==
'checkbox'
|| t ==
'radio'
) {
701.
this.checked = false;
702.
}
703.
else
if
(tag ==
'select'
) {
704.
this.selectedIndex = -1;
705.
}
706.
});
707.
};
708.
709.
710.
711.
712.
$.fn.resetForm =
function
() {
713.
return
this.each(
function
() {
714.
715.
716.
if
(typeof this.reset ==
'function'
|| (typeof this.reset ==
'object'
&& !this.reset.nodeType)) {
717.
this.reset();
718.
}
719.
});
720.
};
721.
722.
723.
724.
725.
$.fn.enable =
function
(b) {
726.
if
(b === undefined) {
727.
b = true;
728.
}
729.
return
this.each(
function
() {
730.
this.disabled = !b;
731.
});
732.
};
733.
734.
735.
736.
737.
738.
$.fn.selected =
function
(select) {
739.
if
(select === undefined) {
740.
select = true;
741.
}
742.
return
this.each(
function
() {
743.
var
t = this.type;
744.
if
(t ==
'checkbox'
|| t ==
'radio'
) {
745.
this.checked = select;
746.
}
747.
else
if
(this.tagName.toLowerCase() ==
'option'
) {
748.
var
$sel
= $(this).parent(
'select'
);
749.
if
(select &&
$sel
[0] &&
$sel
[0].type ==
'select-one'
) {
750.
751.
$sel
.find(
'option'
).selected(false);
752.
}
753.
this.selected = select;
754.
}
755.
});
756.
};
757.
758.
759.
760.
function
log() {
761.
if
($.fn.ajaxSubmit.debug) {
762.
var
msg =
'[jquery.form] '
+ Array.prototype.join.call(arguments,
''
);
763.
if
(window.console && window.console.log) {
764.
window.console.log(msg);
765.
}
766.
else
if
(window.opera && window.opera.postError) {
767.
window.opera.postError(msg);
768.
}
769.
}
770.
};
771.
772.
})(jQuery);