Author: dolander
Date: Wed Jan 19 21:01:43 2005
New Revision: 125711

URL: http://svn.apache.org/viewcvs?view=rev&rev=125711
Log:
Add a new JavaScript support tag.  The ScriptBlock allows JavaScript to be 
inserted either
before or after the framework generated JavaScript.  This tag is necessary if 
you want to
write HTML through JavaScript and keep the HTML/XHTML generated legal. (It's 
not legal for
the <script> tag to appear after the body, but because the end body tag 
commonly writes out the
framework JavaScript, there is no way without this tag to insert <script> after 
the framework
created JavaScript but before the </body> tag.

In addition, we support placement directly before the framework generated 
script and also inline.

Added a smoke test which has not yet been added to the DRTs.



Added:
   
incubator/beehive/trunk/netui/src/tags-html/org/apache/beehive/netui/tags/html/ScriptBlock.java
   incubator/beehive/trunk/netui/test/webapps/drt/coreWeb/coretags/sb/
   incubator/beehive/trunk/netui/test/webapps/drt/coreWeb/coretags/sb/base/
   
incubator/beehive/trunk/netui/test/webapps/drt/coreWeb/coretags/sb/base/Controller.jpf
   
incubator/beehive/trunk/netui/test/webapps/drt/coreWeb/coretags/sb/base/index.jsp
Modified:
   
incubator/beehive/trunk/netui/src/tags-html/org/apache/beehive/netui/tags/IScriptReporter.java
   
incubator/beehive/trunk/netui/src/tags-html/org/apache/beehive/netui/tags/html/ScriptContainer.java

Modified: 
incubator/beehive/trunk/netui/src/tags-html/org/apache/beehive/netui/tags/IScriptReporter.java
Url: 
http://svn.apache.org/viewcvs/incubator/beehive/trunk/netui/src/tags-html/org/apache/beehive/netui/tags/IScriptReporter.java?view=diff&rev=125711&p1=incubator/beehive/trunk/netui/src/tags-html/org/apache/beehive/netui/tags/IScriptReporter.java&r1=125710&p2=incubator/beehive/trunk/netui/src/tags-html/org/apache/beehive/netui/tags/IScriptReporter.java&r2=125711
==============================================================================
--- 
incubator/beehive/trunk/netui/src/tags-html/org/apache/beehive/netui/tags/IScriptReporter.java
      (original)
+++ 
incubator/beehive/trunk/netui/src/tags-html/org/apache/beehive/netui/tags/IScriptReporter.java
      Wed Jan 19 21:01:43 2005
@@ -36,6 +36,11 @@
     void addScriptCode(String script);
 
     /**
+     *
+     */
+    void placeScriptCode(String script, boolean after);
+
+    /**
      * This method will add Script as a function.
      * @param script the text of the function. This value must not be null.
      */

Added: 
incubator/beehive/trunk/netui/src/tags-html/org/apache/beehive/netui/tags/html/ScriptBlock.java
Url: 
http://svn.apache.org/viewcvs/incubator/beehive/trunk/netui/src/tags-html/org/apache/beehive/netui/tags/html/ScriptBlock.java?view=auto&rev=125711
==============================================================================
--- (empty file)
+++ 
incubator/beehive/trunk/netui/src/tags-html/org/apache/beehive/netui/tags/html/ScriptBlock.java
     Wed Jan 19 21:01:43 2005
@@ -0,0 +1,98 @@
+/*
+ * Copyright 2005 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.
+ *
+ * $Header:$
+ */
+package org.apache.beehive.netui.tags.html;
+
+import org.apache.beehive.netui.tags.AbstractSimpleTag;
+import org.apache.beehive.netui.tags.IScriptReporter;
+import org.apache.beehive.netui.tags.rendering.WriteRenderAppender;
+
+import javax.servlet.jsp.JspException;
+import javax.servlet.jsp.PageContext;
+import java.io.IOException;
+
+/**
+ * This class will output a script block into the generated HTML.  The primary 
reason
+ * for this class is to add the ability to add JavaScript either before or 
after the
+ * framework provided JavaScript.
+ * @netui:tag name="scriptBlock" body-content="scriptless" description="Used 
to place JavaScript in relationship to the framework genrated JavaScript"
+ * @netui.tldx:tag whitespace="indent"
+ * renderer=""
+ */
+public class ScriptBlock extends AbstractSimpleTag
+{
+    public enum Placement { INLINE, BEFORE, AFTER }
+
+    private Placement _placement = Placement.INLINE;
+
+
+    /**
+     * Return the name of the Tag.
+     */
+    public String getTagName()
+    {
+        return "Content";
+    }
+
+    /**
+     * Place the JavaScript inside in relationship to the frameword generated 
JavaScript.
+     * @param placement - The placement of the JavaScript
+     * @jsptagref.attributedescription A ScriptBlock.Placement.
+     * @jsptagref.attributesyntaxvalue <i>string_or_expression_output</i>
+     * @netui:attribute rtexprvalue="true"
+     * description="The String literal or expression used to output the 
content."
+     * @netui.tldx:attribute
+     */
+    public void setPlacement(String placement)
+    {
+        if (placement.equals("after"))
+            _placement = Placement.AFTER;
+        else if (placement.equals("before"))
+            _placement = Placement.BEFORE;
+        else
+            _placement = Placement.INLINE;
+    }
+
+    /**
+     * Render the content.
+     * @throws javax.servlet.jsp.JspException if a JSP exception has occurred
+     */
+    public void doTag()
+            throws JspException, IOException
+    {
+        // report any errors...
+        if (hasErrors()) {
+            reportErrors();
+            return;
+        }
+
+        PageContext pageContext = getPageContext();
+        WriteRenderAppender writer = new WriteRenderAppender(pageContext);
+        String script = getBufferBody(false);
+
+        IScriptReporter sr = getScriptReporter();
+
+        // if we are writting the javaScript inline then do it....
+       if (_placement == Placement.INLINE || sr == null) {
+            JavaScriptUtils.writeScriptBlock(writer,script);
+            return;
+        }
+
+         boolean after =  (_placement == Placement.AFTER);
+        sr.placeScriptCode(script,after);
+    }
+}

Modified: 
incubator/beehive/trunk/netui/src/tags-html/org/apache/beehive/netui/tags/html/ScriptContainer.java
Url: 
http://svn.apache.org/viewcvs/incubator/beehive/trunk/netui/src/tags-html/org/apache/beehive/netui/tags/html/ScriptContainer.java?view=diff&rev=125711&p1=incubator/beehive/trunk/netui/src/tags-html/org/apache/beehive/netui/tags/html/ScriptContainer.java&r1=125710&p2=incubator/beehive/trunk/netui/src/tags-html/org/apache/beehive/netui/tags/html/ScriptContainer.java&r2=125711
==============================================================================
--- 
incubator/beehive/trunk/netui/src/tags-html/org/apache/beehive/netui/tags/html/ScriptContainer.java
 (original)
+++ 
incubator/beehive/trunk/netui/src/tags-html/org/apache/beehive/netui/tags/html/ScriptContainer.java
 Wed Jan 19 21:01:43 2005
@@ -69,6 +69,8 @@
     private String _idScope = null;
     private ArrayList _funcBlocks;
     private ArrayList _codeBlocks;
+    private ArrayList<String> _codeBefore;
+    private ArrayList<String> _codeAfter;
     private HashMap _initNames;
     private HashMap _idMap;
     private HashMap _idToNameMap;
@@ -120,6 +122,20 @@
         _codeBlocks.add(script);
     }
 
