Author: hlship
Date: Thu Nov  6 11:36:46 2008
New Revision: 711951

URL: http://svn.apache.org/viewvc?rev=711951&view=rev
Log:
TAP5-157: Add a LinkSubmit component, for submitting a Form via a clickable link

Added:
    
tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/corelib/components/LinkSubmit.java
    
tapestry/tapestry5/trunk/tapestry-core/src/main/resources/org/apache/tapestry5/corelib/components/linksubmit.js
    tapestry/tapestry5/trunk/tapestry-core/src/test/app1/LinkSubmitDemo.tml
    
tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/pages/LinkSubmitDemo.java
Modified:
    tapestry/tapestry5/trunk/src/site/apt/index.apt
    
tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/IntegrationTests.java
    
tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/pages/Start.java

Modified: tapestry/tapestry5/trunk/src/site/apt/index.apt
URL: 
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/src/site/apt/index.apt?rev=711951&r1=711950&r2=711951&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/src/site/apt/index.apt (original)
+++ tapestry/tapestry5/trunk/src/site/apt/index.apt Thu Nov  6 11:36:46 2008
@@ -68,6 +68,8 @@
 
 New And Of Note
 
+  * At long last, an official 
{{{tapestry-core/ref/org/apache/tapestry5/corelib/components/LinkSubmit.html}LinkSubmit}}
 component.
+
   * A {{{tapestry-ioc/injection.html}detailed guide to Injection}} has been 
added.
 
   * You can now configure Tapestry to move \<script\> links to the top of the 
page.

Added: 
tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/corelib/components/LinkSubmit.java
URL: 
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/corelib/components/LinkSubmit.java?rev=711951&view=auto
==============================================================================
--- 
tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/corelib/components/LinkSubmit.java
 (added)
+++ 
tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/corelib/components/LinkSubmit.java
 Thu Nov  6 11:36:46 2008
@@ -0,0 +1,142 @@
+//  Copyright 2008 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.
+
+package org.apache.tapestry5.corelib.components;
+
+import org.apache.tapestry5.*;
+import org.apache.tapestry5.annotations.Environmental;
+import org.apache.tapestry5.annotations.IncludeJavaScriptLibrary;
+import org.apache.tapestry5.annotations.Parameter;
+import org.apache.tapestry5.annotations.SupportsInformalParameters;
+import org.apache.tapestry5.ioc.annotations.Inject;
+import org.apache.tapestry5.services.FormSupport;
+import org.apache.tapestry5.services.Heartbeat;
+import org.apache.tapestry5.services.Request;
+
+/**
+ * Generates a client-side hyperlink that submits the enclosing form. If the 
link is clicked in the browser, the
+ * component will trigger an event ([EMAIL PROTECTED] EventConstants#SELECTED 
selected} by default) , just like [EMAIL PROTECTED]
+ * Submit}.
+ */
[EMAIL PROTECTED]
[EMAIL PROTECTED]("linksubmit.js")
+public class LinkSubmit implements ClientElement
+{
+    /**
+     * If true, then no link (or accompanying JavaScript) is written (though 
the body still is).
+     */
+    @Parameter
+    private boolean disabled;
+
+    /**
+     * The name of the event that will be triggered if this component is the 
cause of the form submission. The default
+     * is "selected".
+     */
+    @Parameter(allowNull = false, defaultPrefix = BindingConstants.LITERAL)
+    private String event = EventConstants.SELECTED;
+
+    /**
+     * If true (the default), then any notification sent by the component will 
be deferred until the end of the form
+     * submission (this is usually desirable).
+     */
+    @Parameter
+    private boolean defer = true;
+
+    @Inject
+    private ComponentResources resources;
+
+    @Inject
+    private RenderSupport renderSupport;
+
+    @Environmental
+    private FormSupport formSupport;
+
+    @Environmental
+    private Heartbeat heartbeat;
+
+    @Inject
+    private Request request;
+
+    private String clientId;
+
+    private static class ProcessSubmission implements 
ComponentAction<LinkSubmit>
+    {
+        private final String clientId;
+
+        public ProcessSubmission(String clientId)
+        {
+            this.clientId = clientId;
+        }
+
+        public void execute(LinkSubmit component)
+        {
+            component.processSubmission(clientId);
+        }
+    }
+
+    private void processSubmission(String clientId)
+    {
+        this.clientId = clientId;
+
+        String hiddenFieldName = this.clientId + ":hidden";
+
+        if (request.getParameter(hiddenFieldName) != null)
+        {
+            Runnable notification = new Runnable()
+            {
+                public void run()
+                {
+                    resources.triggerEvent(event, null, null);
+                }
+            };
+
+            if (defer)
+                formSupport.defer(notification);
+            else
+                heartbeat.defer(notification);
+        }
+    }
+
+    void beginRender(MarkupWriter writer)
+    {
+        if (!disabled)
+        {
+            clientId = renderSupport.allocateClientId(resources);
+
+            formSupport.store(this, new ProcessSubmission(clientId));
+
+            writer.element("a",
+                           "id", clientId,
+                           "href", "#");
+
+            resources.renderInformalParameters(writer);
+        }
+    }
+
+    void afterRender(MarkupWriter writer)
+    {
+        if (!disabled)
+        {
+            writer.end();
+
+            renderSupport.addInit("linkSubmit", formSupport.getClientId(), 
clientId);
+        }
+    }
+
+    public String getClientId()
+    {
+        return clientId;
+    }
+}
+

Added: 
tapestry/tapestry5/trunk/tapestry-core/src/main/resources/org/apache/tapestry5/corelib/components/linksubmit.js
URL: 
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/resources/org/apache/tapestry5/corelib/components/linksubmit.js?rev=711951&view=auto
==============================================================================
--- 
tapestry/tapestry5/trunk/tapestry-core/src/main/resources/org/apache/tapestry5/corelib/components/linksubmit.js
 (added)
