01 
02 /*
03  *  JScripter Standard 1.0 - To Script In Java
04  *  Copyright (C) 2008-2011  J.J.Liu<jianjunliu@126.com> <http://www.jscripter.org>
05  *  
06  *  This program is free software: you can redistribute it and/or modify
07  *  it under the terms of the GNU Affero General Public License as published by
08  *  the Free Software Foundation, either version 3 of the License, or
09  *  (at your option) any later version.
10  *  
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU Affero General Public License for more details.
15  *  
16  *  You should have received a copy of the GNU Affero General Public License
17  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 package jsx.ui.ctrl;
21 
22 import js.Id;
23 import js.core.JsFunction;
24 import jsx.dom.Markups;
25 import jsx.ui.Component;
26 import jsx.ui.Control;
27 import jsx.ui.css.Pseudo;
28 import jsx.ui.html.Div;
29 
30 /**
31  * <p>A base class for menu widgets.</p>
32  * <p>A menu widget is basically a menu-item that has a menu-list that may hold other controls.</p>
33  * <p>A menu widget is firstly a {@link Clickable} that listens to high level events.</p>
34  * 
35  * @author <a href="mailto:jianjunliu@126.com">J.J.Liu (Jianjun Liu)</a> at <a href="http://www.jscripter.org" target="_blank">http://www.jscripter.org</a>
36  */
37 public class Menu extends Clickable
38 {
39     /**
40      * <p>Constructs a menu widget.</p>
41      * <p>This constructor invokes the super constructor. It sets up the menu as just a menu-item 
42      * by expanding the CSS sub selector "item" to the underlying component.</p>
43      * @param title The title text for the menu.
44      * @since 1.0
45      */
46     public Menu(String title) {
47         super(Markups.span(title));
48         Div<Component> d = new Div<Component>();
49         d.block();
50         ini(this).var(DIV, d);
51         Component.addClass(unwrap(), NOSELECT);
52         Component.addClasses(unwrap(), subs("item"));
53     }
54 
55     private final static Id<Div<Component>> DIV = new Id<Div<Component>>();
56 
57     /**
58      * <p>Appends a control to this menu.</p>
59      * <p>This method makes the menu to have a menu-list by removing the CSS sub selector "item" 
60      * from the underlying component.</p>
61      * @param c A control widget to append.
62      * @return The menu widget itself.
63      * @since 1.0
64      */
65     public Menu add(Control c) {
66         ini(this).var(DIV).add(c.unwrap());
67         Component.removeClasses(unwrap(), subs("item"));
68         return this;
69     }
70 
71     /**
72      * <p>Initializes the current widget for the newly rendered underlying HTML element.</p>
73      * <p>This method firstly invokes the overridden method of super class for it to behave like a
74      * clickable widget. It then renders the menu-list if existed and sets up necessary 
75      * {@link Pseudo} widgets for the menu behaviors.</p>
76      * @since 1.0
77      */
78     @Override
79     protected void init() {
80         super.init();
81         Component b = ini(this).var(DIV).unwrap();
82         Pseudo hide = b.pseudo(HIDE, "hide");
83         Component c = unwrap();
84         Component.appendChild(c, b);
85         hide.attach(c, "mouseout", "mouseover");
86         JsFunction<?> hider = hide.getAdder();
87         hider.invoke();
88         b.handle("click", hider);
89         c.handle("click", hider);
90     }
91 }