You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@click.apache.org by sa...@apache.org on 2009/05/17 16:19:36 UTC

svn commit: r775657 - in /incubator/click/trunk/click: extras/src/org/apache/click/extras/control/ extras/src/org/apache/click/extras/prototype/ framework/src/org/apache/click/element/ framework/src/org/apache/click/util/

Author: sabob
Date: Sun May 17 14:19:36 2009
New Revision: 775657

URL: http://svn.apache.org/viewvc?rev=775657&view=rev
Log:
added support to JsScript for automatic addLoadEvent rendering

Modified:
    incubator/click/trunk/click/extras/src/org/apache/click/extras/control/AutoCompleteTextField.java
    incubator/click/trunk/click/extras/src/org/apache/click/extras/prototype/CalendarField.java
    incubator/click/trunk/click/framework/src/org/apache/click/element/JsScript.java
    incubator/click/trunk/click/framework/src/org/apache/click/util/PageImports.java

Modified: incubator/click/trunk/click/extras/src/org/apache/click/extras/control/AutoCompleteTextField.java
URL: http://svn.apache.org/viewvc/incubator/click/trunk/click/extras/src/org/apache/click/extras/control/AutoCompleteTextField.java?rev=775657&r1=775656&r2=775657&view=diff
==============================================================================
--- incubator/click/trunk/click/extras/src/org/apache/click/extras/control/AutoCompleteTextField.java (original)
+++ incubator/click/trunk/click/extras/src/org/apache/click/extras/control/AutoCompleteTextField.java Sun May 17 14:19:36 2009
@@ -288,14 +288,17 @@
         JsScript script = new JsScript();
         script.setId(fieldId + "_autocomplete");
         if (!headElements.contains(script)) {
+            // Script must be executed as soon as browser dom is ready
+            script.setExecuteOnDomReady(true);
+
             String contextPath = context.getRequest().getContextPath();
             HtmlStringBuffer buffer = new HtmlStringBuffer(150);
-            buffer.append("addLoadEvent(function() { new Ajax.Autocompleter(");
+            buffer.append("new Ajax.Autocompleter(");
             buffer.append("'").append(fieldId).append("'");
             buffer.append(",'").append(fieldId).append("_auto_complete_div'");
             buffer.append(",'").append(contextPath).append(page.getPath()).append(
                 "'");
-            buffer.append(",").append(getAutoCompleteOptions()).append(");})");
+            buffer.append(",").append(getAutoCompleteOptions()).append(");");
             script.setContent(buffer);
             headElements.add(script);
         }

Modified: incubator/click/trunk/click/extras/src/org/apache/click/extras/prototype/CalendarField.java
URL: http://svn.apache.org/viewvc/incubator/click/trunk/click/extras/src/org/apache/click/extras/prototype/CalendarField.java?rev=775657&r1=775656&r2=775657&view=diff
==============================================================================
--- incubator/click/trunk/click/extras/src/org/apache/click/extras/prototype/CalendarField.java (original)
+++ incubator/click/trunk/click/extras/src/org/apache/click/extras/prototype/CalendarField.java Sun May 17 14:19:36 2009
@@ -410,8 +410,11 @@
         // is contained in the headElement. This caters for when the field is
         // used in a fly-weight pattern such as FormTable.
         if (!headElements.contains(script)) {
+
+            // Script must be executed as soon as browser dom is ready
+            script.setExecuteOnDomReady(true);
+
             HtmlStringBuffer buffer = new HtmlStringBuffer(150);
-            buffer.append("addLoadEvent( function(){");
             buffer.append("  Event.observe('").append(imgId).append(
                 "', 'click', function(){");
             buffer.append("  Date.first_day_of_week=").append(getFirstDayOfWeek() - 1);
@@ -427,7 +430,6 @@
             buffer.append(", year_range: [1930,2050]});");
 
             buffer.append("  });");
-            buffer.append("});");
             script.setContent(buffer);
             headElements.add(script);
         }

Modified: incubator/click/trunk/click/framework/src/org/apache/click/element/JsScript.java
URL: http://svn.apache.org/viewvc/incubator/click/trunk/click/framework/src/org/apache/click/element/JsScript.java?rev=775657&r1=775656&r2=775657&view=diff
==============================================================================
--- incubator/click/trunk/click/framework/src/org/apache/click/element/JsScript.java (original)
+++ incubator/click/trunk/click/framework/src/org/apache/click/element/JsScript.java Sun May 17 14:19:36 2009
@@ -156,6 +156,12 @@
      */
     private boolean characterData = false;
 
+    /**
+     * Indicates the JsScript content must be executed as soon as the browser
+     * DOM is available, default value is false
+     */
+    private boolean executeOnDomReady = false;
+
     /** The path of the template to render. */
     private String template;
 
