Hey Martin,

We all kinda unofficially agreed while you were on vacation that we
need open JIRA issues for any non-trivial changes to core or tomahawk
:-)  Thus, we can create changelogs and release notes.



On 9/11/06, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote:
Author: mmarinschek
Date: Mon Sep 11 01:31:53 2006
New Revision: 442124

URL: http://svn.apache.org/viewvc?view=rev&rev=442124
Log:
more revamping of MyFaces JavaScript - submitForm method is now independent of 
form

Modified:
    
myfaces/shared/trunk/core/src/main/java/org/apache/myfaces/shared/renderkit/html/HtmlLinkRendererBase.java
    
myfaces/shared/trunk/core/src/main/java/org/apache/myfaces/shared/renderkit/html/HtmlRendererUtils.java
    
myfaces/shared/trunk/core/src/main/java/org/apache/myfaces/shared/renderkit/html/util/JavascriptUtils.java

Modified: 
myfaces/shared/trunk/core/src/main/java/org/apache/myfaces/shared/renderkit/html/HtmlLinkRendererBase.java
URL: 
http://svn.apache.org/viewvc/myfaces/shared/trunk/core/src/main/java/org/apache/myfaces/shared/renderkit/html/HtmlLinkRendererBase.java?view=diff&rev=442124&r1=442123&r2=442124
==============================================================================
--- 
myfaces/shared/trunk/core/src/main/java/org/apache/myfaces/shared/renderkit/html/HtmlLinkRendererBase.java
 (original)
+++ 
myfaces/shared/trunk/core/src/main/java/org/apache/myfaces/shared/renderkit/html/HtmlLinkRendererBase.java
 Mon Sep 11 01:31:53 2006
@@ -35,6 +35,7 @@
 import java.io.UnsupportedEncodingException;
 import java.net.URLEncoder;
 import java.util.Iterator;