+    public void placeScriptCode(String script, boolean after)
+    {
+        if (after) {
+            if (_codeAfter == null)
+                _codeAfter = new ArrayList<String>();
+            _codeAfter.add(script);
+        }
+        else {
+            if (_codeBefore == null)
+                _codeBefore = new ArrayList<String>();
+            _codeBefore.add(script);
+        }
+    }
+
     /**
      * Adds a tagID and tagName to the Html's getId javascript function.
      * @param tagId   - the id of a child tag.
@@ -197,11 +213,14 @@
     {
         assert(sb != null) : "The paramter 'sb' must not be null;";
 
+        writeBeforeBlocks(sb);
+
         if (isRunAtClient()) {
             addInitCode();
             addAnchorFormRewriters();
         }
         writeScriptBlock(sb);
+        writeAfterBlocks(sb);
         _writeScript = true;
     }
 
@@ -298,6 +317,9 @@
     {
         String scope = getRealIdScope();
 
+        WriteRenderAppender writer = new WriteRenderAppender(pageContext);
+        writeBeforeBlocks(writer);
+
         // if there is a scopeId, then we need to create a div to contains 
everything
         if (_idScope != null) {
             write("<div");
@@ -317,6 +339,7 @@
     public int doEndTag()
             throws JspException
     {
+
         // Add the code to support the JavaScript framework
         addInitCode();
         addAnchorFormRewriters();
@@ -330,6 +353,7 @@
         }
 
         writeScriptBlock(writer);
+        writeAfterBlocks(writer);
         localRelease();
         return EVAL_PAGE;
     }
@@ -451,6 +475,33 @@
         addScriptFunction(script);
     }
 
+    protected void writeBeforeBlocks(AbstractRenderAppender sb)
+    {
+        if (_codeBefore == null || _codeBefore.size() == 0)
+            return;
+
+        StringBuilder s = new StringBuilder(256);
+        for (String code : _codeBefore) {
+            s.append(code);
+            s.append("\n");
+        }
+        JavaScriptUtils.writeScriptBlock(sb, s.toString());
+
+    }
+
+    protected void writeAfterBlocks(AbstractRenderAppender sb)
+    {
+        if (_codeAfter == null || _codeAfter.size() == 0)
+            return;
+
+        StringBuilder s = new StringBuilder(256);
+        for (String code : _codeAfter) {
+            s.append(code);
+            s.append("\n");
+        }
+        JavaScriptUtils.writeScriptBlock(sb, s.toString());
+    }
+
     /**
      * This will write the script block.
      */
