1 /**
2 * This file is part of the equanda project.
3 *
4 * The contents of this file are subject to the Mozilla Public License Version 1.1 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at http://www.mozilla.org/MPL/
7 *
8 * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF
9 * ANY KIND, either express or implied. See the License for the specific language governing rights and
10 * limitations under the License.
11 *
12 * Alternatively, the contents of this file may be used under the terms of
13 * either the GNU General Public License Version 2 or later (the "GPL"), or
14 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
15 * in which case the provisions of the GPL or the LGPL are applicable instead
16 * of those above. If you wish to allow use of your version of this file only
17 * under the terms of either the GPL or the LGPL, and not to allow others to
18 * use your version of this file under the terms of the MPL, indicate your
19 * decision by deleting the provisions above and replace them with the notice
20 * and other provisions required by the GPL or the LGPL. If you do not delete
21 * the provisions above, a recipient may use your version of this file under
22 * the terms of any one of the MPL, the GPL or the LGPL.
23 */
24
25 package org.equanda.tapestry5.components;
26
27 import org.apache.tapestry5.*;
28 import org.apache.tapestry5.annotations.Environmental;
29 import org.apache.tapestry5.annotations.Parameter;
30 import org.apache.tapestry5.annotations.SupportsInformalParameters;
31 import org.apache.tapestry5.dom.Element;
32 import org.apache.tapestry5.ioc.annotations.Inject;
33 import org.apache.tapestry5.json.JSONObject;
34 import org.apache.tapestry5.services.ClientBehaviorSupport;
35
36 /**
37 * ...
38 *
39 * @author <a href="mailto:joachim@progs.be">Joachim Van der Auwera</a>
40 */
41 @SupportsInformalParameters
42 public class Zone
43 implements ClientElement
44 {
45 /**
46 * Name of a function on the client-side Tapestry.ElementEffect object that is invoked to make the Zone's
47 * <div> visible before being updated. If not specified, then the basic "show" method is used.
48 */
49 @Parameter( defaultPrefix = BindingConstants.LITERAL )
50 private String show;
51
52 /**
53 * Name of a function on the client-side Tapestry.ElementEffect object that is invoked after the Zone's content has
54 * been updated. If not specified, then the basic "highlight" method is used, which performs a classic "yellow fade"
55 * to indicate to the user that and update has taken place.
56 */
57 @Parameter( defaultPrefix = BindingConstants.LITERAL )
58 private String update;
59
60 /**
61 * If bound, then the id attribute of the rendered element will be this exact value. If not bound, then a unique id
62 * is generated for the element.
63 */
64 @Parameter( name = "id", defaultPrefix = BindingConstants.LITERAL )
65 private String clientId;
66 private boolean clientIdInitialized;
67
68 @Environmental
69 private RenderSupport renderSupport;
70
71 @Environmental
72 private ClientBehaviorSupport clientBehaviorSupport;
73
74 /**
75 * If true (the default) then the zone will render normally. If false, then the "t-invisible" CSS class is added,
76 * which will make the zone initially invisible.
77 */
78 @Parameter
79 private boolean visible = true;
80
81 @Inject
82 private ComponentResources resources;
83
84 void beginRender( MarkupWriter writer )
85 {
86 Element e = writer.element( "div", "id", getClientId() );
87
88 resources.renderInformalParameters( writer );
89
90 e.addClassName( "t-zone" );
91
92 if ( !visible ) e.addClassName( "t-invisible" );
93
94 // And continue on to render the body
95
96 JSONObject spec = new JSONObject();
97 spec.put( "div", getClientId() );
98
99 clientBehaviorSupport.addZone( getClientId(), show, update );
100 }
101
102 void afterRender( MarkupWriter writer )
103 {
104 writer.end(); // div
105 }
106
107 public String getClientId()
108 {
109 if ( !clientIdInitialized || null == clientId || clientId.length() == 0 )
110 {
111 if ( !resources.isBound( "id" ) )
112 {
113 clientId = renderSupport.allocateClientId( resources );
114 }
115 else
116 {
117 clientId = renderSupport.allocateClientId( clientId );
118 }
119 clientIdInitialized = true;
120 }
121 return clientId;
122 }
123
124 /**
125 * Returns the zone's body (the content enclosed by its start and end tags). This is often used as part of an Ajax
126 * partial page render to update the client with a fresh render of the content inside the zone.
127 *
128 * @return the zone's body as a Block
129 */
130 public Block getBody()
131 {
132 return resources.getBody();
133 }
134 }