Debian Codegraph @

closure-library/closure/goog/ui/submenurenderer.js

raw
1 // Copyright 2008 The Closure Library Authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS-IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 /**
16 * @fileoverview Renderer for {@link goog.ui.SubMenu}s.
17 *
18 */
19
20 goog.provide('goog.ui.SubMenuRenderer');
21
22 goog.require('goog.a11y.aria');
23 goog.require('goog.a11y.aria.State');
24 goog.require('goog.asserts');
25 goog.require('goog.dom');
26 goog.require('goog.dom.TagName');
27 goog.require('goog.dom.classlist');
28 goog.require('goog.style');
29 goog.require('goog.ui.Menu');
30 goog.require('goog.ui.MenuItemRenderer');
31
32
33
34 /**
35 * Default renderer for {@link goog.ui.SubMenu}s. Each item has the following
36 * structure:
37 *
38 * <div class="goog-submenu">
39 * ...(menuitem content)...
40 * <div class="goog-menu">
41 * ... (submenu content) ...
42 * </div>
43 * </div>
44 *
45 * @constructor
46 * @extends {goog.ui.MenuItemRenderer}
47 * @final
48 */
49 goog.ui.SubMenuRenderer = function() {
50 goog.ui.MenuItemRenderer.call(this);
51 };
52 goog.inherits(goog.ui.SubMenuRenderer, goog.ui.MenuItemRenderer);
53 goog.addSingletonGetter(goog.ui.SubMenuRenderer);
54
55
56 /**
57 * Default CSS class to be applied to the root element of components rendered
58 * by this renderer.
59 * @type {string}
60 */
61 goog.ui.SubMenuRenderer.CSS_CLASS = goog.getCssName('goog-submenu');
62
63
64 /**
65 * The CSS class for submenus that displays the submenu arrow.
66 * @type {string}
67 * @private
68 */
69 goog.ui.SubMenuRenderer.CSS_CLASS_SUBMENU_ =
70 goog.getCssName('goog-submenu-arrow');
71
72
73 /**
74 * Overrides {@link goog.ui.MenuItemRenderer#createDom} by adding
75 * the additional class 'goog-submenu' to the created element,
76 * and passes the element to {@link goog.ui.SubMenuItemRenderer#addArrow_}
77 * to add an child element that can be styled to show an arrow.
78 * @param {goog.ui.Control} control goog.ui.SubMenu to render.
79 * @return {!Element} Root element for the item.
80 * @override
81 */
82 goog.ui.SubMenuRenderer.prototype.createDom = function(control) {
83 var subMenu = /** @type {goog.ui.SubMenu} */ (control);
84 var element =
85 goog.ui.SubMenuRenderer.superClass_.createDom.call(this, subMenu);
86 goog.asserts.assert(element);
87 goog.dom.classlist.add(element, goog.ui.SubMenuRenderer.CSS_CLASS);
88 this.addArrow_(subMenu, element);
89 return element;
90 };
91
92
93 /**
94 * Overrides {@link goog.ui.MenuItemRenderer#decorate} by adding
95 * the additional class 'goog-submenu' to the decorated element,
96 * and passing the element to {@link goog.ui.SubMenuItemRenderer#addArrow_}
97 * to add a child element that can be styled to show an arrow.
98 * Also searches the element for a child with the class goog-menu. If a
99 * matching child element is found, creates a goog.ui.Menu, uses it to
100 * decorate the child element, and passes that menu to subMenu.setMenu.
101 * @param {goog.ui.Control} control goog.ui.SubMenu to render.
102 * @param {Element} element Element to decorate.
103 * @return {!Element} Root element for the item.
104 * @override
105 */
106 goog.ui.SubMenuRenderer.prototype.decorate = function(control, element) {
107 var subMenu = /** @type {goog.ui.SubMenu} */ (control);
108 element =
109 goog.ui.SubMenuRenderer.superClass_.decorate.call(this, subMenu, element);
110 goog.asserts.assert(element);
111 goog.dom.classlist.add(element, goog.ui.SubMenuRenderer.CSS_CLASS);
112 this.addArrow_(subMenu, element);
113
114 // Search for a child menu and decorate it.
115 var childMenuEls = goog.dom.getElementsByTagNameAndClass(
116 goog.dom.TagName.DIV, goog.getCssName('goog-menu'), element);
117 if (childMenuEls.length) {
118 var childMenu = new goog.ui.Menu(subMenu.getDomHelper());
119 var childMenuEl = childMenuEls[0];
120 // Hide the menu element before attaching it to the document body; see
121 // bug 1089244.
122 goog.style.setElementShown(childMenuEl, false);
123 subMenu.getDomHelper().getDocument().body.appendChild(childMenuEl);
124 childMenu.decorate(childMenuEl);
125 subMenu.setMenu(childMenu, true);
126 }
127 return element;
128 };
129
130
131 /**
132 * Takes a menu item's root element, and sets its content to the given text
133 * caption or DOM structure. Overrides the superclass immplementation by
134 * making sure that the submenu arrow structure is preserved.
135 * @param {Element} element The item's root element.
136 * @param {goog.ui.ControlContent} content Text caption or DOM structure to be
137 * set as the item's content.
138 * @override
139 */
140 goog.ui.SubMenuRenderer.prototype.setContent = function(element, content) {
141 // Save the submenu arrow element, if present.
142 var contentElement = this.getContentElement(element);
143 var arrowElement = contentElement && contentElement.lastChild;
144 goog.ui.SubMenuRenderer.superClass_.setContent.call(this, element, content);
145 // If the arrowElement was there, is no longer there, and really was an arrow,
146 // reappend it.
147 if (arrowElement && contentElement.lastChild != arrowElement &&
148 goog.dom.classlist.contains(
149 /** @type {!Element} */ (arrowElement),
150 goog.ui.SubMenuRenderer.CSS_CLASS_SUBMENU_)) {
151 contentElement.appendChild(arrowElement);
152 }
153 };
154
155
156 /**
157 * Overrides {@link goog.ui.MenuItemRenderer#initializeDom} to tweak
158 * the DOM structure for the span.goog-submenu-arrow element
159 * depending on the text direction (LTR or RTL). When the SubMenu is RTL
160 * the arrow will be given the additional class of goog-submenu-arrow-rtl,
161 * and the arrow will be moved up to be the first child in the SubMenu's
162 * element. Otherwise the arrow will have the class goog-submenu-arrow-ltr,
163 * and be kept as the last child of the SubMenu's element.
164 * @param {goog.ui.Control} control goog.ui.SubMenu whose DOM is to be
165 * initialized as it enters the document.
166 * @override
167 */
168 goog.ui.SubMenuRenderer.prototype.initializeDom = function(control) {
169 var subMenu = /** @type {goog.ui.SubMenu} */ (control);
170 goog.ui.SubMenuRenderer.superClass_.initializeDom.call(this, subMenu);
171 var element = subMenu.getContentElement();
172 var arrow = subMenu.getDomHelper().getElementsByTagNameAndClass(
173 goog.dom.TagName.SPAN, goog.ui.SubMenuRenderer.CSS_CLASS_SUBMENU_,
174 element)[0];
175 goog.ui.SubMenuRenderer.setArrowTextContent_(subMenu, arrow);
176 if (arrow != element.lastChild) {
177 element.appendChild(arrow);
178 }
179 var subMenuElement = subMenu.getElement();
180 goog.asserts.assert(
181 subMenuElement, 'The sub menu DOM element cannot be null.');
182 goog.a11y.aria.setState(
183 subMenuElement, goog.a11y.aria.State.HASPOPUP, 'true');
184 };
185
186
187 /**
188 * Appends a child node with the class goog.getCssName('goog-submenu-arrow') or
189 * 'goog-submenu-arrow-rtl' which can be styled to show an arrow.
190 * @param {goog.ui.SubMenu} subMenu SubMenu to render.
191 * @param {Element} element Element to decorate.
192 * @private
193 */
194 goog.ui.SubMenuRenderer.prototype.addArrow_ = function(subMenu, element) {
195 var arrow = subMenu.getDomHelper().createDom(goog.dom.TagName.SPAN);
196 arrow.className = goog.ui.SubMenuRenderer.CSS_CLASS_SUBMENU_;
197 goog.ui.SubMenuRenderer.setArrowTextContent_(subMenu, arrow);
198 this.getContentElement(element).appendChild(arrow);
199 };
200
201
202 /**
203 * The unicode char for a left arrow.
204 * @type {string}
205 * @private
206 */
207 goog.ui.SubMenuRenderer.LEFT_ARROW_ = '\u25C4';
208
209
210 /**
211 * The unicode char for a right arrow.
212 * @type {string}
213 * @private
214 */
215 goog.ui.SubMenuRenderer.RIGHT_ARROW_ = '\u25BA';
216
217
218 /**
219 * Set the text content of an arrow.
220 * @param {goog.ui.SubMenu} subMenu The sub menu that owns the arrow.
221 * @param {Element} arrow The arrow element.
222 * @private
223 */
224 goog.ui.SubMenuRenderer.setArrowTextContent_ = function(subMenu, arrow) {
225 // Fix arrow rtl
226 var leftArrow = goog.ui.SubMenuRenderer.LEFT_ARROW_;
227 var rightArrow = goog.ui.SubMenuRenderer.RIGHT_ARROW_;
228
229 goog.asserts.assert(arrow);
230
231 if (subMenu.isRightToLeft()) {
232 goog.dom.classlist.add(arrow, goog.getCssName('goog-submenu-arrow-rtl'));
233 // Unicode character - Black left-pointing pointer iff aligned to end.
234 goog.dom.setTextContent(
235 arrow, subMenu.isAlignedToEnd() ? leftArrow : rightArrow);
236 } else {
237 goog.dom.classlist.remove(arrow, goog.getCssName('goog-submenu-arrow-rtl'));
238 // Unicode character - Black right-pointing pointer iff aligned to end.
239 goog.dom.setTextContent(
240 arrow, subMenu.isAlignedToEnd() ? rightArrow : leftArrow);
241 }
242 };