You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tapestry.apache.org by hl...@apache.org on 2013/01/13 03:18:32 UTC

[2/7] git commit: Add a ControlGroup mixin for Field components

Add a ControlGroup mixin for Field components

Adds the outer divs and label for a field to work well with Twitter Bootstrap


Project: http://git-wip-us.apache.org/repos/asf/tapestry-5/repo
Commit: http://git-wip-us.apache.org/repos/asf/tapestry-5/commit/bd0bd9e7
Tree: http://git-wip-us.apache.org/repos/asf/tapestry-5/tree/bd0bd9e7
Diff: http://git-wip-us.apache.org/repos/asf/tapestry-5/diff/bd0bd9e7

Branch: refs/heads/master
Commit: bd0bd9e7fde92863944307aac43cadb1624eca61
Parents: 2a55572
Author: Howard M. Lewis Ship <hl...@apache.org>
Authored: Fri Jan 11 13:54:20 2013 -0500
Committer: Howard M. Lewis Ship <hl...@apache.org>
Committed: Fri Jan 11 13:56:11 2013 -0500

----------------------------------------------------------------------
 54_RELEASE_NOTES.txt                               |    5 +
 .../tapestry5/corelib/mixins/ControlGroup.java     |   64 +++++++++++++++
 tapestry-core/src/test/app1/AutocompleteDemo.tml   |    9 +--
 tapestry-core/src/test/app1/SelectZoneDemo.tml     |   18 +---
 .../tapestry5/integration/app1/MiscTests.groovy    |    9 ++
 5 files changed, 84 insertions(+), 21 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/bd0bd9e7/54_RELEASE_NOTES.txt
----------------------------------------------------------------------
diff --git a/54_RELEASE_NOTES.txt b/54_RELEASE_NOTES.txt
index 099155e..5d0f13a 100644
--- a/54_RELEASE_NOTES.txt
+++ b/54_RELEASE_NOTES.txt
@@ -34,6 +34,11 @@ to running initialization functions, deals with DOM updates better, and is overa
 Tapestry is moving away from "magic class names" to a more uniform approach based on the use of HTML 5 compliant "data-"
 attributes.
 
+## ControlGroup Mixin
+
+This new mixin for Field components adds the outer <div> and <label> elements for a Field to layout correctly
+inside a Twitter Bootstrap form.
+
 # Breaking Changes:
 
 ## RenderSupport Removed

http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/bd0bd9e7/tapestry-core/src/main/java/org/apache/tapestry5/corelib/mixins/ControlGroup.java
----------------------------------------------------------------------
diff --git a/tapestry-core/src/main/java/org/apache/tapestry5/corelib/mixins/ControlGroup.java b/tapestry-core/src/main/java/org/apache/tapestry5/corelib/mixins/ControlGroup.java
new file mode 100644
index 0000000..e0f8296
--- /dev/null
+++ b/tapestry-core/src/main/java/org/apache/tapestry5/corelib/mixins/ControlGroup.java
@@ -0,0 +1,64 @@
+// Copyright (c) 2013. 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.mixins;
+
+import org.apache.tapestry5.Field;
+import org.apache.tapestry5.MarkupWriter;
+import org.apache.tapestry5.annotations.HeartbeatDeferred;
+import org.apache.tapestry5.annotations.InjectContainer;
+import org.apache.tapestry5.dom.Element;
+
+/**
+ * Applied to a {@link org.apache.tapestry5.Field}, this provides the outer layers of Bootstrap:
+ * an outer {@code <div class="control-group">}, a {@code <label class="control-label">}, and a
+ * {@code <div class="controls">} around the field itself. This control is not appropriate
+ * for radio buttons or check boxes, as those want to have the label element directly around the control.
+ * As with the {@link org.apache.tapestry5.corelib.components.Label} component, the {@code for} attribute is set (after the field itself
+ * renders).
+ *
+ * @tapestrydoc
+ * @since 5.4
+ */
+public class ControlGroup
+{
+
+    @InjectContainer
+    private Field field;
+
+    private Element label;
+
+    void beginRender(MarkupWriter writer)
+    {
+        writer.element("div", "class", "control-group");
+        label = writer.element("label", "class", "control-label");
+        writer.end();
+        writer.element("div", "class", "controls");
+
+        fillInLabelAttributes();
+    }
+
+    @HeartbeatDeferred
+    void fillInLabelAttributes()
+    {
+        label.attribute("for", field.getClientId());
+        label.text(field.getLabel());
+    }
+
+    void afterRender(MarkupWriter writer)
+    {
+        writer.end(); // div.controls
+        writer.end(); // div.control-group
+    }
+}

