popupcolorpicker.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  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 Popup Color Picker implementation. This is intended to be
  16. * less general than goog.ui.ColorPicker and presents a default set of colors
  17. * that CCC apps currently use in their color pickers.
  18. *
  19. * @see ../demos/popupcolorpicker.html
  20. */
  21. goog.provide('goog.ui.PopupColorPicker');
  22. goog.require('goog.asserts');
  23. goog.require('goog.dom.classlist');
  24. goog.require('goog.events.EventType');
  25. goog.require('goog.positioning.AnchoredPosition');
  26. goog.require('goog.positioning.Corner');
  27. goog.require('goog.ui.ColorPicker');
  28. goog.require('goog.ui.Component');
  29. goog.require('goog.ui.Popup');
  30. /**
  31. * Popup color picker widget.
  32. *
  33. * @param {goog.dom.DomHelper=} opt_domHelper Optional DOM helper.
  34. * @param {goog.ui.ColorPicker=} opt_colorPicker Optional color picker to use
  35. * for this popup.
  36. * @extends {goog.ui.Component}
  37. * @constructor
  38. */
  39. goog.ui.PopupColorPicker = function(opt_domHelper, opt_colorPicker) {
  40. goog.ui.Component.call(this, opt_domHelper);
  41. if (opt_colorPicker) {
  42. this.colorPicker_ = opt_colorPicker;
  43. }
  44. };
  45. goog.inherits(goog.ui.PopupColorPicker, goog.ui.Component);
  46. goog.tagUnsealableClass(goog.ui.PopupColorPicker);
  47. /**
  48. * Whether the color picker is initialized.
  49. * @type {boolean}
  50. * @private
  51. */
  52. goog.ui.PopupColorPicker.prototype.initialized_ = false;
  53. /**
  54. * Instance of a color picker control.
  55. * @type {goog.ui.ColorPicker}
  56. * @private
  57. */
  58. goog.ui.PopupColorPicker.prototype.colorPicker_ = null;
  59. /**
  60. * Instance of goog.ui.Popup used to manage the behavior of the color picker.
  61. * @type {goog.ui.Popup}
  62. * @private
  63. */
  64. goog.ui.PopupColorPicker.prototype.popup_ = null;
  65. /**
  66. * Corner of the popup which is pinned to the attaching element.
  67. * @type {goog.positioning.Corner}
  68. * @private
  69. */
  70. goog.ui.PopupColorPicker.prototype.pinnedCorner_ =
  71. goog.positioning.Corner.TOP_START;
  72. /**
  73. * Corner of the attaching element where the popup shows.
  74. * @type {goog.positioning.Corner}
  75. * @private
  76. */
  77. goog.ui.PopupColorPicker.prototype.popupCorner_ =
  78. goog.positioning.Corner.BOTTOM_START;
  79. /**
  80. * Reference to the element that triggered the last popup.
  81. * @type {Element}
  82. * @private
  83. */
  84. goog.ui.PopupColorPicker.prototype.lastTarget_ = null;
  85. /** @private {boolean} */
  86. goog.ui.PopupColorPicker.prototype.rememberSelection_;
  87. /**
  88. * Whether the color picker can move the focus to its key event target when it
  89. * is shown. The default is true. Setting to false can break keyboard
  90. * navigation, but this is needed for certain scenarios, for example the
  91. * toolbar menu in trogedit which can't have the selection changed.
  92. * @type {boolean}
  93. * @private
  94. */
  95. goog.ui.PopupColorPicker.prototype.allowAutoFocus_ = true;
  96. /**
  97. * Whether the color picker can accept focus.
  98. * @type {boolean}
  99. * @private
  100. */
  101. goog.ui.PopupColorPicker.prototype.focusable_ = true;
  102. /**
  103. * If true, then the colorpicker will toggle off if it is already visible.
  104. *
  105. * @type {boolean}
  106. * @private
  107. */
  108. goog.ui.PopupColorPicker.prototype.toggleMode_ = true;
  109. /**
  110. * If true, the colorpicker will appear on hover.
  111. * @type {boolean}
  112. * @private
  113. */
  114. goog.ui.PopupColorPicker.prototype.showOnHover_ = false;
  115. /** @override */
  116. goog.ui.PopupColorPicker.prototype.createDom = function() {
  117. goog.ui.PopupColorPicker.superClass_.createDom.call(this);
  118. this.popup_ = new goog.ui.Popup(this.getElement());
  119. this.popup_.setPinnedCorner(this.pinnedCorner_);
  120. goog.dom.classlist.set(
  121. goog.asserts.assert(this.getElement()),
  122. goog.getCssName('goog-popupcolorpicker'));
  123. this.getElement().unselectable = 'on';
  124. };
  125. /** @override */
  126. goog.ui.PopupColorPicker.prototype.disposeInternal = function() {
  127. goog.ui.PopupColorPicker.superClass_.disposeInternal.call(this);
  128. this.colorPicker_ = null;
  129. this.lastTarget_ = null;
  130. this.initialized_ = false;
  131. if (this.popup_) {
  132. this.popup_.dispose();
  133. this.popup_ = null;
  134. }
  135. };
  136. /**
  137. * ColorPickers cannot be used to decorate pre-existing html, since the
  138. * structure they build is fairly complicated.
  139. * @param {Element} element Element to decorate.
  140. * @return {boolean} Returns always false.
  141. * @override
  142. */
  143. goog.ui.PopupColorPicker.prototype.canDecorate = function(element) {
  144. return false;
  145. };
  146. /**
  147. * @return {goog.ui.ColorPicker} The color picker instance.
  148. */
  149. goog.ui.PopupColorPicker.prototype.getColorPicker = function() {
  150. return this.colorPicker_;
  151. };
  152. /**
  153. * Returns whether the Popup dismisses itself when the user clicks outside of
  154. * it.
  155. * @return {boolean} Whether the Popup autohides on an external click.
  156. */
  157. goog.ui.PopupColorPicker.prototype.getAutoHide = function() {
  158. return !!this.popup_ && this.popup_.getAutoHide();
  159. };
  160. /**
  161. * Sets whether the Popup dismisses itself when the user clicks outside of it -
  162. * must be called after the Popup has been created (in createDom()),
  163. * otherwise it does nothing.
  164. *
  165. * @param {boolean} autoHide Whether to autohide on an external click.
  166. */
  167. goog.ui.PopupColorPicker.prototype.setAutoHide = function(autoHide) {
  168. if (this.popup_) {
  169. this.popup_.setAutoHide(autoHide);
  170. }
  171. };
  172. /**
  173. * Returns the region inside which the Popup dismisses itself when the user
  174. * clicks, or null if it was not set. Null indicates the entire document is
  175. * the autohide region.
  176. * @return {Element} The DOM element for autohide, or null if it hasn't been
  177. * set.
  178. */
  179. goog.ui.PopupColorPicker.prototype.getAutoHideRegion = function() {
  180. return this.popup_ && this.popup_.getAutoHideRegion();
  181. };
  182. /**
  183. * Sets the region inside which the Popup dismisses itself when the user
  184. * clicks - must be called after the Popup has been created (in createDom()),
  185. * otherwise it does nothing.
  186. *
  187. * @param {Element} element The DOM element for autohide.
  188. */
  189. goog.ui.PopupColorPicker.prototype.setAutoHideRegion = function(element) {
  190. if (this.popup_) {
  191. this.popup_.setAutoHideRegion(element);
  192. }
  193. };
  194. /**
  195. * Returns the {@link goog.ui.PopupBase} from this picker. Returns null if the
  196. * popup has not yet been created.
  197. *
  198. * NOTE: This should *ONLY* be called from tests. If called before createDom(),
  199. * this should return null.
  200. *
  201. * @return {goog.ui.PopupBase?} The popup or null if it hasn't been created.
  202. */
  203. goog.ui.PopupColorPicker.prototype.getPopup = function() {
  204. return this.popup_;
  205. };
  206. /**
  207. * @return {Element} The last element that triggered the popup.
  208. */
  209. goog.ui.PopupColorPicker.prototype.getLastTarget = function() {
  210. return this.lastTarget_;
  211. };
  212. /**
  213. * Attaches the popup color picker to an element.
  214. * @param {Element} element The element to attach to.
  215. */
  216. goog.ui.PopupColorPicker.prototype.attach = function(element) {
  217. if (this.showOnHover_) {
  218. this.getHandler().listen(
  219. element, goog.events.EventType.MOUSEOVER, this.show_);
  220. } else {
  221. this.getHandler().listen(
  222. element, goog.events.EventType.MOUSEDOWN, this.show_);
  223. }
  224. };
  225. /**
  226. * Detatches the popup color picker from an element.
  227. * @param {Element} element The element to detach from.
  228. */
  229. goog.ui.PopupColorPicker.prototype.detach = function(element) {
  230. if (this.showOnHover_) {
  231. this.getHandler().unlisten(
  232. element, goog.events.EventType.MOUSEOVER, this.show_);
  233. } else {
  234. this.getHandler().unlisten(
  235. element, goog.events.EventType.MOUSEOVER, this.show_);
  236. }
  237. };
  238. /**
  239. * Gets the color that is currently selected in this color picker.
  240. * @return {?string} The hex string of the color selected, or null if no
  241. * color is selected.
  242. */
  243. goog.ui.PopupColorPicker.prototype.getSelectedColor = function() {
  244. return this.colorPicker_.getSelectedColor();
  245. };
  246. /**
  247. * Sets whether the color picker can accept focus.
  248. * @param {boolean} focusable True iff the color picker can accept focus.
  249. */
  250. goog.ui.PopupColorPicker.prototype.setFocusable = function(focusable) {
  251. this.focusable_ = focusable;
  252. if (this.colorPicker_) {
  253. // TODO(user): In next revision sort the behavior of passing state to
  254. // children correctly
  255. this.colorPicker_.setFocusable(focusable);
  256. }
  257. };
  258. /**
  259. * Sets whether the color picker can automatically move focus to its key event
  260. * target when it is set to visible.
  261. * @param {boolean} allow Whether to allow auto focus.
  262. */
  263. goog.ui.PopupColorPicker.prototype.setAllowAutoFocus = function(allow) {
  264. this.allowAutoFocus_ = allow;
  265. };
  266. /**
  267. * @return {boolean} Whether the color picker can automatically move focus to
  268. * its key event target when it is set to visible.
  269. */
  270. goog.ui.PopupColorPicker.prototype.getAllowAutoFocus = function() {
  271. return this.allowAutoFocus_;
  272. };
  273. /**
  274. * Sets whether the color picker should toggle off if it is already open.
  275. * @param {boolean} toggle The new toggle mode.
  276. */
  277. goog.ui.PopupColorPicker.prototype.setToggleMode = function(toggle) {
  278. this.toggleMode_ = toggle;
  279. };
  280. /**
  281. * Gets whether the colorpicker is in toggle mode
  282. * @return {boolean} toggle.
  283. */
  284. goog.ui.PopupColorPicker.prototype.getToggleMode = function() {
  285. return this.toggleMode_;
  286. };
  287. /**
  288. * Sets whether the picker remembers the last selected color between popups.
  289. *
  290. * @param {boolean} remember Whether to remember the selection.
  291. */
  292. goog.ui.PopupColorPicker.prototype.setRememberSelection = function(remember) {
  293. this.rememberSelection_ = remember;
  294. };
  295. /**
  296. * @return {boolean} Whether the picker remembers the last selected color
  297. * between popups.
  298. */
  299. goog.ui.PopupColorPicker.prototype.getRememberSelection = function() {
  300. return this.rememberSelection_;
  301. };
  302. /**
  303. * Add an array of colors to the colors displayed by the color picker.
  304. * Does not add duplicated colors.
  305. * @param {Array<string>} colors The array of colors to be added.
  306. */
  307. goog.ui.PopupColorPicker.prototype.addColors = function(colors) {
  308. };
  309. /**
  310. * Clear the colors displayed by the color picker.
  311. */
  312. goog.ui.PopupColorPicker.prototype.clearColors = function() {
  313. };
  314. /**
  315. * Set the pinned corner of the popup.
  316. * @param {goog.positioning.Corner} corner The corner of the popup which is
  317. * pinned to the attaching element.
  318. */
  319. goog.ui.PopupColorPicker.prototype.setPinnedCorner = function(corner) {
  320. this.pinnedCorner_ = corner;
  321. if (this.popup_) {
  322. this.popup_.setPinnedCorner(this.pinnedCorner_);
  323. }
  324. };
  325. /**
  326. * Sets which corner of the attaching element this popup shows up.
  327. * @param {goog.positioning.Corner} corner The corner of the attaching element
  328. * where to show the popup.
  329. */
  330. goog.ui.PopupColorPicker.prototype.setPopupCorner = function(corner) {
  331. this.popupCorner_ = corner;
  332. };
  333. /**
  334. * Sets whether the popup shows up on hover. By default, appears on click.
  335. * @param {boolean} showOnHover True if popup should appear on hover.
  336. */
  337. goog.ui.PopupColorPicker.prototype.setShowOnHover = function(showOnHover) {
  338. this.showOnHover_ = showOnHover;
  339. };
  340. /**
  341. * Handles click events on the targets and shows the color picker.
  342. * @param {goog.events.BrowserEvent} e The browser event.
  343. * @private
  344. */
  345. goog.ui.PopupColorPicker.prototype.show_ = function(e) {
  346. if (!this.initialized_) {
  347. this.colorPicker_ = this.colorPicker_ ||
  348. goog.ui.ColorPicker.createSimpleColorGrid(this.getDomHelper());
  349. this.colorPicker_.setFocusable(this.focusable_);
  350. this.addChild(this.colorPicker_, true);
  351. this.getHandler().listen(
  352. this.colorPicker_, goog.ui.ColorPicker.EventType.CHANGE,
  353. this.onColorPicked_);
  354. this.initialized_ = true;
  355. }
  356. if (this.popup_.isOrWasRecentlyVisible() && this.toggleMode_ &&
  357. this.lastTarget_ == e.currentTarget) {
  358. this.popup_.setVisible(false);
  359. return;
  360. }
  361. this.lastTarget_ = /** @type {Element} */ (e.currentTarget);
  362. this.popup_.setPosition(
  363. new goog.positioning.AnchoredPosition(
  364. this.lastTarget_, this.popupCorner_));
  365. if (!this.rememberSelection_) {
  366. this.colorPicker_.setSelectedIndex(-1);
  367. }
  368. this.popup_.setVisible(true);
  369. if (this.allowAutoFocus_) {
  370. this.colorPicker_.focus();
  371. }
  372. };
  373. /**
  374. * Handles the color change event.
  375. * @param {goog.events.Event} e The event.
  376. * @private
  377. */
  378. goog.ui.PopupColorPicker.prototype.onColorPicked_ = function(e) {
  379. // When we show the color picker we reset the color, which triggers an event.
  380. // Here we block that event so that it doesn't dismiss the popup
  381. // TODO(user): Update the colorpicker to allow selection to be cleared
  382. if (this.colorPicker_.getSelectedIndex() == -1) {
  383. e.stopPropagation();
  384. return;
  385. }
  386. this.popup_.setVisible(false);
  387. if (this.allowAutoFocus_) {
  388. this.lastTarget_.focus();
  389. }
  390. };