| 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/css3button.html |
| 28 |
*/ |
| 29 |
|
| 30 |
goog.provide('goog.ui.Css3ButtonRenderer'); |
| 31 |
|
| 32 |
goog.require('goog.asserts'); |
| 33 |
goog.require('goog.dom.TagName'); |
| 34 |
goog.require('goog.dom.classlist'); |
| 35 |
goog.require('goog.ui.Button'); |
| 36 |
goog.require('goog.ui.ButtonRenderer'); |
| 37 |
goog.require('goog.ui.Component'); |
| 38 |
goog.require('goog.ui.INLINE_BLOCK_CLASSNAME'); |
| 39 |
goog.require('goog.ui.registry'); |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
/** |
| 44 |
* Custom renderer for {@link goog.ui.Button}s. Css3 buttons can contain |
| 45 |
* almost arbitrary HTML content, will flow like inline elements, but can be |
| 46 |
* styled like block-level elements. |
| 47 |
* |
| 48 |
* @constructor |
| 49 |
* @extends {goog.ui.ButtonRenderer} |
| 50 |
* @final |
| 51 |
*/ |
| 52 |
goog.ui.Css3ButtonRenderer = function() { |
| 53 |
goog.ui.ButtonRenderer.call(this); |
| 54 |
}; |
| 55 |
goog.inherits(goog.ui.Css3ButtonRenderer, goog.ui.ButtonRenderer); |
| 56 |
goog.addSingletonGetter(goog.ui.Css3ButtonRenderer); |
| 57 |
|
| 58 |
|
| 59 |
/** |
| 60 |
* Default CSS class to be applied to the root element of components rendered |
| 61 |
* by this renderer. |
| 62 |
* @type {string} |
| 63 |
*/ |
| 64 |
goog.ui.Css3ButtonRenderer.CSS_CLASS = goog.getCssName('goog-css3-button'); |
| 65 |
|
| 66 |
|
| 67 |
/** @override */ |
| 68 |
goog.ui.Css3ButtonRenderer.prototype.getContentElement = function(element) { |
| 69 |
return /** @type {Element} */ (element); |
| 70 |
}; |
| 71 |
|
| 72 |
|
| 73 |
/** |
| 74 |
* Returns the button's contents wrapped in the following DOM structure: |
| 75 |
* |
| 76 |
* <div class="goog-inline-block goog-css3-button"> |
| 77 |
* Contents... |
| 78 |
* </div> |
| 79 |
* |
| 80 |
* Overrides {@link goog.ui.ButtonRenderer#createDom}. |
| 81 |
* @param {goog.ui.Control} control goog.ui.Button to render. |
| 82 |
* @return {!Element} Root element for the button. |
| 83 |
* @override |
| 84 |
*/ |
| 85 |
goog.ui.Css3ButtonRenderer.prototype.createDom = function(control) { |
| 86 |
var button = /** @type {goog.ui.Button} */ (control); |
| 87 |
var classNames = this.getClassNames(button); |
| 88 |
return button.getDomHelper().createDom( |
| 89 |
goog.dom.TagName.DIV, { |
| 90 |
'class': goog.ui.INLINE_BLOCK_CLASSNAME + ' ' + classNames.join(' '), |
| 91 |
'title': button.getTooltip() || '' |
| 92 |
}, |
| 93 |
button.getContent()); |
| 94 |
}; |
| 95 |
|
| 96 |
|
| 97 |
/** |
| 98 |
* Returns true if this renderer can decorate the element. Overrides |
| 99 |
* {@link goog.ui.ButtonRenderer#canDecorate} by returning true if the |
| 100 |
* element is a DIV, false otherwise. |
| 101 |
* @param {Element} element Element to decorate. |
| 102 |
* @return {boolean} Whether the renderer can decorate the element. |
| 103 |
* @override |
| 104 |
*/ |
| 105 |
goog.ui.Css3ButtonRenderer.prototype.canDecorate = function(element) { |
| 106 |
return element.tagName == goog.dom.TagName.DIV; |
| 107 |
}; |
| 108 |
|
| 109 |
|
| 110 |
/** @override */ |
| 111 |
goog.ui.Css3ButtonRenderer.prototype.decorate = function(button, element) { |
| 112 |
goog.asserts.assert(element); |
| 113 |
goog.dom.classlist.addAll( |
| 114 |
element, [goog.ui.INLINE_BLOCK_CLASSNAME, this.getCssClass()]); |
| 115 |
return goog.ui.Css3ButtonRenderer.superClass_.decorate.call( |
| 116 |
this, button, element); |
| 117 |
}; |
| 118 |
|
| 119 |
|
| 120 |
/** |
| 121 |
* Returns the CSS class to be applied to the root element of components |
| 122 |
* rendered using this renderer. |
| 123 |
* @return {string} Renderer-specific CSS class. |
| 124 |
* @override |
| 125 |
*/ |
| 126 |
goog.ui.Css3ButtonRenderer.prototype.getCssClass = function() { |
| 127 |
return goog.ui.Css3ButtonRenderer.CSS_CLASS; |
| 128 |
}; |
| 129 |
|
| 130 |
|
| 131 |
// Register a decorator factory function for goog.ui.Css3ButtonRenderer. |
| 132 |
goog.ui.registry.setDecoratorByClassName( |
| 133 |
goog.ui.Css3ButtonRenderer.CSS_CLASS, function() { |
| 134 |
return new goog.ui.Button(null, goog.ui.Css3ButtonRenderer.getInstance()); |
| 135 |
}); |
| 136 |
|
| 137 |
|
| 138 |
// Register a decorator factory function for toggle buttons using the |
| 139 |
// goog.ui.Css3ButtonRenderer. |
| 140 |
goog.ui.registry.setDecoratorByClassName( |
| 141 |
goog.getCssName('goog-css3-toggle-button'), function() { |
| 142 |
var button = |
| 143 |
new goog.ui.Button(null, goog.ui.Css3ButtonRenderer.getInstance()); |
| 144 |
button.setSupportedState(goog.ui.Component.State.CHECKED, true); |
| 145 |
return button; |
| 146 |
}); |