Path: blob/trunk/third_party/closure/goog/ui/menubuttonrenderer.js
4101 views
/**1* @license2* Copyright The Closure Library Authors.3* SPDX-License-Identifier: Apache-2.04*/56/**7* @fileoverview Renderer for {@link goog.ui.MenuButton}s and subclasses.8*/910goog.provide('goog.ui.MenuButtonRenderer');1112goog.require('goog.dom');13goog.require('goog.dom.TagName');14goog.require('goog.style');15goog.require('goog.ui.CustomButtonRenderer');16goog.require('goog.ui.INLINE_BLOCK_CLASSNAME');17goog.require('goog.ui.Menu');18goog.require('goog.ui.MenuRenderer');19goog.requireType('goog.ui.Control');20goog.requireType('goog.ui.ControlContent');21goog.requireType('goog.ui.MenuButton');22232425/**26* Renderer for {@link goog.ui.MenuButton}s. This implementation overrides27* {@link goog.ui.CustomButtonRenderer#createButton} to create a separate28* caption and dropdown element.29* @constructor30* @extends {goog.ui.CustomButtonRenderer}31*/32goog.ui.MenuButtonRenderer = function() {33'use strict';34goog.ui.CustomButtonRenderer.call(this);35};36goog.inherits(goog.ui.MenuButtonRenderer, goog.ui.CustomButtonRenderer);37goog.addSingletonGetter(goog.ui.MenuButtonRenderer);383940/**41* Default CSS class to be applied to the root element of components rendered42* by this renderer.43* @type {string}44*/45goog.ui.MenuButtonRenderer.CSS_CLASS = goog.getCssName('goog-menu-button');464748/**49* Takes the button's root element and returns the parent element of the50* button's contents. Overrides the superclass implementation by taking51* the nested DIV structure of menu buttons into account.52* @param {Element} element Root element of the button whose content element53* is to be returned.54* @return {Element} The button's content element.55* @override56*/57goog.ui.MenuButtonRenderer.prototype.getContentElement = function(element) {58'use strict';59return goog.ui.MenuButtonRenderer.superClass_.getContentElement.call(60this,61/** @type {Element} */ (element && element.firstChild));62};636465/**66* Takes an element, decorates it with the menu button control, and returns67* the element. Overrides {@link goog.ui.CustomButtonRenderer#decorate} by68* looking for a child element that can be decorated by a menu, and if it69* finds one, decorates it and attaches it to the menu button.70* @param {goog.ui.Control} control goog.ui.MenuButton to decorate the element.71* @param {Element} element Element to decorate.72* @return {Element} Decorated element.73* @override74*/75goog.ui.MenuButtonRenderer.prototype.decorate = function(control, element) {76'use strict';77var button = /** @type {goog.ui.MenuButton} */ (control);78// TODO(attila): Add more robust support for subclasses of goog.ui.Menu.79var menuElem = goog.dom.getElementsByTagNameAndClass(80'*', goog.ui.MenuRenderer.CSS_CLASS, element)[0];81if (menuElem) {82// Move the menu element directly under the body (but hide it first to83// prevent flicker; see bug 1089244).84goog.style.setElementShown(menuElem, false);85goog.dom.appendChild(goog.dom.getOwnerDocument(menuElem).body, menuElem);8687// Decorate the menu and attach it to the button.88var menu = new goog.ui.Menu();89menu.decorate(menuElem);90button.setMenu(menu);91}9293// Let the superclass do the rest.94return goog.ui.MenuButtonRenderer.superClass_.decorate.call(95this, button, element);96};979899/**100* Takes a text caption or existing DOM structure, and returns the content and101* a dropdown arrow element wrapped in a pseudo-rounded-corner box. Creates102* the following DOM structure:103*104* <div class="goog-inline-block goog-menu-button-outer-box">105* <div class="goog-inline-block goog-menu-button-inner-box">106* <div class="goog-inline-block goog-menu-button-caption">107* Contents...108* </div>109* <div class="goog-inline-block goog-menu-button-dropdown">110* 111* </div>112* </div>113* </div>114*115* @param {goog.ui.ControlContent} content Text caption or DOM structure116* to wrap in a box.117* @param {goog.dom.DomHelper} dom DOM helper, used for document interaction.118* @return {!Element} Pseudo-rounded-corner box containing the content.119* @override120*/121goog.ui.MenuButtonRenderer.prototype.createButton = function(content, dom) {122'use strict';123return goog.ui.MenuButtonRenderer.superClass_.createButton.call(124this, [this.createCaption(content, dom), this.createDropdown(dom)], dom);125};126127128/**129* Takes a text caption or existing DOM structure, and returns it wrapped in130* an appropriately-styled DIV. Creates the following DOM structure:131*132* <div class="goog-inline-block goog-menu-button-caption">133* Contents...134* </div>135*136* @param {goog.ui.ControlContent} content Text caption or DOM structure137* to wrap in a box.138* @param {goog.dom.DomHelper} dom DOM helper, used for document interaction.139* @return {!Element} Caption element.140*/141goog.ui.MenuButtonRenderer.prototype.createCaption = function(content, dom) {142'use strict';143return goog.ui.MenuButtonRenderer.wrapCaption(144content, this.getCssClass(), dom);145};146147148/**149* Takes a text caption or existing DOM structure, and returns it wrapped in150* an appropriately-styled DIV. Creates the following DOM structure:151*152* <div class="goog-inline-block goog-menu-button-caption">153* Contents...154* </div>155*156* @param {goog.ui.ControlContent} content Text caption or DOM structure157* to wrap in a box.158* @param {string} cssClass The CSS class for the renderer.159* @param {goog.dom.DomHelper} dom DOM helper, used for document interaction.160* @return {!Element} Caption element.161*/162goog.ui.MenuButtonRenderer.wrapCaption = function(content, cssClass, dom) {163'use strict';164return dom.createDom(165goog.dom.TagName.DIV,166goog.ui.INLINE_BLOCK_CLASSNAME + ' ' +167goog.getCssName(cssClass, 'caption'),168content);169};170171172/**173* Returns an appropriately-styled DIV containing a dropdown arrow element.174* Creates the following DOM structure:175*176* <div class="goog-inline-block goog-menu-button-dropdown">177* 178* </div>179*180* @param {goog.dom.DomHelper} dom DOM helper, used for document interaction.181* @return {!Element} Dropdown element.182*/183goog.ui.MenuButtonRenderer.prototype.createDropdown = function(dom) {184'use strict';185// 00A0 is 186return dom.createDom(187goog.dom.TagName.DIV, goog.ui.INLINE_BLOCK_CLASSNAME + ' ' +188goog.getCssName(this.getCssClass(), 'dropdown'),189'\u00A0');190};191192193/**194* Returns the CSS class to be applied to the root element of components195* rendered using this renderer.196* @return {string} Renderer-specific CSS class.197* @override198*/199goog.ui.MenuButtonRenderer.prototype.getCssClass = function() {200'use strict';201return goog.ui.MenuButtonRenderer.CSS_CLASS;202};203204205