3 <title>The source code</title>
\r
4 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
\r
5 <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
\r
7 <body onload="prettyPrint();">
\r
8 <pre class="prettyprint lang-js"><div id="cls-Ext.ColorPalette"></div>/**
9 * @class Ext.ColorPalette
10 * @extends Ext.Component
11 * Simple color palette class for choosing colors. The palette can be rendered to any container.<br />
12 * Here's an example of typical usage:
14 var cp = new Ext.ColorPalette({value:'993300'}); // initial selected color
17 cp.on('select', function(palette, selColor){
18 // do something with selColor
22 * Create a new ColorPalette
23 * @param {Object} config The config object
26 Ext.ColorPalette = function(config){
27 Ext.ColorPalette.superclass.constructor.call(this, config);
29 <div id="event-Ext.ColorPalette-select"></div>/**
31 * Fires when a color is selected
32 * @param {ColorPalette} this
33 * @param {String} color The 6-digit color hex code (without the # symbol)
39 this.on('select', this.handler, this.scope, true);
42 Ext.extend(Ext.ColorPalette, Ext.Component, {
43 <div id="cfg-Ext.ColorPalette-tpl"></div>/**
44 * @cfg {String} tpl An existing XTemplate instance to be used in place of the default template for rendering the component.
46 <div id="cfg-Ext.ColorPalette-itemCls"></div>/**
47 * @cfg {String} itemCls
48 * The CSS class to apply to the containing element (defaults to 'x-color-palette')
50 itemCls : 'x-color-palette',
51 <div id="cfg-Ext.ColorPalette-value"></div>/**
53 * The initial color to highlight (should be a valid 6-digit color hex code without the # symbol). Note that
54 * the hex codes are case-sensitive.
59 ctype : 'Ext.ColorPalette',
61 <div id="cfg-Ext.ColorPalette-allowReselect"></div>/**
62 * @cfg {Boolean} allowReselect If set to true then reselecting a color that is already selected fires the {@link #select} event
64 allowReselect : false,
66 <div id="prop-Ext.ColorPalette-colors"></div>/**
67 * <p>An array of 6-digit color hex code strings (without the # symbol). This array can contain any number
68 * of colors, and each hex code should be unique. The width of the palette is controlled via CSS by adjusting
69 * the width property of the 'x-color-palette' class (or assigning a custom class), so you can balance the number
70 * of colors with the width setting until the box is symmetrical.</p>
71 * <p>You can override individual colors if needed:</p>
73 var cp = new Ext.ColorPalette();
74 cp.colors[0] = 'FF0000'; // change the first box to red
77 Or you can provide a custom array of your own for complete control:
79 var cp = new Ext.ColorPalette();
80 cp.colors = ['000000', '993300', '333300'];
85 '000000', '993300', '333300', '003300', '003366', '000080', '333399', '333333',
86 '800000', 'FF6600', '808000', '008000', '008080', '0000FF', '666699', '808080',
87 'FF0000', 'FF9900', '99CC00', '339966', '33CCCC', '3366FF', '800080', '969696',
88 'FF00FF', 'FFCC00', 'FFFF00', '00FF00', '00FFFF', '00CCFF', '993366', 'C0C0C0',
89 'FF99CC', 'FFCC99', 'FFFF99', 'CCFFCC', 'CCFFFF', '99CCFF', 'CC99FF', 'FFFFFF'
92 <div id="cfg-Ext.ColorPalette-handler"></div>/**
93 * @cfg {Function} handler
94 * Optional. A function that will handle the select event of this palette.
95 * The handler is passed the following parameters:<div class="mdetail-params"><ul>
96 * <li><code>palette</code> : ColorPalette<div class="sub-desc">The {@link #palette Ext.ColorPalette}.</div></li>
97 * <li><code>color</code> : String<div class="sub-desc">The 6-digit color hex code (without the # symbol).</div></li>
100 <div id="cfg-Ext.ColorPalette-scope"></div>/**
101 * @cfg {Object} scope
102 * The scope (<tt><b>this</b></tt> reference) in which the <code>{@link #handler}</code>
103 * function will be called. Defaults to this ColorPalette instance.
107 onRender : function(container, position){
108 var t = this.tpl || new Ext.XTemplate(
109 '<tpl for="."><a href="#" class="color-{.}" hidefocus="on"><em><span style="background:#{.}" unselectable="on"> </span></em></a></tpl>'
111 var el = document.createElement('div');
112 el.id = this.getId();
113 el.className = this.itemCls;
114 t.overwrite(el, this.colors);
115 container.dom.insertBefore(el, position);
116 this.el = Ext.get(el);
117 this.mon(this.el, this.clickEvent, this.handleClick, this, {delegate: 'a'});
118 if(this.clickEvent != 'click'){
119 this.mon(this.el, 'click', Ext.emptyFn, this, {delegate: 'a', preventDefault: true});
124 afterRender : function(){
125 Ext.ColorPalette.superclass.afterRender.call(this);
134 handleClick : function(e, t){
137 var c = t.className.match(/(?:^|\s)color-(.{6})(?:\s|$)/)[1];
138 this.select(c.toUpperCase());
142 <div id="method-Ext.ColorPalette-select"></div>/**
143 * Selects the specified color in the palette (fires the {@link #select} event)
144 * @param {String} color A valid 6-digit color hex code (# will be stripped if included)
146 select : function(color){
147 color = color.replace('#', '');
148 if(color != this.value || this.allowReselect){
151 el.child('a.color-'+this.value).removeClass('x-color-palette-sel');
153 el.child('a.color-'+color).addClass('x-color-palette-sel');
155 this.fireEvent('select', this, color);
159 <div id="cfg-Ext.ColorPalette-autoEl"></div>/**
160 * @cfg {String} autoEl @hide
163 Ext.reg('colorpalette', Ext.ColorPalette);