83198c338a
git-svn-id: https://svn.code.sf.net/p/xcat/code/xcat-core/trunk@8655 8638fb3e-16cb-4fca-ae20-7b5d299a9bcd
29 lines
9.0 KiB
JavaScript
29 lines
9.0 KiB
JavaScript
/*
|
|
jQuery Form Plugin
|
|
version: 2.43 (12-MAR-2010)
|
|
@requires jQuery v1.3.2 or later
|
|
|
|
Examples and documentation at: http://malsup.com/jquery/form/
|
|
Dual licensed under the MIT and GPL licenses:
|
|
http://www.opensource.org/licenses/mit-license.php
|
|
http://www.gnu.org/licenses/gpl.html
|
|
*/
|
|
(function(b){function o(){if(b.fn.ajaxSubmit.debug){var a="[jquery.form] "+Array.prototype.join.call(arguments,"");if(window.console&&window.console.log)window.console.log(a);else window.opera&&window.opera.postError&&window.opera.postError(a)}}b.fn.ajaxSubmit=function(a){function e(){function s(){var q=h.attr("target"),n=h.attr("action");k.setAttribute("target",z);k.getAttribute("method")!="POST"&&k.setAttribute("method","POST");k.getAttribute("action")!=g.url&&k.setAttribute("action",g.url);g.skipEncodingOverride||
|
|
h.attr({encoding:"multipart/form-data",enctype:"multipart/form-data"});g.timeout&&setTimeout(function(){C=true;t()},g.timeout);var m=[];try{if(g.extraData)for(var v in g.extraData)m.push(b('<input type="hidden" name="'+v+'" value="'+g.extraData[v]+'" />').appendTo(k)[0]);u.appendTo("body");u.data("form-plugin-onload",t);k.submit()}finally{k.setAttribute("action",n);q?k.setAttribute("target",q):h.removeAttr("target");b(m).remove()}}function t(){if(!D){var q=true;try{if(C)throw"timeout";var n,m;m=w.contentWindow?
|
|
w.contentWindow.document:w.contentDocument?w.contentDocument:w.document;var v=g.dataType=="xml"||m.XMLDocument||b.isXMLDoc(m);o("isXml="+v);if(!v&&(m.body==null||m.body.innerHTML=="")){if(--G){o("requeing onLoad callback, DOM not available");setTimeout(t,250);return}o("Could not access iframe DOM after 100 tries.");return}o("response detected");D=true;j.responseText=m.body?m.body.innerHTML:null;j.responseXML=m.XMLDocument?m.XMLDocument:m;j.getResponseHeader=function(H){return{"content-type":g.dataType}[H]};
|
|
if(g.dataType=="json"||g.dataType=="script"){var E=m.getElementsByTagName("textarea")[0];if(E)j.responseText=E.value;else{var F=m.getElementsByTagName("pre")[0];if(F)j.responseText=F.innerHTML}}else if(g.dataType=="xml"&&!j.responseXML&&j.responseText!=null)j.responseXML=A(j.responseText);n=b.httpData(j,g.dataType)}catch(B){o("error caught:",B);q=false;j.error=B;b.handleError(g,j,"error",B)}if(q){g.success(n,"success");x&&b.event.trigger("ajaxSuccess",[j,g])}x&&b.event.trigger("ajaxComplete",[j,g]);
|
|
x&&!--b.active&&b.event.trigger("ajaxStop");if(g.complete)g.complete(j,q?"success":"error");setTimeout(function(){u.removeData("form-plugin-onload");u.remove();j.responseXML=null},100)}}function A(q,n){if(window.ActiveXObject){n=new ActiveXObject("Microsoft.XMLDOM");n.async="false";n.loadXML(q)}else n=(new DOMParser).parseFromString(q,"text/xml");return n&&n.documentElement&&n.documentElement.tagName!="parsererror"?n:null}var k=h[0];if(b(":input[name=submit]",k).length)alert('Error: Form elements must not be named "submit".');
|
|
else{var g=b.extend({},b.ajaxSettings,a),r=b.extend(true,{},b.extend(true,{},b.ajaxSettings),g),z="jqFormIO"+(new Date).getTime(),u=b('<iframe id="'+z+'" name="'+z+'" src="'+g.iframeSrc+'" onload="(jQuery(this).data(\'form-plugin-onload\'))()" />'),w=u[0];u.css({position:"absolute",top:"-1000px",left:"-1000px"});var j={aborted:0,responseText:null,responseXML:null,status:0,statusText:"n/a",getAllResponseHeaders:function(){},getResponseHeader:function(){},setRequestHeader:function(){},abort:function(){this.aborted=
|
|
1;u.attr("src",g.iframeSrc)}},x=g.global;x&&!b.active++&&b.event.trigger("ajaxStart");x&&b.event.trigger("ajaxSend",[j,g]);if(r.beforeSend&&r.beforeSend(j,r)===false)r.global&&b.active--;else if(!j.aborted){var D=false,C=0;if(r=k.clk){var y=r.name;if(y&&!r.disabled){g.extraData=g.extraData||{};g.extraData[y]=r.value;if(r.type=="image"){g.extraData[y+".x"]=k.clk_x;g.extraData[y+".y"]=k.clk_y}}}g.forceSync?s():setTimeout(s,10);var G=100}}}if(!this.length){o("ajaxSubmit: skipping submit process - no element selected");
|
|
return this}if(typeof a=="function")a={success:a};var c=b.trim(this.attr("action"));if(c)c=(c.match(/^([^#]+)/)||[])[1];c=c||window.location.href||"";a=b.extend({url:c,type:this.attr("method")||"GET",iframeSrc:/^https/i.test(window.location.href||"")?"javascript:false":"about:blank"},a||{});c={};this.trigger("form-pre-serialize",[this,a,c]);if(c.veto){o("ajaxSubmit: submit vetoed via form-pre-serialize trigger");return this}if(a.beforeSerialize&&a.beforeSerialize(this,a)===false){o("ajaxSubmit: submit aborted via beforeSerialize callback");
|
|
return this}var f=this.formToArray(a.semantic);if(a.data){a.extraData=a.data;for(var d in a.data)if(a.data[d]instanceof Array)for(var l in a.data[d])f.push({name:d,value:a.data[d][l]});else f.push({name:d,value:a.data[d]})}if(a.beforeSubmit&&a.beforeSubmit(f,this,a)===false){o("ajaxSubmit: submit aborted via beforeSubmit callback");return this}this.trigger("form-submit-validate",[f,this,a,c]);if(c.veto){o("ajaxSubmit: submit vetoed via form-submit-validate trigger");return this}d=b.param(f);if(a.type.toUpperCase()==
|
|
"GET"){a.url+=(a.url.indexOf("?")>=0?"&":"?")+d;a.data=null}else a.data=d;var h=this,i=[];a.resetForm&&i.push(function(){h.resetForm()});a.clearForm&&i.push(function(){h.clearForm()});if(!a.dataType&&a.target){var p=a.success||function(){};i.push(function(s){var t=a.replaceTarget?"replaceWith":"html";b(a.target)[t](s).each(p,arguments)})}else a.success&&i.push(a.success);a.success=function(s,t,A){for(var k=0,g=i.length;k<g;k++)i[k].apply(a,[s,t,A||h,h])};d=b("input:file",this).fieldValue();l=false;
|
|
for(c=0;c<d.length;c++)if(d[c])l=true;if(d.length&&a.iframe!==false||a.iframe||l||0)a.closeKeepAlive?b.get(a.closeKeepAlive,e):e();else b.ajax(a);this.trigger("form-submit-notify",[this,a]);return this};b.fn.ajaxForm=function(a){return this.ajaxFormUnbind().bind("submit.form-plugin",function(e){e.preventDefault();b(this).ajaxSubmit(a)}).bind("click.form-plugin",function(e){var c=e.target,f=b(c);if(!f.is(":submit,input:image")){c=f.closest(":submit");if(c.length==0)return;c=c[0]}var d=this;d.clk=c;
|
|
if(c.type=="image")if(e.offsetX!=undefined){d.clk_x=e.offsetX;d.clk_y=e.offsetY}else if(typeof b.fn.offset=="function"){f=f.offset();d.clk_x=e.pageX-f.left;d.clk_y=e.pageY-f.top}else{d.clk_x=e.pageX-c.offsetLeft;d.clk_y=e.pageY-c.offsetTop}setTimeout(function(){d.clk=d.clk_x=d.clk_y=null},100)})};b.fn.ajaxFormUnbind=function(){return this.unbind("submit.form-plugin click.form-plugin")};b.fn.formToArray=function(a){var e=[];if(this.length==0)return e;var c=this[0],f=a?c.getElementsByTagName("*"):c.elements;
|
|
if(!f)return e;for(var d=0,l=f.length;d<l;d++){var h=f[d],i=h.name;if(i)if(a&&c.clk&&h.type=="image"){if(!h.disabled&&c.clk==h){e.push({name:i,value:b(h).val()});e.push({name:i+".x",value:c.clk_x},{name:i+".y",value:c.clk_y})}}else if((h=b.fieldValue(h,true))&&h.constructor==Array)for(var p=0,s=h.length;p<s;p++)e.push({name:i,value:h[p]});else h!==null&&typeof h!="undefined"&&e.push({name:i,value:h})}if(!a&&c.clk){a=b(c.clk);f=a[0];if((i=f.name)&&!f.disabled&&f.type=="image"){e.push({name:i,value:a.val()});
|
|
e.push({name:i+".x",value:c.clk_x},{name:i+".y",value:c.clk_y})}}return e};b.fn.formSerialize=function(a){return b.param(this.formToArray(a))};b.fn.fieldSerialize=function(a){var e=[];this.each(function(){var c=this.name;if(c){var f=b.fieldValue(this,a);if(f&&f.constructor==Array)for(var d=0,l=f.length;d<l;d++)e.push({name:c,value:f[d]});else f!==null&&typeof f!="undefined"&&e.push({name:this.name,value:f})}});return b.param(e)};b.fn.fieldValue=function(a){for(var e=[],c=0,f=this.length;c<f;c++){var d=
|
|
b.fieldValue(this[c],a);d===null||typeof d=="undefined"||d.constructor==Array&&!d.length||(d.constructor==Array?b.merge(e,d):e.push(d))}return e};b.fieldValue=function(a,e){var c=a.name,f=a.type,d=a.tagName.toLowerCase();if(typeof e=="undefined")e=true;if(e&&(!c||a.disabled||f=="reset"||f=="button"||(f=="checkbox"||f=="radio")&&!a.checked||(f=="submit"||f=="image")&&a.form&&a.form.clk!=a||d=="select"&&a.selectedIndex==-1))return null;if(d=="select"){var l=a.selectedIndex;if(l<0)return null;c=[];d=
|
|
a.options;var h=(f=f=="select-one")?l+1:d.length;for(l=f?l:0;l<h;l++){var i=d[l];if(i.selected){var p=i.value;p||(p=i.attributes&&i.attributes.value&&!i.attributes.value.specified?i.text:i.value);if(f)return p;c.push(p)}}return c}return a.value};b.fn.clearForm=function(){return this.each(function(){b("input,select,textarea",this).clearFields()})};b.fn.clearFields=b.fn.clearInputs=function(){return this.each(function(){var a=this.type,e=this.tagName.toLowerCase();if(a=="text"||a=="password"||e=="textarea")this.value=
|
|
"";else if(a=="checkbox"||a=="radio")this.checked=false;else if(e=="select")this.selectedIndex=-1})};b.fn.resetForm=function(){return this.each(function(){if(typeof this.reset=="function"||typeof this.reset=="object"&&!this.reset.nodeType)this.reset()})};b.fn.enable=function(a){if(a==undefined)a=true;return this.each(function(){this.disabled=!a})};b.fn.selected=function(a){if(a==undefined)a=true;return this.each(function(){var e=this.type;if(e=="checkbox"||e=="radio")this.checked=a;else if(this.tagName.toLowerCase()==
|
|
"option"){e=b(this).parent("select");a&&e[0]&&e[0].type=="select-one"&&e.find("option").selected(false);this.selected=a}})}})(jQuery);
|