| 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.Button}s in App style. |
| 17 |
* |
| 18 |
* Based on ImagelessButtonRender. Uses even more CSS voodoo than the default |
| 19 |
* implementation to render custom buttons with fake rounded corners and |
| 20 |
* dimensionality (via a subtle flat shadow on the bottom half of the button) |
| 21 |
* without the use of images. |
| 22 |
* |
| 23 |
* Based on the Custom Buttons 3.1 visual specification, see |
| 24 |
* http://go/custombuttons |
| 25 |
* |
| 26 |
* @author [email protected] (Emil A Eklund) |
| 27 |
*/ |
| 28 |
|
| 29 |
goog.provide('goog.ui.style.app.ButtonRenderer'); |
| 30 |
|
| 31 |
goog.require('goog.dom.TagName'); |
| 32 |
goog.require('goog.dom.classlist'); |
| 33 |
goog.require('goog.ui.Button'); |
| 34 |
goog.require('goog.ui.CustomButtonRenderer'); |
| 35 |
goog.require('goog.ui.INLINE_BLOCK_CLASSNAME'); |
| 36 |
goog.require('goog.ui.registry'); |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
/** |
| 41 |
* Custom renderer for {@link goog.ui.Button}s. Imageless buttons can contain |
| 42 |
* almost arbitrary HTML content, will flow like inline elements, but can be |
| 43 |
* styled like block-level elements. |
| 44 |
* |
| 45 |
* @constructor |
| 46 |
* @extends {goog.ui.CustomButtonRenderer} |
| 47 |
*/ |
| 48 |
goog.ui.style.app.ButtonRenderer = function() { |
| 49 |
goog.ui.CustomButtonRenderer.call(this); |
| 50 |
}; |
| 51 |
goog.inherits(goog.ui.style.app.ButtonRenderer, goog.ui.CustomButtonRenderer); |
| 52 |
goog.addSingletonGetter(goog.ui.style.app.ButtonRenderer); |
| 53 |
|
| 54 |
|
| 55 |
/** |
| 56 |
* Default CSS class to be applied to the root element of components rendered |
| 57 |
* by this renderer. |
| 58 |
* @type {string} |
| 59 |
*/ |
| 60 |
goog.ui.style.app.ButtonRenderer.CSS_CLASS = goog.getCssName('goog-button'); |
| 61 |
|
| 62 |
|
| 63 |
/** |
| 64 |
* Array of arrays of CSS classes that we want composite classes added and |
| 65 |
* removed for in IE6 and lower as a workaround for lack of multi-class CSS |
| 66 |
* selector support. |
| 67 |
* @type {Array<Array<string>>} |
| 68 |
*/ |
| 69 |
goog.ui.style.app.ButtonRenderer.IE6_CLASS_COMBINATIONS = []; |
| 70 |
|
| 71 |
|
| 72 |
/** |
| 73 |
* Returns the button's contents wrapped in the following DOM structure: |
| 74 |
* |
| 75 |
* <div class="goog-inline-block goog-button-base goog-button"> |
| 76 |
* <div class="goog-inline-block goog-button-base-outer-box"> |
| 77 |
* <div class="goog-button-base-inner-box"> |
| 78 |
* <div class="goog-button-base-pos"> |
| 79 |
* <div class="goog-button-base-top-shadow"> </div> |
| 80 |
* <div class="goog-button-base-content">Contents...</div> |
| 81 |
* </div> |
| 82 |
* </div> |
| 83 |
* </div> |
| 84 |
* </div> |
| 85 |
* @override |
| 86 |
*/ |
| 87 |
goog.ui.style.app.ButtonRenderer.prototype.createDom; |
| 88 |
|
| 89 |
|
| 90 |
/** @override */ |
| 91 |
goog.ui.style.app.ButtonRenderer.prototype.getContentElement = function( |
| 92 |
element) { |
| 93 |
return element && /** @type {Element} */ ( |
| 94 |
element.firstChild.firstChild.firstChild.lastChild); |
| 95 |
}; |
| 96 |
|
| 97 |
|
| 98 |
/** |
| 99 |
* Takes a text caption or existing DOM structure, and returns the content |
| 100 |
* wrapped in a pseudo-rounded-corner box. Creates the following DOM structure: |
| 101 |
* |
| 102 |
* <div class="goog-inline-block goog-button-base-outer-box"> |
| 103 |
* <div class="goog-inline-block goog-button-base-inner-box"> |
| 104 |
* <div class="goog-button-base-pos"> |
| 105 |
* <div class="goog-button-base-top-shadow"> </div> |
| 106 |
* <div class="goog-button-base-content">Contents...</div> |
| 107 |
* </div> |
| 108 |
* </div> |
| 109 |
* </div> |
| 110 |
* |
| 111 |
* Used by both {@link #createDom} and {@link #decorate}. To be overridden |
| 112 |
* by subclasses. |
| 113 |
* @param {goog.ui.ControlContent} content Text caption or DOM structure to wrap |
| 114 |
* in a box. |
| 115 |
* @param {goog.dom.DomHelper} dom DOM helper, used for document interaction. |
| 116 |
* @return {Element} Pseudo-rounded-corner box containing the content. |
| 117 |
* @override |
| 118 |
*/ |
| 119 |
goog.ui.style.app.ButtonRenderer.prototype.createButton = function( |
| 120 |
content, dom) { |
| 121 |
var baseClass = this.getStructuralCssClass(); |
| 122 |
var inlineBlock = goog.ui.INLINE_BLOCK_CLASSNAME + ' '; |
| 123 |
return dom.createDom( |
| 124 |
goog.dom.TagName.DIV, |
| 125 |
inlineBlock + goog.getCssName(baseClass, 'outer-box'), |
| 126 |
dom.createDom( |
| 127 |
goog.dom.TagName.DIV, |
| 128 |
inlineBlock + goog.getCssName(baseClass, 'inner-box'), |
| 129 |
dom.createDom( |
| 130 |
goog.dom.TagName.DIV, goog.getCssName(baseClass, 'pos'), |
| 131 |
dom.createDom( |
| 132 |
goog.dom.TagName.DIV, |
| 133 |
goog.getCssName(baseClass, 'top-shadow'), '\u00A0'), |
| 134 |
dom.createDom( |
| 135 |
goog.dom.TagName.DIV, goog.getCssName(baseClass, 'content'), |
| 136 |
content)))); |
| 137 |
}; |
| 138 |
|
| 139 |
|
| 140 |
/** |
| 141 |
* Check if the button's element has a box structure. |
| 142 |
* @param {goog.ui.Button} button Button instance whose structure is being |
| 143 |
* checked. |
| 144 |
* @param {Element} element Element of the button. |
| 145 |
* @return {boolean} Whether the element has a box structure. |
| 146 |
* @protected |
| 147 |
* @override |
| 148 |
*/ |
| 149 |
goog.ui.style.app.ButtonRenderer.prototype.hasBoxStructure = function( |
| 150 |
button, element) { |
| 151 |
|
| 152 |
var baseClass = this.getStructuralCssClass(); |
| 153 |
var outer = button.getDomHelper().getFirstElementChild(element); |
| 154 |
var outerClassName = goog.getCssName(baseClass, 'outer-box'); |
| 155 |
if (outer && goog.dom.classlist.contains(outer, outerClassName)) { |
| 156 |
var inner = button.getDomHelper().getFirstElementChild(outer); |
| 157 |
var innerClassName = goog.getCssName(baseClass, 'inner-box'); |
| 158 |
if (inner && goog.dom.classlist.contains(inner, innerClassName)) { |
| 159 |
var pos = button.getDomHelper().getFirstElementChild(inner); |
| 160 |
var posClassName = goog.getCssName(baseClass, 'pos'); |
| 161 |
if (pos && goog.dom.classlist.contains(pos, posClassName)) { |
| 162 |
var shadow = button.getDomHelper().getFirstElementChild(pos); |
| 163 |
var shadowClassName = goog.getCssName(baseClass, 'top-shadow'); |
| 164 |
if (shadow && goog.dom.classlist.contains(shadow, shadowClassName)) { |
| 165 |
var content = button.getDomHelper().getNextElementSibling(shadow); |
| 166 |
var contentClassName = goog.getCssName(baseClass, 'content'); |
| 167 |
if (content && |
| 168 |
goog.dom.classlist.contains(content, contentClassName)) { |
| 169 |
// We have a proper box structure. |
| 170 |
return true; |
| 171 |
} |
| 172 |
} |
| 173 |
} |
| 174 |
} |
| 175 |
} |
| 176 |
return false; |
| 177 |
}; |
| 178 |
|
| 179 |
|
| 180 |
/** @override */ |
| 181 |
goog.ui.style.app.ButtonRenderer.prototype.getCssClass = function() { |
| 182 |
return goog.ui.style.app.ButtonRenderer.CSS_CLASS; |
| 183 |
}; |
| 184 |
|
| 185 |
|
| 186 |
/** @override */ |
| 187 |
goog.ui.style.app.ButtonRenderer.prototype.getStructuralCssClass = function() { |
| 188 |
// TODO(user): extract to a constant. |
| 189 |
return goog.getCssName('goog-button-base'); |
| 190 |
}; |
| 191 |
|
| 192 |
|
| 193 |
/** @override */ |
| 194 |
goog.ui.style.app.ButtonRenderer.prototype.getIe6ClassCombinations = |
| 195 |
function() { |
| 196 |
return goog.ui.style.app.ButtonRenderer.IE6_CLASS_COMBINATIONS; |
| 197 |
}; |
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
// Register a decorator factory function for goog.ui.style.app.ButtonRenderer. |
| 202 |
goog.ui.registry.setDecoratorByClassName( |
| 203 |
goog.ui.style.app.ButtonRenderer.CSS_CLASS, function() { |
| 204 |
return new goog.ui.Button( |
| 205 |
null, goog.ui.style.app.ButtonRenderer.getInstance()); |
| 206 |
}); |