+++ 
tapestry/tapestry5/trunk/tapestry-core/src/main/resources/org/apache/tapestry5/corelib/components/linksubmit.js
 Thu Nov  6 11:36:46 2008
@@ -0,0 +1,55 @@
+//  Copyright 2008 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.
+
+Tapestry.LinkSubmit = Class.create({
+
+    initialize: function(formId, clientId)
+    {
+        this.form = $(formId);
+        this.element = $(clientId);
+
+        this.element.observe("click", this.onClick.bindAsEventListener(this));
+    },
+
+    createHidden : function()
+    {
+        var hidden = new Element("input", { "type":"hidden",
+            "name": this.element.id + ":hidden",
+            "value": this.element.id});
+
+        this.element.insert({after:hidden});
+    },
+
+    onClick : function(event)
+    {
+        // Tapestry.debug("LinkSubmit #{id} clicked.", this.element);
+
+        Event.stop(event);
+
+        var onsubmit = this.form.onsubmit;
+
+        if (onsubmit == undefined || onsubmit.call(window.document, event))
+        {
+            this.createHidden();
+            this.form.submit();
+        }
+
+        return false;
+    }
+});
+
+Tapestry.Initializer.linkSubmit = function(formId, clientId)
+{
+    new Tapestry.LinkSubmit(formId, clientId);
+}
\ No newline at end of file

Added: tapestry/tapestry5/trunk/tapestry-core/src/test/app1/LinkSubmitDemo.tml
URL: 
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/test/app1/LinkSubmitDemo.tml?rev=711951&view=auto
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/test/app1/LinkSubmitDemo.tml 
(added)
+++ tapestry/tapestry5/trunk/tapestry-core/src/test/app1/LinkSubmitDemo.tml Thu 
Nov  6 11:36:46 2008
@@ -0,0 +1,21 @@
+<html t:type="border" 
xmlns:t="http://tapestry.apache.org/schema/tapestry_5_0_0.xsd";>
+    <h1>Link Submit Demo</h1>
+
+    <t:form>
+        <t:errors/>
+
+        <t:textfield t:id="name"/>
+        <br/>
+        <t:linksubmit t:id="fred">Fred</t:linksubmit>
+        <t:linksubmit t:id="barney" defer="true" 
event="neighbor">Barney</t:linksubmit>
+
+    </t:form>
+
+    <dl>
+        <dt>Name</dt>
+        <dd id="name-value">${name}</dd>
+        <dt>Last Clicked</dt>
+        <dd id="last-clicked">${lastClicked}</dd>
+    </dl>
+
+</html>
\ No newline at end of file

Modified: 
tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/IntegrationTests.java
URL: 
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/IntegrationTests.java?rev=711951&r1=711950&r2=711951&view=diff
==============================================================================
--- 
tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/IntegrationTests.java
 (original)
+++ 
tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/IntegrationTests.java
 Thu Nov  6 11:36:46 2008
@@ -2475,4 +2475,32 @@
 
         assertTextPresent("You must provide at least 20 characters for User 
Id.");
     }
+
+    /**
+     * TAP5-157
+     */
+    public void link_submit_component()
+    {
+        start("LinkSubmit Demo");
+
+        click("link=Fred");
+
+        
waitForCondition("selenium.browserbot.getCurrentWindow().$('name:errorpopup')",
+                         PAGE_LOAD_TIMEOUT);
+
+        assertTextPresent("You must provide a value for Name.");
+
+        type("name", "Wilma");
+
+        clickAndWait("link=Fred");
+
+        assertText("name-value", "Wilma");
+        assertText("last-clicked", "Fred");
+
+        type("name", "Betty");
+        clickAndWait("link=Barney");
+
+        assertText("name-value", "Betty");
+        assertText("last-clicked", "Barney");
+    }
 }

Added: 
tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/pages/LinkSubmitDemo.java
URL: 
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/pages/LinkSubmitDemo.java?rev=711951&view=auto
==============================================================================
--- 
tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/pages/LinkSubmitDemo.java
 (added)
+++ 
tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/pages/LinkSubmitDemo.java
 Thu Nov  6 11:36:46 2008
@@ -0,0 +1,35 @@
+//  Copyright 2008 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.
+
+package org.apache.tapestry5.integration.app1.pages;
+
+import org.apache.tapestry5.annotations.Persist;
+import org.apache.tapestry5.annotations.Property;
+import org.apache.tapestry5.beaneditor.Validate;
+
+public class LinkSubmitDemo
+{
+    @Property
+    @Persist
+    @Validate("required,minlength=5")
+    private String name;
+
+    @Property
+    @Persist
+    private String lastClicked;
+
+    void onSelectedFromFred() { lastClicked = "Fred"; }
+
+    void onNeighbor() { lastClicked = "Barney"; }
+}

Modified: 
tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/pages/Start.java
URL: 
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/pages/Start.java?rev=711951&r1=711950&r2=711951&view=diff
==============================================================================
--- 
tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/pages/Start.java
 (original)
+++ 
tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/pages/Start.java
 Thu Nov  6 11:36:46 2008
@@ -65,6 +65,8 @@
 
     private static final List<Item> ITEMS = CollectionFactory.newList(
 
+            new Item("LinkSubmitDemo", "LinkSubmit Demo", "JavaScript 
LinkSubmit component"),
+
             new Item("PerFormValidationMessageDemo", "Per-Form Validation 
Messages",
                      "Per-form configuration of validation messages and 
constraints."),
 


Reply via email to