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