4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
5 <title>The source code</title>
6 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
7 <script type="text/javascript" src="../resources/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> * Color picker provides a simple color palette for choosing colors. The picker can be rendered to any container. The
20 * available default to a standard 40-color palette; this can be customized with the {@link #colors} config.
22 * Typically you will need to implement a handler function to be notified when the user chooses a color from the picker;
23 * you can register the handler using the {@link #select} event, or by implementing the {@link #handler} method.
26 * Ext.create('Ext.picker.Color', {
27 * value: '993300', // initial selected color
28 * renderTo: Ext.getBody(),
30 * select: function(picker, selColor) {
36 Ext.define('Ext.picker.Color', {
37 extend: 'Ext.Component',
38 requires: 'Ext.XTemplate',
39 alias: 'widget.colorpicker',
40 alternateClassName: 'Ext.ColorPalette',
42 <span id='Ext-picker-Color-cfg-componentCls'> /**
43 </span> * @cfg {String} [componentCls='x-color-picker']
44 * The CSS class to apply to the containing element.
46 componentCls : Ext.baseCSSPrefix + 'color-picker',
48 <span id='Ext-picker-Color-cfg-selectedCls'> /**
49 </span> * @cfg {String} [selectedCls='x-color-picker-selected']
50 * The CSS class to apply to the selected element
52 selectedCls: Ext.baseCSSPrefix + 'color-picker-selected',
54 <span id='Ext-picker-Color-cfg-value'> /**
55 </span> * @cfg {String} value
56 * The initial color to highlight (should be a valid 6-digit color hex code without the # symbol). Note that the hex
57 * codes are case-sensitive.
61 <span id='Ext-picker-Color-cfg-clickEvent'> /**
62 </span> * @cfg {String} clickEvent
63 * The DOM event that will cause a color to be selected. This can be any valid event name (dblclick, contextmenu).
67 <span id='Ext-picker-Color-cfg-allowReselect'> /**
68 </span> * @cfg {Boolean} allowReselect
69 * If set to true then reselecting a color that is already selected fires the {@link #select} event
71 allowReselect : false,
73 <span id='Ext-picker-Color-property-colors'> /**
74 </span> * @property {String[]} colors
75 * An array of 6-digit color hex code strings (without the # symbol). This array can contain any number of colors,
76 * and each hex code should be unique. The width of the picker is controlled via CSS by adjusting the width property
77 * of the 'x-color-picker' class (or assigning a custom class), so you can balance the number of colors with the
78 * width setting until the box is symmetrical.
80 * You can override individual colors if needed:
82 * var cp = new Ext.picker.Color();
83 * cp.colors[0] = 'FF0000'; // change the first box to red
85 * Or you can provide a custom array of your own for complete control:
87 * var cp = new Ext.picker.Color();
88 * cp.colors = ['000000', '993300', '333300'];
91 '000000', '993300', '333300', '003300', '003366', '000080', '333399', '333333',
92 '800000', 'FF6600', '808000', '008000', '008080', '0000FF', '666699', '808080',
93 'FF0000', 'FF9900', '99CC00', '339966', '33CCCC', '3366FF', '800080', '969696',
94 'FF00FF', 'FFCC00', 'FFFF00', '00FF00', '00FFFF', '00CCFF', '993366', 'C0C0C0',
95 'FF99CC', 'FFCC99', 'FFFF99', 'CCFFCC', 'CCFFFF', '99CCFF', 'CC99FF', 'FFFFFF'
98 <span id='Ext-picker-Color-cfg-handler'> /**
99 </span> * @cfg {Function} handler
100 * A function that will handle the select event of this picker. The handler is passed the following parameters:
102 * - `picker` : ColorPicker
104 * The {@link Ext.picker.Color picker}.
108 * The 6-digit color hex code (without the # symbol).
111 <span id='Ext-picker-Color-cfg-scope'> /**
112 </span> * @cfg {Object} scope
113 * The scope (`this` reference) in which the `{@link #handler}` function will be called. Defaults to this
114 * Color picker instance.
117 colorRe: /(?:^|\s)color-(.{6})(?:\s|$)/,
120 '<tpl for="colors">',
121 '<a href="#" class="color-{.}" hidefocus="on">',
122 '<em><span style="background:#{.}" unselectable="on">&#160;</span></em>',
128 initComponent : function(){
131 me.callParent(arguments);
133 <span id='Ext-picker-Color-event-select'> /**
134 </span> * @event select
135 * Fires when a color is selected
136 * @param {Ext.picker.Color} this
137 * @param {String} color The 6-digit color hex code (without the # symbol)
143 me.on('select', me.handler, me.scope, true);
149 onRender : function(container, position){
151 clickEvent = me.clickEvent;
153 Ext.apply(me.renderData, {
157 me.callParent(arguments);
159 me.mon(me.el, clickEvent, me.handleClick, me, {delegate: 'a'});
160 // always stop following the anchors
161 if(clickEvent != 'click'){
162 me.mon(me.el, 'click', Ext.emptyFn, me, {delegate: 'a', stopEvent: true});
167 afterRender : function(){
171 me.callParent(arguments);
175 me.select(value, true);
180 handleClick : function(event, target){
186 color = target.className.match(me.colorRe)[1];
187 me.select(color.toUpperCase());
191 <span id='Ext-picker-Color-method-select'> /**
192 </span> * Selects the specified color in the picker (fires the {@link #select} event)
193 * @param {String} color A valid 6-digit color hex code (# will be stripped if included)
194 * @param {Boolean} suppressEvent (optional) True to stop the select event from firing. Defaults to false.
196 select : function(color, suppressEvent){
199 selectedCls = me.selectedCls,
203 color = color.replace('#', '');
210 if (color != value || me.allowReselect) {
214 el.down('a.color-' + value).removeCls(selectedCls);
216 el.down('a.color-' + color).addCls(selectedCls);
218 if (suppressEvent !== true) {
219 me.fireEvent('select', me, color);
224 <span id='Ext-picker-Color-method-getValue'> /**
225 </span> * Get the currently selected color value.
226 * @return {String} value The selected value. Null if nothing is selected.
228 getValue: function(){
229 return this.value || null;