Make <s:tabbedpanel> being able to remember last selected tab using a cookie

git-svn-id: https://svn.apache.org/repos/asf/struts/struts2/trunk@565264 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
René Gielen
2007-08-13 08:13:30 +00:00
parent 5e68db2e90
commit 8f411c1d12
7 changed files with 146 additions and 28 deletions
@@ -34,6 +34,11 @@ import com.opensymphony.xwork2.util.ValueStack;
* <!-- START SNIPPET: javadoc -->
* The tabbedpanel widget is primarily an AJAX component, where each tab can either be local content or remote
* content (refreshed each time the user selects that tab).</p>
* If the useSelectedTabCookie attribute is set to true, the id of the selected tab is saved in a cookie on activation.
* When coming back to this view, the cookie is read and the tab will be activated again, unless an actual value for the
* selectedTab attribute is specified.</p>
* If you want to use the cookie feature, please be sure that you provide a unique id for your tabbedpanel component,
* since this will also be the identifying name component of the stored cookie.</p>
* <!-- END SNIPPET: javadoc -->
*
* <p/> <b>Examples</b>
@@ -92,7 +97,8 @@ public class TabbedPanel extends ClosingUIBean {
protected String beforeSelectTabNotifyTopics;
protected String afterSelectTabNotifyTopics;
protected String disabledTabCssClass;
protected String useSelectedTabCookie;
public TabbedPanel(ValueStack stack, HttpServletRequest request, HttpServletResponse response) {
super(stack, request, response);
}
@@ -123,7 +129,10 @@ public class TabbedPanel extends ClosingUIBean {
addParameter("afterSelectTabNotifyTopics", findString(afterSelectTabNotifyTopics));
if (disabledTabCssClass!= null)
addParameter("disabledTabCssClass", findString(disabledTabCssClass));
if(useSelectedTabCookie != null) {
addParameter("useSelectedTabCookie", findString(useSelectedTabCookie));
}
}
@Override
@@ -195,4 +204,11 @@ public class TabbedPanel extends ClosingUIBean {
public void setDisabledTabCssClass(String disabledTabCssClass) {
this.disabledTabCssClass = disabledTabCssClass;
}
@StrutsTagAttribute(required = false, defaultValue = "false", description = "If set to true, the id of the last selected " +
"tab will be stored in cookie. If the view is rendered, it will be tried to read this cookie and activate " +
"the corresponding tab on success, unless overridden by the selectedTab attribute. The cookie name is \"Struts2TabbedPanel_selectedTab_\"+id.")
public void setUseSelectedTabCookie( String useSelectedTabCookie ) {
this.useSelectedTabCookie = useSelectedTabCookie;
}
}
@@ -42,8 +42,9 @@ public class TabbedPanelTag extends AbstractClosingTag {
private String templateCssPath;
private String beforeSelectTabNotifyTopics;
private String afterSelectTabNotifyTopics;
protected String disabledTabCssClass;
private String disabledTabCssClass;
private String useSelectedTabCookie;
public Component getBean(ValueStack stack, HttpServletRequest req, HttpServletResponse res) {
return new TabbedPanel(stack, req, res);
}
@@ -59,6 +60,7 @@ public class TabbedPanelTag extends AbstractClosingTag {
tabbedPanel.setBeforeSelectTabNotifyTopics(beforeSelectTabNotifyTopics);
tabbedPanel.setAfterSelectTabNotifyTopics(afterSelectTabNotifyTopics);
tabbedPanel.setDisabledTabCssClass(disabledTabCssClass);
tabbedPanel.setUseSelectedTabCookie(useSelectedTabCookie);
}
public void setSelectedTab(String selectedTab) {
@@ -88,4 +90,8 @@ public class TabbedPanelTag extends AbstractClosingTag {
public void setDisabledTabCssClass(String disabledTabCssClass) {
this.disabledTabCssClass = disabledTabCssClass;
}
public void setUseSelectedTabCookie( String useSelectedTabCookie ) {
this.useSelectedTabCookie = useSelectedTabCookie;
}
}
@@ -21,6 +21,30 @@
*/
-->
<link rel="stylesheet" href="${base}/struts/TabbedPanel.css" type="text/css"/>
<#if parameters.useSelectedTabCookie?exists && parameters.useSelectedTabCookie=="true">
<script type="text/javascript">
dojo.require("dojo.io.cookie");
dojo.addOnLoad (
function() {
var tabContainer = dojo.widget.byId("${parameters.escapedId?html}");
<#if !(parameters.selectedTab?if_exists != "")>
var selectedTab = dojo.io.cookie.getCookie("Struts2TabbedPanel_selectedTab_${parameters.escapedId?html}");
if (selectedTab) {
tabContainer.selectChild(selectedTab, tabContainer.correspondingPageButton);
}
</#if>
dojo.event.connect(tabContainer, "selectChild",
function (evt) {
dojo.io.cookie.setCookie("Struts2TabbedPanel_selectedTab_${parameters.escapedId?html}", evt.widgetId, 1, null, null, null);
}
)
}
);
</script>
</#if>
<div dojoType="struts:StrutsTabContainer"
<#if parameters.cssStyle?if_exists != "">
style="${parameters.cssStyle?html}"<#rt/>
@@ -0,0 +1,68 @@
/*
* $Id$
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.struts2.dojo.views.jsp.ui;
import org.apache.struts2.views.jsp.AbstractUITagTest;
/**
* TabbedPanelTagTest.
*/
public class TabbedPanelTagTest extends AbstractUITagTest {
public void testSimple() throws Exception {
TabbedPanelTag tag = new TabbedPanelTag();
tag.setPageContext(pageContext);
tag.doStartTag();
tag.doEndTag();
verify(TabbedPanelTag.class.getResource("TabbedPanel-1.txt"));
}
public void testCookieCodeAvailable() throws Exception {
TabbedPanelTag tag = new TabbedPanelTag();
tag.setPageContext(pageContext);
tag.setId("foo");
tag.setUseSelectedTabCookie("true");
tag.doStartTag();
tag.doEndTag();
verify(TabbedPanelTag.class.getResource("TabbedPanel-2.txt"));
}
public void testCookieCodeAvailableWithOverriddenSelectedTab() throws Exception {
TabbedPanelTag tag = new TabbedPanelTag();
tag.setPageContext(pageContext);
tag.setId("foo");
tag.setUseSelectedTabCookie("true");
tag.setSelectedTab("bar");
tag.doStartTag();
tag.doEndTag();
verify(TabbedPanelTag.class.getResource("TabbedPanel-3.txt"));
}
}
@@ -1,12 +1,5 @@
<script type="text/javascript">
dojo.require("dojo.widget.TabContainer");
dojo.require("dojo.widget.LinkPane");
dojo.require("dojo.widget.ContentPane");
</script>
<linkrel="stylesheet" href="/struts/TabbedPanel.css" type="text/css"/>
<div
dojoType="TabContainer"
selectedTab="a"
labelPosition="b"
closeButton="c"
doLayout="true">
</div>
dojoType="struts:StrutsTabContainer"
doLayout="false">
</div>
@@ -1,9 +1,17 @@
<linkrel="stylesheet" href="/struts/TabbedPanel.css" type="text/css"/>
<script type="text/javascript">
dojo.require("dojo.widget.TabContainer");
dojo.require("dojo.widget.LinkPane");
dojo.require("dojo.widget.ContentPane");
dojo.require("dojo.io.cookie");
dojo.addOnLoad(function(){
var tabContainer=dojo.widget.byId("foo");
var selectedTab=dojo.io.cookie.getCookie("Struts2TabbedPanel_selectedTab_foo");
if( selectedTab ){
tabContainer.selectChild(selectedTab,tabContainer.correspondingPageButton);
}
dojo.event.connect(tabContainer,"selectChild",function(evt){
dojo.io.cookie.setCookie("Struts2TabbedPanel_selectedTab_foo",evt.widgetId,1,null,null,null);
}
)
});
</script>
<div
dojoType="TabContainer"
doLayout="false">
<div dojoType="struts:StrutsTabContainer" id="foo" doLayout="false">
</div>
@@ -1,10 +1,13 @@
<linkrel="stylesheet" href="/struts/TabbedPanel.css" type="text/css"/>
<script type="text/javascript">
dojo.require("dojo.widget.TabContainer");
dojo.require("dojo.widget.LinkPane");
dojo.require("dojo.widget.ContentPane");
dojo.require("dojo.io.cookie");
dojo.addOnLoad(function(){
var tabContainer=dojo.widget.byId("foo");
dojo.event.connect(tabContainer,"selectChild",function(evt){
dojo.io.cookie.setCookie("Struts2TabbedPanel_selectedTab_foo",evt.widgetId,1,null,null,null);
}
)
});
</script>
<div
dojoType="TabContainer"
labelPosition="right-h"
doLayout="true">
<div dojoType="struts:StrutsTabContainer" id="foo" selectedTab="bar" doLayout="false">
</div>