colormenubutton.js 7.2 KB

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