]>
Commit | Line | Data |
---|---|---|
6527f429 DM |
1 | /**\r |
2 | * Color picker provides a simple color palette for choosing colors. The picker can be rendered to any container. The\r | |
3 | * available default to a standard 40-color palette; this can be customized with the {@link #colors} config.\r | |
4 | *\r | |
5 | * Typically you will need to implement a handler function to be notified when the user chooses a color from the picker;\r | |
6 | * you can register the handler using the {@link #event-select} event, or by implementing the {@link #handler} method.\r | |
7 | *\r | |
8 | * @example\r | |
9 | * Ext.create('Ext.picker.Color', {\r | |
10 | * value: '993300', // initial selected color\r | |
11 | * renderTo: Ext.getBody(),\r | |
12 | * listeners: {\r | |
13 | * select: function(picker, selColor) {\r | |
14 | * alert(selColor);\r | |
15 | * }\r | |
16 | * }\r | |
17 | * });\r | |
18 | */\r | |
19 | Ext.define('Ext.picker.Color', {\r | |
20 | extend: 'Ext.Component',\r | |
21 | requires: 'Ext.XTemplate',\r | |
22 | alias: 'widget.colorpicker',\r | |
23 | alternateClassName: 'Ext.ColorPalette',\r | |
24 | \r | |
25 | focusable: true,\r | |
26 | \r | |
27 | /**\r | |
28 | * @cfg {String} [componentCls='x-color-picker']\r | |
29 | * The CSS class to apply to the containing element.\r | |
30 | */\r | |
31 | componentCls : Ext.baseCSSPrefix + 'color-picker',\r | |
32 | \r | |
33 | /**\r | |
34 | * @cfg {String} [selectedCls='x-color-picker-selected']\r | |
35 | * The CSS class to apply to the selected element\r | |
36 | */\r | |
37 | selectedCls: Ext.baseCSSPrefix + 'color-picker-selected',\r | |
38 | \r | |
39 | /**\r | |
40 | * @cfg {String} itemCls\r | |
41 | * The CSS class to apply to the color picker's items\r | |
42 | */\r | |
43 | itemCls: Ext.baseCSSPrefix + 'color-picker-item',\r | |
44 | \r | |
45 | /**\r | |
46 | * @cfg {String} value\r | |
47 | * The initial color to highlight (should be a valid 6-digit color hex code without the # symbol). Note that the hex\r | |
48 | * codes are case-sensitive.\r | |
49 | */\r | |
50 | value : null,\r | |
51 | \r | |
52 | /**\r | |
53 | * @cfg {String} clickEvent\r | |
54 | * The DOM event that will cause a color to be selected. This can be any valid event name (dblclick, contextmenu).\r | |
55 | */\r | |
56 | clickEvent :'click',\r | |
57 | \r | |
58 | /**\r | |
59 | * @cfg {Boolean} allowReselect\r | |
60 | * If set to true then reselecting a color that is already selected fires the {@link #event-select} event\r | |
61 | */\r | |
62 | allowReselect : false,\r | |
63 | \r | |
64 | /**\r | |
65 | * @property {String[]} colors\r | |
66 | * An array of 6-digit color hex code strings (without the # symbol). This array can contain any number of colors,\r | |
67 | * and each hex code should be unique. The width of the picker is controlled via CSS by adjusting the width property\r | |
68 | * of the 'x-color-picker' class (or assigning a custom class), so you can balance the number of colors with the\r | |
69 | * width setting until the box is symmetrical.\r | |
70 | *\r | |
71 | * You can override individual colors if needed:\r | |
72 | *\r | |
73 | * var cp = new Ext.picker.Color();\r | |
74 | * cp.colors[0] = 'FF0000'; // change the first box to red\r | |
75 | *\r | |
76 | * Or you can provide a custom array of your own for complete control:\r | |
77 | *\r | |
78 | * var cp = new Ext.picker.Color();\r | |
79 | * cp.colors = ['000000', '993300', '333300'];\r | |
80 | */\r | |
81 | colors : [\r | |
82 | '000000', '993300', '333300', '003300', '003366', '000080', '333399', '333333',\r | |
83 | '800000', 'FF6600', '808000', '008000', '008080', '0000FF', '666699', '808080',\r | |
84 | 'FF0000', 'FF9900', '99CC00', '339966', '33CCCC', '3366FF', '800080', '969696',\r | |
85 | 'FF00FF', 'FFCC00', 'FFFF00', '00FF00', '00FFFF', '00CCFF', '993366', 'C0C0C0',\r | |
86 | 'FF99CC', 'FFCC99', 'FFFF99', 'CCFFCC', 'CCFFFF', '99CCFF', 'CC99FF', 'FFFFFF'\r | |
87 | ],\r | |
88 | \r | |
89 | /**\r | |
90 | * @cfg {Function/String} handler\r | |
91 | * A function that will handle the select event of this picker. The handler is passed the following parameters:\r | |
92 | *\r | |
93 | * - `picker` : ColorPicker\r | |
94 | *\r | |
95 | * The {@link Ext.picker.Color picker}.\r | |
96 | *\r | |
97 | * - `color` : String\r | |
98 | *\r | |
99 | * The 6-digit color hex code (without the # symbol).\r | |
100 | * \r | |
101 | * @declarativeHandler\r | |
102 | */\r | |
103 | \r | |
104 | /**\r | |
105 | * @cfg {Object} scope\r | |
106 | * The scope (`this` reference) in which the `{@link #handler}` function will be called.\r | |
107 | *\r | |
108 | * Defaults to this Color picker instance.\r | |
109 | */\r | |
110 | \r | |
111 | colorRe: /(?:^|\s)color-(.{6})(?:\s|$)/,\r | |
112 | \r | |
113 | renderTpl: [\r | |
114 | '<tpl for="colors">',\r | |
115 | '<a href="#" role="button" class="color-{.} {parent.itemCls}" hidefocus="on">',\r | |
116 | '<span class="{parent.itemCls}-inner" style="background:#{.}"> </span>',\r | |
117 | '</a>',\r | |
118 | '</tpl>'\r | |
119 | ],\r | |
120 | \r | |
121 | /**\r | |
122 | * @event select\r | |
123 | * Fires when a color is selected\r | |
124 | * @param {Ext.picker.Color} this\r | |
125 | * @param {String} color The 6-digit color hex code (without the # symbol)\r | |
126 | */\r | |
127 | \r | |
128 | /**\r | |
129 | * @private\r | |
130 | */\r | |
131 | initComponent : function(){\r | |
132 | var me = this;\r | |
133 | \r | |
134 | me.callParent(arguments);\r | |
135 | \r | |
136 | if (me.handler) {\r | |
137 | me.on('select', me.handler, me.scope, true);\r | |
138 | }\r | |
139 | },\r | |
140 | \r | |
141 | \r | |
142 | /**\r | |
143 | * @private\r | |
144 | */\r | |
145 | initRenderData : function(){\r | |
146 | var me = this;\r | |
147 | return Ext.apply(me.callParent(), {\r | |
148 | itemCls: me.itemCls,\r | |
149 | colors: me.colors\r | |
150 | });\r | |
151 | },\r | |
152 | \r | |
153 | onRender : function(){\r | |
154 | var me = this,\r | |
155 | clickEvent = me.clickEvent;\r | |
156 | \r | |
157 | me.callParent(arguments);\r | |
158 | \r | |
159 | me.mon(me.el, clickEvent, me.handleClick, me, {delegate: 'a'});\r | |
160 | // always stop following the anchors\r | |
161 | if (clickEvent !== 'click'){\r | |
162 | me.mon(me.el, 'click', Ext.emptyFn, me, {delegate: 'a', stopEvent: true});\r | |
163 | }\r | |
164 | },\r | |
165 | \r | |
166 | /**\r | |
167 | * @private\r | |
168 | */\r | |
169 | afterRender : function(){\r | |
170 | var me = this,\r | |
171 | value;\r | |
172 | \r | |
173 | me.callParent(arguments);\r | |
174 | if (me.value) {\r | |
175 | value = me.value;\r | |
176 | me.value = null;\r | |
177 | me.select(value, true);\r | |
178 | }\r | |
179 | },\r | |
180 | \r | |
181 | /**\r | |
182 | * @private\r | |
183 | */\r | |
184 | handleClick : function(event){\r | |
185 | var me = this,\r | |
186 | color;\r | |
187 | \r | |
188 | event.stopEvent();\r | |
189 | if (!me.disabled) {\r | |
190 | color = event.currentTarget.className.match(me.colorRe)[1];\r | |
191 | me.select(color.toUpperCase());\r | |
192 | }\r | |
193 | },\r | |
194 | \r | |
195 | /**\r | |
196 | * Selects the specified color in the picker (fires the {@link #event-select} event)\r | |
197 | * @param {String} color A valid 6-digit color hex code (# will be stripped if included)\r | |
198 | * @param {Boolean} [suppressEvent=false] True to stop the select event from firing.\r | |
199 | */\r | |
200 | select : function(color, suppressEvent){\r | |
201 | \r | |
202 | var me = this,\r | |
203 | selectedCls = me.selectedCls,\r | |
204 | value = me.value,\r | |
205 | el, item;\r | |
206 | \r | |
207 | color = color.replace('#', '');\r | |
208 | if (!me.rendered) {\r | |
209 | me.value = color;\r | |
210 | return;\r | |
211 | }\r | |
212 | \r | |
213 | \r | |
214 | if (color !== value || me.allowReselect) {\r | |
215 | el = me.el;\r | |
216 | \r | |
217 | if (me.value) {\r | |
218 | item = el.down('a.color-' + value, true);\r | |
219 | Ext.fly(item).removeCls(selectedCls);\r | |
220 | }\r | |
221 | item = el.down('a.color-' + color, true);\r | |
222 | Ext.fly(item).addCls(selectedCls);\r | |
223 | me.value = color;\r | |
224 | if (suppressEvent !== true) {\r | |
225 | me.fireEvent('select', me, color);\r | |
226 | }\r | |
227 | }\r | |
228 | },\r | |
229 | \r | |
230 | /**\r | |
231 | * Clears any selection and sets the value to `null`.\r | |
232 | */\r | |
233 | clear: function(){\r | |
234 | var me = this,\r | |
235 | value = me.value,\r | |
236 | el;\r | |
237 | \r | |
238 | if (value && me.rendered) {\r | |
239 | el = me.el.down('a.color-' + value, true);\r | |
240 | Ext.fly(el).removeCls(me.selectedCls);\r | |
241 | }\r | |
242 | me.value = null; \r | |
243 | },\r | |
244 | \r | |
245 | /**\r | |
246 | * Get the currently selected color value.\r | |
247 | * @return {String} value The selected value. Null if nothing is selected.\r | |
248 | */\r | |
249 | getValue: function(){\r | |
250 | return this.value || null;\r | |
251 | }\r | |
252 | });\r |