3 This file is part of Ext JS 4
5 Copyright (c) 2011 Sencha Inc
7 Contact: http://www.sencha.com/contact
9 GNU General Public License Usage
10 This file may be used under the terms of the GNU General Public License version 3.0 as published by the Free Software Foundation and appearing in the file LICENSE included in the packaging of this file. Please review the following information to ensure the GNU General Public License version 3.0 requirements will be met: http://www.gnu.org/copyleft/gpl.html.
12 If you are unsure which license is appropriate for your use, please contact the sales department at http://www.sencha.com/contact.
16 * @class Ext.picker.Color
17 * @extends Ext.Component
18 * <p>ColorPicker provides a simple color palette for choosing colors. The picker can be rendered to any container.
19 * The available default to a standard 40-color palette; this can be customized with the {@link #colors} config.</p>
20 * <p>Typically you will need to implement a handler function to be notified when the user chooses a color from the
21 * picker; you can register the handler using the {@link #select} event, or by implementing the {@link #handler}
23 * <p>Here's an example of typical usage:</p>
24 * <pre><code>var cp = new Ext.picker.Color({
25 value: '993300', // initial selected color
29 cp.on('select', function(picker, selColor){
30 // do something with selColor
33 * {@img Ext.picker.Color/Ext.picker.Color.png Ext.picker.Color component}
36 Ext.define('Ext.picker.Color', {
37 extend: 'Ext.Component',
38 requires: 'Ext.XTemplate',
39 alias: 'widget.colorpicker',
40 alternateClassName: 'Ext.ColorPalette',
43 * @cfg {String} componentCls
44 * The CSS class to apply to the containing element (defaults to 'x-color-picker')
46 componentCls : Ext.baseCSSPrefix + 'color-picker',
49 * @cfg {String} selectedCls
50 * The CSS class to apply to the selected element
52 selectedCls: Ext.baseCSSPrefix + 'color-picker-selected',
56 * The initial color to highlight (should be a valid 6-digit color hex code without the # symbol). Note that
57 * the hex codes are case-sensitive.
62 * @cfg {String} clickEvent
63 * The DOM event that will cause a color to be selected. This can be any valid event name (dblclick, contextmenu).
64 * Defaults to <tt>'click'</tt>.
69 * @cfg {Boolean} allowReselect If set to true then reselecting a color that is already selected fires the {@link #select} event
71 allowReselect : false,
74 * <p>An array of 6-digit color hex code strings (without the # symbol). This array can contain any number
75 * of colors, and each hex code should be unique. The width of the picker is controlled via CSS by adjusting
76 * the width property of the 'x-color-picker' class (or assigning a custom class), so you can balance the number
77 * of colors with the width setting until the box is symmetrical.</p>
78 * <p>You can override individual colors if needed:</p>
80 var cp = new Ext.picker.Color();
81 cp.colors[0] = 'FF0000'; // change the first box to red
84 Or you can provide a custom array of your own for complete control:
86 var cp = new Ext.picker.Color();
87 cp.colors = ['000000', '993300', '333300'];
92 '000000', '993300', '333300', '003300', '003366', '000080', '333399', '333333',
93 '800000', 'FF6600', '808000', '008000', '008080', '0000FF', '666699', '808080',
94 'FF0000', 'FF9900', '99CC00', '339966', '33CCCC', '3366FF', '800080', '969696',
95 'FF00FF', 'FFCC00', 'FFFF00', '00FF00', '00FFFF', '00CCFF', '993366', 'C0C0C0',
96 'FF99CC', 'FFCC99', 'FFFF99', 'CCFFCC', 'CCFFFF', '99CCFF', 'CC99FF', 'FFFFFF'
100 * @cfg {Function} handler
101 * Optional. A function that will handle the select event of this picker.
102 * The handler is passed the following parameters:<div class="mdetail-params"><ul>
103 * <li><code>picker</code> : ColorPicker<div class="sub-desc">The {@link #picker Ext.picker.Color}.</div></li>
104 * <li><code>color</code> : String<div class="sub-desc">The 6-digit color hex code (without the # symbol).</div></li>
108 * @cfg {Object} scope
109 * The scope (<tt><b>this</b></tt> reference) in which the <code>{@link #handler}</code>
110 * function will be called. Defaults to this ColorPicker instance.
113 colorRe: /(?:^|\s)color-(.{6})(?:\s|$)/,
115 constructor: function() {
116 this.renderTpl = Ext.create('Ext.XTemplate', '<tpl for="colors"><a href="#" class="color-{.}" hidefocus="on"><em><span style="background:#{.}" unselectable="on"> </span></em></a></tpl>');
117 this.callParent(arguments);
121 initComponent : function(){
124 this.callParent(arguments);
128 * Fires when a color is selected
129 * @param {Ext.picker.Color} this
130 * @param {String} color The 6-digit color hex code (without the # symbol)
136 me.on('select', me.handler, me.scope, true);
142 onRender : function(container, position){
144 clickEvent = me.clickEvent;
146 Ext.apply(me.renderData, {
150 this.callParent(arguments);
152 me.mon(me.el, clickEvent, me.handleClick, me, {delegate: 'a'});
153 // always stop following the anchors
154 if(clickEvent != 'click'){
155 me.mon(me.el, 'click', Ext.emptyFn, me, {delegate: 'a', stopEvent: true});
160 afterRender : function(){
164 this.callParent(arguments);
168 me.select(value, true);
173 handleClick : function(event, target){
179 color = target.className.match(me.colorRe)[1];
180 me.select(color.toUpperCase());
185 * Selects the specified color in the picker (fires the {@link #select} event)
186 * @param {String} color A valid 6-digit color hex code (# will be stripped if included)
187 * @param {Boolean} suppressEvent (optional) True to stop the select event from firing. Defaults to <tt>false</tt>.
189 select : function(color, suppressEvent){
192 selectedCls = me.selectedCls,
196 color = color.replace('#', '');
203 if (color != value || me.allowReselect) {
207 el.down('a.color-' + value).removeCls(selectedCls);
209 el.down('a.color-' + color).addCls(selectedCls);
211 if (suppressEvent !== true) {
212 me.fireEvent('select', me, color);
218 * Get the currently selected color value.
219 * @return {String} value The selected value. Null if nothing is selected.
221 getValue: function(){
222 return this.value || null;