@@ -580,6 +631,10 @@
             _funcBlocks.clear();
         if (_codeBlocks != null)
             _codeBlocks.clear();
+        if (_codeBefore != null)
+            _codeBefore.clear();
+        if (_codeAfter != null)
+            _codeAfter.clear();
         if (_initNames != null)
             _initNames.clear();
 

Added: 
incubator/beehive/trunk/netui/test/webapps/drt/coreWeb/coretags/sb/base/Controller.jpf
Url: 
http://svn.apache.org/viewcvs/incubator/beehive/trunk/netui/test/webapps/drt/coreWeb/coretags/sb/base/Controller.jpf?view=auto&rev=125711
==============================================================================
--- (empty file)
+++ 
incubator/beehive/trunk/netui/test/webapps/drt/coreWeb/coretags/sb/base/Controller.jpf
      Wed Jan 19 21:01:43 2005
@@ -0,0 +1,114 @@
+package coretags.sb.base;
+
+import javax.servlet.http.HttpSession;
+import org.apache.beehive.netui.pageflow.FormData;
+import org.apache.beehive.netui.pageflow.PageFlowController;
+import org.apache.beehive.netui.pageflow.Forward;
+import org.apache.beehive.netui.pageflow.annotations.Jpf;
+
+/**
+ * This is the default controller for a blank web application.
+ */
[EMAIL PROTECTED]
+public class Controller extends PageFlowController
+{
+    private String _text;
+    public String getText() {
+        return _text;
+    }
+
+    private String _method;
+    public String getMethod() {
+        return _method;
+    }
+
+    @Jpf.Action(
+        forwards={
+           @Jpf.Forward(name="index", path="index.jsp")
+        }
+    )
+    protected Forward begin()
+    {
+        return new Forward("index");
+    }
+
+
+    /**
+     * Callback that is invoked when this controller instance is created.
+     */
+    protected void onCreate()
+    {
+    }
+
+    /**
+     * Callback that is invoked when this controller instance is destroyed.
+     */
+    protected void onDestroy(HttpSession session)
+    {
+    }
+
+        
+    @Jpf.Action(forwards = { 
+        @Jpf.Forward(name = "success",
+                     path = "index.jsp")
+    })
+    protected Forward post(Bean form)
+    {
+        Forward forward = new Forward("success");
+        _text = form.getText1();
+        _method = "post";
+        return forward;
+    }
+
+    @Jpf.Action(forwards = { 
+        @Jpf.Forward(name = "success",
+                     path = "index.jsp")
+    })
+    protected Forward override(Bean form)
+    {
+        Forward forward = new Forward("success");
+        _text = form.getText1();
+        _method = "override";
+        return forward;
+    }
+                    
+    public static class Bean extends FormData
+    {
+        private String text1;
+        private String text2;
+        
+        public String getText1()
+        {
+            return text1;
+        }
+        
+        public void setText1(String value)
+        {
+            text1 = value;
+        }
+
+        public String getText2()
+        {
+            return text2;
+        }
+        
+        public void setText2(String value)
+        {
+            text2 = value;
+        }
+    }
+}
+
[EMAIL PROTECTED](value= {"<!-- This data is auto-generated. Hand-editing this 
section is not recommended. -->",
+"<view-properties>",
+"<pageflow-object id='pageflow:/coretags/imagebutton/id/Controller.jpf'/>",
+"<pageflow-object id='page:index.jsp'><property value='220' 
name='x'/><property value='100' name='y'/></pageflow-object>",
+"<pageflow-object id='formbean:Bean'/>",
+"<pageflow-object id='action:begin.do'><property value='80' 
name='x'/><property value='100' name='y'/></pageflow-object>",
+"<pageflow-object 
id='action:post.do#coretags.imagebutton.id.Controller.Bean'><property name='x' 
value='340'/><property name='y' value='100'/></pageflow-object>",
+"<pageflow-object id='forward:[EMAIL PROTECTED]:begin.do@'><property 
value='116,140,140,164' name='elbowsX'/><property value='92,92,92,92' 
name='elbowsY'/><property value='East_1' name='fromPort'/><property 
value='West_1' name='toPort'/><property value='index' 
name='label'/></pageflow-object>",
+"<pageflow-object id='action-call:@page:[EMAIL 
PROTECTED]@action:post.do#coretags.imagebutton.id.Controller.Bean@'><property 
name='elbowsX' value='256,280,280,304'/><property name='elbowsY' 
value='92,92,92,92'/><property name='fromPort' value='East_1'/><property 
name='toPort' value='West_1'/></pageflow-object>",
+"<pageflow-object id='forward:[EMAIL 
PROTECTED]:post.do#coretags.imagebutton.id.Controller.Bean@'><property 
name='elbowsX' value='304,280,280,256'/><property name='elbowsY' 
value='103,103,103,103'/><property name='fromPort' value='West_2'/><property 
name='toPort' value='East_2'/><property name='label' 
value='success'/></pageflow-object>",
+"</view-properties>"
+})
+interface VIEW_PROPERTIES { }

