From b26d294ea18a1216c15a0e96e338b22d723a1b02 Mon Sep 17 00:00:00 2001 From: rjy7 Date: Thu, 27 May 2010 22:12:24 +0000 Subject: [PATCH] NIHVIVO-517 Fix identification of active tab in main menu --- .../freemarker/FreeMarkerHttpServlet.java | 1 + .../controller/freemarker/UrlBuilder.java | 11 +++++---- .../vitro/webapp/view/menu/MainMenu.java | 16 ++++++------- .../vitro/webapp/view/menu/TabMenu.java | 24 +++++++++++++++---- 4 files changed, 36 insertions(+), 16 deletions(-) diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/freemarker/FreeMarkerHttpServlet.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/freemarker/FreeMarkerHttpServlet.java index 8beb8a154..b4c231928 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/freemarker/FreeMarkerHttpServlet.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/freemarker/FreeMarkerHttpServlet.java @@ -105,6 +105,7 @@ public class FreeMarkerHttpServlet extends VitroHttpServlet { // body template. portalId = portal.getPortalId(); root.put("portalId", portalId); + System.out.println("PORTAL ID: " + portalId); appName = portal.getAppName(); setSharedVariable("siteName", appName); diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/freemarker/UrlBuilder.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/freemarker/UrlBuilder.java index 085d101a8..4d463313c 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/freemarker/UrlBuilder.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/freemarker/UrlBuilder.java @@ -127,14 +127,17 @@ public class UrlBuilder { } public static String getUrl(String path, Params params) { - - String url = getUrl(path); + path = getPath(path, params); + return getUrl(path); + } + + public static String getPath(String path, Params params) { String glue = "?"; for (String key : params.keySet()) { - url += glue + key + "=" + urlEncode(params.get(key)); + path += glue + key + "=" + urlEncode(params.get(key)); glue = "&"; } - return url; + return path; } public static String urlEncode(String url) { diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/view/menu/MainMenu.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/view/menu/MainMenu.java index e841f47c1..c38882b4f 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/view/menu/MainMenu.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/view/menu/MainMenu.java @@ -24,16 +24,16 @@ public class MainMenu extends Menu { } public void addItem(String text, String path) { - boolean active = isActiveItem(path); - MainMenuItem i = new MainMenuItem(text, path, active); - items.add(i); + boolean isActive = isActiveItem(path); + addItem(text, path, isActive); } + + public void addItem(String text, String path, boolean isActive) { + MainMenuItem i = new MainMenuItem(text, path, isActive); + items.add(i); + } - // RY NEED TO FIX: doesn't work for Home and other generated tabs: - // vreq.getServletPath() = /templates/page/basicPage.jsp - // vreq.getRequestURL() = http://localhost:8080/vivo/templates/page/basicPage.jsp - // vreq.getRequestURI() = /vivo/templates/page/basicPage.jsp - private boolean isActiveItem(String path) { + protected boolean isActiveItem(String path) { return vreq.getServletPath().equals(path); } } diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/view/menu/TabMenu.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/view/menu/TabMenu.java index e09c74832..d3be7f761 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/view/menu/TabMenu.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/view/menu/TabMenu.java @@ -10,7 +10,8 @@ import org.apache.commons.logging.LogFactory; import edu.cornell.mannlib.vitro.webapp.beans.Tab; import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest; - +import edu.cornell.mannlib.vitro.webapp.controller.freemarker.UrlBuilder; +import edu.cornell.mannlib.vitro.webapp.controller.freemarker.UrlBuilder.Params; import edu.cornell.mannlib.vitro.webapp.web.TabWebUtil; /** A main menu constructed from persisted tab data @@ -22,6 +23,9 @@ public class TabMenu extends MainMenu { private static final long serialVersionUID = 1L; private static final Log log = LogFactory.getLog(TabMenu.class.getName()); + + private static String TAB_PARAM = "primary"; + private static String PATH = "/index.jsp"; public TabMenu(VitroRequest vreq, int portalId) { super(vreq); @@ -37,14 +41,26 @@ public class TabMenu extends MainMenu { Tab tab; while (primaryTabIterator.hasNext()) { tab = (Tab) primaryTabIterator.next(); - addItem(tab.getTitle(), "/index.jsp?primary=" + tab.getTabId()); + addItem(tab); // RY Also need to loop through nested tab levels, but not doing that now. } - // Hard-coded tabs. It's not really a good idea to have these here, since any menu item that doesn't + // Hard-coded tabs. It's not a good idea to have these here, since any menu item that doesn't // come from the db should be accessible to the template to change the text. But we need them here - // to apply the "active" mechanism. + // (rather than adding directly from the template) to apply the "active" mechanism. addItem("Index", "/browse"); } + + private void addItem(Tab tab) { + boolean isActive = isActiveItem(tab); + String text = tab.getTitle(); + String path = UrlBuilder.getPath(PATH, new Params(TAB_PARAM, "" + tab.getTabId())); + addItem(text, path, isActive); + } + + private boolean isActiveItem(Tab tab) { + String requestedTabId = vreq.getParameter(TAB_PARAM); + return requestedTabId != null && Integer.parseInt(requestedTabId) == tab.getTabId(); + } }