3 <title>The source code</title>
4 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
5 <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
7 <body onload="prettyPrint();">
8 <pre class="prettyprint lang-js">/*!
10 * Copyright(c) 2006-2009 Ext JS, LLC
12 * http://www.extjs.com/license
14 <div id="cls-Ext.ColorPalette"></div>/**
15 * @class Ext.ColorPalette
16 * @extends Ext.Component
17 * Simple color palette class for choosing colors. The palette can be rendered to any container.<br />
18 * Here's an example of typical usage:
20 var cp = new Ext.ColorPalette({value:'993300'}); // initial selected color
23 cp.on('select', function(palette, selColor){
24 // do something with selColor
28 * Create a new ColorPalette
29 * @param {Object} config The config object
32 Ext.ColorPalette = function(config){
33 Ext.ColorPalette.superclass.constructor.call(this, config);
35 <div id="event-Ext.ColorPalette-select"></div>/**
37 * Fires when a color is selected
38 * @param {ColorPalette} this
39 * @param {String} color The 6-digit color hex code (without the # symbol)
45 this.on('select', this.handler, this.scope, true);
48 Ext.extend(Ext.ColorPalette, Ext.Component, {
49 <div id="cfg-Ext.ColorPalette-tpl"></div>/**
50 * @cfg {String} tpl An existing XTemplate instance to be used in place of the default template for rendering the component.
52 <div id="cfg-Ext.ColorPalette-itemCls"></div>/**
53 * @cfg {String} itemCls
54 * The CSS class to apply to the containing element (defaults to 'x-color-palette')
56 itemCls : 'x-color-palette',
57 <div id="cfg-Ext.ColorPalette-value"></div>/**
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.
65 ctype : 'Ext.ColorPalette',
67 <div id="cfg-Ext.ColorPalette-allowReselect"></div>/**
68 * @cfg {Boolean} allowReselect If set to true then reselecting a color that is already selected fires the {@link #select} event
70 allowReselect : false,
72 <div id="prop-Ext.ColorPalette-colors"></div>/**
73 * <p>An array of 6-digit color hex code strings (without the # symbol). This array can contain any number
74 * of colors, and each hex code should be unique. The width of the palette is controlled via CSS by adjusting
75 * the width property of the 'x-color-palette' class (or assigning a custom class), so you can balance the number
76 * of colors with the width setting until the box is symmetrical.</p>
77 * <p>You can override individual colors if needed:</p>
79 var cp = new Ext.ColorPalette();
80 cp.colors[0] = 'FF0000'; // change the first box to red
83 Or you can provide a custom array of your own for complete control:
85 var cp = new Ext.ColorPalette();
86 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 <div id="cfg-Ext.ColorPalette-handler"></div>/**
99 * @cfg {Function} handler
100 * Optional. A function that will handle the select event of this palette.
101 * The handler is passed the following parameters:<div class="mdetail-params"><ul>
102 * <li><code>palette</code> : ColorPalette<div class="sub-desc">The {@link #palette Ext.ColorPalette}.</div></li>
103 * <li><code>color</code> : String<div class="sub-desc">The 6-digit color hex code (without the # symbol).</div></li>
106 <div id="cfg-Ext.ColorPalette-scope"></div>/**
107 * @cfg {Object} scope
108 * The scope (<tt><b>this</b></tt> reference) in which the <code>{@link #handler}</code>
109 * function will be called. Defaults to this ColorPalette instance.
113 onRender : function(container, position){
114 var t = this.tpl || new Ext.XTemplate(
115 '<tpl for="."><a href="#" class="color-{.}" hidefocus="on"><em><span style="background:#{.}" unselectable="on"> </span></em></a></tpl>'
117 var el = document.createElement('div');
118 el.id = this.getId();
119 el.className = this.itemCls;
120 t.overwrite(el, this.colors);
121 container.dom.insertBefore(el, position);
122 this.el = Ext.get(el);
123 this.mon(this.el, this.clickEvent, this.handleClick, this, {delegate: 'a'});
124 if(this.clickEvent != 'click'){
125 this.mon(this.el, 'click', Ext.emptyFn, this, {delegate: 'a', preventDefault: true});
130 afterRender : function(){
131 Ext.ColorPalette.superclass.afterRender.call(this);
140 handleClick : function(e, t){
143 var c = t.className.match(/(?:^|\s)color-(.{6})(?:\s|$)/)[1];
144 this.select(c.toUpperCase());
148 <div id="method-Ext.ColorPalette-select"></div>/**
149 * Selects the specified color in the palette (fires the {@link #select} event)
150 * @param {String} color A valid 6-digit color hex code (# will be stripped if included)
152 select : function(color){
153 color = color.replace('#', '');
154 if(color != this.value || this.allowReselect){
157 el.child('a.color-'+this.value).removeClass('x-color-palette-sel');
159 el.child('a.color-'+color).addClass('x-color-palette-sel');
161 this.fireEvent('select', this, color);
165 <div id="cfg-Ext.ColorPalette-autoEl"></div>/**
166 * @cfg {String} autoEl @hide
169 Ext.reg('colorpalette', Ext.ColorPalette);