| 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 color palette with a button for adding additional colors |
| 17 |
* manually. |
| 18 |
* |
| 19 |
*/ |
| 20 |
|
| 21 |
goog.provide('goog.ui.CustomColorPalette'); |
| 22 |
|
| 23 |
goog.require('goog.color'); |
| 24 |
goog.require('goog.dom'); |
| 25 |
goog.require('goog.dom.TagName'); |
| 26 |
goog.require('goog.dom.classlist'); |
| 27 |
goog.require('goog.ui.ColorPalette'); |
| 28 |
goog.require('goog.ui.Component'); |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
/** |
| 33 |
* A custom color palette is a grid of color swatches and a button that allows |
| 34 |
* the user to add additional colors to the palette |
| 35 |
* |
| 36 |
* @param {Array<string>} initColors Array of initial colors to populate the |
| 37 |
* palette with. |
| 38 |
* @param {goog.ui.PaletteRenderer=} opt_renderer Renderer used to render or |
| 39 |
* decorate the palette; defaults to {@link goog.ui.PaletteRenderer}. |
| 40 |
* @param {goog.dom.DomHelper=} opt_domHelper Optional DOM helper, used for |
| 41 |
* document interaction. |
| 42 |
* @constructor |
| 43 |
* @extends {goog.ui.ColorPalette} |
| 44 |
* @final |
| 45 |
*/ |
| 46 |
goog.ui.CustomColorPalette = function(initColors, opt_renderer, opt_domHelper) { |
| 47 |
goog.ui.ColorPalette.call(this, initColors, opt_renderer, opt_domHelper); |
| 48 |
this.setSupportedState(goog.ui.Component.State.OPENED, true); |
| 49 |
}; |
| 50 |
goog.inherits(goog.ui.CustomColorPalette, goog.ui.ColorPalette); |
| 51 |
|
| 52 |
|
| 53 |
/** |
| 54 |
* Returns an array of DOM nodes for each color, and an additional cell with a |
| 55 |
* '+'. |
| 56 |
* @return {!Array<Node>} Array of div elements. |
| 57 |
* @override |
| 58 |
*/ |
| 59 |
goog.ui.CustomColorPalette.prototype.createColorNodes = function() { |
| 60 |
/** @desc Hover caption for the button that allows the user to add a color. */ |
| 61 |
var MSG_CLOSURE_CUSTOM_COLOR_BUTTON = goog.getMsg('Add a color'); |
| 62 |
|
| 63 |
var nl = goog.ui.CustomColorPalette.base(this, 'createColorNodes'); |
| 64 |
nl.push( |
| 65 |
goog.dom.createDom( |
| 66 |
goog.dom.TagName.DIV, { |
| 67 |
'class': goog.getCssName('goog-palette-customcolor'), |
| 68 |
'title': MSG_CLOSURE_CUSTOM_COLOR_BUTTON |
| 69 |
}, |
| 70 |
'+')); |
| 71 |
return nl; |
| 72 |
}; |
| 73 |
|
| 74 |
|
| 75 |
/** |
| 76 |
* @override |
| 77 |
* @param {goog.events.Event} e Mouse or key event that triggered the action. |
| 78 |
* @return {boolean} True if the action was allowed to proceed, false otherwise. |
| 79 |
*/ |
| 80 |
goog.ui.CustomColorPalette.prototype.performActionInternal = function(e) { |
| 81 |
var item = /** @type {Element} */ (this.getHighlightedItem()); |
| 82 |
if (item) { |
| 83 |
if (goog.dom.classlist.contains( |
| 84 |
item, goog.getCssName('goog-palette-customcolor'))) { |
| 85 |
// User activated the special "add custom color" swatch. |
| 86 |
this.promptForCustomColor(); |
| 87 |
} else { |
| 88 |
// User activated a normal color swatch. |
| 89 |
this.setSelectedItem(item); |
| 90 |
return this.dispatchEvent(goog.ui.Component.EventType.ACTION); |
| 91 |
} |
| 92 |
} |
| 93 |
return false; |
| 94 |
}; |
| 95 |
|
| 96 |
|
| 97 |
/** |
| 98 |
* Prompts the user to enter a custom color. Currently uses a window.prompt |
| 99 |
* but could be updated to use a dialog box with a WheelColorPalette. |
| 100 |
*/ |
| 101 |
goog.ui.CustomColorPalette.prototype.promptForCustomColor = function() { |
| 102 |
/** @desc Default custom color dialog. */ |
| 103 |
var MSG_CLOSURE_CUSTOM_COLOR_PROMPT = goog.getMsg( |
| 104 |
'Input custom color, i.e. pink, #F00, #D015FF or rgb(100, 50, 25)'); |
| 105 |
|
| 106 |
// A CustomColorPalette is considered "open" while the color selection prompt |
| 107 |
// is open. Enabling state transition events for the OPENED state and |
| 108 |
// listening for OPEN events allows clients to save the selection before |
| 109 |
// it is destroyed (see e.g. bug 1064701). |
| 110 |
var response = null; |
| 111 |
this.setOpen(true); |
| 112 |
if (this.isOpen()) { |
| 113 |
// The OPEN event wasn't canceled; prompt for custom color. |
| 114 |
response = window.prompt(MSG_CLOSURE_CUSTOM_COLOR_PROMPT, '#FFFFFF'); |
| 115 |
this.setOpen(false); |
| 116 |
} |
| 117 |
|
| 118 |
if (!response) { |
| 119 |
// The user hit cancel |
| 120 |
return; |
| 121 |
} |
| 122 |
|
| 123 |
var color; |
| 124 |
|
| 125 |
try { |
| 126 |
color = goog.color.parse(response).hex; |
| 127 |
} catch (er) { |
| 128 |
/** @desc Alert message sent when the input string is not a valid color. */ |
| 129 |
var MSG_CLOSURE_CUSTOM_COLOR_INVALID_INPUT = goog.getMsg( |
| 130 |
'ERROR: "{$color}" is not a valid color.', {'color': response}); |
| 131 |
alert(MSG_CLOSURE_CUSTOM_COLOR_INVALID_INPUT); |
| 132 |
return; |
| 133 |
} |
| 134 |
|
| 135 |
// TODO(user): This is relatively inefficient. Consider adding |
| 136 |
// functionality to palette to add individual items after render time. |
| 137 |
var colors = this.getColors(); |
| 138 |
colors.push(color); |
| 139 |
this.setColors(colors); |
| 140 |
|
| 141 |
// Set the selected color to the new color and notify listeners of the action. |
| 142 |
this.setSelectedColor(color); |
| 143 |
this.dispatchEvent(goog.ui.Component.EventType.ACTION); |
| 144 |
}; |