colorpalette.js 5.5 KB

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