Upgrade to ExtJS 4.0.2 - Released 06/09/2011
[extjs.git] / src / picker / Color.js
1 /*
2
3 This file is part of Ext JS 4
4
5 Copyright (c) 2011 Sencha Inc
6
7 Contact:  http://www.sencha.com/contact
8
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.
11
12 If you are unsure which license is appropriate for your use, please contact the sales department at http://www.sencha.com/contact.
13
14 */
15 /**
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}
22  * method.</p>
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
26     renderTo: 'my-div'
27 });
28
29 cp.on('select', function(picker, selColor){
30     // do something with selColor
31 });
32 </code></pre>
33  * {@img Ext.picker.Color/Ext.picker.Color.png Ext.picker.Color component}
34  *
35  */
36 Ext.define('Ext.picker.Color', {
37     extend: 'Ext.Component',
38     requires: 'Ext.XTemplate',
39     alias: 'widget.colorpicker',
40     alternateClassName: 'Ext.ColorPalette',
41     
42     /**
43      * @cfg {String} componentCls
44      * The CSS class to apply to the containing element (defaults to 'x-color-picker')
45      */
46     componentCls : Ext.baseCSSPrefix + 'color-picker',
47     
48     /**
49      * @cfg {String} selectedCls
50      * The CSS class to apply to the selected element
51      */
52     selectedCls: Ext.baseCSSPrefix + 'color-picker-selected',
53     
54     /**
55      * @cfg {String} value
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.
58      */
59     value : null,
60     
61     /**
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>.
65      */
66     clickEvent :'click',
67
68     /**
69      * @cfg {Boolean} allowReselect If set to true then reselecting a color that is already selected fires the {@link #select} event
70      */
71     allowReselect : false,
72
73     /**
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>
79      * <pre><code>
80 var cp = new Ext.picker.Color();
81 cp.colors[0] = 'FF0000';  // change the first box to red
82 </code></pre>
83
84 Or you can provide a custom array of your own for complete control:
85 <pre><code>
86 var cp = new Ext.picker.Color();
87 cp.colors = ['000000', '993300', '333300'];
88 </code></pre>
89      * @type Array
90      */
91     colors : [
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'
97     ],
98
99     /**
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>
105      * </ul></div>
106      */
107     /**
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.
111      */
112     
113     colorRe: /(?:^|\s)color-(.{6})(?:\s|$)/,
114     
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">&#160;</span></em></a></tpl>');
117         this.callParent(arguments);
118     },
119     
120     // private
121     initComponent : function(){
122         var me = this;
123         
124         this.callParent(arguments);
125         me.addEvents(
126             /**
127              * @event select
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)
131              */
132             'select'
133         );
134
135         if (me.handler) {
136             me.on('select', me.handler, me.scope, true);
137         }
138     },
139
140
141     // private
142     onRender : function(container, position){
143         var me = this,
144             clickEvent = me.clickEvent;
145             
146         Ext.apply(me.renderData, {
147             itemCls: me.itemCls,
148             colors: me.colors    
149         });
150         this.callParent(arguments);
151
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});
156         }
157     },
158
159     // private
160     afterRender : function(){
161         var me = this,
162             value;
163             
164         this.callParent(arguments);
165         if (me.value) {
166             value = me.value;
167             me.value = null;
168             me.select(value, true);
169         }
170     },
171
172     // private
173     handleClick : function(event, target){
174         var me = this,
175             color;
176             
177         event.stopEvent();
178         if (!me.disabled) {
179             color = target.className.match(me.colorRe)[1];
180             me.select(color.toUpperCase());
181         }
182     },
183
184     /**
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>.
188      */
189     select : function(color, suppressEvent){
190         
191         var me = this,
192             selectedCls = me.selectedCls,
193             value = me.value,
194             el;
195             
196         color = color.replace('#', '');
197         if (!me.rendered) {
198             me.value = color;
199             return;
200         }
201         
202         
203         if (color != value || me.allowReselect) {
204             el = me.el;
205
206             if (me.value) {
207                 el.down('a.color-' + value).removeCls(selectedCls);
208             }
209             el.down('a.color-' + color).addCls(selectedCls);
210             me.value = color;
211             if (suppressEvent !== true) {
212                 me.fireEvent('select', me, color);
213             }
214         }
215     },
216     
217     /**
218      * Get the currently selected color value.
219      * @return {String} value The selected value. Null if nothing is selected.
220      */
221     getValue: function(){
222         return this.value || null;
223     }
224 });
225