Debian Codegraph @

closure-library/closure/goog/ui/custombuttonrenderer.js

raw
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 A custom button renderer that uses CSS voodoo to render a
17 * button-like object with fake rounded corners.
18 *
19 * @author [email protected] (Attila Bodis)
20 */
21
22 goog.provide('goog.ui.CustomButtonRenderer');
23
24 goog.require('goog.a11y.aria.Role');
25 goog.require('goog.asserts');
26 goog.require('goog.dom.NodeType');
27 goog.require('goog.dom.TagName');
28 goog.require('goog.dom.classlist');
29 goog.require('goog.string');
30 goog.require('goog.ui.ButtonRenderer');
31 goog.require('goog.ui.INLINE_BLOCK_CLASSNAME');
32
33
34
35 /**
36 * Custom renderer for {@link goog.ui.Button}s. Custom buttons can contain
37 * almost arbitrary HTML content, will flow like inline elements, but can be
38 * styled like block-level elements.
39 *
40 * @constructor
41 * @extends {goog.ui.ButtonRenderer}
42 */
43 goog.ui.CustomButtonRenderer = function() {
44 goog.ui.ButtonRenderer.call(this);
45 };
46 goog.inherits(goog.ui.CustomButtonRenderer, goog.ui.ButtonRenderer);
47 goog.addSingletonGetter(goog.ui.CustomButtonRenderer);
48
49
50 /**
51 * Default CSS class to be applied to the root element of components rendered
52 * by this renderer.
53 * @type {string}
54 */
55 goog.ui.CustomButtonRenderer.CSS_CLASS = goog.getCssName('goog-custom-button');
56
57
58 /**
59 * Returns the button's contents wrapped in the following DOM structure:
60 *
61 * <div class="goog-inline-block goog-custom-button">
62 * <div class="goog-inline-block goog-custom-button-outer-box">
63 * <div class="goog-inline-block goog-custom-button-inner-box">
64 * Contents...
65 * </div>
66 * </div>
67 * </div>
68 *
69 * Overrides {@link goog.ui.ButtonRenderer#createDom}.
70 * @param {goog.ui.Control} control goog.ui.Button to render.
71 * @return {!Element} Root element for the button.
72 * @override
73 */
74 goog.ui.CustomButtonRenderer.prototype.createDom = function(control) {
75 var button = /** @type {goog.ui.Button} */ (control);
76 var classNames = this.getClassNames(button);
77 var buttonElement = button.getDomHelper().createDom(
78 goog.dom.TagName.DIV,
79 goog.ui.INLINE_BLOCK_CLASSNAME + ' ' + classNames.join(' '),
80 this.createButton(button.getContent(), button.getDomHelper()));
81 this.setTooltip(buttonElement, /** @type {!string}*/ (button.getTooltip()));
82
83 return buttonElement;
84 };
85
86
87 /**
88 * Returns the ARIA role to be applied to custom buttons.
89 * @return {goog.a11y.aria.Role|undefined} ARIA role.
90 * @override
91 */
92 goog.ui.CustomButtonRenderer.prototype.getAriaRole = function() {
93 return goog.a11y.aria.Role.BUTTON;
94 };
95
96
97 /**
98 * Takes the button's root element and returns the parent element of the
99 * button's contents. Overrides the superclass implementation by taking
100 * the nested DIV structure of custom buttons into account.
101 * @param {Element} element Root element of the button whose content
102 * element is to be returned.
103 * @return {Element} The button's content element (if any).
104 * @override
105 */
106 goog.ui.CustomButtonRenderer.prototype.getContentElement = function(element) {
107 return element && element.firstChild &&
108 /** @type {Element} */ (element.firstChild.firstChild);
109 };
110
111
112 /**
113 * Takes a text caption or existing DOM structure, and returns the content
114 * wrapped in a pseudo-rounded-corner box. Creates the following DOM structure:
115 *
116 * <div class="goog-inline-block goog-custom-button-outer-box">
117 * <div class="goog-inline-block goog-custom-button-inner-box">
118 * Contents...
119 * </div>
120 * </div>
121 *
122 * Used by both {@link #createDom} and {@link #decorate}. To be overridden
123 * by subclasses.
124 * @param {goog.ui.ControlContent} content Text caption or DOM structure to wrap
125 * in a box.
126 * @param {goog.dom.DomHelper} dom DOM helper, used for document interaction.
127 * @return {Element} Pseudo-rounded-corner box containing the content.
128 */
129 goog.ui.CustomButtonRenderer.prototype.createButton = function(content, dom) {
130 return dom.createDom(
131 goog.dom.TagName.DIV, goog.ui.INLINE_BLOCK_CLASSNAME + ' ' +
132 goog.getCssName(this.getCssClass(), 'outer-box'),
133 dom.createDom(
134 goog.dom.TagName.DIV, goog.ui.INLINE_BLOCK_CLASSNAME + ' ' +
135 goog.getCssName(this.getCssClass(), 'inner-box'),
136 content));
137 };
138
139
140 /**
141 * Returns true if this renderer can decorate the element. Overrides
142 * {@link goog.ui.ButtonRenderer#canDecorate} by returning true if the
143 * element is a DIV, false otherwise.
144 * @param {Element} element Element to decorate.
145 * @return {boolean} Whether the renderer can decorate the element.
146 * @override
147 */
148 goog.ui.CustomButtonRenderer.prototype.canDecorate = function(element) {
149 return element.tagName == goog.dom.TagName.DIV;
150 };
151
152
153 /**
154 * Check if the button's element has a box structure.
155 * @param {goog.ui.Button} button Button instance whose structure is being
156 * checked.
157 * @param {Element} element Element of the button.
158 * @return {boolean} Whether the element has a box structure.
159 * @protected
160 */
161 goog.ui.CustomButtonRenderer.prototype.hasBoxStructure = function(
162 button, element) {
163 var outer = button.getDomHelper().getFirstElementChild(element);
164 var outerClassName = goog.getCssName(this.getCssClass(), 'outer-box');
165 if (outer && goog.dom.classlist.contains(outer, outerClassName)) {
166 var inner = button.getDomHelper().getFirstElementChild(outer);
167 var innerClassName = goog.getCssName(this.getCssClass(), 'inner-box');
168 if (inner && goog.dom.classlist.contains(inner, innerClassName)) {
169 // We have a proper box structure.
170 return true;
171 }
172 }
173 return false;
174 };
175
176
177 /**
178 * Takes an existing element and decorates it with the custom button control.
179 * Initializes the control's ID, content, tooltip, value, and state based
180 * on the ID of the element, its child nodes, and its CSS classes, respectively.
181 * Returns the element. Overrides {@link goog.ui.ButtonRenderer#decorate}.
182 * @param {goog.ui.Control} control Button instance to decorate the element.
183 * @param {Element} element Element to decorate.
184 * @return {Element} Decorated element.
185 * @override
186 */
187 goog.ui.CustomButtonRenderer.prototype.decorate = function(control, element) {
188 goog.asserts.assert(element);
189
190 var button = /** @type {goog.ui.Button} */ (control);
191 // Trim text nodes in the element's child node list; otherwise madness
192 // ensues (i.e. on Gecko, buttons will flicker and shift when moused over).
193 goog.ui.CustomButtonRenderer.trimTextNodes_(element, true);
194 goog.ui.CustomButtonRenderer.trimTextNodes_(element, false);
195
196 // Create the buttom dom if it has not been created.
197 if (!this.hasBoxStructure(button, element)) {
198 element.appendChild(
199 this.createButton(element.childNodes, button.getDomHelper()));
200 }
201
202 goog.dom.classlist.addAll(
203 element, [goog.ui.INLINE_BLOCK_CLASSNAME, this.getCssClass()]);
204 return goog.ui.CustomButtonRenderer.superClass_.decorate.call(
205 this, button, element);
206 };
207
208
209 /**
210 * Returns the CSS class to be applied to the root element of components
211 * rendered using this renderer.
212 * @return {string} Renderer-specific CSS class.
213 * @override
214 */
215 goog.ui.CustomButtonRenderer.prototype.getCssClass = function() {
216 return goog.ui.CustomButtonRenderer.CSS_CLASS;
217 };
218
219
220 /**
221 * Takes an element and removes leading or trailing whitespace from the start
222 * or the end of its list of child nodes. The Boolean argument determines
223 * whether to trim from the start or the end of the node list. Empty text
224 * nodes are removed, and the first non-empty text node is trimmed from the
225 * left or the right as appropriate. For example,
226 *
227 * <div class="goog-inline-block">
228 * #text ""
229 * #text "\n Hello "
230 * <span>...</span>
231 * #text " World! \n"
232 * #text ""
233 * </div>
234 *
235 * becomes
236 *
237 * <div class="goog-inline-block">
238 * #text "Hello "
239 * <span>...</span>
240 * #text " World!"
241 * </div>
242 *
243 * This is essential for Gecko, where leading/trailing whitespace messes with
244 * the layout of elements with -moz-inline-box (used in goog-inline-block), and
245 * optional but harmless for non-Gecko.
246 *
247 * @param {Element} element Element whose child node list is to be trimmed.
248 * @param {boolean} fromStart Whether to trim from the start or from the end.
249 * @private
250 */
251 goog.ui.CustomButtonRenderer.trimTextNodes_ = function(element, fromStart) {
252 if (element) {
253 var node = fromStart ? element.firstChild : element.lastChild, next;
254 // Tag soup HTML may result in a DOM where siblings have different parents.
255 while (node && node.parentNode == element) {
256 // Get the next/previous sibling here, since the node may be removed.
257 next = fromStart ? node.nextSibling : node.previousSibling;
258 if (node.nodeType == goog.dom.NodeType.TEXT) {
259 // Found a text node.
260 var text = node.nodeValue;
261 if (goog.string.trim(text) == '') {
262 // Found an empty text node; remove it.
263 element.removeChild(node);
264 } else {
265 // Found a non-empty text node; trim from the start/end, then exit.
266 node.nodeValue = fromStart ? goog.string.trimLeft(text) :
267 goog.string.trimRight(text);
268 break;
269 }
270 } else {
271 // Found a non-text node; done.
272 break;
273 }
274 node = next;
275 }
276 }
277 };