http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/bd0bd9e7/tapestry-core/src/test/app1/AutocompleteDemo.tml
----------------------------------------------------------------------
diff --git a/tapestry-core/src/test/app1/AutocompleteDemo.tml b/tapestry-core/src/test/app1/AutocompleteDemo.tml
index 831e42f..226b1f3 100644
--- a/tapestry-core/src/test/app1/AutocompleteDemo.tml
+++ b/tapestry-core/src/test/app1/AutocompleteDemo.tml
@@ -1,16 +1,11 @@
-<t:border xmlns:t="http://tapestry.apache.org/schema/tapestry_5_0_0.xsd">
+<t:border xmlns:t="http://tapestry.apache.org/schema/tapestry_5_3.xsd">
     <h1>Autocomplete Mixin Demo</h1>
 
     <t:form class="form-horizontal">
 
         <t:errors/>
 
-        <div class="control-group">
-            <t:label  for="title"/>
-            <div class="controls">
-                <t:textfield t:id="title" t:mixins="autocomplete" tokens=",;" size="60"/>
-            </div>
-        </div>
+        <t:textfield t:id="title" t:mixins="autocomplete,controlgroup" tokens=",;" size="60"/>
 
         <div class="form-actions">
             <input type="submit" class="btn btn-primary" value="Show Track"/>

http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/bd0bd9e7/tapestry-core/src/test/app1/SelectZoneDemo.tml
----------------------------------------------------------------------
diff --git a/tapestry-core/src/test/app1/SelectZoneDemo.tml b/tapestry-core/src/test/app1/SelectZoneDemo.tml
index d291b74..6928ae6 100644
--- a/tapestry-core/src/test/app1/SelectZoneDemo.tml
+++ b/tapestry-core/src/test/app1/SelectZoneDemo.tml
@@ -12,14 +12,9 @@
     <t:form class="well">
         <t:errors/>
 
-        <div class="control-group">
-            <t:label for="carMaker"/>
-            <div class="controls">
-                <t:select t:id="carMaker" validate="required"
-                          model="makeModel" zone="modelZone" encoder="makeEncoder"/>
+        <t:select t:id="carMaker" validate="required" t:mixins="controlgroup"
+                  model="makeModel" zone="modelZone" encoder="makeEncoder"/>
 
-            </div>
-        </div>
 
         <t:zone t:id="modelZone">
             <t:if test="carMaker">
@@ -28,13 +23,8 @@
         </t:zone>
 
         <t:block id="modelBlock">
-            <div class="control-group">
-                <t:label for="carModel"/>
-                <div class="controls">
-
-                    <t:select t:id="carModel" model="availableModels" validate="required" blankOption="always"/>
-                </div>
-            </div>
+            <t:select t:id="carModel" model="availableModels" validate="required" blankOption="always"
+                      t:mixins="controlgroup"/>
         </t:block>
 
         <div class="form-actions">

http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/bd0bd9e7/tapestry-core/src/test/groovy/org/apache/tapestry5/integration/app1/MiscTests.groovy
----------------------------------------------------------------------
diff --git a/tapestry-core/src/test/groovy/org/apache/tapestry5/integration/app1/MiscTests.groovy b/tapestry-core/src/test/groovy/org/apache/tapestry5/integration/app1/MiscTests.groovy
index 1bd6c4f..e57ae5a 100644
--- a/tapestry-core/src/test/groovy/org/apache/tapestry5/integration/app1/MiscTests.groovy
+++ b/tapestry-core/src/test/groovy/org/apache/tapestry5/integration/app1/MiscTests.groovy
@@ -23,4 +23,13 @@ class MiscTests extends TapestryCoreTestCase {
 
         assertAttribute "//meta[@name='tapestry-page-name']/@content", "nested/ZoneDemo"
     }
+
+    @Test
+    void ControlGroup_mixin() {
+        openLinks "Autocomplete Mixin Demo"
+
+        assertText "css=div.control-group > label", "Title"
+
+        // Using Geb, we could do a lot more. Sigh.
+    }
 }