Debian Codegraph @

closure-library/closure/goog/ui/colormenubutton.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 color menu button. Extends {@link goog.ui.MenuButton} by
17 * showing the currently selected color in the button caption.
18 *
19 * @author [email protected] (Robby Walker)
20 * @author [email protected] (Attila Bodis)
21 */
22
23 goog.provide('goog.ui.ColorMenuButton');
24
25 goog.require('goog.array');
26 goog.require('goog.object');
27 goog.require('goog.ui.ColorMenuButtonRenderer');
28 goog.require('goog.ui.ColorPalette');
29 goog.require('goog.ui.Component');
30 goog.require('goog.ui.Menu');
31 goog.require('goog.ui.MenuButton');
32 goog.require('goog.ui.registry');
33
34
35
36 /**
37 * A color menu button control. Extends {@link goog.ui.MenuButton} by adding
38 * an API for getting and setting the currently selected color from a menu of
39 * color palettes.
40 *
41 * @param {goog.ui.ControlContent} content Text caption or existing DOM
42 * structure to display as the button's caption.
43 * @param {goog.ui.Menu=} opt_menu Menu to render under the button when clicked;
44 * should contain at least one {@link goog.ui.ColorPalette} if present.
45 * @param {goog.ui.MenuButtonRenderer=} opt_renderer Button renderer;
46 * defaults to {@link goog.ui.ColorMenuButtonRenderer}.
47 * @param {goog.dom.DomHelper=} opt_domHelper Optional DOM helper, used for
48 * document interaction.
49 * @constructor
50 * @extends {goog.ui.MenuButton}
51 */
52 goog.ui.ColorMenuButton = function(
53 content, opt_menu, opt_renderer, opt_domHelper) {
54 goog.ui.MenuButton.call(
55 this, content, opt_menu,
56 opt_renderer || goog.ui.ColorMenuButtonRenderer.getInstance(),
57 opt_domHelper);
58 };
59 goog.inherits(goog.ui.ColorMenuButton, goog.ui.MenuButton);
60
61
62 /**
63 * Default color palettes.
64 * @type {!Object}
65 */
66 goog.ui.ColorMenuButton.PALETTES = {
67 /** Default grayscale colors. */
68 GRAYSCALE:
69 ['#000', '#444', '#666', '#999', '#ccc', '#eee', '#f3f3f3', '#fff'],
70
71 /** Default solid colors. */
72 SOLID: ['#f00', '#f90', '#ff0', '#0f0', '#0ff', '#00f', '#90f', '#f0f'],
73
74 /** Default pastel colors. */
75 PASTEL: [
76 '#f4cccc', '#fce5cd', '#fff2cc', '#d9ead3', '#d0e0e3', '#cfe2f3', '#d9d2e9',
77 '#ead1dc', '#ea9999', '#f9cb9c', '#ffe599', '#b6d7a8', '#a2c4c9', '#9fc5e8',
78 '#b4a7d6', '#d5a6bd', '#e06666', '#f6b26b', '#ffd966', '#93c47d', '#76a5af',
79 '#6fa8dc', '#8e7cc3', '#c27ba0', '#cc0000', '#e69138', '#f1c232', '#6aa84f',
80 '#45818e', '#3d85c6', '#674ea7', '#a64d79', '#990000', '#b45f06', '#bf9000',
81 '#38761d', '#134f5c', '#0b5394', '#351c75', '#741b47', '#660000', '#783f04',
82 '#7f6000', '#274e13', '#0c343d', '#073763', '#20124d', '#4c1130'
83 ]
84 };
85
86
87 /**
88 * Value for the "no color" menu item object in the color menu (if present).
89 * The {@link goog.ui.ColorMenuButton#handleMenuAction} method interprets
90 * ACTION events dispatched by an item with this value as meaning "clear the
91 * selected color."
92 * @type {string}
93 */
94 goog.ui.ColorMenuButton.NO_COLOR = 'none';
95
96
97 /**
98 * Factory method that creates and returns a new {@link goog.ui.Menu} instance
99 * containing default color palettes.
100 * @param {Array<goog.ui.Control>=} opt_extraItems Optional extra menu items to
101 * add before the color palettes.
102 * @param {goog.dom.DomHelper=} opt_domHelper Optional DOM helper, used for
103 * document interaction.
104 * @return {!goog.ui.Menu} Color menu.
105 */
106 goog.ui.ColorMenuButton.newColorMenu = function(opt_extraItems, opt_domHelper) {
107 var menu = new goog.ui.Menu(opt_domHelper);
108
109 if (opt_extraItems) {
110 goog.array.forEach(
111 opt_extraItems, function(item) { menu.addChild(item, true); });
112 }
113
114 goog.object.forEach(goog.ui.ColorMenuButton.PALETTES, function(colors) {
115 var palette = new goog.ui.ColorPalette(colors, null, opt_domHelper);
116 palette.setSize(8);
117 menu.addChild(palette, true);
118 });
119
120 return menu;
121 };
122
123
124 /**
125 * Returns the currently selected color (null if none).
126 * @return {string} The selected color.
127 */
128 goog.ui.ColorMenuButton.prototype.getSelectedColor = function() {
129 return /** @type {string} */ (this.getValue());
130 };
131
132
133 /**
134 * Sets the selected color, or clears the selected color if the argument is
135 * null or not any of the available color choices.
136 * @param {?string} color New color.
137 */
138 goog.ui.ColorMenuButton.prototype.setSelectedColor = function(color) {
139 this.setValue(color);
140 };
141
142
143 /**
144 * Sets the value associated with the color menu button. Overrides
145 * {@link goog.ui.Button#setValue} by interpreting the value as a color
146 * spec string.
147 * @param {*} value New button value; should be a color spec string.
148 * @override
149 */
150 goog.ui.ColorMenuButton.prototype.setValue = function(value) {
151 var color = /** @type {?string} */ (value);
152 for (var i = 0, item; item = this.getItemAt(i); i++) {
153 if (typeof item.setSelectedColor == 'function') {
154 // This menu item looks like a color palette.
155 item.setSelectedColor(color);
156 }
157 }
158 goog.ui.ColorMenuButton.superClass_.setValue.call(this, color);
159 };
160
161
162 /**
163 * Handles {@link goog.ui.Component.EventType.ACTION} events dispatched by
164 * the menu item clicked by the user. Updates the button, calls the superclass
165 * implementation to hide the menu, stops the propagation of the event, and
166 * dispatches an ACTION event on behalf of the button itself. Overrides
167 * {@link goog.ui.MenuButton#handleMenuAction}.
168 * @param {goog.events.Event} e Action event to handle.
169 * @override
170 */
171 goog.ui.ColorMenuButton.prototype.handleMenuAction = function(e) {
172 if (typeof e.target.getSelectedColor == 'function') {
173 // User clicked something that looks like a color palette.
174 this.setValue(e.target.getSelectedColor());
175 } else if (e.target.getValue() == goog.ui.ColorMenuButton.NO_COLOR) {
176 // User clicked the special "no color" menu item.
177 this.setValue(null);
178 }
179 goog.ui.ColorMenuButton.superClass_.handleMenuAction.call(this, e);
180 e.stopPropagation();
181 this.dispatchEvent(goog.ui.Component.EventType.ACTION);
182 };
183
184
185 /**
186 * Opens or closes the menu. Overrides {@link goog.ui.MenuButton#setOpen} by
187 * generating a default color menu on the fly if needed.
188 * @param {boolean} open Whether to open or close the menu.
189 * @param {goog.events.Event=} opt_e Mousedown event that caused the menu to
190 * be opened.
191 * @override
192 */
193 goog.ui.ColorMenuButton.prototype.setOpen = function(open, opt_e) {
194 if (open && this.getItemCount() == 0) {
195 this.setMenu(
196 goog.ui.ColorMenuButton.newColorMenu(null, this.getDomHelper()));
197 this.setValue(/** @type {?string} */ (this.getValue()));
198 }
199 goog.ui.ColorMenuButton.superClass_.setOpen.call(this, open, opt_e);
200 };
201
202
203 // Register a decorator factory function for goog.ui.ColorMenuButtons.
204 goog.ui.registry.setDecoratorByClassName(
205 goog.ui.ColorMenuButtonRenderer.CSS_CLASS,
206 function() { return new goog.ui.ColorMenuButton(null); });