Debian Codegraph @

closure-library/closure/goog/ui/colormenubuttonrenderer.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 Renderer for {@link goog.ui.ColorMenuButton}s.
17 *
18 * @author [email protected] (Robby Walker)
19 * @author [email protected] (Attila Bodis)
20 */
21
22 goog.provide('goog.ui.ColorMenuButtonRenderer');
23
24 goog.require('goog.asserts');
25 goog.require('goog.color');
26 goog.require('goog.dom.TagName');
27 goog.require('goog.dom.classlist');
28 goog.require('goog.ui.MenuButtonRenderer');
29 goog.require('goog.userAgent');
30
31
32
33 /**
34 * Renderer for {@link goog.ui.ColorMenuButton}s.
35 * @constructor
36 * @extends {goog.ui.MenuButtonRenderer}
37 */
38 goog.ui.ColorMenuButtonRenderer = function() {
39 goog.ui.MenuButtonRenderer.call(this);
40 };
41 goog.inherits(goog.ui.ColorMenuButtonRenderer, goog.ui.MenuButtonRenderer);
42 goog.addSingletonGetter(goog.ui.ColorMenuButtonRenderer);
43
44
45 /**
46 * Default CSS class to be applied to the root element of components rendered
47 * by this renderer.
48 * @type {string}
49 */
50 goog.ui.ColorMenuButtonRenderer.CSS_CLASS =
51 goog.getCssName('goog-color-menu-button');
52
53
54 /**
55 * Overrides the superclass implementation by wrapping the caption text or DOM
56 * structure in a color indicator element. Creates the following DOM structure:
57 *
58 * <div class="goog-inline-block goog-menu-button-caption">
59 * <div class="goog-color-menu-button-indicator">
60 * Contents...
61 * </div>
62 * </div>
63 *
64 * The 'goog-color-menu-button-indicator' style should be defined to have a
65 * bottom border of nonzero width and a default color that blends into its
66 * background.
67 * @param {goog.ui.ControlContent} content Text caption or DOM structure.
68 * @param {goog.dom.DomHelper} dom DOM helper, used for document interaction.
69 * @return {Element} Caption element.
70 * @override
71 */
72 goog.ui.ColorMenuButtonRenderer.prototype.createCaption = function(
73 content, dom) {
74 return goog.ui.ColorMenuButtonRenderer.superClass_.createCaption.call(
75 this, goog.ui.ColorMenuButtonRenderer.wrapCaption(content, dom), dom);
76 };
77
78
79 /**
80 * Wrap a caption in a div with the color-menu-button-indicator CSS class.
81 * @param {goog.ui.ControlContent} content Text caption or DOM structure.
82 * @param {goog.dom.DomHelper} dom DOM helper, used for document interaction.
83 * @return {!Element} Caption element.
84 */
85 goog.ui.ColorMenuButtonRenderer.wrapCaption = function(content, dom) {
86 return dom.createDom(
87 goog.dom.TagName.DIV,
88 goog.getCssName(goog.ui.ColorMenuButtonRenderer.CSS_CLASS, 'indicator'),
89 content);
90 };
91
92
93 /**
94 * Takes a color menu button control's root element and a value object
95 * (which is assumed to be a color), and updates the button's DOM to reflect
96 * the new color. Overrides {@link goog.ui.ButtonRenderer#setValue}.
97 * @param {Element} element The button control's root element (if rendered).
98 * @param {*} value New value; assumed to be a color spec string.
99 * @override
100 */
101 goog.ui.ColorMenuButtonRenderer.prototype.setValue = function(element, value) {
102 if (element) {
103 goog.ui.ColorMenuButtonRenderer.setCaptionValue(
104 this.getContentElement(element), value);
105 }
106 };
107
108
109 /**
110 * Takes a control's content element and a value object (which is assumed
111 * to be a color), and updates its DOM to reflect the new color.
112 * @param {Element} caption A content element of a control.
113 * @param {*} value New value; assumed to be a color spec string.
114 */
115 goog.ui.ColorMenuButtonRenderer.setCaptionValue = function(caption, value) {
116 // Assume that the caption's first child is the indicator.
117 if (caption && caption.firstChild) {
118 // Normalize the value to a hex color spec or null (otherwise setting
119 // borderBottomColor will cause a JS error on IE).
120 var hexColor;
121
122 var strValue = /** @type {string} */ (value);
123 hexColor = strValue && goog.color.isValidColor(strValue) ?
124 goog.color.parse(strValue).hex :
125 null;
126
127 // Stupid IE6/7 doesn't do transparent borders.
128 // TODO(attila): Add user-agent version check when IE8 comes out...
129 caption.firstChild.style.borderBottomColor =
130 hexColor || (goog.userAgent.IE ? '' : 'transparent');
131 }
132 };
133
134
135 /**
136 * Initializes the button's DOM when it enters the document. Overrides the
137 * superclass implementation by making sure the button's color indicator is
138 * initialized.
139 * @param {goog.ui.Control} button goog.ui.ColorMenuButton whose DOM is to be
140 * initialized as it enters the document.
141 * @override
142 */
143 goog.ui.ColorMenuButtonRenderer.prototype.initializeDom = function(button) {
144 var buttonElement = button.getElement();
145 goog.asserts.assert(buttonElement);
146 this.setValue(buttonElement, button.getValue());
147 goog.dom.classlist.add(
148 buttonElement, goog.ui.ColorMenuButtonRenderer.CSS_CLASS);
149 goog.ui.ColorMenuButtonRenderer.superClass_.initializeDom.call(this, button);
150 };