common-forms.js

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

JS
325
字号
/*
* Copyright 1999-2004 The Apache Software Foundation
*
* 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 lookupWidget(startwidget,path)
{
  //print("=========================== LookupWidget =========================== ");
  //print("lookupwidget called: "+startwidget+" ; "+path);
  try
  {
    path = new Packages.java.lang.String(path);
  
    if(startwidget==null)
      return null;
      
    // we are there
    if(path.equals(""))
      return startwidget;
      
    // precompute
    var sep = path.indexOf("/");
    var nextPath = new Packages.java.lang.String();
    var curPath = path;
    if(sep>-1)
    {
      nextPath = path.substring(sep+1);
      curPath = path.substring(0,sep);
    }
    
    //print("path:"+path);
    //print("nextPath:"+nextPath);
      
    // predicates
    var parentIsRow = false;
    try
    {
      var numrow = java.lang.Integer.parseInt(startwidget.getParent().getId());
      parentIsRow = true;
    }
    catch(ignore) {}
    

    var nextSegmentIsIndexed = false;
    if(path.startsWith("../"))
    {
      var i = path.indexOf("/",3);
      if(i>-1)
      {
        var next = path.substring(3,i);
        if(next.indexOf("[")>-1)
          nextSegmentIsIndexed = true;
      }
    }

    var curSegmentIsIndexed = curPath.indexOf("[")>-1;
    var nextSegmentIsParent = nextPath.startsWith("../");
    var curSegmentIndex = new Packages.java.lang.String("illegalrow");
    var curSegmentIndexStr = new Packages.java.lang.String("illegalrow");
    
    // precompute again
    if(curSegmentIsIndexed)
    {
      var ind = curPath.indexOf("[");
      try
      {
        curSegmentIndexStr = curPath.substring(ind+1,curPath.length()-1);
        curSegmentIndex = java.lang.Integer.parseInt(curPath.substring(ind+1,curPath.length()-1));
      }
      catch(ignore){}
      
      curPath = curPath.substring(0,ind);
    }
    
    //print("nextPath:"+nextPath);
    //print("parentIsRow:"+parentIsRow);
    //print("nextSegmentIsIndexed:"+nextSegmentIsIndexed);
    //print("nextSegmentIsParent:"+nextSegmentIsParent);
    //print("parentwidget:"+startwidget.getParent());
    //print("stat");
    
    // MEAT

    // we want to go to the parent
    if(path.startsWith("../"))
    {
      //print("par");
      
      if(parentIsRow && (nextSegmentIsParent) )
      {
        //print("going up two levels");
        return lookupWidget(startwidget.getParent().getParent(), nextPath);
      }
      else
      {
        //print("going up one level");
        return lookupWidget(startwidget.getParent(), nextPath);
      }
    }
    else if(path.startsWith("/"))
    {
      //print("abs");
      var name = Packages.nl.hippo.cocoon.forms.XSDHelper.getIdFromXPath(path);
      return startwidget.lookupWidget("/"+name);
    }
    else
    {
      //print("curSegmentIsIndexed:"+curSegmentIsIndexed);
      //print("child index:"+curSegmentIndex);
      //print("curpath:"+curPath);
      var depth = 0;
      var prevSep;
      var keepLooking = true;
      while (keepLooking){

        var extra = Packages.nl.hippo.cocoon.forms.XSDHelper.getRevSeparator() + "repeat";

        if (sep > -1) curPath = path.substring(0,sep);

        var name = Packages.nl.hippo.cocoon.forms.XSDHelper.getIdFromXPath("/"+curPath);
        //print("3.0 curpath = "+curPath);
        
        // recompute predicates for current path
        curSegmentIsIndexed = curPath.indexOf("[")>-1;
        nextPath = new Packages.java.lang.String();
        if (sep > -1 && sep+1 < path.length())
          nextPath = path.substring(sep+1); 
        // indexed
        if(curSegmentIsIndexed)
        {
          curSegmentIndexStr = new Packages.java.lang.String("illegalrow");
          // precompute again
          var ind = curPath.indexOf("[");
          try
          {
            curSegmentIndexStr = curPath.substring(ind+1,curPath.length()-1);
            curSegmentIndex = java.lang.Integer.parseInt(curPath.substring(ind+1,curPath.length()-1));
          }
          catch(ignore){}
          
          curPath = curPath.substring(0,ind);
          
          name = Packages.nl.hippo.cocoon.forms.XSDHelper.getIdFromXPath("/"+curPath);
          
          if(curSegmentIndexStr.equals("last") || curSegmentIndexStr.equals("last()") || curSegmentIndexStr.equals("position()=last()") || curSegmentIndexStr.equals("-1")  )
          {
            startwidget = getChildEndingIn(startwidget,name+extra);
            startwidget = startwidget.getRow(startwidget.getSize()-1);
          }
          else{
            try{
              startwidget = getChildEndingIn(startwidget,name+extra).getRow(curSegmentIndex);
            } catch(e){
              //print("index out of bounds");
            }            
          }
            
          return lookupWidget(startwidget,nextPath);
        }
        
        var it = startwidget.getChildren();
        while(it.hasNext())
        {
          var candidate = it.next();
          if(candidate.getId().endsWith(name))
          {
            return lookupWidget(candidate,nextPath);
          }
        }
        prevSep=sep; // remember current separator index;
        keepLooking = (sep > -1);
        sep = path.indexOf("/",sep+1);
        if (sep == -1) curPath = path;
        /*
        if (keepLooking) 
            print("#3.8 no matching child found at depth "+depth+"! Trying one level deeper");
          else 
            print("#3.8 no matching child found at depth "+depth+"! No more levels");
        */
        depth++;
      } // end while

    }
    //print("done!");
  }
  catch(e)
  {
    print(e);
    return null;
  }
  
  return null;
}

