| 1 |
// Copyright 2010 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 An alternative imageless button renderer that uses CSS3 rather |
| 17 |
* than voodoo to render custom buttons with rounded corners and dimensionality |
| 18 |
* (via a subtle flat shadow on the bottom half of the button) without the use |
| 19 |
* of images. |
| 20 |
* |
| 21 |
* Based on the Custom Buttons 3.1 visual specification, see |
| 22 |
* http://go/custombuttons |
| 23 |
* |
| 24 |
* Tested and verified to work in Gecko 1.9.2+ and WebKit 528+. |
| 25 |
* |
| 26 |
* @author [email protected] (Emil A Eklund) |
| 27 |
* @see ../demos/css3menubutton.html |
| 28 |
*/ |
| 29 |
|
| 30 |
goog.provide('goog.ui.Css3MenuButtonRenderer'); |
| 31 |
|
| 32 |
goog.require('goog.dom'); |
| 33 |
goog.require('goog.dom.TagName'); |
| 34 |
goog.require('goog.ui.INLINE_BLOCK_CLASSNAME'); |
| 35 |
goog.require('goog.ui.MenuButton'); |
| 36 |
goog.require('goog.ui.MenuButtonRenderer'); |
| 37 |
goog.require('goog.ui.registry'); |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
/** |
| 42 |
* Custom renderer for {@link goog.ui.MenuButton}s. Css3 buttons can contain |
| 43 |
* almost arbitrary HTML content, will flow like inline elements, but can be |
| 44 |
* styled like block-level elements. |
| 45 |
* |
| 46 |
* @constructor |
| 47 |
* @extends {goog.ui.MenuButtonRenderer} |
| 48 |
* @final |
| 49 |
*/ |
| 50 |
goog.ui.Css3MenuButtonRenderer = function() { |
| 51 |
goog.ui.MenuButtonRenderer.call(this); |
| 52 |
}; |
| 53 |
goog.inherits(goog.ui.Css3MenuButtonRenderer, goog.ui.MenuButtonRenderer); |
| 54 |
goog.addSingletonGetter(goog.ui.Css3MenuButtonRenderer); |
| 55 |
|
| 56 |
|
| 57 |
/** |
| 58 |
* Default CSS class to be applied to the root element of components rendered |
| 59 |
* by this renderer. |
| 60 |
* @type {string} |
| 61 |
*/ |
| 62 |
goog.ui.Css3MenuButtonRenderer.CSS_CLASS = goog.getCssName('goog-css3-button'); |
| 63 |
|
| 64 |
|
| 65 |
/** @override */ |
| 66 |
goog.ui.Css3MenuButtonRenderer.prototype.getContentElement = function(element) { |
| 67 |
if (element) { |
| 68 |
var captionElem = goog.dom.getElementsByTagNameAndClass( |
| 69 |
'*', goog.getCssName(this.getCssClass(), 'caption'), element)[0]; |
| 70 |
return captionElem; |
| 71 |
} |
| 72 |
return null; |
| 73 |
}; |
| 74 |
|
| 75 |
|
| 76 |
/** |
| 77 |
* Returns true if this renderer can decorate the element. Overrides |
| 78 |
* {@link goog.ui.MenuButtonRenderer#canDecorate} by returning true if the |
| 79 |
* element is a DIV, false otherwise. |
| 80 |
* @param {Element} element Element to decorate. |
| 81 |
* @return {boolean} Whether the renderer can decorate the element. |
| 82 |
* @override |
| 83 |
*/ |
| 84 |
goog.ui.Css3MenuButtonRenderer.prototype.canDecorate = function(element) { |
| 85 |
return element.tagName == goog.dom.TagName.DIV; |
| 86 |
}; |
| 87 |
|
| 88 |
|
| 89 |
/** |
| 90 |
* Takes a text caption or existing DOM structure, and returns the content |
| 91 |
* wrapped in a pseudo-rounded-corner box. Creates the following DOM structure: |
| 92 |
* |
| 93 |
* <div class="goog-inline-block goog-css3-button goog-css3-menu-button"> |
| 94 |
* <div class="goog-css3-button-caption">Contents...</div> |
| 95 |
* <div class="goog-css3-button-dropdown"></div> |
| 96 |
* </div> |
| 97 |
* |
| 98 |
* Used by both {@link #createDom} and {@link #decorate}. To be overridden |
| 99 |
* by subclasses. |
| 100 |
* @param {goog.ui.ControlContent} content Text caption or DOM structure to wrap |
| 101 |
* in a box. |
| 102 |
* @param {goog.dom.DomHelper} dom DOM helper, used for document interaction. |
| 103 |
* @return {!Element} Pseudo-rounded-corner box containing the content. |
| 104 |
* @override |
| 105 |
*/ |
| 106 |
goog.ui.Css3MenuButtonRenderer.prototype.createButton = function(content, dom) { |
| 107 |
var baseClass = this.getCssClass(); |
| 108 |
var inlineBlock = goog.ui.INLINE_BLOCK_CLASSNAME + ' '; |
| 109 |
return dom.createDom( |
| 110 |
goog.dom.TagName.DIV, inlineBlock, |
| 111 |
dom.createDom( |
| 112 |
goog.dom.TagName.DIV, |
| 113 |
[ |
| 114 |
goog.getCssName(baseClass, 'caption'), |
| 115 |
goog.getCssName('goog-inline-block') |
| 116 |
], |
| 117 |
content), |
| 118 |
dom.createDom(goog.dom.TagName.DIV, [ |
| 119 |
goog.getCssName(baseClass, 'dropdown'), |
| 120 |
goog.getCssName('goog-inline-block') |
| 121 |
])); |
| 122 |
}; |
| 123 |
|
| 124 |
|
| 125 |
/** |
| 126 |
* Returns the CSS class to be applied to the root element of components |
| 127 |
* rendered using this renderer. |
| 128 |
* @return {string} Renderer-specific CSS class. |
| 129 |
* @override |
| 130 |
*/ |
| 131 |
goog.ui.Css3MenuButtonRenderer.prototype.getCssClass = function() { |
| 132 |
return goog.ui.Css3MenuButtonRenderer.CSS_CLASS; |
| 133 |
}; |
| 134 |
|
| 135 |
|
| 136 |
// Register a decorator factory function for goog.ui.Css3MenuButtonRenderer. |
| 137 |
// Since we're using goog-css3-button as the base class in order to get the |
| 138 |
// same styling as goog.ui.Css3ButtonRenderer, we need to be explicit about |
| 139 |
// giving goog-css3-menu-button here. |
| 140 |
goog.ui.registry.setDecoratorByClassName( |
| 141 |
goog.getCssName('goog-css3-menu-button'), function() { |
| 142 |
return new goog.ui.MenuButton( |
| 143 |
null, null, goog.ui.Css3MenuButtonRenderer.getInstance()); |
| 144 |
}); |