Added: 
incubator/beehive/trunk/netui/test/webapps/drt/coreWeb/coretags/sb/base/index.jsp
Url: 
http://svn.apache.org/viewcvs/incubator/beehive/trunk/netui/test/webapps/drt/coreWeb/coretags/sb/base/index.jsp?view=auto&rev=125711
==============================================================================
--- (empty file)
+++ 
incubator/beehive/trunk/netui/test/webapps/drt/coreWeb/coretags/sb/base/index.jsp
   Wed Jan 19 21:01:43 2005
@@ -0,0 +1,63 @@
+<%@ page language="java" contentType="text/html;charset=UTF-8"%>
+<%@ taglib prefix="netui" uri="http://beehive.apache.org/netui/tags-html-1.0"%>
+<%@ taglib prefix="netui-data" 
uri="http://beehive.apache.org/netui/tags-databinding-1.0"%>
+<%@ taglib prefix="netui-template" 
uri="http://beehive.apache.org/netui/tags-template-1.0"%>
+
+
+<netui:html idScope="html">
+    <head>
+        <netui:base/>
+    </head>
+    <netui:body>
+        <p style="color:green">Test of the ScriptContainer tag with a scopeId 
set on
+       it.
+        </p>
+        <span id="scopeOneSpan" />
+        <netui:form tagId="form" action="post" focus="b1">
+            <table>
+                <tr valign="top">
+                    <td>Text:</td>
+                    <td>
+                    <netui:textBox tagId="t1" dataSource="actionForm.text1" 
/><br>
+                    <netui:textBox tagId="t2" dataSource="actionForm.text2" />
+                    </td>
+                </tr>
+            </table>
+            <br>
+            <netui:button tagId="b1" value="Submit" />&nbsp;
+            <netui:button tagId="b2" value="Override" action="override"/>&nbsp;
+
+        </netui:form>
+        <hr>
+        Post Value: <netui:span tagId="postValue" 
value="${pageFlow.text}"/><br>
+        Method: <netui:span tagId="method" value="${pageFlow.method}"/>
+        <hr>
+        <p id="javaOut"></p>
+
+    <netui:scriptBlock placement="after">
+
+    var p = document.getElementById("javaOut");
+    var s = document.getElementById("scopeOneSpan");
+
+    var val = "<b>Document Access</b><br>";
+    val = val + "JavaScript lookup real name is null: <b>" +
+       (document["bean"] == null) + "</b><br>";
+    val = val + "Form by name is null: <b>" +
+        (document[lookupNameByTagId("form",s)] == null) + "</b><br>";
+
+
+    val = val + "<br><b>ScopeId value</b><br>";
+    val = val + "ScopeId: <b>" + getScopeId(s) + "</b><br>";
+
+    val = val + "<br><b>NetUI Access Methods</b><br>";
+    val = val + "TextBox 1 by id is null: <b>" +
+        (document.getElementById(lookupIdByTagId("t1",s)) == null) + 
"</b><br>";
+    val = val + "TextBox 1 by name is null: <b>" +
+        (document["bean"][lookupNameByTagId("t1",s)] == null) + "</b><br>";
+
+    p.innerHTML = val;
+    </netui:scriptBlock>
+    </netui:body>
+</netui:html>
+
+  
\ No newline at end of file

Reply via email to