tapestry-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From hls...@apache.org
Subject cvs commit: jakarta-tapestry/framework/src/java/org/apache/tapestry/html Body.java
Date Thu, 27 Jan 2005 14:20:45 GMT
hlship      2005/01/27 06:20:45

  Modified:    .        status.xml
               framework/src/java/org/apache/tapestry/html Body.java
  Log:
  Make the Body component output initializations in a script block at the end of the page
(just before the close tag), rather than in a window.onload event handler.
  
  Revision  Changes    Path
  1.55      +5 -0      jakarta-tapestry/status.xml
  
  Index: status.xml
  ===================================================================
  RCS file: /home/cvs/jakarta-tapestry/status.xml,v
  retrieving revision 1.54
  retrieving revision 1.55
  diff -u -r1.54 -r1.55
  --- status.xml	5 Jan 2005 23:17:27 -0000	1.54
  +++ status.xml	27 Jan 2005 14:20:45 -0000	1.55
  @@ -321,6 +321,11 @@
    		as persistent page properties.	
    	</action>
   
  +  <action type="update" dev="HLS">
  +    Make the Body component output initializations in a script block at the
  +    end of the page (just before the close tag), rather than in a window.onload
  +    event handler.
  +  </action>
    	</release>
     	
   <!-- Use due-to to give acknowledgement for patches. -->
  
  
  
  1.5       +108 -132  jakarta-tapestry/framework/src/java/org/apache/tapestry/html/Body.java
  
  Index: Body.java
  ===================================================================
  RCS file: /home/cvs/jakarta-tapestry/framework/src/java/org/apache/tapestry/html/Body.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- Body.java	9 Jan 2005 20:02:53 -0000	1.4
  +++ Body.java	27 Jan 2005 14:20:45 -0000	1.5
  @@ -31,27 +31,19 @@
   import org.apache.tapestry.util.IdAllocator;
   
   /**
  - *  The body of a Tapestry page.  This is used since it allows components on the
  - *  page access to an initialization script (that is written the start, just inside
  - *  the &lt;body&gt; tag).  This is currently used by {@link Rollover} and {@link
Script}
  - *  components.
  + * The body of a Tapestry page. This is used since it allows components on the page access
to an
  + * initialization script (that is written the start, just inside the &lt;body&gt;
tag). This is
  + * currently used by {@link Rollover}and {@link Script}components. [ <a
  + * href="../../../../../ComponentReference/Body.html">Component Reference </a>]
    * 
  - *  [<a href="../../../../../ComponentReference/Body.html">Component Reference</a>]
  - * 
  - *  @author Howard Lewis Ship
  - * 
  - **/
  + * @author Howard Lewis Ship
  + */
   
   public abstract class Body extends AbstractComponent implements IScriptProcessor
   {
       // Lines that belong inside the onLoad event handler for the <body> tag.
       private StringBuffer _initializationScript;
   
  -    // The writer initially passed to render() ... wrapped elements render
  -    // into a nested response writer.
  -
  -    private IMarkupWriter _outerWriter;
  -
       // Any other scripting desired
   
       private StringBuffer _bodyScript;
  @@ -61,18 +53,16 @@
       private StringBuffer _imageInitializations;
   
       /**
  -     *  Map of URLs to Strings (preloaded image references).
  -     *
  -     **/
  +     * Map of URLs to Strings (preloaded image references).
  +     */
   
       private Map _imageMap;
   
       /**
  -     *  List of included scripts.  Values are Strings.
  -     *
  -     *  @since 1.0.5
  -     *
  -     **/
  +     * List of included scripts. Values are Strings.
  +     * 
  +     * @since 1.0.5
  +     */
   
       private List _externalScripts;
   
  @@ -81,19 +71,17 @@
       private static final String ATTRIBUTE_NAME = "org.apache.tapestry.active.Body";
   
       /**
  -     *  Tracks a particular preloaded image.
  -     *
  -     **/
  +     * Tracks a particular preloaded image.
  +     */
   
       /**
  -     *  Adds to the script an initialization for the named variable as
  -     *  an Image(), to the given URL.
  -     *
  -     *  <p>Returns a reference, a string that can be used to represent
  -     *  the preloaded image in a JavaScript function.
  -     *
  -     *  @since 1.0.2
  -     **/
  +     * Adds to the script an initialization for the named variable as an Image(), to the
given URL.
  +     * <p>
  +     * Returns a reference, a string that can be used to represent the preloaded image
in a
  +     * JavaScript function.
  +     * 
  +     * @since 1.0.2
  +     */
   
       public String getPreloadedImageReference(String URL)
       {
  @@ -127,12 +115,10 @@
       }
   
       /**
  -     *  Adds other initialization, in the form of additional JavaScript
  -     *  code to execute from the &lt;body&gt;'s <code>onLoad</code>
event
  -     *  handler.  The caller is responsible for adding a semicolon (statement
  -     *  terminator).  This method will add a newline after the script.
  -     *
  -     **/
  +     * Adds other initialization, in the form of additional JavaScript code to execute
from the
  +     * &lt;body&gt;'s <code>onLoad</code> event handler. The caller
is responsible for adding a
  +     * semicolon (statement terminator). This method will add a newline after the script.
  +     */
   
       public void addInitializationScript(String script)
       {
  @@ -145,27 +131,21 @@
       }
   
       /**
  -     *  Adds additional scripting code to the page.  This code
  -     *  will be added to a large block of scripting code at the
  -     *  top of the page (i.e., the before the &lt;body&gt; tag).
  -     *
  -     *  <p>This is typically used to add some form of JavaScript
  -     *  event handler to a page.  For example, the
  -     *  {@link Rollover} component makes use of this.
  -     *
  -     *  <p>Another way this is invoked is by using the
  -     *  {@link Script} component.
  -     *
  -     *  <p>The string will be added, as-is, within
  -     *  the &lt;script&gt; block generated by this <code>Body</code>
component.
  -     *  The script should <em>not</em> contain HTML comments, those will
  -     *  be supplied by this Body component.
  -     *
  -     *  <p>A frequent use is to add an initialization function using
  -     *  this method, then cause it to be executed using
  -     *  {@link #addInitializationScript(String)}.
  -     *
  -     **/
  +     * Adds additional scripting code to the page. This code will be added to a large block
of
  +     * scripting code at the top of the page (i.e., the before the &lt;body&gt;
tag).
  +     * <p>
  +     * This is typically used to add some form of JavaScript event handler to a page. For
example,
  +     * the {@link Rollover}component makes use of this.
  +     * <p>
  +     * Another way this is invoked is by using the {@link Script}component.
  +     * <p>
  +     * The string will be added, as-is, within the &lt;script&gt; block generated
by this
  +     * <code>Body</code> component. The script should <em>not</em>
contain HTML comments, those
  +     * will be supplied by this Body component.
  +     * <p>
  +     * A frequent use is to add an initialization function using this method, then cause
it to be
  +     * executed using {@link #addInitializationScript(String)}.
  +     */
   
       public void addBodyScript(String script)
       {
  @@ -176,15 +156,12 @@
       }
   
       /**
  -     *  Used to include a script from an outside URL (the scriptLocation
  -     *  is a URL, probably obtained from an asset.  This adds
  -     *  an &lt;script src="..."&gt; tag before the main
  -     *  &lt;script&gt; tag.  The Body component ensures
  -     *  that each URL is included only once.
  -     *
  -     *  @since 1.0.5
  -     *
  -     **/
  +     * Used to include a script from an outside URL (the scriptLocation is a URL, probably
obtained
  +     * from an asset. This adds an &lt;script src="..."&gt; tag before the main
&lt;script&gt; tag.
  +     * The Body component ensures that each URL is included only once.
  +     * 
  +     * @since 1.0.5
  +     */
   
       public void addExternalScript(Resource scriptLocation)
       {
  @@ -197,11 +174,9 @@
           // Alas, this won't give a good ILocation for the actual problem.
   
           if (!(scriptLocation instanceof ClasspathResource))
  -            throw new ApplicationRuntimeException(
  -                Tapestry.format("Body.include-classpath-script-only", scriptLocation),
  -                this,
  -                null,
  -                null);
  +            throw new ApplicationRuntimeException(Tapestry.format(
  +                    "Body.include-classpath-script-only",
  +                    scriptLocation), this, null, null);
   
           // Record the URL so we don't include it twice.
   
  @@ -216,10 +191,9 @@
           int count = Tapestry.size(_externalScripts);
           for (int i = 0; i < count; i++)
           {
  -            ClasspathResource scriptLocation =
  -                (ClasspathResource) _externalScripts.get(i);
  +            ClasspathResource scriptLocation = (ClasspathResource) _externalScripts.get(i);
   
  -            // This is still very awkward!  Should move the code inside PrivateAsset somewhere
  +            // This is still very awkward! Should move the code inside PrivateAsset somewhere
               // else, so that an asset does not have to be created to to build the URL.
               PrivateAsset asset = new PrivateAsset(scriptLocation, null);
               String url = asset.buildURL(getPage().getRequestCycle());
  @@ -238,12 +212,10 @@
       }
   
       /**
  -     *  Retrieves the <code>Body</code> that was stored into the
  -     *  request cycle.  This allows components wrapped by the
  -     *  <code>Body</code> to locate it and access the services it
  -     *  provides.
  -     *
  -     **/
  +     * Retrieves the <code>Body</code> that was stored into the request cycle.
This allows
  +     * components wrapped by the <code>Body</code> to locate it and access
the services it
  +     * provides.
  +     */
   
       public static Body get(IRequestCycle cycle)
       {
  @@ -253,16 +225,11 @@
       protected void renderComponent(IMarkupWriter writer, IRequestCycle cycle)
       {
           if (cycle.getAttribute(ATTRIBUTE_NAME) != null)
  -            throw new ApplicationRuntimeException(
  -                Tapestry.getMessage("Body.may-not-nest"),
  -                this,
  -                null,
  -                null);
  +            throw new ApplicationRuntimeException(Tapestry.getMessage("Body.may-not-nest"),
this,
  +                    null, null);
   
           cycle.setAttribute(ATTRIBUTE_NAME, this);
   
  -        _outerWriter = writer;
  -
           IMarkupWriter nested = writer.getNestedWriter();
   
           renderBody(nested, cycle);
  @@ -274,18 +241,24 @@
   
           writer.println();
   
  -        // Write the page's scripting.  This is included scripts
  -        // and dynamic JavaScript, including initialization.
  +        // Write the page's scripting. This is included scripts
  +        // and dynamic JavaScript.
   
  -        writeScript();
  +        writeScript(writer);
   
           // Close the nested writer, which dumps its buffered content
           // into its parent.
   
           nested.close();
   
  -        writer.end(); // <body>
  +        // Any initialization should go at the very end of the document
  +        // just before the close body tag. Older version of Tapestry
  +        // would create a window.onload event handler, but this is better
  +        // (it doesn't have to wait for external images to load).
   
  +        writeInitializationScript(writer);
  +
  +        writer.end(); // <body>
       }
   
       protected void cleanupAfterRender(IRequestCycle cycle)
  @@ -309,66 +282,69 @@
   
           if (_bodyScript != null)
               _bodyScript.setLength(0);
  -
  -        _outerWriter = null;
  -        _outerWriter = null;
       }
   
       /**
  -     *  Writes a single large JavaScript block containing:
  -     *  <ul>
  -     *  <li>Any image initializations
  -     *  <li>Any scripting
  -     *  <li>Any initializations
  -     *  </ul>
  -     *
  -     *  <p>The script is written into a nested markup writer.
  -     *
  -     *  <p>If there are any other initializations 
  -     *  (see {@link #addOtherInitialization(String)}),
  -     *  then a function to execute them is created.
  -     **/
  +     * Writes a single large JavaScript block containing:
  +     * <ul>
  +     * <li>Any image initializations
  +     * <li>Any scripting
  +     * <li>Any initializations
  +     * </ul>
  +     * <p>
  +     * The script is written into a nested markup writer.
  +     * <p>
  +     * If there are any other initializations (see {@link #addOtherInitialization(String)}),
then a
  +     * function to execute them is created.
  +     */
   
  -    private void writeScript()
  +    private void writeScript(IMarkupWriter writer)
       {
           if (!Tapestry.isEmpty(_externalScripts))
  -            writeExternalScripts(_outerWriter);
  +            writeExternalScripts(writer);
   
  -        if (!(any(_initializationScript) || any(_bodyScript) || any(_imageInitializations)))
  +        if (!(any(_bodyScript) || any(_imageInitializations)))
               return;
   
  -        _outerWriter.begin("script");
  -        _outerWriter.attribute("language", "JavaScript");
  -        _outerWriter.attribute("type", "text/javascript");
  -        _outerWriter.printRaw("<!--");
  +        writer.begin("script");
  +        writer.attribute("language", "JavaScript");
  +        writer.attribute("type", "text/javascript");
  +        writer.printRaw("<!--");
   
           if (any(_imageInitializations))
           {
  -            _outerWriter.printRaw("\n\nvar tapestry_preload = new Array();\n");
  -            _outerWriter.printRaw("if (document.images)\n");
  -            _outerWriter.printRaw("{\n");
  -            _outerWriter.printRaw(_imageInitializations.toString());
  -            _outerWriter.printRaw("}\n");
  +            writer.printRaw("\n\nvar tapestry_preload = new Array();\n");
  +            writer.printRaw("if (document.images)\n");
  +            writer.printRaw("{\n");
  +            writer.printRaw(_imageInitializations.toString());
  +            writer.printRaw("}\n");
           }
   
           if (any(_bodyScript))
           {
  -            _outerWriter.printRaw("\n\n");
  -            _outerWriter.printRaw(_bodyScript.toString());
  +            writer.printRaw("\n\n");
  +            writer.printRaw(_bodyScript.toString());
           }
   
  -        if (any(_initializationScript))
  -        {
  +        writer.printRaw("\n\n// -->");
  +        writer.end();
  +    }
   
  -            _outerWriter.printRaw("\n\n" + "window.onload = function ()\n" + "{\n");
  +    /** @since 3.1 */
  +    private void writeInitializationScript(IMarkupWriter writer)
  +    {
  +        if (!any(_initializationScript))
  +            return;
   
  -            _outerWriter.printRaw(_initializationScript.toString());
  +        writer.begin("script");
  +        writer.attribute("language", "JavaScript");
  +        writer.attribute("type", "text/javascript");
  +        writer.printRaw("<!--\n");
   
  -            _outerWriter.printRaw("}");
  -        }
  +        writer.printRaw(_initializationScript.toString());
   
  -        _outerWriter.printRaw("\n\n// -->");
  -        _outerWriter.end();
  +        writer.printRaw("\n// -->");
  +        writer.end();
       }
   
       private boolean any(StringBuffer buffer)
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-dev-help@jakarta.apache.org


Mime
View raw message