📄 form.js
字号:
/* * * Copyright (c) 2004-2005 by Zapatec, Inc. * http://www.zapatec.com * 1700 MLK Way, Berkeley, California, * 94709, U.S.A. * All rights reserved. * * */Zapatec.formPath = Zapatec.getPath("Zapatec.Form");Zapatec.Utils.createNestedHash(Zapatec,["Langs","Zapatec.Form","eng"],{'initializeError':'Form already initialized!','loadingForm':'loading form','noFormError':"Couldn't find form",'submitOneError':'There is 1 error.','submitManyErrors':'There are %1 errors.','isRequiredError':'This field is required','maskNotFilledError':'Does not conform to mask %1','noSuchAutoCompleteValueError':'No such value','invalidURLError':'Invalid URL','invalidEmailError':'Invalid email address','invalidCreditCardError':'Invalid credit card number','invalidUSPhoneError':'Invalid US phone number','invalidInternationalPhoneError':'Invalid international phone number','invalidUSZipError':'Invalid US zip code','invalidDateError':'Invalid date','invalidIntError':'Not an integer','invalidFloatError':'Not a float','maxLengthError':'Please enter less then %1 symbols','minLengthError':'Please enter more then %1 symbols','ajaxDebugSeparator':'-----------------------','ajaxDebugSubmitTitle':'Sending request for AJAX submit form.','ajaxDebugValidateTitle':'Sending request for AJAX validate field %1.','ajaxDebugFillTitle':'Sending request for AJAX fill for field %1.','ajaxDebugQuery':'Query: %1','ajaxDebugResponse':'Response received: %1','ajaxDebugResponseError':'Error response received: %1','ajaxSubmitCantParseError':"Can't parse received JSON: %1",'ajaxSubmitNoResponseError':'No response','ajaxValidateCantParseError':"Can't parse received JSON: %1",'ajaxValidateNoResponseError':'No response','ajaxValidateValidationError':'This field is not valid','ajaxFillCantParseError':"Can't parse received JSON: %1",'ajaxFillNoResponseError':'No response','ajaxFillGeneralError':"Can't retrieve fill values",'ajaxSuggestCantParseError':"Can't parse received JSON: %1",'ajaxSuggestNoResponseError':'No response','ajaxSuggestGeneralError':"Can't retrieve value"});Zapatec.Form=function(){var objArgs={};switch(arguments.length){case 1:objArgs=arguments[0];break;case 2:objArgs=arguments[1];objArgs.form=arguments[0];break;}Zapatec.Form.SUPERconstructor.call(this,objArgs);};Zapatec.Form.id="Zapatec.Form";Zapatec.inherit(Zapatec.Form,Zapatec.Widget);Zapatec.Form.prototype.init=function(objArgs){this.container=null;Zapatec.Form.SUPERclass.init.call(this,objArgs);if(this.config.form.zpForm!=null){Zapatec.Log({description:this.getMessage('initializeError')});return null;}this.container=this.config.form;this.createProperty(this.container,"zpForm",this);this.addCircularRef("container");this.container.className=this.container.className.replace(/\bzpForm.*?\b/,"")+" "+this.getClassName({prefix:"zpForm"});var self=this;var oldOnSubmit=this.container.onsubmit||function(){return true;};if(typeof(this.config.asyncSubmitFunc)=='function'){if(this.config.serverCallback){var targetName="form-iframe-"+this.id;var tmp=document.createElement("span");tmp.innerHTML='<iframe name="'+targetName+'" style="display: none"></iframe>';var iframe=tmp.firstChild;this.container.parentNode.insertBefore(iframe,this.container);this.container.setAttribute("target",targetName);var hidden=document.createElement("input");hidden.setAttribute("type","hidden");hidden.setAttribute("name",this.config.serverCallback);hidden.setAttribute("value","window.parent.Zapatec.Widget.getWidgetById("+this.id+").processAsyncResponse");this.container.appendChild(hidden);}this.container.onsubmit=function(ev){var retVal=oldOnSubmit(ev);return self.asyncSubmit(ev)&&retVal;};}else{this.container.onsubmit=function(ev){return self.submit(ev)&&oldOnSubmit(ev);};}this.addCircularRef(this.container,"onsubmit");this.container.onreset=function(ev){setTimeout(function(ev){self.reset(ev);},1);};this.addCircularRef(this.container,"onreset");var focusedFlag=false;var els=[];for(var ii=0;ii<this.container.elements.length;ii++){els.push(this.container.elements[ii]);}for(var ii=0;ii<els.length;ii++){var el=els[ii];if(!Zapatec.Form.Utils.ignoreField(el)){var zpField=new Zapatec.Form.Field({form:this,field:el,langId:this.config.langId,lang:this.config.lang,langCountryCode:this.config.langCountryCode,langEncoding:this.config.langEncoding,formConfig:this.config});if(this.config.startupFocusPosition){if(el.zpFormField!=null&&focusedFlag==false&&typeof(el.focus)!='undefined'&&(typeof(el.type)=='undefined'||typeof(el.type)!='undefined'&&el.type.toLowerCase()!='hidden')&&!el.disabled&&!el.readOnly){if(this.config.startupFocusPosition=="firstField"||(this.config.startupFocusPosition=="firstRequiredField"&&zpField.hasFeature("zpFormRequired"))||(this.config.startupFocusPosition=="firstIncorrectField"&&zpField.validate(true)!=null&&zpField.validate(true).length>0)){var tmp=el.getAttribute("autocomplete");try{el.setAttribute('autocomplete','off');el.focus();focusedFlag=true;}catch(e){}el.setAttribute('autocomplete',tmp);}}}}}var childElements=this.container.all?this.container.all:this.container.getElementsByTagName("*");for(var ii=childElements.length-1;ii>=0;ii--){var el=childElements[ii];Zapatec.Form.Utils.initMultipleField(el,true,this);this.initConditionalField(el);}if(Zapatec.windowLoaded){this.formLoaded();}else{Zapatec.Utils.addEvent(window,"load",new Function("Zapatec.Widget.callMethod("+this.id+", 'formLoaded')"));}};Zapatec.Form.prototype.configure=function(objArgs){this.defineConfigOption('langId',Zapatec.Form.id);this.defineConfigOption('lang',"eng");this.defineConfigOption('form',null);this.defineConfigOption('statusImgPos','beforeField');this.defineConfigOption('showErrors',null);this.defineConfigOption('showErrorsOnSubmit',true);this.defineConfigOption('submitErrorFunc',Zapatec.Form.submitErrorFunc);this.defineConfigOption('submitValidFunc',null);this.defineConfigOption('asyncSubmitFunc',null);this.defineConfigOption('strict',false);this.defineConfigOption('asyncTheme',true);this.defineConfigOption('theme',"alternate");this.defineConfigOption('ajaxDebugFunc',null);this.defineConfigOption('dropDownTheme',"default");this.defineConfigOption('hideUntilThemeLoaded',false);this.defineConfigOption('putTabIndexesOnError',true);this.defineConfigOption('startupFocusPosition',"firstField");this.defineConfigOption('displayErrorWhileTyping',true);this.defineConfigOption('multipleCallback',Zapatec.Form.Utils.generateMultipleId);this.defineConfigOption('serverCallback');this.defineConfigOption('disableButtonsWhenAsyncSubmit',true);this.defineConfigOption('conditionalEvents');this.defineConfigOption('busyConfig');this.defineConfigOption('autoCompleteConfig');this.defineConfigOption('maskPlaceholder','_');Zapatec.Form.SUPERclass.configure.call(this,objArgs);this.config.form=Zapatec.Widget.getElementById(this.config.form);if(this.config.form==null||!this.config.form.nodeName||this.config.form.nodeName.toLowerCase()!="form"){Zapatec.Log({description:this.getMessage('noFormError')});throw this.getMessage('noFormError');}if(typeof(this.config.conditionalEvents)=='string'){this.config.conditionalEvents=[this.config.conditionalEvents];}};Zapatec.Form.prototype.reconfigure=function(objArgs){if(objArgs.theme){Zapatec.Utils.removeClass(this.container,this.getClassName({prefix:"zpForm"}));}Zapatec.Form.SUPERclass.reconfigure.call(this,objArgs);Zapatec.Utils.addClass(this.container,this.getClassName({prefix:"zpForm"}));};Zapatec.Form.prototype.addStandardEventListeners=function(){if(this.config.multipleCallback==Zapatec.Form.Utils.generateMultipleId){this.addEventListener("beforeDeleteMultiple",Zapatec.Form.Utils.beforeDeleteMultiple);}}Zapatec.Form.prototype.asyncSubmit=function(){var self=this;if(this.processing==true){return false;}if(this.config.showErrorsOnSubmit&&typeof(this.config.submitErrorFunc)=='function'&&!this.submit()){return false;}var strUrl=this.container.getAttribute("action");if(Zapatec.is_ie){strUrl=this.container.attributes["action"];if(strUrl){strUrl=strUrl.nodeValue;}}if(!strUrl){return false;}if(this.config.disableButtonsWhenAsyncSubmit){this.toggleSubmits(true);this.processing=true;}if(this.config.busyConfig){Zapatec.Transport.showBusy(this.config.busyConfig);}if(this.config.serverCallback){return true;}var arrContent=[];var objFormElements=this.container.elements;for(var iElm=0;iElm<objFormElements.length;iElm++){var formEl=objFormElements[iElm];if(!formEl.name||formEl.disabled){continue;}if(formEl.nodeName.toLowerCase()=='input'&&(formEl.type.toLowerCase()=='radio'||formEl.type.toLowerCase()=='checkbox')&&!formEl.checked){continue;}arrContent.push(formEl.name+'='+escape(formEl.value));}var strMethod=this.container.getAttribute("method");if(Zapatec.is_ie){strMethod=this.container.attributes["method"];if(strMethod){strMethod=strMethod.nodeValue;}}if(strMethod){strMethod=strMethod.toUpperCase();}var strContent=arrContent.join('&');if(strMethod!='POST'){if(strMethod===''){strMethod="GET";}strUrl+='?'+strContent;strContent=null;}if(this.config.ajaxDebugFunc){this.config.ajaxDebugFunc(this.getMessage('ajaxDebugSeparator'));this.config.ajaxDebugFunc(this.getMessage('ajaxDebugSubmitTitle'));this.config.ajaxDebugFunc(strMethod+" "+strUrl);this.config.ajaxDebugFunc(this.getMessage('ajaxDebugQuery',strContent));}var strEncoding=this.container.getAttribute("encoding");if(Zapatec.is_ie){strEncoding=this.container.attributes["encoding"];if(strEncoding){strEncoding=strEncoding.nodeValue;}}Zapatec.Transport.fetch({url:strUrl,method:strMethod,contentType:strEncoding,content:strContent,onLoad:function(objText){if(self.config.ajaxDebugFunc){self.config.ajaxDebugFunc(self.getMessage('ajaxDebugResponse',objText.responseText));}if(self.config.disableButtonsWhenAsyncSubmit){self.processing=false;self.toggleSubmits(false);}if(objText.responseText==null){Zapatec.Log({description:self.getMessage('ajaxSubmitNoResponseError',objText.responseText)});return null;}var objResponse=Zapatec.Transport.parseJson({strJson:objText.responseText});if(objResponse==null){Zapatec.Log({description:self.getMessage('ajaxSubmitCantParseError',objText.responseText)});return null;}return self.processAsyncResponse(objResponse);},onError:function(objError){if(self.config.disableButtonsWhenAsyncSubmit){self.processing=false;self.toggleSubmits(false);}var strError='';if(objError.errorCode){strError=objError.errorCode+' ';}strError+=objError.errorDescription;if(self.config.ajaxDebugFunc){self.config.ajaxDebugFunc(self.getMessage('ajaxDebugResponseError',strError));}if(self.config.showErrorsOnSubmit&&typeof(self.config.submitErrorFunc)=='function'){self.config.submitErrorFunc({serverSide:true,generalError:strError});}}});return false;};Zapatec.Form.prototype.processAsyncResponse=function(objResponse){if(this.config.disableButtonsWhenAsyncSubmit){this.processing=false;this.toggleSubmits(false);}if(this.config.busyConfig){Zapatec.Transport.removeBusy(this.config.busyConfig);}if(objResponse){if(objResponse.success){this.config.asyncSubmitFunc(objResponse.callbackArgs);}else if(this.config.showErrorsOnSubmit){var arrFieldErrors=[];var boolFocusSet=false;if(objResponse.fieldErrors){for(var strFieldName in objResponse.fieldErrors){for(var iElm=0;iElm<this.container.elements.length;iElm++){var objField=this.container.elements[iElm];if(objField.name&&objField.name==strFieldName){arrFieldErrors.push({field:objField,errorMessage:objResponse.fieldErrors[strFieldName],validator:''});if(objField.zpFormField!=null){objField.zpFormField.setImageStatus(objResponse.fieldErrors[strFieldName]);}break;}}}}if(typeof(this.config.submitErrorFunc)=='function'){this.config.submitErrorFunc({serverSide:true,generalError:objResponse.generalError||'',fieldErrors:arrFieldErrors});}}}else if(this.config.showErrorsOnSubmit&&typeof(this.config.submitErrorFunc)=='function'){this.config.submitErrorFunc({serverSide:true,generalError:this.getMessage('ajaxSubmitNoResponseError')});}};Zapatec.Form.prototype.toggleSubmits=function(disable){var inputs=this.container.getElementsByTagName("input");for(var ii=0;ii<inputs.length;ii++){if(inputs[ii].type=="submit"){inputs[ii].disabled=disable==true;}}};Zapatec.Form.prototype.reset=function(ev){for(var ii=0;ii<this.container.elements.length;ii++){var field=this.container.elements[ii].zpFormField;if(field!=null){field.setValueFromField();field.firstRun=true;field.blur();}}if(!ev){ev=window.event;}this.fireEvent("reset",ev);this.fireEvent("all",ev,"reset");};Zapatec.Form.prototype.submit=function(ev){var errors=this.validate(false);if(errors!=null&&errors.length>0&&this.config.showErrorsOnSubmit&&typeof(this.config.submitErrorFunc)=='function'){this.config.submitErrorFunc({serverSide:false,generalError:errors.length==1?this.getMessage('submitOneError'):this.getMessage('submitManyErrors',errors.length),fieldErrors:errors});try{errors[0].field.focus();}catch(e){}return false;}if(typeof(this.config.submitValidFunc)=='function'){this.config.submitValidFunc();}if(!ev){ev=window.event;}this.fireEvent("submit",ev);this.fireEvent("all",ev,"submit");return true;};Zapatec.Form.prototype.validate=function(onlyValidate){if(typeof(onlyValidate)=="undefined"){onlyValidate=true;}var valid=true;var tabIndex=1;var errors=[];for(var ii=0;ii<this.container.elements.length;ii++){var el=this.container.elements[ii];if(el.zpFormField==null){continue;}if(!onlyValidate){el.zpFormField.firstRun=false;}var validate=el.zpFormField.validate(onlyValidate);var fieldValid=(validate==null||validate.length==0);if(this.config.putTabIndexesOnError){if(fieldValid){el.tabIndex=100+tabIndex++;if(!Zapatec.is_ie){delete(el.tabIndex);}}else{el.tabIndex=tabIndex;}}if(!fieldValid){for(var jj=0;jj<validate.length;jj++){errors.push(validate[jj]);}}valid=valid&&fieldValid;}if(errors.length==0){errors=null;}return errors;};Zapatec.Form.prototype.initConditionalField=function(field){var md=null;if(field.className&&(md=field.className.match(/zpForm(Display|Visible)When=([^\s]+)/))){var func=eval(md[2]);if(typeof(func)!="function"){return null;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -