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