| 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 control for representing a palette of colors, that the user |
| 17 |
* can highlight or select via the keyboard or the mouse. |
| 18 |
* |
| 19 |
*/ |
| 20 |
|
| 21 |
goog.provide('goog.ui.ColorPalette'); |
| 22 |
|
| 23 |
goog.require('goog.array'); |
| 24 |
goog.require('goog.color'); |
| 25 |
goog.require('goog.dom.TagName'); |
| 26 |
goog.require('goog.style'); |
| 27 |
goog.require('goog.ui.Palette'); |
| 28 |
goog.require('goog.ui.PaletteRenderer'); |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
/** |
| 33 |
* A color palette is a grid of color swatches that the user can highlight or |
| 34 |
* select via the keyboard or the mouse. The selection state of the palette is |
| 35 |
* controlled by a selection model. When the user makes a selection, the |
| 36 |
* component fires an ACTION event. Event listeners may retrieve the selected |
| 37 |
* color using the {@link #getSelectedColor} method. |
| 38 |
* |
| 39 |
* @param {Array<string>=} opt_colors Array of colors in any valid CSS color |
| 40 |
* format. |
| 41 |
* @param {goog.ui.PaletteRenderer=} opt_renderer Renderer used to render or |
| 42 |
* decorate the palette; defaults to {@link goog.ui.PaletteRenderer}. |
| 43 |
* @param {goog.dom.DomHelper=} opt_domHelper Optional DOM helper, used for |
| 44 |
* document interaction. |
| 45 |
* @constructor |
| 46 |
* @extends {goog.ui.Palette} |
| 47 |
*/ |
| 48 |
goog.ui.ColorPalette = function(opt_colors, opt_renderer, opt_domHelper) { |
| 49 |
/** |
| 50 |
* Array of colors to show in the palette. |
| 51 |
* @type {Array<string>} |
| 52 |
* @private |
| 53 |
*/ |
| 54 |
this.colors_ = opt_colors || []; |
| 55 |
|
| 56 |
goog.ui.Palette.call( |
| 57 |
this, null, opt_renderer || goog.ui.PaletteRenderer.getInstance(), |
| 58 |
opt_domHelper); |
| 59 |
|
| 60 |
// Set the colors separately from the super call since we need the correct |
| 61 |
// DomHelper to be initialized for this class. |
| 62 |
this.setColors(this.colors_); |
| 63 |
}; |
| 64 |
goog.inherits(goog.ui.ColorPalette, goog.ui.Palette); |
| 65 |
goog.tagUnsealableClass(goog.ui.ColorPalette); |
| 66 |
|
| 67 |
|
| 68 |
/** |
| 69 |
* Array of normalized colors. Initialized lazily as often never needed. |
| 70 |
* @type {?Array<string>} |
| 71 |
* @private |
| 72 |
*/ |
| 73 |
goog.ui.ColorPalette.prototype.normalizedColors_ = null; |
| 74 |
|
| 75 |
|
| 76 |
/** |
| 77 |
* Array of labels for the colors. Will be used for the tooltips and |
| 78 |
* accessibility. |
| 79 |
* @type {?Array<string>} |
| 80 |
* @private |
| 81 |
*/ |
| 82 |
goog.ui.ColorPalette.prototype.labels_ = null; |
| 83 |
|
| 84 |
|
| 85 |
/** |
| 86 |
* Returns the array of colors represented in the color palette. |
| 87 |
* @return {Array<string>} Array of colors. |
| 88 |
*/ |
| 89 |
goog.ui.ColorPalette.prototype.getColors = function() { |
| 90 |
return this.colors_; |
| 91 |
}; |
| 92 |
|
| 93 |
|
| 94 |
/** |
| 95 |
* Sets the colors that are contained in the palette. |
| 96 |
* @param {Array<string>} colors Array of colors in any valid CSS color format. |
| 97 |
* @param {Array<string>=} opt_labels The array of labels to be used as |
| 98 |
* tooltips. When not provided, the color value will be used. |
| 99 |
*/ |
| 100 |
goog.ui.ColorPalette.prototype.setColors = function(colors, opt_labels) { |
| 101 |
this.colors_ = colors; |
| 102 |
this.labels_ = opt_labels || null; |
| 103 |
this.normalizedColors_ = null; |
| 104 |
this.setContent(this.createColorNodes()); |
| 105 |
}; |
| 106 |
|
| 107 |
|
| 108 |
/** |
| 109 |
* @return {?string} The current selected color in hex, or null. |
| 110 |
*/ |
| 111 |
goog.ui.ColorPalette.prototype.getSelectedColor = function() { |
| 112 |
var selectedItem = /** @type {Element} */ (this.getSelectedItem()); |
| 113 |
if (selectedItem) { |
| 114 |
var color = goog.style.getStyle(selectedItem, 'background-color'); |
| 115 |
return goog.ui.ColorPalette.parseColor_(color); |
| 116 |
} else { |
| 117 |
return null; |
| 118 |
} |
| 119 |
}; |
| 120 |
|
| 121 |
|
| 122 |
/** |
| 123 |
* Sets the selected color. Clears the selection if the argument is null or |
| 124 |
* can't be parsed as a color. |
| 125 |
* @param {?string} color The color to set as selected; null clears the |
| 126 |
* selection. |
| 127 |
*/ |
| 128 |
goog.ui.ColorPalette.prototype.setSelectedColor = function(color) { |
| 129 |
var hexColor = goog.ui.ColorPalette.parseColor_(color); |
| 130 |
if (!this.normalizedColors_) { |
| 131 |
this.normalizedColors_ = goog.array.map(this.colors_, function(color) { |
| 132 |
return goog.ui.ColorPalette.parseColor_(color); |
| 133 |
}); |
| 134 |
} |
| 135 |
this.setSelectedIndex( |
| 136 |
hexColor ? goog.array.indexOf(this.normalizedColors_, hexColor) : -1); |
| 137 |
}; |
| 138 |
|
| 139 |
|
| 140 |
/** |
| 141 |
* @return {!Array<!Node>} An array of DOM nodes for each color. |
| 142 |
* @protected |
| 143 |
*/ |
| 144 |
goog.ui.ColorPalette.prototype.createColorNodes = function() { |
| 145 |
return goog.array.map(this.colors_, function(color, index) { |
| 146 |
var swatch = this.getDomHelper().createDom(goog.dom.TagName.DIV, { |
| 147 |
'class': goog.getCssName(this.getRenderer().getCssClass(), 'colorswatch'), |
| 148 |
'style': 'background-color:' + color |
| 149 |
}); |
| 150 |
if (this.labels_ && this.labels_[index]) { |
| 151 |
swatch.title = this.labels_[index]; |
| 152 |
} else { |
| 153 |
swatch.title = color.charAt(0) == '#' ? |
| 154 |
'RGB (' + goog.color.hexToRgb(color).join(', ') + ')' : |
| 155 |
color; |
| 156 |
} |
| 157 |
return swatch; |
| 158 |
}, this); |
| 159 |
}; |
| 160 |
|
| 161 |
|
| 162 |
/** |
| 163 |
* Takes a string, attempts to parse it as a color spec, and returns a |
| 164 |
* normalized hex color spec if successful (null otherwise). |
| 165 |
* @param {?string} color String possibly containing a color spec; may be null. |
| 166 |
* @return {?string} Normalized hex color spec, or null if the argument can't |
| 167 |
* be parsed as a color. |
| 168 |
* @private |
| 169 |
*/ |
| 170 |
goog.ui.ColorPalette.parseColor_ = function(color) { |
| 171 |
if (color) { |
| 172 |
|
| 173 |
try { |
| 174 |
return goog.color.parse(color).hex; |
| 175 |
} catch (ex) { |
| 176 |
// Fall through. |
| 177 |
} |
| 178 |
} |
| 179 |
return null; |
| 180 |
}; |