@@ -269,6 +275,38 @@
     }
 
     /**
+     * Return true if the JsScript content must be executed as soon as the
+     * browser DOM is ready, false otherwise. Default value is false.
+     *
+     * @see #setExecuteOnDomReady(boolean)
+     *
+     * @return true if the JsScript content must be executed as soon as the
+     * browser DOM is ready.
+     */
+    public boolean isExecuteOnDomReady() {
+        return executeOnDomReady;
+    }
+
+    /**
+     * Sets whether the JsScript content must be executed as soon as the browser
+     * DOM is ready.
+     * <p/>
+     * If this flag is true, the JsScript content will be registered with
+     * the "<tt>addLoadEvent</tt>" function from the JavaScript file
+     * "<tt>control.js</tt>".
+     * <p/>
+     * <b>Please note:</b> when setting this flag to true, the file JavaScript
+     * file "<tt>control.js</tt>" must already be included in the Page or
+     * Control, it won't be included automatically.
+     *
+     * @param executeOnDomReady indicates whether the JsScript content must be
+     * executed as soon as the browser DOM is ready.
+     */
+    public void setExecuteOnDomReady(boolean executeOnDomReady) {
+        this.executeOnDomReady = executeOnDomReady;
+    }
+
+    /**
      * Return the path of the template to render.
      *
      * @see #setTemplate(java.lang.String)
@@ -353,8 +391,13 @@
         // Render CDATA tag if necessary
         renderCharacterDataPrefix(buffer);
 
+        // Render the DOM ready function prefix
+        renderDomReadyPrefix(buffer);
+
         renderContent(buffer);
 
+        renderDomReadySuffix(buffer);
+
         renderCharacterDataSuffix(buffer);
 
         buffer.append("\n");
@@ -434,13 +477,46 @@
         buffer.append(getContent());
     }
 
+    /**
+     * Render the "<tt>addLoadEvent</tt>" function prefix to ensure the script
+     * is executed as soon as the browser DOM is available. The prefix is
+     * "<tt>addLoadEvent(function(){</tt>".
+     *
+     * @see #renderDomReadySuffix(org.apache.click.util.HtmlStringBuffer)
+     *
+     * @param buffer buffer to append the addLoadEvent function to
+     */
+    protected void renderDomReadyPrefix(HtmlStringBuffer buffer) {
+        // Wrap content in addLoadEvent function
+        if (isExecuteOnDomReady()) {
+            buffer.append("addLoadEvent(function(){\n");
+        }
+    }
+
+    /**
+     * Render the "<tt>addLoadEvent</tt>" function suffix. The suffix is
+     * "<tt>});</tt>".
+     *
+     * @see #renderDomReadyPrefix(org.apache.click.util.HtmlStringBuffer)
+     *
+     * @param buffer buffer to append the conditional comment prefix
+     */
+    protected void renderDomReadySuffix(HtmlStringBuffer buffer) {
+        // Close addLoadEvent function
+        if (isExecuteOnDomReady()) {
+            buffer.append("});");
+        }
+    }
+
     // ------------------------------------------------ Package Private Methods
 
     /**
      * Render the CDATA tag prefix to the specified buffer if
-     * {@link #isCharacterData()} returns true. The default value is
+     * {@link #isCharacterData()} returns true. The prefix is
      * <tt>/&lowast;&lt;![CDATA[&lowast;/</tt>.
      *
+     * @see #renderCharacterDataSuffix(org.apache.click.util.HtmlStringBuffer)
+     *
      * @param buffer buffer to append the conditional comment prefix
      */
     void renderCharacterDataPrefix(HtmlStringBuffer buffer) {
@@ -452,9 +528,11 @@
 
     /**
      * Render the CDATA tag suffix to the specified buffer if
-     * {@link #isCharacterData()} returns true. The default value is
+     * {@link #isCharacterData()} returns true. The prefix is
      * <tt>/&lowast;]]&gt;&lowast;/</tt>.
      *
+     * @see #renderCharacterDataPrefix(org.apache.click.util.HtmlStringBuffer)
+     *
      * @param buffer buffer to append the conditional comment prefix
      */
     void renderCharacterDataSuffix(HtmlStringBuffer buffer) {

Modified: incubator/click/trunk/click/framework/src/org/apache/click/util/PageImports.java
URL: http://svn.apache.org/viewvc/incubator/click/trunk/click/framework/src/org/apache/click/util/PageImports.java?rev=775657&r1=775656&r2=775657&view=diff
==============================================================================
--- incubator/click/trunk/click/framework/src/org/apache/click/util/PageImports.java (original)
+++ incubator/click/trunk/click/framework/src/org/apache/click/util/PageImports.java Sun May 17 14:19:36 2009
@@ -697,7 +697,13 @@
     private JsScript asJsScript(String line) {
         JsScript jsScript = new JsScript();
         copyAttributes(jsScript, line);
-        jsScript.append(extractJsContent(line));
+        jsScript.append(extractJsContent(line, jsScript));
+
+        // If the script must be executed on dom ready, set the domready
+        // attribute to true.
+        if (jsScript.getContent().toString().indexOf("addLoadEvent") >= 0) {
+            jsScript.setExecuteOnDomReady(true);
+        }
         return jsScript;
     }
 
@@ -729,9 +735,9 @@
      * Extract the JavaScript content from the given HTML import line.
      *
      * @param line the HTML import line
-     * @return the Javacript content contained in the HTML import line
+     * @param jsScript the JsScript to replace the line with
      */
-    private String extractJsContent(String line) {
+    private String extractJsContent(String line, JsScript jsScript) {
         if (line.endsWith("/>")) {
             // If tag has no content, exit early
             return "";
@@ -746,7 +752,19 @@
         if (end == -1) {
             return "";
         }
-        return line.substring(start + 1, end);
+        line = line.substring(start + 1, end).trim();
+
+        // Strip addLoadEvent function
+        int addLoadEventStart = line.indexOf("addLoadEvent");
+        if (addLoadEventStart == 0) {
+            start = line.indexOf("{", addLoadEventStart);
+            line = line.substring(start + 1);
+            end = line.lastIndexOf("});");
+            line = line.substring(0, end);
+            jsScript.setExecuteOnDomReady(true);
+        }
+
+        return line;
     }
 
     /**