You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by im...@apache.org on 2007/09/03 09:02:55 UTC

svn commit: r572241 - in /myfaces/tomahawk/trunk: core/src/main/java/org/apache/myfaces/custom/dojo/ sandbox/core/src/main/java/org/apache/myfaces/custom/submitOnEvent/ sandbox/core/src/main/resources/org/apache/myfaces/custom/submitOnEvent/resource/

Author: imario
Date: Mon Sep  3 00:02:54 2007
New Revision: 572241

URL: http://svn.apache.org/viewvc?rev=572241&view=rev
Log:
Add a way to check if dojo is going to be used within the rendered page. The check will return true after the main dojo initialization has been added to the dojo scripts.

Make use of dojo (if it is already in use) in SubmitOnEventRenderer to check if dojo.addOnLoad can be used insted of the setTimeout way which makes problems with the dojo modal dialog widget (timeout might be too short before the widget has been created).
Avoid recursive adding of onHide method to the dialog component (makes it work with ppr).
Notice: SubmitOnEvent uses dojo only if there is already a component requireing it. This avoids loading the heavy-weight library when alternatives exist.

Modified:
    myfaces/tomahawk/trunk/core/src/main/java/org/apache/myfaces/custom/dojo/DojoUtils.java
    myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/submitOnEvent/SubmitOnEventRenderer.java
    myfaces/tomahawk/trunk/sandbox/core/src/main/resources/org/apache/myfaces/custom/submitOnEvent/resource/submitOnEvent.js

Modified: myfaces/tomahawk/trunk/core/src/main/java/org/apache/myfaces/custom/dojo/DojoUtils.java
URL: http://svn.apache.org/viewvc/myfaces/tomahawk/trunk/core/src/main/java/org/apache/myfaces/custom/dojo/DojoUtils.java?rev=572241&r1=572240&r2=572241&view=diff
==============================================================================
--- myfaces/tomahawk/trunk/core/src/main/java/org/apache/myfaces/custom/dojo/DojoUtils.java (original)
+++ myfaces/tomahawk/trunk/core/src/main/java/org/apache/myfaces/custom/dojo/DojoUtils.java Mon Sep  3 00:02:54 2007
@@ -41,7 +41,7 @@
  * mechanisms note this class uses its own double entries filter due to the fact
  * that we can mix and match header and body scripts as needed (we do not want
  * to lose portal functionality do we?)
- * 
+ *
  * @author Werner Punz (latest modification by $Author$)
  * @version $Revision$ $Date: 2006-09-08 20:56:28 +0000 (Fri, 08 Sep
  *          2006) $
