Debian Codegraph @

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

raw
1 // Copyright 2007 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 picker component. A color picker can compose several
17 * instances of goog.ui.ColorPalette.
18 *
19 * NOTE: The ColorPicker is in a state of transition towards the common
20 * component/control/container interface we are developing. If the API changes
21 * we will do our best to update your code. The end result will be that a
22 * color picker will compose multiple color palettes. In the simple case this
23 * will be one grid, but may consistute 3 distinct grids, a custom color picker
24 * or even a color wheel.
25 *
26 */
27
28 goog.provide('goog.ui.ColorPicker');
29 goog.provide('goog.ui.ColorPicker.EventType');
30
31 goog.require('goog.ui.ColorPalette');
32 goog.require('goog.ui.Component');
33
34
35
36 /**
37 * Create a new, empty color picker.
38 *
39 * @param {goog.dom.DomHelper=} opt_domHelper Optional DOM helper.
40 * @param {goog.ui.ColorPalette=} opt_colorPalette Optional color palette to
41 * use for this color picker.
42 * @extends {goog.ui.Component}
43 * @constructor
44 * @final
45 */
46 goog.ui.ColorPicker = function(opt_domHelper, opt_colorPalette) {
47 goog.ui.Component.call(this, opt_domHelper);
48
49 /**
50 * The color palette used inside the color picker.
51 * @type {goog.ui.ColorPalette?}
52 * @private
53 */
54 this.colorPalette_ = opt_colorPalette || null;
55
56 this.getHandler().listen(
57 this, goog.ui.Component.EventType.ACTION, this.onColorPaletteAction_);
58 };
59 goog.inherits(goog.ui.ColorPicker, goog.ui.Component);
60
61
62 /**
63 * Default number of columns in the color palette. May be overridden by calling
64 * setSize.
65 *
66 * @type {number}
67 */
68 goog.ui.ColorPicker.DEFAULT_NUM_COLS = 5;
69
70
71 /**
72 * Constants for event names.
73 * @enum {string}
74 */
75 goog.ui.ColorPicker.EventType = {
76 CHANGE: 'change'
77 };
78
79
80 /**
81 * Whether the component is focusable.
82 * @type {boolean}
83 * @private
84 */
85 goog.ui.ColorPicker.prototype.focusable_ = true;
86
87
88 /**
89 * Gets the array of colors displayed by the color picker.
90 * Modifying this array will lead to unexpected behavior.
91 * @return {Array<string>?} The colors displayed by this widget.
92 */
93 goog.ui.ColorPicker.prototype.getColors = function() {
94 return this.colorPalette_ ? this.colorPalette_.getColors() : null;
95 };
96
97
98 /**
99 * Sets the array of colors to be displayed by the color picker.
100 * @param {Array<string>} colors The array of colors to be added.
101 */
102 goog.ui.ColorPicker.prototype.setColors = function(colors) {
103 // TODO(user): Don't add colors directly, we should add palettes and the
104 // picker should support multiple palettes.
105 if (!this.colorPalette_) {
106 this.createColorPalette_(colors);
107 } else {
108 this.colorPalette_.setColors(colors);
109 }
110 };
111
112
113 /**
114 * Sets the array of colors to be displayed by the color picker.
115 * @param {Array<string>} colors The array of colors to be added.
116 * @deprecated Use setColors.
117 */
118 goog.ui.ColorPicker.prototype.addColors = function(colors) {
119 this.setColors(colors);
120 };
121
122
123 /**
124 * Sets the size of the palette. Will throw an error after the picker has been
125 * rendered.
126 * @param {goog.math.Size|number} size The size of the grid.
127 */
128 goog.ui.ColorPicker.prototype.setSize = function(size) {
129 // TODO(user): The color picker should contain multiple palettes which will
130 // all be resized at this point.
131 if (!this.colorPalette_) {
132 this.createColorPalette_([]);
133 }
134 this.colorPalette_.setSize(size);
135 };
136
137
138 /**
139 * Gets the number of columns displayed.
140 * @return {goog.math.Size?} The size of the grid.
141 */
142 goog.ui.ColorPicker.prototype.getSize = function() {
143 return this.colorPalette_ ? this.colorPalette_.getSize() : null;
144 };
145
146
147 /**
148 * Sets the number of columns. Will throw an error after the picker has been
149 * rendered.
150 * @param {number} n The number of columns.
151 * @deprecated Use setSize.
152 */
153 goog.ui.ColorPicker.prototype.setColumnCount = function(n) {
154 this.setSize(n);
155 };
156
157
158 /**
159 * @return {number} The index of the color selected.
160 */
161 goog.ui.ColorPicker.prototype.getSelectedIndex = function() {
162 return this.colorPalette_ ? this.colorPalette_.getSelectedIndex() : -1;
163 };
164
165
166 /**
167 * Sets which color is selected. A value that is out-of-range means that no
168 * color is selected.
169 * @param {number} ind The index in this.colors_ of the selected color.
170 */
171 goog.ui.ColorPicker.prototype.setSelectedIndex = function(ind) {
172 if (this.colorPalette_) {
173 this.colorPalette_.setSelectedIndex(ind);
174 }
175 };
176
177
178 /**
179 * Gets the color that is currently selected in this color picker.
180 * @return {?string} The hex string of the color selected, or null if no
181 * color is selected.
182 */
183 goog.ui.ColorPicker.prototype.getSelectedColor = function() {
184 return this.colorPalette_ ? this.colorPalette_.getSelectedColor() : null;
185 };
186
187
188 /**
189 * Sets which color is selected. Noop if the color palette hasn't been created
190 * yet.
191 * @param {string} color The selected color.
192 */
193 goog.ui.ColorPicker.prototype.setSelectedColor = function(color) {
194 // TODO(user): This will set the color in the first available palette that
195 // contains it
196 if (this.colorPalette_) {
197 this.colorPalette_.setSelectedColor(color);
198 }
199 };
200
201
202 /**
203 * Returns true if the component is focusable, false otherwise. The default
204 * is true. Focusable components always have a tab index and allocate a key
205 * handler to handle keyboard events while focused.
206 * @return {boolean} True iff the component is focusable.
207 */
208 goog.ui.ColorPicker.prototype.isFocusable = function() {
209 return this.focusable_;
210 };
211
212
213 /**
214 * Sets whether the component is focusable. The default is true.
215 * Focusable components always have a tab index and allocate a key handler to
216 * handle keyboard events while focused.
217 * @param {boolean} focusable True iff the component is focusable.
218 */
219 goog.ui.ColorPicker.prototype.setFocusable = function(focusable) {
220 this.focusable_ = focusable;
221 if (this.colorPalette_) {
222 this.colorPalette_.setSupportedState(
223 goog.ui.Component.State.FOCUSED, focusable);
224 }
225 };
226
227
228 /**
229 * ColorPickers cannot be used to decorate pre-existing html, since the
230 * structure they build is fairly complicated.
231 * @param {Element} element Element to decorate.
232 * @return {boolean} Returns always false.
233 * @override
234 */
235 goog.ui.ColorPicker.prototype.canDecorate = function(element) {
236 return false;
237 };
238
239
240 /**
241 * Renders the color picker inside the provided element. This will override the
242 * current content of the element.
243 * @override
244 */
245 goog.ui.ColorPicker.prototype.enterDocument = function() {
246 goog.ui.ColorPicker.superClass_.enterDocument.call(this);
247 if (this.colorPalette_) {
248 this.colorPalette_.render(this.getElement());
249 }
250 this.getElement().unselectable = 'on';
251 };
252
253
254 /** @override */
255 goog.ui.ColorPicker.prototype.disposeInternal = function() {
256 goog.ui.ColorPicker.superClass_.disposeInternal.call(this);
257 if (this.colorPalette_) {
258 this.colorPalette_.dispose();
259 this.colorPalette_ = null;
260 }
261 };
262
263
264 /**
265 * Sets the focus to the color picker's palette.
266 */
267 goog.ui.ColorPicker.prototype.focus = function() {
268 if (this.colorPalette_) {
269 this.colorPalette_.getElement().focus();
270 }
271 };
272
273
274 /**
275 * Handles actions from the color palette.
276 *
277 * @param {goog.events.Event} e The event.
278 * @private
279 */
280 goog.ui.ColorPicker.prototype.onColorPaletteAction_ = function(e) {
281 e.stopPropagation();
282 this.dispatchEvent(goog.ui.ColorPicker.EventType.CHANGE);
283 };
284
285
286 /**
287 * Create a color palette for the color picker.
288 * @param {Array<string>} colors Array of colors.
289 * @private
290 */
291 goog.ui.ColorPicker.prototype.createColorPalette_ = function(colors) {
292 // TODO(user): The color picker should eventually just contain a number of
293 // palettes and manage the interactions between them. This will go away then.
294 var cp = new goog.ui.ColorPalette(colors, null, this.getDomHelper());
295 cp.setSize(goog.ui.ColorPicker.DEFAULT_NUM_COLS);
296 cp.setSupportedState(goog.ui.Component.State.FOCUSED, this.focusable_);
297 // TODO(user): Use addChild(cp, true) and remove calls to render.
298 this.addChild(cp);
299 this.colorPalette_ = cp;
300 if (this.isInDocument()) {
301 this.colorPalette_.render(this.getElement());
302 }
303 };
304
305
306 /**
307 * Returns an unrendered instance of the color picker. The colors and layout
308 * are a simple color grid, the same as the old Gmail color picker.
309 * @param {goog.dom.DomHelper=} opt_domHelper Optional DOM helper.
310 * @return {!goog.ui.ColorPicker} The unrendered instance.
311 */
312 goog.ui.ColorPicker.createSimpleColorGrid = function(opt_domHelper) {
313 var cp = new goog.ui.ColorPicker(opt_domHelper);
314 cp.setSize(7);
315 cp.setColors(goog.ui.ColorPicker.SIMPLE_GRID_COLORS);
316 return cp;
317 };
318
319
320 /**
321 * Array of colors for a 7-cell wide simple-grid color picker.
322 * @type {Array<string>}
323 */
324 goog.ui.ColorPicker.SIMPLE_GRID_COLORS = [
325 // grays
326 '#ffffff', '#cccccc', '#c0c0c0', '#999999', '#666666', '#333333', '#000000',
327 // reds
328 '#ffcccc', '#ff6666', '#ff0000', '#cc0000', '#990000', '#660000', '#330000',
329 // oranges
330 '#ffcc99', '#ff9966', '#ff9900', '#ff6600', '#cc6600', '#993300', '#663300',
331 // yellows
332 '#ffff99', '#ffff66', '#ffcc66', '#ffcc33', '#cc9933', '#996633', '#663333',
333 // olives
334 '#ffffcc', '#ffff33', '#ffff00', '#ffcc00', '#999900', '#666600', '#333300',
335 // greens
336 '#99ff99', '#66ff99', '#33ff33', '#33cc00', '#009900', '#006600', '#003300',
337 // turquoises
338 '#99ffff', '#33ffff', '#66cccc', '#00cccc', '#339999', '#336666', '#003333',
339 // blues
340 '#ccffff', '#66ffff', '#33ccff', '#3366ff', '#3333ff', '#000099', '#000066',
341 // purples
342 '#ccccff', '#9999ff', '#6666cc', '#6633ff', '#6600cc', '#333399', '#330099',
343 // violets
344 '#ffccff', '#ff99ff', '#cc66cc', '#cc33cc', '#993399', '#663366', '#330033'
345 ];