formresulthandler.js

来自「Hippo CMS是一个以信息为中心的开源内容管理系统。Hippo CMS目标是」· JavaScript 代码 · 共 90 行

JS
90
字号
/*
* Copyright 2001-2007 Hippo (www.hippo.nl)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*   http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
function FormResultHandler(targets) {
  this.targets = targets;
}

FormResultHandler.prototype.processResult = function(form, finished) {
  this.clearMessages(form);
  var result = true;
  var _formIsValid = this.formIsValid(form.form);
  if (finished && _formIsValid){
    result=this.makePersistent(form);
    editorFlowContext.setDirtyFlag(true);
  } else {
    // TODO: check if messages should be displayed
    if (!_formIsValid) {
      this.setValidationMessage(form,'GENERAL_VALIDATION_ERROR');
   }
  }
  return result;
}
FormResultHandler.prototype.clearMessages = function(form) {
  this.setValidationMessage(form,'');
  this.setMessage(form,'');
}

FormResultHandler.prototype.setValidationMessage = function(form,msg) {
  var msgWidget = form.lookupWidget('CFormsEditorValidationMessageWidget');
  if (msgWidget != null) msgWidget.setValue(msg);
}

FormResultHandler.prototype.setMessage = function(form,msg) {
  var msgWidget = form.lookupWidget('CFormsEditorMessageWidget');
  if (msgWidget != null) msgWidget.setValue(msg);
}

FormResultHandler.prototype.makePersistent = function(form) {
  var i;
  var result = true;
  for (i=0; i<this.targets.length; i++)
     result = result && this.targets[i].makePersistent(form);
    if(result) {    
       if(cocoon.request.get("submitid").indexOf("publish")>0)
       {
         this.setMessage(form,'PUBLISH-SAVE-SUCCESSFUL');
       }
    else
       {
         this.setMessage(form,'SAVE-SUCCESSFUL');
       }
    }
    else {
       this.setMessage(form,'SAVE-UNSUCCESSFUL');
    }
  return result;
}

FormResultHandler.prototype.nestedIsValid = function(widget){
  if (widget instanceof org.apache.cocoon.forms.validation.ValidationErrorAware)
    if (widget.getValidationError() != null) return false; 
  if (widget.getChildren != null && widget.getChildren() != null){
    var children = widget.getChildren();
    while (children.hasNext())
      if (!this.nestedIsValid(children.next())) return false;
  } else
  if (widget instanceof org.apache.cocoon.forms.formmodel.Repeater){
    var i;    
    for (i=0; i<widget.getSize(); i++)
      if (!this.nestedIsValid(widget.getRow(i))) return false;
  }
  return true;
} 

FormResultHandler.prototype.formIsValid = function(form){  
  return this.nestedIsValid(form);
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?