Debian Codegraph @

closure-library/closure/goog/ui/flatbuttonrenderer.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 Similar functionality of {@link goog.ui.ButtonRenderer},
17 * but uses a <div> element instead of a <button> or <input> element.
18 *
19 */
20
21 goog.provide('goog.ui.FlatButtonRenderer');
22
23 goog.require('goog.a11y.aria.Role');
24 goog.require('goog.asserts');
25 goog.require('goog.dom.TagName');
26 goog.require('goog.dom.classlist');
27 goog.require('goog.ui.Button');
28 goog.require('goog.ui.ButtonRenderer');
29 goog.require('goog.ui.INLINE_BLOCK_CLASSNAME');
30 goog.require('goog.ui.registry');
31
32
33
34 /**
35 * Flat renderer for {@link goog.ui.Button}s. Flat buttons can contain
36 * almost arbitrary HTML content, will flow like inline elements, but can be
37 * styled like block-level elements.
38 * @constructor
39 * @extends {goog.ui.ButtonRenderer}
40 */
41 goog.ui.FlatButtonRenderer = function() {
42 goog.ui.ButtonRenderer.call(this);
43 };
44 goog.inherits(goog.ui.FlatButtonRenderer, goog.ui.ButtonRenderer);
45 goog.addSingletonGetter(goog.ui.FlatButtonRenderer);
46
47
48 /**
49 * Default CSS class to be applied to the root element of components rendered
50 * by this renderer.
51 * @type {string}
52 */
53 goog.ui.FlatButtonRenderer.CSS_CLASS = goog.getCssName('goog-flat-button');
54
55
56 /**
57 * Returns the control's contents wrapped in a div element, with
58 * the renderer's own CSS class and additional state-specific classes applied
59 * to it, and the button's disabled attribute set or cleared as needed.
60 * Overrides {@link goog.ui.ButtonRenderer#createDom}.
61 * @param {goog.ui.Control} button Button to render.
62 * @return {!Element} Root element for the button.
63 * @override
64 */
65 goog.ui.FlatButtonRenderer.prototype.createDom = function(button) {
66 var classNames = this.getClassNames(button);
67 var element = button.getDomHelper().createDom(
68 goog.dom.TagName.DIV,
69 goog.ui.INLINE_BLOCK_CLASSNAME + ' ' + classNames.join(' '),
70 button.getContent());
71 this.setTooltip(element, button.getTooltip());
72 return element;
73 };
74
75
76 /**
77 * Returns the ARIA role to be applied to flat buttons.
78 * @return {goog.a11y.aria.Role|undefined} ARIA role.
79 * @override
80 */
81 goog.ui.FlatButtonRenderer.prototype.getAriaRole = function() {
82 return goog.a11y.aria.Role.BUTTON;
83 };
84
85
86 /**
87 * Returns true if this renderer can decorate the element. Overrides
88 * {@link goog.ui.ButtonRenderer#canDecorate} by returning true if the
89 * element is a DIV, false otherwise.
90 * @param {Element} element Element to decorate.
91 * @return {boolean} Whether the renderer can decorate the element.
92 * @override
93 */
94 goog.ui.FlatButtonRenderer.prototype.canDecorate = function(element) {
95 return element.tagName == goog.dom.TagName.DIV;
96 };
97
98
99 /**
100 * Takes an existing element and decorates it with the flat button control.
101 * Initializes the control's ID, content, tooltip, value, and state based
102 * on the ID of the element, its child nodes, and its CSS classes, respectively.
103 * Returns the element. Overrides {@link goog.ui.ButtonRenderer#decorate}.
104 * @param {goog.ui.Control} button Button instance to decorate the element.
105 * @param {Element} element Element to decorate.
106 * @return {Element} Decorated element.
107 * @override
108 */
109 goog.ui.FlatButtonRenderer.prototype.decorate = function(button, element) {
110 goog.asserts.assert(element);
111 goog.dom.classlist.add(element, goog.ui.INLINE_BLOCK_CLASSNAME);
112 return goog.ui.FlatButtonRenderer.superClass_.decorate.call(
113 this, button, element);
114 };
115
116
117 /**
118 * Flat buttons can't use the value attribute since they are div elements.
119 * Overrides {@link goog.ui.ButtonRenderer#getValue} to prevent trying to
120 * access the element's value.
121 * @param {Element} element The button control's root element.
122 * @return {string} Value not valid for flat buttons.
123 * @override
124 */
125 goog.ui.FlatButtonRenderer.prototype.getValue = function(element) {
126 // Flat buttons don't store their value in the DOM.
127 return '';
128 };
129
130
131 /**
132 * Returns the CSS class to be applied to the root element of components
133 * rendered using this renderer.
134 * @return {string} Renderer-specific CSS class.
135 * @override
136 */
137 goog.ui.FlatButtonRenderer.prototype.getCssClass = function() {
138 return goog.ui.FlatButtonRenderer.CSS_CLASS;
139 };
140
141
142 // Register a decorator factory function for Flat Buttons.
143 goog.ui.registry.setDecoratorByClassName(
144 goog.ui.FlatButtonRenderer.CSS_CLASS, function() {
145 // Uses goog.ui.Button, but with FlatButtonRenderer.
146 return new goog.ui.Button(null, goog.ui.FlatButtonRenderer.getInstance());
147 });