001 /*
002 * Apache License
003 * Version 2.0, January 2004
004 * http://www.apache.org/licenses/
005 *
006 * Copyright 2008-2010 by chenillekit.org
007 *
008 * Licensed under the Apache License, Version 2.0 (the "License");
009 * you may not use this file except in compliance with the License.
010 * You may obtain a copy of the License at
011 *
012 * http://www.apache.org/licenses/LICENSE-2.0
013 */
014
015 package org.chenillekit.tapestry.core.components;
016
017 import org.apache.tapestry5.BindingConstants;
018 import org.apache.tapestry5.ClientElement;
019 import org.apache.tapestry5.ComponentResources;
020 import org.apache.tapestry5.MarkupWriter;
021 import org.apache.tapestry5.annotations.Environmental;
022 import org.apache.tapestry5.annotations.Import;
023 import org.apache.tapestry5.annotations.Parameter;
024 import org.apache.tapestry5.annotations.SupportsInformalParameters;
025 import org.apache.tapestry5.ioc.annotations.Inject;
026 import org.apache.tapestry5.services.javascript.JavaScriptSupport;
027
028 /**
029 * diplays a panel that can open/close its content.
030 *
031 * @version $Id: SlidingPanel.java 674 2010-07-29 12:47:25Z homburgs $
032 */
033 @SupportsInformalParameters
034 @Import(library = {"../Chenillekit.js", "SlidingPanel.js"}, stylesheet = {"SlidingPanel.css"})
035 public class SlidingPanel implements ClientElement
036 {
037 /**
038 * The id used to generate a page-unique client-side identifier for the component. If a component renders multiple
039 * times, a suffix will be appended to the to id to ensure uniqueness.
040 */
041 @Parameter(value = "prop:componentResources.id", defaultPrefix = BindingConstants.LITERAL)
042 private String clientId;
043
044 /**
045 * the panel subject.
046 */
047 @Parameter(value = "", required = false, defaultPrefix = BindingConstants.LITERAL)
048 private String subject;
049
050 /**
051 * show the panel initialy closed or open.
052 */
053 @Parameter(value = "false", required = false)
054 private boolean closed;
055
056 /**
057 * the panel options (e.g. {duration:2.5}).
058 * <p/>
059 * look at http://github.com/madrobby/scriptaculous/wikis/core-effects for possible values.
060 */
061 @Parameter(value = "", required = false, defaultPrefix = BindingConstants.LITERAL)
062 private String options;
063
064 @Inject
065 private ComponentResources resources;
066
067 @Environmental
068 private JavaScriptSupport javascriptSupport;
069
070 private String assignedClientId;
071
072 void setupRender()
073 {
074 assignedClientId = javascriptSupport.allocateClientId(clientId);
075 }
076
077 /**
078 * Tapestry render phase method.
079 * Start a tag here, end it in afterRender
080 */
081 void beginRender(MarkupWriter writer)
082 {
083 writer.element("div", "id", getClientId(), "class", "ck_slidingpanel");
084 resources.renderInformalParameters(writer);
085
086 writer.element("div", "id", getClientId() + "_subject", "class", "ck_slidingPanelSubject");
087 writer.element("div", "id", getClientId() + "_toggler", "class", "ck_slidingPanelSubject-toggler");
088 writer.end(); // Tag 'toggler'
089 writer.write(subject);
090 writer.end(); // Tag 'div.subject'
091
092 writer.element("div", "id", getClientId() + "_content", "class", "ck_slidingPanelContent", "style", "display: none;");
093 writer.element("span");
094 }
095
096 /**
097 * Tapestry render phase method. End a tag here.
098 */
099 void afterRender(MarkupWriter writer)
100 {
101 writer.end(); // Tag 'div.outer_panel'
102 writer.end(); // Tag 'div.outer_panel'
103 writer.end(); // main div
104 javascriptSupport.addScript("new Ck.SlidingPanel('%s', %s, %s);", getClientId(), closed, options);
105 }
106
107 /**
108 * Returns a unique id for the element. This value will be unique for any given rendering of a
109 * page. This value is intended for use as the id attribute of the client-side element, and will
110 * be used with any DHTML/Ajax related JavaScript.
111 */
112 public String getClientId()
113 {
114 return assignedClientId;
115 }
116 }