| 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 Similar functionality of {@link goog.ui.MenuButtonRenderer}, |
| 17 |
* but inherits from {@link goog.ui.FlatButtonRenderer} instead of |
| 18 |
* {@link goog.ui.CustomButtonRenderer}. This creates a simpler menu button |
| 19 |
* that will look more like a traditional <select> menu. |
| 20 |
* |
| 21 |
*/ |
| 22 |
|
| 23 |
goog.provide('goog.ui.FlatMenuButtonRenderer'); |
| 24 |
|
| 25 |
goog.require('goog.dom'); |
| 26 |
goog.require('goog.dom.TagName'); |
| 27 |
goog.require('goog.style'); |
| 28 |
goog.require('goog.ui.FlatButtonRenderer'); |
| 29 |
goog.require('goog.ui.INLINE_BLOCK_CLASSNAME'); |
| 30 |
goog.require('goog.ui.Menu'); |
| 31 |
goog.require('goog.ui.MenuButton'); |
| 32 |
goog.require('goog.ui.MenuRenderer'); |
| 33 |
goog.require('goog.ui.registry'); |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
/** |
| 38 |
* Flat Menu Button renderer. Creates a simpler version of |
| 39 |
* {@link goog.ui.MenuButton} that doesn't look like a button and |
| 40 |
* doesn't have rounded corners. Uses just a `<div>` and looks more like |
| 41 |
* a traditional `<select>` element. |
| 42 |
* @constructor |
| 43 |
* @extends {goog.ui.FlatButtonRenderer} |
| 44 |
*/ |
| 45 |
goog.ui.FlatMenuButtonRenderer = function() { |
| 46 |
goog.ui.FlatButtonRenderer.call(this); |
| 47 |
}; |
| 48 |
goog.inherits(goog.ui.FlatMenuButtonRenderer, goog.ui.FlatButtonRenderer); |
| 49 |
goog.addSingletonGetter(goog.ui.FlatMenuButtonRenderer); |
| 50 |
|
| 51 |
|
| 52 |
/** |
| 53 |
* Default CSS class to be applied to the root element of components rendered |
| 54 |
* by this renderer. |
| 55 |
* @type {string} |
| 56 |
*/ |
| 57 |
goog.ui.FlatMenuButtonRenderer.CSS_CLASS = |
| 58 |
goog.getCssName('goog-flat-menu-button'); |
| 59 |
|
| 60 |
|
| 61 |
/** |
| 62 |
* Returns the button's contents wrapped in the following DOM structure: |
| 63 |
* |
| 64 |
* <div class="goog-inline-block goog-flat-menu-button"> |
| 65 |
* <div class="goog-inline-block goog-flat-menu-button-caption"> |
| 66 |
* Contents... |
| 67 |
* </div> |
| 68 |
* <div class="goog-inline-block goog-flat-menu-button-dropdown"> |
| 69 |
* |
| 70 |
* </div> |
| 71 |
* </div> |
| 72 |
* |
| 73 |
* Overrides {@link goog.ui.FlatButtonRenderer#createDom}. |
| 74 |
* @param {goog.ui.Control} control Button to render. |
| 75 |
* @return {!Element} Root element for the button. |
| 76 |
* @override |
| 77 |
*/ |
| 78 |
goog.ui.FlatMenuButtonRenderer.prototype.createDom = function(control) { |
| 79 |
var button = /** @type {goog.ui.Button} */ (control); |
| 80 |
var classNames = this.getClassNames(button); |
| 81 |
var element = button.getDomHelper().createDom( |
| 82 |
goog.dom.TagName.DIV, |
| 83 |
goog.ui.INLINE_BLOCK_CLASSNAME + ' ' + classNames.join(' '), [ |
| 84 |
this.createCaption(button.getContent(), button.getDomHelper()), |
| 85 |
this.createDropdown(button.getDomHelper()) |
| 86 |
]); |
| 87 |
this.setTooltip(element, /** @type {!string}*/ (button.getTooltip())); |
| 88 |
return element; |
| 89 |
}; |
| 90 |
|
| 91 |
|
| 92 |
/** |
| 93 |
* Takes the button's root element and returns the parent element of the |
| 94 |
* button's contents. |
| 95 |
* @param {Element} element Root element of the button whose content |
| 96 |
* element is to be returned. |
| 97 |
* @return {Element} The button's content element (if any). |
| 98 |
* @override |
| 99 |
*/ |
| 100 |
goog.ui.FlatMenuButtonRenderer.prototype.getContentElement = function(element) { |
| 101 |
return element && /** @type {Element} */ (element.firstChild); |
| 102 |
}; |
| 103 |
|
| 104 |
|
| 105 |
/** |
| 106 |
* Takes an element, decorates it with the menu button control, and returns |
| 107 |
* the element. Overrides {@link goog.ui.CustomButtonRenderer#decorate} by |
| 108 |
* looking for a child element that can be decorated by a menu, and if it |
| 109 |
* finds one, decorates it and attaches it to the menu button. |
| 110 |
* @param {goog.ui.Control} button Menu button to decorate the element. |
| 111 |
* @param {Element} element Element to decorate. |
| 112 |
* @return {Element} Decorated element. |
| 113 |
* @override |
| 114 |
*/ |
| 115 |
goog.ui.FlatMenuButtonRenderer.prototype.decorate = function(button, element) { |
| 116 |
// TODO(user): MenuButtonRenderer uses the exact same code. |
| 117 |
// Refactor this block to its own module where both can use it. |
| 118 |
var menuElem = goog.dom.getElementsByTagNameAndClass( |
| 119 |
'*', goog.ui.MenuRenderer.CSS_CLASS, element)[0]; |
| 120 |
if (menuElem) { |
| 121 |
// Move the menu element directly under the body, but hide it first; see |
| 122 |
// bug 1089244. |
| 123 |
goog.style.setElementShown(menuElem, false); |
| 124 |
button.getDomHelper().getDocument().body.appendChild(menuElem); |
| 125 |
|
| 126 |
// Decorate the menu and attach it to the button. |
| 127 |
var menu = new goog.ui.Menu(); |
| 128 |
menu.decorate(menuElem); |
| 129 |
button.setMenu(menu); |
| 130 |
} |
| 131 |
|
| 132 |
// Add the caption if it's not already there. |
| 133 |
var captionElem = goog.dom.getElementsByTagNameAndClass( |
| 134 |
'*', goog.getCssName(this.getCssClass(), 'caption'), element)[0]; |
| 135 |
if (!captionElem) { |
| 136 |
element.appendChild( |
| 137 |
this.createCaption(element.childNodes, button.getDomHelper())); |
| 138 |
} |
| 139 |
|
| 140 |
// Add the dropdown icon if it's not already there. |
| 141 |
var dropdownElem = goog.dom.getElementsByTagNameAndClass( |
| 142 |
'*', goog.getCssName(this.getCssClass(), 'dropdown'), element)[0]; |
| 143 |
if (!dropdownElem) { |
| 144 |
element.appendChild(this.createDropdown(button.getDomHelper())); |
| 145 |
} |
| 146 |
|
| 147 |
// Let the superclass do the rest. |
| 148 |
return goog.ui.FlatMenuButtonRenderer.superClass_.decorate.call( |
| 149 |
this, button, element); |
| 150 |
}; |
| 151 |
|
| 152 |
|
| 153 |
/** |
| 154 |
* Takes a text caption or existing DOM structure, and returns it wrapped in |
| 155 |
* an appropriately-styled DIV. Creates the following DOM structure: |
| 156 |
* |
| 157 |
* <div class="goog-inline-block goog-flat-menu-button-caption"> |
| 158 |
* Contents... |
| 159 |
* </div> |
| 160 |
* |
| 161 |
* @param {goog.ui.ControlContent} content Text caption or DOM structure to wrap |
| 162 |
* in a box. |
| 163 |
* @param {goog.dom.DomHelper} dom DOM helper, used for document interaction. |
| 164 |
* @return {Element} Caption element. |
| 165 |
*/ |
| 166 |
goog.ui.FlatMenuButtonRenderer.prototype.createCaption = function( |
| 167 |
content, dom) { |
| 168 |
return dom.createDom( |
| 169 |
goog.dom.TagName.DIV, goog.ui.INLINE_BLOCK_CLASSNAME + ' ' + |
| 170 |
goog.getCssName(this.getCssClass(), 'caption'), |
| 171 |
content); |
| 172 |
}; |
| 173 |
|
| 174 |
|
| 175 |
/** |
| 176 |
* Returns an appropriately-styled DIV containing a dropdown arrow element. |
| 177 |
* Creates the following DOM structure: |
| 178 |
* |
| 179 |
* <div class="goog-inline-block goog-flat-menu-button-dropdown"> |
| 180 |
* |
| 181 |
* </div> |
| 182 |
* |
| 183 |
* @param {goog.dom.DomHelper} dom DOM helper, used for document interaction. |
| 184 |
* @return {!Element} Dropdown element. |
| 185 |
*/ |
| 186 |
goog.ui.FlatMenuButtonRenderer.prototype.createDropdown = function(dom) { |
| 187 |
// 00A0 is |
| 188 |
return dom.createDom( |
| 189 |
goog.dom.TagName.DIV, { |
| 190 |
'class': goog.ui.INLINE_BLOCK_CLASSNAME + ' ' + |
| 191 |
goog.getCssName(this.getCssClass(), 'dropdown'), |
| 192 |
'aria-hidden': true |
| 193 |
}, |
| 194 |
'\u00A0'); |
| 195 |
}; |
| 196 |
|
| 197 |
|
| 198 |
/** |
| 199 |
* Returns the CSS class to be applied to the root element of components |
| 200 |
* rendered using this renderer. |
| 201 |
* @return {string} Renderer-specific CSS class. |
| 202 |
* @override |
| 203 |
*/ |
| 204 |
goog.ui.FlatMenuButtonRenderer.prototype.getCssClass = function() { |
| 205 |
return goog.ui.FlatMenuButtonRenderer.CSS_CLASS; |
| 206 |
}; |
| 207 |
|
| 208 |
|
| 209 |
// Register a decorator factory function for Flat Menu Buttons. |
| 210 |
goog.ui.registry.setDecoratorByClassName( |
| 211 |
goog.ui.FlatMenuButtonRenderer.CSS_CLASS, function() { |
| 212 |
// Uses goog.ui.MenuButton, but with FlatMenuButtonRenderer. |
| 213 |
return new goog.ui.MenuButton( |
| 214 |
null, null, goog.ui.FlatMenuButtonRenderer.getInstance()); |
| 215 |
}); |