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