4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
5 <title>The source code</title>
6 <link href="../prettify/prettify.css" type="text/css" rel="stylesheet" />
7 <script type="text/javascript" src="../prettify/prettify.js"></script>
8 <style type="text/css">
9 .highlight { display: block; background-color: #ddd; }
11 <script type="text/javascript">
12 function highlight() {
13 document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
17 <body onload="prettyPrint(); highlight();">
18 <pre class="prettyprint lang-js"><span id='Ext-picker-Color'>/**
19 </span> * @class Ext.picker.Color
20 * @extends Ext.Component
21 * <p>ColorPicker provides a simple color palette for choosing colors. The picker can be rendered to any container.
22 * The available default to a standard 40-color palette; this can be customized with the {@link #colors} config.</p>
23 * <p>Typically you will need to implement a handler function to be notified when the user chooses a color from the
24 * picker; you can register the handler using the {@link #select} event, or by implementing the {@link #handler}
26 * <p>Here's an example of typical usage:</p>
27 * <pre><code>var cp = new Ext.picker.Color({
28 value: '993300', // initial selected color
32 cp.on('select', function(picker, selColor){
33 // do something with selColor
35 </code></pre>
36 * {@img Ext.picker.Color/Ext.picker.Color.png Ext.picker.Color component}
39 Ext.define('Ext.picker.Color', {
40 extend: 'Ext.Component',
41 requires: 'Ext.XTemplate',
42 alias: 'widget.colorpicker',
43 alternateClassName: 'Ext.ColorPalette',
45 <span id='Ext-picker-Color-cfg-componentCls'> /**
46 </span> * @cfg {String} componentCls
47 * The CSS class to apply to the containing element (defaults to 'x-color-picker')
49 componentCls : Ext.baseCSSPrefix + 'color-picker',
51 <span id='Ext-picker-Color-cfg-selectedCls'> /**
52 </span> * @cfg {String} selectedCls
53 * The CSS class to apply to the selected element
55 selectedCls: Ext.baseCSSPrefix + 'color-picker-selected',
57 <span id='Ext-picker-Color-cfg-value'> /**
58 </span> * @cfg {String} value
59 * The initial color to highlight (should be a valid 6-digit color hex code without the # symbol). Note that
60 * the hex codes are case-sensitive.
64 <span id='Ext-picker-Color-cfg-clickEvent'> /**
65 </span> * @cfg {String} clickEvent
66 * The DOM event that will cause a color to be selected. This can be any valid event name (dblclick, contextmenu).
67 * Defaults to <tt>'click'</tt>.
71 <span id='Ext-picker-Color-cfg-allowReselect'> /**
72 </span> * @cfg {Boolean} allowReselect If set to true then reselecting a color that is already selected fires the {@link #select} event
74 allowReselect : false,
76 <span id='Ext-picker-Color-property-colors'> /**
77 </span> * <p>An array of 6-digit color hex code strings (without the # symbol). This array can contain any number
78 * of colors, and each hex code should be unique. The width of the picker is controlled via CSS by adjusting
79 * the width property of the 'x-color-picker' class (or assigning a custom class), so you can balance the number
80 * of colors with the width setting until the box is symmetrical.</p>
81 * <p>You can override individual colors if needed:</p>
82 * <pre><code>
83 var cp = new Ext.picker.Color();
84 cp.colors[0] = 'FF0000'; // change the first box to red
85 </code></pre>
87 Or you can provide a custom array of your own for complete control:
88 <pre><code>
89 var cp = new Ext.picker.Color();
90 cp.colors = ['000000', '993300', '333300'];
91 </code></pre>
95 '000000', '993300', '333300', '003300', '003366', '000080', '333399', '333333',
96 '800000', 'FF6600', '808000', '008000', '008080', '0000FF', '666699', '808080',
97 'FF0000', 'FF9900', '99CC00', '339966', '33CCCC', '3366FF', '800080', '969696',
98 'FF00FF', 'FFCC00', 'FFFF00', '00FF00', '00FFFF', '00CCFF', '993366', 'C0C0C0',
99 'FF99CC', 'FFCC99', 'FFFF99', 'CCFFCC', 'CCFFFF', '99CCFF', 'CC99FF', 'FFFFFF'
102 <span id='Ext-picker-Color-cfg-handler'> /**
103 </span> * @cfg {Function} handler
104 * Optional. A function that will handle the select event of this picker.
105 * The handler is passed the following parameters:<div class="mdetail-params"><ul>
106 * <li><code>picker</code> : ColorPicker<div class="sub-desc">The {@link #picker Ext.picker.Color}.</div></li>
107 * <li><code>color</code> : String<div class="sub-desc">The 6-digit color hex code (without the # symbol).</div></li>
108 * </ul></div>
110 <span id='Ext-picker-Color-cfg-scope'> /**
111 </span> * @cfg {Object} scope
112 * The scope (<tt><b>this</b></tt> reference) in which the <code>{@link #handler}</code>
113 * function will be called. Defaults to this ColorPicker instance.
116 colorRe: /(?:^|\s)color-(.{6})(?:\s|$)/,
118 constructor: function() {
119 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>');
120 this.callParent(arguments);
124 initComponent : function(){
127 this.callParent(arguments);
129 <span id='Ext-picker-Color-event-select'> /**
130 </span> * @event select
131 * Fires when a color is selected
132 * @param {Ext.picker.Color} this
133 * @param {String} color The 6-digit color hex code (without the # symbol)
139 me.on('select', me.handler, me.scope, true);
145 onRender : function(container, position){
147 clickEvent = me.clickEvent;
149 Ext.apply(me.renderData, {
153 this.callParent(arguments);
155 me.mon(me.el, clickEvent, me.handleClick, me, {delegate: 'a'});
156 // always stop following the anchors
157 if(clickEvent != 'click'){
158 me.mon(me.el, 'click', Ext.emptyFn, me, {delegate: 'a', stopEvent: true});
163 afterRender : function(){
167 this.callParent(arguments);
171 me.select(value, true);
176 handleClick : function(event, target){
182 color = target.className.match(me.colorRe)[1];
183 me.select(color.toUpperCase());
187 <span id='Ext-picker-Color-method-select'> /**
188 </span> * Selects the specified color in the picker (fires the {@link #select} event)
189 * @param {String} color A valid 6-digit color hex code (# will be stripped if included)
190 * @param {Boolean} suppressEvent (optional) True to stop the select event from firing. Defaults to <tt>false</tt>.
192 select : function(color, suppressEvent){
195 selectedCls = me.selectedCls,
199 color = color.replace('#', '');
206 if (color != value || me.allowReselect) {
210 el.down('a.color-' + value).removeCls(selectedCls);
212 el.down('a.color-' + color).addCls(selectedCls);
214 if (suppressEvent !== true) {
215 me.fireEvent('select', me, color);
220 <span id='Ext-picker-Color-method-getValue'> /**
221 </span> * Get the currently selected color value.
222 * @return {String} value The selected value. Null if nothing is selected.
224 getValue: function(){
225 return this.value || null;