Upgrade to ExtJS 3.0.3 - Released 10/11/2009
[extjs.git] / src / widgets / ColorPalette.js
1 /*!
2  * Ext JS Library 3.0.3
3  * Copyright(c) 2006-2009 Ext JS, LLC
4  * licensing@extjs.com
5  * http://www.extjs.com/license
6  */
7 /**
8  * @class Ext.ColorPalette
9  * @extends Ext.Component
10  * Simple color palette class for choosing colors.  The palette can be rendered to any container.<br />
11  * Here's an example of typical usage:
12  * <pre><code>
13 var cp = new Ext.ColorPalette({value:'993300'});  // initial selected color
14 cp.render('my-div');
15
16 cp.on('select', function(palette, selColor){
17     // do something with selColor
18 });
19 </code></pre>
20  * @constructor
21  * Create a new ColorPalette
22  * @param {Object} config The config object
23  * @xtype colorpalette
24  */
25 Ext.ColorPalette = function(config){
26     Ext.ColorPalette.superclass.constructor.call(this, config);
27     this.addEvents(
28         /**
29              * @event select
30              * Fires when a color is selected
31              * @param {ColorPalette} this
32              * @param {String} color The 6-digit color hex code (without the # symbol)
33              */
34         'select'
35     );
36
37     if(this.handler){
38         this.on('select', this.handler, this.scope, true);
39     }
40 };
41 Ext.extend(Ext.ColorPalette, Ext.Component, {
42         /**
43          * @cfg {String} tpl An existing XTemplate instance to be used in place of the default template for rendering the component.
44          */
45     /**
46      * @cfg {String} itemCls
47      * The CSS class to apply to the containing element (defaults to 'x-color-palette')
48      */
49     itemCls : 'x-color-palette',
50     /**
51      * @cfg {String} value
52      * The initial color to highlight (should be a valid 6-digit color hex code without the # symbol).  Note that
53      * the hex codes are case-sensitive.
54      */
55     value : null,
56     clickEvent :'click',
57     // private
58     ctype : 'Ext.ColorPalette',
59
60     /**
61      * @cfg {Boolean} allowReselect If set to true then reselecting a color that is already selected fires the {@link #select} event
62      */
63     allowReselect : false,
64
65     /**
66      * <p>An array of 6-digit color hex code strings (without the # symbol).  This array can contain any number
67      * of colors, and each hex code should be unique.  The width of the palette is controlled via CSS by adjusting
68      * the width property of the 'x-color-palette' class (or assigning a custom class), so you can balance the number
69      * of colors with the width setting until the box is symmetrical.</p>
70      * <p>You can override individual colors if needed:</p>
71      * <pre><code>
72 var cp = new Ext.ColorPalette();
73 cp.colors[0] = 'FF0000';  // change the first box to red
74 </code></pre>
75
76 Or you can provide a custom array of your own for complete control:
77 <pre><code>
78 var cp = new Ext.ColorPalette();
79 cp.colors = ['000000', '993300', '333300'];
80 </code></pre>
81      * @type Array
82      */
83     colors : [
84         '000000', '993300', '333300', '003300', '003366', '000080', '333399', '333333',
85         '800000', 'FF6600', '808000', '008000', '008080', '0000FF', '666699', '808080',
86         'FF0000', 'FF9900', '99CC00', '339966', '33CCCC', '3366FF', '800080', '969696',
87         'FF00FF', 'FFCC00', 'FFFF00', '00FF00', '00FFFF', '00CCFF', '993366', 'C0C0C0',
88         'FF99CC', 'FFCC99', 'FFFF99', 'CCFFCC', 'CCFFFF', '99CCFF', 'CC99FF', 'FFFFFF'
89     ],
90
91     /**
92      * @cfg {Function} handler
93      * Optional. A function that will handle the select event of this palette.
94      * The handler is passed the following parameters:<div class="mdetail-params"><ul>
95      * <li><code>palette</code> : ColorPalette<div class="sub-desc">The {@link #palette Ext.ColorPalette}.</div></li>
96      * <li><code>color</code> : String<div class="sub-desc">The 6-digit color hex code (without the # symbol).</div></li>
97      * </ul></div>
98      */
99     /**
100      * @cfg {Object} scope
101      * The scope (<tt><b>this</b></tt> reference) in which the <code>{@link #handler}</code>
102      * function will be called.  Defaults to this ColorPalette instance.
103      */
104
105     // private
106     onRender : function(container, position){
107         var t = this.tpl || new Ext.XTemplate(
108             '<tpl for="."><a href="#" class="color-{.}" hidefocus="on"><em><span style="background:#{.}" unselectable="on">&#160;</span></em></a></tpl>'
109         );
110         var el = document.createElement('div');
111         el.id = this.getId();
112         el.className = this.itemCls;
113         t.overwrite(el, this.colors);
114         container.dom.insertBefore(el, position);
115         this.el = Ext.get(el);
116         this.mon(this.el, this.clickEvent, this.handleClick, this, {delegate: 'a'});
117         if(this.clickEvent != 'click'){
118                 this.mon(this.el, 'click', Ext.emptyFn, this, {delegate: 'a', preventDefault: true});
119         }
120     },
121
122     // private
123     afterRender : function(){
124         Ext.ColorPalette.superclass.afterRender.call(this);
125         if(this.value){
126             var s = this.value;
127             this.value = null;
128             this.select(s);
129         }
130     },
131
132     // private
133     handleClick : function(e, t){
134         e.preventDefault();
135         if(!this.disabled){
136             var c = t.className.match(/(?:^|\s)color-(.{6})(?:\s|$)/)[1];
137             this.select(c.toUpperCase());
138         }
139     },
140
141     /**
142      * Selects the specified color in the palette (fires the {@link #select} event)
143      * @param {String} color A valid 6-digit color hex code (# will be stripped if included)
144      */
145     select : function(color){
146         color = color.replace('#', '');
147         if(color != this.value || this.allowReselect){
148             var el = this.el;
149             if(this.value){
150                 el.child('a.color-'+this.value).removeClass('x-color-palette-sel');
151             }
152             el.child('a.color-'+color).addClass('x-color-palette-sel');
153             this.value = color;
154             this.fireEvent('select', this, color);
155         }
156     }
157
158     /**
159      * @cfg {String} autoEl @hide
160      */
161 });
162 Ext.reg('colorpalette', Ext.ColorPalette);