+import java.util.Map;

 /**
  * @author Manfred Geiler
@@ -44,6 +45,7 @@
     extends HtmlRenderer {
     public static final String URL_STATE_MARKER = "JSF_URL_STATE_MARKER=DUMMY";
     public static final int URL_STATE_MARKER_LEN = URL_STATE_MARKER.length();
+    private static final String FIRST_LINK_ON_PAGE = 
"org.apache.MyFaces.FIRST_LINK_ON_PAGE";

     //private static final Log log = LogFactory.getLog(HtmlLinkRenderer.class);

@@ -214,17 +216,24 @@
         }
         else
         {
-            renderFormSubmitScriptIfNecessary(facesContext, formInfo);
+            renderFormSubmitScriptIfNecessary(facesContext);

             StringBuffer params = addChildParameters(component, nestingForm);

             String target = getTarget(component);

-            onClick.append("return 
myFaces_submitForm_").append(JavascriptUtils.getValidJavascriptName(formName,false)).append("('").
-                    append(clientId).append("',").
+            onClick.append("return ").
+                    append(HtmlRendererUtils.SUBMIT_FORM_FN_NAME).append("('").
+                    append(formName).append("','").
+                    append(clientId).append("'");
+
+            if(params.length()>2 || target != null)
+            {
+                onClick.append(",").
                     append(params).append(",").
-                    append(target == null ? "null" : ("'" + target + "'")).
-                    append(");");
+                    append(target == null ? "null" : ("'" + target + "'"));
+            }
+            onClick.append(");");

             //add id parameter for decode to form - todo: better do that 
dynamically instead of referring to the form
             String hiddenFieldName = 
HtmlRendererUtils.getHiddenCommandLinkFieldName(formInfo);
@@ -236,19 +245,18 @@
         writer.writeAttribute(HTML.ONCLICK_ATTR, onClick.toString(), null);
     }

-    private static void renderFormSubmitScriptIfNecessary(FacesContext 
facesContext, FormInfo formInfo)
+    private static void renderFormSubmitScriptIfNecessary(FacesContext 
facesContext)
         throws IOException {

-        if 
(facesContext.getExternalContext().getRequestMap().get(getFormSubmitScriptName(formInfo))
 == null)
+        Map map = facesContext.getExternalContext().getRequestMap();
+        Boolean firstLink = (Boolean) map.get(FIRST_LINK_ON_PAGE);
+
+        if(firstLink == null || firstLink.equals(Boolean.TRUE))
         {
-            HtmlRendererUtils.renderFormSubmitScript(facesContext, formInfo);
-            
facesContext.getExternalContext().getRequestMap().put(getFormSubmitScriptName(formInfo),
 Boolean.TRUE);
-        }
-    }
+            map.put(FIRST_LINK_ON_PAGE,Boolean.FALSE);
+            HtmlRendererUtils.renderFormSubmitScript(facesContext);

-    private static String getFormSubmitScriptName(FormInfo formInfo)
-    {
-        return "FORM_SUBMIT_SCRIPT_FOR_"+formInfo.getFormName();
+        }
     }

     private String getTarget(UIComponent component)

Modified: 
myfaces/shared/trunk/core/src/main/java/org/apache/myfaces/shared/renderkit/html/HtmlRendererUtils.java
URL: 
http://svn.apache.org/viewvc/myfaces/shared/trunk/core/src/main/java/org/apache/myfaces/shared/renderkit/html/HtmlRendererUtils.java?view=diff&rev=442124&r1=442123&r2=442124
==============================================================================
--- 
myfaces/shared/trunk/core/src/main/java/org/apache/myfaces/shared/renderkit/html/HtmlRendererUtils.java
 (original)
+++ 
myfaces/shared/trunk/core/src/main/java/org/apache/myfaces/shared/renderkit/html/HtmlRendererUtils.java
 Mon Sep 11 01:31:53 2006
@@ -53,6 +53,8 @@

     public static final String CLEAR_HIDDEN_FIELD_FN_NAME =
         "clearFormHiddenParams";
+    private static final String SET_HIDDEN_INPUT_FN_NAME = "oamSetHiddenInput";
+    public static final String SUBMIT_FORM_FN_NAME = "oamSubmitForm";

     private HtmlRendererUtils() {
         // utility class, do not instantiate
@@ -802,17 +804,32 @@

     public static void 
appendClearHiddenCommandFormParamsFunctionCall(ScriptContext context, String 
formName) {

-        context.prettyLine();
-        context.append("if(window.");
-        
context.append(HtmlRendererUtils.getClearHiddenCommandFormParamsFunctionName(formName));
-        context.append("!=undefined)");
-        context.append("{");
-        
context.append(HtmlRendererUtils.getClearHiddenCommandFormParamsFunctionName(formName)).append("('").append(formName).append("');");
-        context.append("}");
+        if(formName == null)
+        {
+            context.prettyLine();
+            context.append("var clearFn = ");
+            
context.append(HtmlRendererUtils.getClearHiddenCommandFormParamsFunctionName(formName));
+            context.append(";");
+            context.prettyLine();
+            context.append("if(eval('window.'+clearFn)!=undefined)");
+            context.append("{");
+            context.append("eval('window.'+clearFn+'(formName)');");
+            context.append("}");
+        }
+        else
+        {
+            context.prettyLine();
+            context.append("if(window.");
+            
context.append(HtmlRendererUtils.getClearHiddenCommandFormParamsFunctionName(formName));
+            context.append("!=undefined)");
+            context.append("{");
+            
context.append(HtmlRendererUtils.getClearHiddenCommandFormParamsFunctionName(formName)).append("('").append(formName).append("');");
+            context.append("}");
+        }
     }


-    public static void renderFormSubmitScript(FacesContext facesContext, 
FormInfo formInfo)
+    public static void renderFormSubmitScript(FacesContext facesContext)
             throws IOException
     {
         ResponseWriter writer = facesContext.getResponseWriter();
@@ -827,77 +844,92 @@
         context.prettyLine();
         context.increaseIndent();

-        prepareScript(context, formInfo, autoScroll);
+        prepareScript(context, autoScroll);

         writer.writeText(context.toString(),null);

         writer.endElement(HTML.SCRIPT_ELEM);
     }

-    private static void prepareScript(ScriptContext context, FormInfo 
formInfo, boolean autoScroll)
+    private static void prepareScript(ScriptContext context, boolean 
autoScroll)
     {
-        String formName = formInfo.getFormName();

-        context.append("function myFaces_submitForm_").append(
-                
JavascriptUtils.getValidJavascriptName(formName,false)).append("(linkId, target, 
params)");
+        context.prettyLine();
+
+        context.append("function ");
+        context.append(SET_HIDDEN_INPUT_FN_NAME).append("(formname, name, 
value)");
+        context.append("{");
+        context.append("var form = document.forms[formname];");
+        context.prettyLine();
+        context.append("if(form.elements[name]==undefined)");
+        context.append("{");
+        context.append("var newInput = document.createElement['input'];");
+        context.prettyLine();
+        context.append("newInput.setAttribute('type','hidden');");
+        context.prettyLine();
+        context.append("newInput.setAttribute('name',name);");
+        context.prettyLine();
+        context.append("newInput.setAttribute('value',value);");
+        context.prettyLine();
+        context.append("form.appendChild(newInput);");
+        context.append("}");
+        context.append("else");
+        context.append("{");
+        context.append("form.elements[name].value=value;");
+        context.append("}");
+
+        context.append("}");
+
+        context.prettyLine();
+
+        context.append("function ");
+        context.append(SUBMIT_FORM_FN_NAME).append("(formName, linkId, target, 
params)");
         context.append("{");

         //call the script to clear the form (clearFormHiddenParams_<formName>) 
method
-        
HtmlRendererUtils.appendClearHiddenCommandFormParamsFunctionCall(context, 
formName);
+        
HtmlRendererUtils.appendClearHiddenCommandFormParamsFunctionCall(context, null);

         if (autoScroll)
         {
-            JavascriptUtils.appendAutoScrollAssignment(context, formName);
+            JavascriptUtils.appendAutoScrollAssignment(context, null);
         }

         context.prettyLine();

         context.append("if(target!=undefined && target != null)");
         context.append("{");
-        context.append("document.forms['"+formName+"'].target=target;");
+        context.append("document.forms[formName].target=target;");
         context.append("}");

         context.append("if(params!=undefined && params != null)");
         context.append("{");
         context.append("for(var i=0; i<params.length; i++)");
         context.append("{");
-        
context.append("if(document.forms['").append(formName).append("'].elements[params[i][0]]==undefined)");
-        context.append("{");
-        context.append("var form = document.forms['"+formName+"'];");
-        context.prettyLine();
-        context.append("var newInput = document.createElement['input'];");
-        context.prettyLine();
-        context.append("newInput.setAttribute('type','hidden');");
-        context.prettyLine();
-        context.append("newInput.setAttribute('name',params[i][0]);");
-        context.prettyLine();
-        context.append("newInput.setAttribute('value',params[i][1]);");
-        context.prettyLine();
-        context.append("form.appendChild(newInput);");
-        context.append("}");
+        context.append(SET_HIDDEN_INPUT_FN_NAME).append("(formName,params[i][0], 
params[i][1]);");
         context.append("}");
         context.append("}");

         context.prettyLine();

-        context.append("document.forms['"+formName+"'].elements['"+
-                
HtmlRendererUtils.getHiddenCommandLinkFieldName(formInfo)+"'].value=linkId;");
+        context.append(SET_HIDDEN_INPUT_FN_NAME);
+        context.append("(formName,formName +'"+NamingContainer.SEPARATOR_CHAR+
+                
"'+'"+HtmlRendererUtils.HIDDEN_COMMANDLINK_FIELD_NAME+"',linkId);");

         context.prettyLine();
         context.prettyLine();

-        context.append("if(document.forms['"+formName+"'].onsubmit)");
+        context.append("if(document.forms[formName].onsubmit)");
         context.append("{");
-        context.append("var 
result=document.forms['"+formName+"'].onsubmit();");
+        context.append("var result=document.forms[formName].onsubmit();");
         context.prettyLine();
         context.append("if( (typeof result == 'undefined') || result )");
         context.append("{");
-        context.append("document.forms['"+formName+"'].submit();");
+        context.append("document.forms[formName].submit();");
         context.append("}");
         context.append("}");
         context.append("else ");
         context.append("{");
-        context.append("document.forms['"+formName+"'].submit();");
+        context.append("document.forms[formName].submit();");
         context.append("}");
         //return false, so that browser does not handle the click
         context.append("return false;");
@@ -1075,10 +1107,18 @@
      */
     public static String getClearHiddenCommandFormParamsFunctionName(
         String formName) {
-        return 
JavascriptUtils.getValidJavascriptNameAsInRI(CLEAR_HIDDEN_FIELD_FN_NAME
-            + "_"
-            + formName
-            .replace(NamingContainer.SEPARATOR_CHAR, '_'));
+        if(formName == null)
+        {
+            return "'" +CLEAR_HIDDEN_FIELD_FN_NAME
+                + 
"_'+formName.replace('"+NamingContainer.SEPARATOR_CHAR+"','_')";
+        }
+         else
+        {
+            return 
JavascriptUtils.getValidJavascriptNameAsInRI(CLEAR_HIDDEN_FIELD_FN_NAME
+                + "_"
+                + formName
+                .replace(NamingContainer.SEPARATOR_CHAR, '_'));
+        }
     }

     /**

Modified: 
myfaces/shared/trunk/core/src/main/java/org/apache/myfaces/shared/renderkit/html/util/JavascriptUtils.java
URL: 
http://svn.apache.org/viewvc/myfaces/shared/trunk/core/src/main/java/org/apache/myfaces/shared/renderkit/html/util/JavascriptUtils.java?view=diff&rev=442124&r1=442123&r2=442124
==============================================================================
--- 
myfaces/shared/trunk/core/src/main/java/org/apache/myfaces/shared/renderkit/html/util/JavascriptUtils.java
 (original)
+++ 
myfaces/shared/trunk/core/src/main/java/org/apache/myfaces/shared/renderkit/html/util/JavascriptUtils.java
 Mon Sep 11 01:31:53 2006
@@ -285,13 +285,26 @@

     public static void 
appendAutoScrollAssignment(HtmlRendererUtils.ScriptContext scriptContext, 
String formName)
     {
-        scriptContext.prettyLine();
-        scriptContext.append("if(window."+AUTO_SCROLL_FUNCTION+"!=undefined)");
-        scriptContext.append("{");
-        scriptContext.append("document.forms['").append(formName).append("']");
-        
scriptContext.append(".elements['").append(AUTO_SCROLL_PARAM).append("']");
-        
scriptContext.append(".value=").append(AUTO_SCROLL_FUNCTION).append("();");
-        scriptContext.append("}");
+        if(formName == null)
+        {
+            scriptContext.prettyLine();
+            
scriptContext.append("if(window."+AUTO_SCROLL_FUNCTION+"!=undefined)");
+            scriptContext.append("{");
+            scriptContext.append("document.forms[formName]");
+            
scriptContext.append(".elements['").append(AUTO_SCROLL_PARAM).append("']");
+            
scriptContext.append(".value=").append(AUTO_SCROLL_FUNCTION).append("();");
+            scriptContext.append("}");
+        }
+        else
+        {
+            scriptContext.prettyLine();
+            
scriptContext.append("if(window."+AUTO_SCROLL_FUNCTION+"!=undefined)");
+            scriptContext.append("{");
+            
scriptContext.append("document.forms['").append(formName).append("']");
+            
scriptContext.append(".elements['").append(AUTO_SCROLL_PARAM).append("']");
+            
scriptContext.append(".value=").append(AUTO_SCROLL_FUNCTION).append("();");
+            scriptContext.append("}");
+        }
     }

     /**



Reply via email to