com.hfg.javascript.PopupMenuJS Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of com_hfg Show documentation
Show all versions of com_hfg Show documentation
com.hfg xml, html, svg, and bioinformatics utility library
package com.hfg.javascript;
import java.awt.Color;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.hfg.html.HTML;
import com.hfg.html.HTMLTag;
import com.hfg.html.Link;
import com.hfg.html.attribute.HTMLColor;
import com.hfg.util.StringBuilderPlus;
import com.hfg.util.StringUtil;
import com.hfg.xml.XMLUtil;
//------------------------------------------------------------------------------
/**
Widget for creating a popup menu that can be used in an href attribute (a link
or an Area object) or an onclick() handler.
See this page for examples.
HTMLDoc doc = new HTMLDoc();
doc.setDoctype(Doctype.XHTML_1_0_TRANSITIONAL);
HTML html = new HTML("PopupMenuJS Test");
doc.setRootTag(html);
Body body = html.getBody();
body.br(3);
// Add the CSS classes needed
html.getHead().addStyle(PopupMenuJS.generateCommonCSS());
// Add the javascript needed
html.getHead().addJavascript(PopupMenuJS.generateCommonJavascript());
PopupMenuJS menu3 = new PopupMenuJS();
menu3.setMenuTitle("Test Menu #2");
menu3.addLink(new Link("http://localhost.com/URL7", "Nifty Site"));
menu3.addLink(new Link("URL8", "Click Me!"));
menu3.addLink(new Link());
menu3.addLink(new Link("javascript:alert('Hello World!');", "Hello World!"));
// Add the menu definition to the page's javascript
html.getHead().addJavascript(menu3.generateMenuJavascript());
// Attach the menu to the desired tag
menu3.attachToTag(body.addLink("javascript:void(0)", "Link Example"));
@author J. Alex Taylor, hairyfatguy.com
*/
//------------------------------------------------------------------------------
// com.hfg XML/HTML Coding Library
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com
// [email protected]
//------------------------------------------------------------------------------
public class PopupMenuJS
{
private String mMenuId;
private List mLinks;
private String mTitle;
private boolean mPin;
private static String sFont = "Arial,sans-serif";
private static String sFontSize = ".8em";
private static boolean sDefaultPin = false;
private static HTMLColor sMenuColor = new HTMLColor(Color.decode("#99ffff"));
private static HTMLColor sHeaderColor = new HTMLColor(Color.decode("#000066"));
private static String sCachedJs;
private static String sCachedCss;
private static int sIndex = 1;
//##########################################################################
// CONSTRUCTORS
//##########################################################################
//--------------------------------------------------------------------------
public PopupMenuJS()
{
super();
mMenuId = "_HfgPopup" + sIndex++;
setPin(sDefaultPin);
}
//##########################################################################
// PUBLIC METHODS
//##########################################################################
//--------------------------------------------------------------------------
public static void setFont(String inValue)
{
sFont = inValue;
sCachedCss = null;
}
//--------------------------------------------------------------------------
public static void setFontSize(String inValue)
{
sFont = inValue;
sCachedCss = null;
}
//--------------------------------------------------------------------------
public static void setDefaultPin(boolean inValue)
{
sDefaultPin = inValue;
}
//--------------------------------------------------------------------------
/**
Specify whether or not to 'pin' the menu once it is opened.
*/
public PopupMenuJS setPin(boolean inValue)
{
mPin = inValue;
return this;
}
//--------------------------------------------------------------------------
public static void setDefaultMenuColor(Color inValue)
{
sMenuColor = new HTMLColor(inValue);
sCachedCss = null;
}
//--------------------------------------------------------------------------
public static void setDefaultHeaderColor(Color inValue)
{
sHeaderColor = new HTMLColor(inValue);
sCachedCss = null;
}
//--------------------------------------------------------------------------
public static String generateCommonCSS()
{
if (null == sCachedCss)
{
Map tokenSubstitutionMap = new HashMap();
tokenSubstitutionMap.put("FONT", sFont);
tokenSubstitutionMap.put("FONT_SIZE", sFontSize);
tokenSubstitutionMap.put("HEADER_COLOR", sHeaderColor.toString());
tokenSubstitutionMap.put("MENU_COLOR", sMenuColor.toString());
sCachedCss = JsUtil.getJSResourceAsString("popupMenu.css", tokenSubstitutionMap);
}
return sCachedCss;
}
//--------------------------------------------------------------------------
/**
Generates the generic javascript methods for creating a popup menu.
*/
public static synchronized String generateCommonJavascript()
{
if (null == sCachedJs)
{
Map tokenSubstitutionMap = new HashMap();
sCachedJs = JsUtil.getJSResourceAsString("popupMenu.js", tokenSubstitutionMap);
}
return sCachedJs;
}
//--------------------------------------------------------------------------
public String getDisplayMenuJavascript()
{
return "javascript:hfg.js.PopupMenu.display(event, " + mMenuId + ");";
}
//--------------------------------------------------------------------------
/**
* Popup menus can be added to any tag which supports onmouseover and onmouseout.
* You will also need to add generateJS() to the javascript in the page's <head> tag.
* @param inTag the tag to which the onclick() or onmouseover() attribute will
* be set to display a menu.
*/
public void attachToTag(HTMLTag inTag)
{
String currentValue = inTag.getAttributeValue(HTML.ONCLICK);
inTag.setAttribute(HTML.ONCLICK,
(currentValue != null ? currentValue + ";" : "") + "hfg.js.PopupMenu.display(event, " + mMenuId + ")");
// currentValue = inTag.getAttributeValue(HTML.CLASS);
// inTag.setAttribute(HTML.CLASS, (currentValue != null ? currentValue + " " : "") + "hoverboard");
}
//--------------------------------------------------------------------------
public PopupMenuJS setMenuTitle(String inValue)
{
mTitle = inValue;
return this;
}
//--------------------------------------------------------------------------
/**
Add a link to the popup menu.
*/
public PopupMenuJS addLink(Link inLink)
{
if (inLink != null)
{
if (null == mLinks) mLinks = new ArrayList();
mLinks.add(inLink);
}
return this;
}
//--------------------------------------------------------------------------
/**
Add a divider to the popup menu.
*/
public PopupMenuJS addDivider()
{
if (null == mLinks) mLinks = new ArrayList();
mLinks.add(new Link());
return this;
}
//--------------------------------------------------------------------------
/**
Add a divider to the popup menu.
*/
public PopupMenuJS addDivider(String inTitle)
{
if (null == mLinks) mLinks = new ArrayList();
mLinks.add(new Link("", inTitle));
return this;
}
//---------------------------------------------------------------------------
public String generateMenuJavascript()
{
StringBuilderPlus buffer = new StringBuilderPlus();
/*
buffer.appendln("var " + mMenuId + " = [");
if (mTitle != null)
{
buffer.appendln("['title', " + StringUtil.singleQuote(XMLUtil.escapeApos(mTitle)) + "],");
}
buffer.appendln("['pin', " + (mPin ? "1" : "0") + "],");
if (mLinks != null)
{
for (int i = 0; i < mLinks.size(); i++)
{
Link link = mLinks.get(i);
if (StringUtil.isSet(link.getURL()))
{
buffer.append("['link', '" + link.getContent() + "', '" + link.getURL() + "']");
}
else
{
buffer.append("['divider'" + (StringUtil.isSet(link.getContent()) ? ", '" + link.getContent() + "'" : "") + "]");
}
buffer.appendln((i < mLinks.size() - 1 ? "," : ""));
}
}
buffer.appendln("]");
*/
buffer.append("var " + mMenuId + " = { 'config': {");
buffer.append("'pin': " + StringUtil.singleQuote(mPin ? "1" : "0"));
if (StringUtil.isSet(mTitle))
{
buffer.append(", 'title': " + StringUtil.singleQuote(XMLUtil.escapeApos(mTitle)));
}
buffer.appendln("}, 'content': [");
if (mLinks != null)
{
for (int i = 0; i < mLinks.size(); i++)
{
Link link = mLinks.get(i);
if (StringUtil.isSet(link.getURL()))
{
// buffer.append("['link', '" + link.getContent() + "', '" + link.getURL() + "']");
link = (Link) link.clone();
link.setContent("• " + link.getContent());
link.addClass("hfgmenu");
buffer.append("['link', '" + StringUtil.replaceAll(link.toHTML(), "'", "\\'") + "']");
}
else
{
buffer.append("['divider'" + (StringUtil.isSet(link.getContent()) ? ", '" + link.getContent() + "'" : "") + "]");
}
buffer.appendln((i < mLinks.size() - 1 ? "," : ""));
}
}
buffer.appendln("]}");
return buffer.toString();
}
}