function getLastChildEndingIn(widget,name)
{
  var it = widget.getChildren();
  var w = null;
  while(it.hasNext())
  {
    var candidate = it.next();
    if(candidate.getId().endsWith(name))
    {
      w = candidate;
    }
  }
  return w;
}

function getChildEndingIn(widget,name)
{
  var it = widget.getChildren();
  while(it.hasNext())
  {
    var candidate = it.next();
    if(candidate.getId().endsWith(name))
    {
      return candidate;
    }
  }
  return null;
}

function makeTargetURI(path) {
    var sfx = ".xml";
    var newSfx = "-result.xml";
    var newPath = path;
    if (path.match(/^.*\.xml$/)) {
        newPath = path.substring(0, path.length - ".xml".length);
    }
    return newPath + newSfx;
}

function loadDocument(uri) {
    var parser = null;
    var source = null;
    var resolver = null;
    try {
        parser = cocoon.getComponent(Packages.org.apache.excalibur.xml.dom.DOMParser.ROLE);
        resolver = cocoon.getComponent(Packages.org.apache.cocoon.environment.SourceResolver.ROLE);
        source = resolver.resolveURI(uri);
        var is = new Packages.org.xml.sax.InputSource(source.getInputStream());
        is.setSystemId(source.getURI());
        return parser.parseDocument(is);
    } finally {
        if (source != null)
            resolver.release(source);
        cocoon.releaseComponent(parser);
        cocoon.releaseComponent(resolver);
    }
}


function clearSelectionList(w){
	w.setSelectionList(new Packages.org.apache.cocoon.forms.datatype.EmptySelectionList(""));
}


function setValidationError(widget,message){
  widget.setValidationError(new Packages.org.apache.cocoon.forms.validation.ValidationError(message,false));
  return false;
}

function clearValidationError(widget){
  widget.setValidationError(null);
}

function stringMatches(str,pattern){
  var _str=new Packages.java.lang.String(str);
  var _pattern=new Packages.java.lang.String(pattern);
  return _str.matches(_pattern);
}

function validEmail(str){
  if (str == null) return false;
  var _str=new Packages.java.lang.String(str);
  var _pattern=new Packages.java.lang.String("[A-Za-z0-9._%-]+@([A-Za-z0-9._%-]+\\.){1,3}[A-Za-z]{2,5}");
  return _str.matches(_pattern);
}

function strCompareIgnoreCase(str,compareStr){
  if (str == null) return false;
  var str2=new Packages.java.lang.String(str.toString());
  var comparestr2=new Packages.java.lang.String(compareStr.toString());
  return str2.equalsIgnoreCase(comparestr2);
}

function validateHTMLArea(widget)
{  
  var tmpString = "";
  var validationError = new Packages.org.apache.cocoon.forms.validation.ValidationError("This field is required.");
  
  if(widget.getValue()== null)
  {
    widget.setValidationError(validationError);
    return false;
  }  
  else if(widget.getValue()!== null)
  {
    if(widget.getValue().length()==22)
    {
      widget.setValidationError(validationError);
    }
    return false;
  }
  
  widget.setValidationError(null);
  return true;
  
}


⌨️ 快捷键说明

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