@@ -93,7 +93,7 @@
 
     /**
      * creates a dojoed attribute map upon the given array of attribute names
-     * 
+     *
      * @param facesContext
      *            standard faces context used internally
      * @param attributeNames
@@ -154,7 +154,7 @@
      * system a debug:true is required for this to work properly it will not be
      * set by this method (due to the avoidance of unwanted automatisms causing
      * sideefects)
-     * 
+     *
      * @param facesContext
      * @param component
      * @return
@@ -189,7 +189,15 @@
         renderWidgetInitializationCode(writer, component, DEBUG_CONSOLE_TYPE, attributeMap, MYFACES_DOJO_DEBUGCONSOLE_ID, true);
     }
 
-    public static void addMainInclude(FacesContext facesContext, UIComponent component, String javascriptLocation, DojoConfig config) throws IOException {
+	/**
+	 * check if dojo is going to be used 
+	 */
+	public static boolean isDojoInitialized(FacesContext facesContext)
+	{
+		return isInlineScriptCheck(facesContext, DJCONFIG_INITKEY);
+	}
+
+	public static void addMainInclude(FacesContext facesContext, UIComponent component, String javascriptLocation, DojoConfig config) throws IOException {
 
         AddResource addResource = AddResourceFactory.getInstance(facesContext);
 
@@ -209,13 +217,13 @@
                 /*
                  * ResponseWriter writer = facesContext.getResponseWriter();
                  * writer.startElement(HTML.SCRIPT_ELEM,component);
-                 * 
+                 *
                  * MyFacesResourceHandler handler = new
                  * MyFacesResourceHandler(DojoResourceLoader.class, dojofile);
                  * String uri = handler.getResourceUri(facesContext); uri =
                  * uri.replaceAll("dojo\\.js\\;jsessionid(.)*\\\"","dojo.js");
                  * writer.writeAttribute(HTML.SRC_ATTR, uri, null);
-                 * 
+                 *
                  * writer.endElement(HTML.SCRIPT_ELEM);
                  * addResource.addJavaScriptAtPosition(facesContext,
                  * AddResource.HEADER_BEGIN, DojoResourceLoader.class,
@@ -230,7 +238,7 @@
 
     /**
      * adds a new namespace location to the mix
-     * 
+     *
      * @param facesContext
      *            the faces context which is used internally
      * @param component
@@ -253,7 +261,7 @@
 
     /**
      * adds a dojo provide to the current list of definitions within the header
-     * 
+     *
      * @param context
      *            the faces context for accessing the resources internally
      * @param provided
@@ -275,7 +283,7 @@
 
     /**
      * adds a dojo provide
-     * 
+     *
      * @param facesContext
      * @param component
      * @param provided
@@ -306,7 +314,7 @@
 
     /**
      * adds a dojo require include to our mix of stuff used
-     * 
+     *
      * @param facesContext
      * @param required
      */
@@ -321,7 +329,7 @@
 
     /**
      * creates a debug statement for the debug console
-     * 
+     *
      * @param stmnt
      *            the debug message displayed by the debug console
      * @return javaScriptcode String
@@ -332,7 +340,7 @@
 
     /**
      * creates a debug statement and a corresponding value for the debug console
-     * 
+     *
      * @param stmnt
      *            the debug message displayed and given value by the debug
      *            console
@@ -344,7 +352,7 @@
 
     /**
      * helper method which does the proper dojo provide script creation
-     * 
+     *
      * @param provided
      *            the provided class name
      * @return dojoProvide String
@@ -361,7 +369,7 @@
 
     /**
      * helper method for the proper dojo require script creation
-     * 
+     *
      * @param required
      *            the creation package for the require functionality
      * @return dojoRequire String
@@ -377,7 +385,7 @@
 
     /**
      * Request singleton getter method for the djConfig object
-     * 
+     *
      * @param context
      * @return
      */
@@ -398,7 +406,7 @@
 
     /**
      * getter for the expanded flat
-     * 
+     *
      * @param facesContext
      * @return
      */
@@ -416,7 +424,7 @@
 
     /**
      * Inline script set
-     * 
+     *
      * @param context
      *            standard faces context
      * @param inlineScript
@@ -438,14 +446,26 @@
         return true;
     }
 
-    /**
+	/**
+	 * check if the script with the given inlineScript-name has been added
+	 */
+	public static boolean isInlineScriptCheck(FacesContext context, String inlineScript)
+	{
+		// TODO move this non neutral code into the resource handler
+		HttpServletRequest request = (HttpServletRequest) context.getExternalContext().getRequest();
+		Set set = getBodyScriptInfos(request);
+
+		return set.contains(inlineScript);
+	}
+
+	/**
      * please, instead of using standard dojo taglib mechanisms use this code
      * for initialisation it will render a clean and proper javascript
      * initialisation instead. There are issues with ADF and the dojo taglib
      * mechanisms and also (Alex Russel wont like to hear this) the dojo taglib
      * initialisation fails on W3C validations. returns the name of the
      * javascript var for further processing
-     * 
+     *
      * @param facesContext
      *            standard faces context
      * @param component
@@ -463,7 +483,7 @@
     /**
      * convenience method to render the widget init code automatically
      * for a given component and a set of attribute names
-     * 
+     *
      * @param facesContext
      * @param component
      * @param dojoType
@@ -479,7 +499,7 @@
 
     /**
      * same for a given neutral id...
-     * 
+     *
      * @param dojoType
      * @param paramMap
      * @param clientId
@@ -500,7 +520,7 @@
         String javascriptVar = (String) paramMap.get("widgetVar");
         if (StringUtils.isBlank(javascriptVar))
             javascriptVar = calculateWidgetVarName(clientId);
-        
+
         Iterator it = paramMap.entrySet().iterator();
 
         writer.write("var ");
@@ -524,7 +544,7 @@
                 					// ambersants, dojo req
                 boolean isString = value instanceof String;
                 if (isString)
-                {                      
+                {
                     if( value.equals("true")
                         || value.equals("false") )
                         isString = false;
@@ -553,7 +573,7 @@
     /**
      * helper method to centralize the widget variable name calculation for our
      * dojo javascript widget init code
-     * 
+     *
      * @param clientId
      *            the client id upon which the var name has to be generated
      * @return the javascript widget var name for the given client id
@@ -563,7 +583,7 @@
     }
 
     /**
-     * 
+     *
      * @return
      */
     public static String calculateWidgetId(FacesContext context, UIComponent widget) {
@@ -577,9 +597,9 @@
         return widgetVarName;
     }
 
-    
+
     /**
-     * 
+     *
      * @return
      */
     public static String calculateWidgetVarName(FacesContext context, UIComponent widget) {
@@ -598,7 +618,7 @@
      * is that an existing entry is overwritten if a new config entry is set
      * make sure that this is not called too often due to the fact that we do
      * heavy reflection in here
-     * 
+     *
      * @param context
      * @param config
      */
@@ -655,7 +675,7 @@
     /**
      * if this flag is set to true somewhere before the rendering, the expanded
      * version is loaded otherwise the nonexpanded version is loaded
-     * 
+     *
      * @param facesContext
      *            context because we again have a full request singleton here
      * @param expanded
@@ -670,7 +690,7 @@
     /**
      * helper to write out debug statements this is only a convenience method to
      * reduce the code bloat
-     * 
+     *
      * @param writer
      * @param stmnt
      * @return
@@ -684,12 +704,12 @@
     /**
      * dojo namespace definition method allows the definition of a new namespace
      * within the parameters of the dojo namespacing system
-     * 
+     *
      * @param namespace
      *            the dojo namespace
      * @param location
      *            the exaclt script location (can be a relative location)
-     * 
+     *
      * @return the namespace script which has to be printed / executed
      */
     private static String createNamespaceScript(String namespace, String location) {
@@ -719,7 +739,7 @@
     /**
      * helper to write an inline javascript at the exact resource location of
      * the call
-     * 
+     *
      * @param facesContext
      * @param component
      * @param script

Modified: myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/submitOnEvent/SubmitOnEventRenderer.java
URL: http://svn.apache.org/viewvc/myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/submitOnEvent/SubmitOnEventRenderer.java?rev=572241&r1=572240&r2=572241&view=diff
==============================================================================
--- myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/submitOnEvent/SubmitOnEventRenderer.java (original)
+++ myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/submitOnEvent/SubmitOnEventRenderer.java Mon Sep  3 00:02:54 2007
@@ -19,6 +19,7 @@
 package org.apache.myfaces.custom.submitOnEvent;
 
 import org.apache.myfaces.custom.dialog.ModalDialog;
+import org.apache.myfaces.custom.dojo.DojoUtils;
 import org.apache.myfaces.renderkit.html.util.AddResource;
 import org.apache.myfaces.renderkit.html.util.AddResourceFactory;
 import org.apache.myfaces.shared_tomahawk.renderkit.html.HTML;
@@ -104,9 +105,24 @@
 				facesContext, AddResource.HEADER_BEGIN, SubmitOnEventRenderer.class,
 				"submitOnEvent.js");
 
-        StringBuffer js = new StringBuffer(80);
-        js.append("setTimeout(\"");
-        js.append("orgApacheMyfacesSubmitOnEventRegister('");
+		// If the dojo library will be loaded, use it to attach to the onLoad handler.
+		// Do NOT use dojo if no other component requires it, this avoids loading of this
+		// heavy-weight javascript-library for just the simple submitOnEvent use-case.
+		// We do this for better integration with dojo widget (e.g. our ModalDialog)
+		// where using the timeout stuff is not appropriate.
+		boolean useDojoForInit = DojoUtils.isDojoInitialized(facesContext);
+
+		StringBuffer js = new StringBuffer(80);
+
+		if (useDojoForInit)
+		{
+			js.append("dojo.addOnLoad(function() {");
+		}
+		else
+		{
+			js.append("setTimeout(\"");
+		}
+		js.append("orgApacheMyfacesSubmitOnEventRegister('");
         if (submitOnEvent.getEvent() != null)
         {
             js.append(submitOnEvent.getEvent().toLowerCase());
@@ -143,8 +159,16 @@
 		}
         js.append("','");
         js.append(forComponent.getClientId(facesContext));
+		js.append("');");
         // js.append("');");
-        js.append("');\", 50)");
+		if (useDojoForInit)
+		{
+			js.append("});");
+		}
+		else
+		{
+			js.append("\", 100)");
+		}
 
         // AddResourceFactory.getInstance(facesContext).addInlineScriptAtPosition(facesContext, AddResource.BODY_END, js.toString());
 

Modified: myfaces/tomahawk/trunk/sandbox/core/src/main/resources/org/apache/myfaces/custom/submitOnEvent/resource/submitOnEvent.js
URL: http://svn.apache.org/viewvc/myfaces/tomahawk/trunk/sandbox/core/src/main/resources/org/apache/myfaces/custom/submitOnEvent/resource/submitOnEvent.js?rev=572241&r1=572240&r2=572241&view=diff
==============================================================================
--- myfaces/tomahawk/trunk/sandbox/core/src/main/resources/org/apache/myfaces/custom/submitOnEvent/resource/submitOnEvent.js (original)
+++ myfaces/tomahawk/trunk/sandbox/core/src/main/resources/org/apache/myfaces/custom/submitOnEvent/resource/submitOnEvent.js Mon Sep  3 00:02:54 2007
@@ -180,11 +180,14 @@
 
 	if (eventType == "dialogok")
 	{
-		component._myfaces_submitOnEvent_onHide=component.onHide;
-		component.onHide=function()
+		if (!component._myfaces_submitOnEvent_onHide)
 		{
-			this._myfaces_submitOnEvent_onHide();
-			setupHandler();
+			component._myfaces_submitOnEvent_onHide=component.onHide;
+			component.onHide=function()
+			{
+				this._myfaces_submitOnEvent_onHide();
+				setupHandler();
+			}
 		}
 	}
 	else if (component.addEventListener)