Upgrade to ExtJS 4.0.0 - Released 04/26/2011
[extjs.git] / docs / source / Color.html
1 <!DOCTYPE html><html><head><title>Sencha Documentation Project</title><link rel="stylesheet" href="../reset.css" type="text/css"><link rel="stylesheet" href="../prettify.css" type="text/css"><link rel="stylesheet" href="../prettify_sa.css" type="text/css"><script type="text/javascript" src="../prettify.js"></script></head><body onload="prettyPrint()"><pre class="prettyprint"><pre><span id='Ext-picker.Color-method-constructor'><span id='Ext-picker.Color'>/**
2 </span></span> * @class Ext.picker.Color
3  * @extends Ext.Component
4  * &lt;p&gt;ColorPicker provides a simple color palette for choosing colors. The picker can be rendered to any container.
5  * The available default to a standard 40-color palette; this can be customized with the {@link #colors} config.&lt;/p&gt;
6  * &lt;p&gt;Typically you will need to implement a handler function to be notified when the user chooses a color from the
7  * picker; you can register the handler using the {@link #select} event, or by implementing the {@link #handler}
8  * method.&lt;/p&gt;
9  * &lt;p&gt;Here's an example of typical usage:&lt;/p&gt;
10  * &lt;pre&gt;&lt;code&gt;var cp = new Ext.picker.Color({
11     value: '993300',  // initial selected color
12     renderTo: 'my-div'
13 });
14
15 cp.on('select', function(picker, selColor){
16     // do something with selColor
17 });
18 &lt;/code&gt;&lt;/pre&gt;
19  * {@img Ext.picker.Color/Ext.picker.Color.png Ext.picker.Color component}
20  *
21  * @constructor
22  * Create a new ColorPicker
23  * @param {Object} config The config object
24  * 
25  * @xtype colorpicker
26  */
27 Ext.define('Ext.picker.Color', {
28     extend: 'Ext.Component',
29     requires: 'Ext.XTemplate',
30     alias: 'widget.colorpicker',
31     alternateClassName: 'Ext.ColorPalette',
32     
33 <span id='Ext-picker.Color-cfg-componentCls'>    /**
34 </span>     * @cfg {String} componentCls
35      * The CSS class to apply to the containing element (defaults to 'x-color-picker')
36      */
37     componentCls : Ext.baseCSSPrefix + 'color-picker',
38     
39 <span id='Ext-picker.Color-cfg-selectedCls'>    /**
40 </span>     * @cfg {String} selectedCls
41      * The CSS class to apply to the selected element
42      */
43     selectedCls: Ext.baseCSSPrefix + 'color-picker-selected',
44     
45 <span id='Ext-picker.Color-cfg-value'>    /**
46 </span>     * @cfg {String} value
47      * The initial color to highlight (should be a valid 6-digit color hex code without the # symbol).  Note that
48      * the hex codes are case-sensitive.
49      */
50     value : null,
51     
52 <span id='Ext-picker.Color-cfg-clickEvent'>    /**
53 </span>     * @cfg {String} clickEvent
54      * The DOM event that will cause a color to be selected. This can be any valid event name (dblclick, contextmenu).
55      * Defaults to &lt;tt&gt;'click'&lt;/tt&gt;.
56      */
57     clickEvent :'click',
58
59 <span id='Ext-picker.Color-cfg-allowReselect'>    /**
60 </span>     * @cfg {Boolean} allowReselect If set to true then reselecting a color that is already selected fires the {@link #select} event
61      */
62     allowReselect : false,
63
64 <span id='Ext-picker.Color-property-colors'>    /**
65 </span>     * &lt;p&gt;An array of 6-digit color hex code strings (without the # symbol).  This array can contain any number
66      * of colors, and each hex code should be unique.  The width of the picker is controlled via CSS by adjusting
67      * the width property of the 'x-color-picker' class (or assigning a custom class), so you can balance the number
68      * of colors with the width setting until the box is symmetrical.&lt;/p&gt;
69      * &lt;p&gt;You can override individual colors if needed:&lt;/p&gt;
70      * &lt;pre&gt;&lt;code&gt;
71 var cp = new Ext.picker.Color();
72 cp.colors[0] = 'FF0000';  // change the first box to red
73 &lt;/code&gt;&lt;/pre&gt;
74
75 Or you can provide a custom array of your own for complete control:
76 &lt;pre&gt;&lt;code&gt;
77 var cp = new Ext.picker.Color();
78 cp.colors = ['000000', '993300', '333300'];
79 &lt;/code&gt;&lt;/pre&gt;
80      * @type Array
81      */
82     colors : [
83         '000000', '993300', '333300', '003300', '003366', '000080', '333399', '333333',
84         '800000', 'FF6600', '808000', '008000', '008080', '0000FF', '666699', '808080',
85         'FF0000', 'FF9900', '99CC00', '339966', '33CCCC', '3366FF', '800080', '969696',
86         'FF00FF', 'FFCC00', 'FFFF00', '00FF00', '00FFFF', '00CCFF', '993366', 'C0C0C0',
87         'FF99CC', 'FFCC99', 'FFFF99', 'CCFFCC', 'CCFFFF', '99CCFF', 'CC99FF', 'FFFFFF'
88     ],
89
90 <span id='Ext-picker.Color-cfg-handler'>    /**
91 </span>     * @cfg {Function} handler
92      * Optional. A function that will handle the select event of this picker.
93      * The handler is passed the following parameters:&lt;div class=&quot;mdetail-params&quot;&gt;&lt;ul&gt;
94      * &lt;li&gt;&lt;code&gt;picker&lt;/code&gt; : ColorPicker&lt;div class=&quot;sub-desc&quot;&gt;The {@link #picker Ext.picker.Color}.&lt;/div&gt;&lt;/li&gt;
95      * &lt;li&gt;&lt;code&gt;color&lt;/code&gt; : String&lt;div class=&quot;sub-desc&quot;&gt;The 6-digit color hex code (without the # symbol).&lt;/div&gt;&lt;/li&gt;
96      * &lt;/ul&gt;&lt;/div&gt;
97      */
98 <span id='Ext-picker.Color-cfg-scope'>    /**
99 </span>     * @cfg {Object} scope
100      * The scope (&lt;tt&gt;&lt;b&gt;this&lt;/b&gt;&lt;/tt&gt; reference) in which the &lt;code&gt;{@link #handler}&lt;/code&gt;
101      * function will be called.  Defaults to this ColorPicker instance.
102      */
103     
104     colorRe: /(?:^|\s)color-(.{6})(?:\s|$)/,
105     
106     constructor: function() {
107         this.renderTpl = Ext.create('Ext.XTemplate', '&lt;tpl for=&quot;colors&quot;&gt;&lt;a href=&quot;#&quot; class=&quot;color-{.}&quot; hidefocus=&quot;on&quot;&gt;&lt;em&gt;&lt;span style=&quot;background:#{.}&quot; unselectable=&quot;on&quot;&gt;&amp;#160;&lt;/span&gt;&lt;/em&gt;&lt;/a&gt;&lt;/tpl&gt;');
108         this.callParent(arguments);
109     },
110     
111     // private
112     initComponent : function(){
113         var me = this;
114         
115         this.callParent(arguments);
116         me.addEvents(
117 <span id='Ext-picker.Color-event-select'>            /**
118 </span>             * @event select
119              * Fires when a color is selected
120              * @param {Ext.picker.Color} this
121              * @param {String} color The 6-digit color hex code (without the # symbol)
122              */
123             'select'
124         );
125
126         if (me.handler) {
127             me.on('select', me.handler, me.scope, true);
128         }
129     },
130
131
132     // private
133     onRender : function(container, position){
134         var me = this,
135             clickEvent = me.clickEvent;
136             
137         Ext.apply(me.renderData, {
138             itemCls: me.itemCls,
139             colors: me.colors    
140         });
141         this.callParent(arguments);
142
143         me.mon(me.el, clickEvent, me.handleClick, me, {delegate: 'a'});
144         // always stop following the anchors
145         if(clickEvent != 'click'){
146             me.mon(me.el, 'click', Ext.emptyFn, me, {delegate: 'a', stopEvent: true});
147         }
148     },
149
150     // private
151     afterRender : function(){
152         var me = this,
153             value;
154             
155         this.callParent(arguments);
156         if (me.value) {
157             value = me.value;
158             me.value = null;
159             me.select(value, true);
160         }
161     },
162
163     // private
164     handleClick : function(event, target){
165         var me = this,
166             color;
167             
168         event.stopEvent();
169         if (!me.disabled) {
170             color = target.className.match(me.colorRe)[1];
171             me.select(color.toUpperCase());
172         }
173     },
174
175 <span id='Ext-picker.Color-method-select'>    /**
176 </span>     * Selects the specified color in the picker (fires the {@link #select} event)
177      * @param {String} color A valid 6-digit color hex code (# will be stripped if included)
178      * @param {Boolean} suppressEvent (optional) True to stop the select event from firing. Defaults to &lt;tt&gt;false&lt;/tt&gt;.
179      */
180     select : function(color, suppressEvent){
181         
182         var me = this,
183             selectedCls = me.selectedCls,
184             value = me.value,
185             el;
186             
187         color = color.replace('#', '');
188         if (!me.rendered) {
189             me.value = color;
190             return;
191         }
192         
193         
194         if (color != value || me.allowReselect) {
195             el = me.el;
196
197             if (me.value) {
198                 el.down('a.color-' + value).removeCls(selectedCls);
199             }
200             el.down('a.color-' + color).addCls(selectedCls);
201             me.value = color;
202             if (suppressEvent !== true) {
203                 me.fireEvent('select', me, color);
204             }
205         }
206     },
207     
208 <span id='Ext-picker.Color-method-getValue'>    /**
209 </span>     * Get the currently selected color value.
210      * @return {String} value The selected value. Null if nothing is selected.
211      */
212     getValue: function(){
213         return this.value || null;
214     }
215 });
216 </pre></pre></body></html>