]>
git.proxmox.com Git - pve-manager.git/blob - www/manager6/form/TagColorGrid.js
1 Ext
.define('PVE.form.ColorPicker', {
2 extend
: 'Ext.form.FieldContainer',
3 alias
: 'widget.pveColorPicker',
5 defaultBindProperty
: 'value',
18 getValue: function() {
19 return this.realvalue
.slice(1);
22 setValue: function(value
) {
25 if (value
&& value
.length
=== 6) {
26 me
.picker
.value
= value
[0] !== '#' ? `#${value}` : value
;
30 setColor: function(value
) {
32 let oldValue
= me
.realvalue
;
34 let color
= value
.length
=== 6 ? `#${value}` : undefined;
35 me
.down('#picker').setStyle('background-color', color
);
36 me
.down('#text').setValue(value
?? "");
37 me
.fireEvent('change', me
, me
.realvalue
, oldValue
);
40 initComponent: function() {
42 me
.picker
= document
.createElement('input');
43 me
.picker
.type
= 'color';
44 me
.picker
.style
= `opacity: 0; border: 0px; width: 100%; height: ${me.height}px`;
45 me
.picker
.value
= `${me.value}`;
51 minLength
: !me
.allowBlank
? 6 : undefined,
53 enforceMaxLength
: true,
54 allowBlank
: me
.allowBlank
,
55 emptyText
: me
.allowBlank
? gettext('Automatic') : undefined,
57 regex
: /^[a-f0-9]{6}$/i,
60 change: function(field
, value
) {
69 border
: '1px solid #cfcfcf',
78 me
.picker
.oninput = function() {
79 me
.setColor(me
.picker
.value
.slice(1));
84 Ext
.define('PVE.form.TagColorGrid', {
85 extend
: 'Ext.grid.Panel',
86 alias
: 'widget.pveTagColorGrid',
89 'Ext.form.field.Field',
96 selModel
: 'checkboxmodel',
102 emptyText
: gettext('No Overrides'),
104 deferEmptyText
: false,
107 setValue: function(value
) {
110 if (Ext
.isObject(value
)) {
111 colors
= value
.colors
;
116 me
.getStore().removeAll();
120 let entries
= (colors
.split(';') || []).map((entry
) => {
121 let [tag
, bg
, fg
] = entry
.split(':');
129 me
.getStore().setData(entries
);
134 getValue: function() {
137 me
.getStore().each((rec
) => {
139 let val
= `${rec.data.tag}:${rec.data.color}`;
141 val
+= `:${rec.data.text}`;
146 return values
.join(';');
149 getErrors: function(value
) {
151 let emptyTag
= false;
152 let notValidColor
= false;
153 let colorRegex
= new RegExp(/^[0-9a-f]{6}$/i);
154 me
.getStore().each((rec
) => {
158 if (!rec
.data
.color
?.match(colorRegex
)) {
159 notValidColor
= true;
161 if (rec
.data
.text
&& !rec
.data
.text
?.match(colorRegex
)) {
162 notValidColor
= true;
167 errors
.push(gettext('Tag must not be empty.'));
170 errors
.push(gettext('Not a valid color.'));
175 // override framework function to implement deleteEmpty behaviour
176 getSubmitData: function() {
180 if (!me
.disabled
&& me
.submitValue
) {
182 if (val
!== null && val
!== '') {
184 data
[me
.getName()] = val
;
185 } else if (me
.getDeleteEmpty()) {
187 data
.delete = me
.getName();
195 xclass
: 'Ext.app.ViewController',
197 addLine: function() {
199 me
.getView().getStore().add({
206 removeSelection: function() {
208 let view
= me
.getView();
209 let selection
= view
.getSelection();
210 if (selection
=== undefined) {
214 selection
.forEach((sel
) => {
215 view
.getStore().remove(sel
);
220 tagChange: function(field
, newValue
, oldValue
) {
222 let rec
= field
.getWidgetRecord();
226 if (newValue
&& newValue
!== oldValue
) {
227 let newrgb
= Proxmox
.Utils
.stringToRGB(newValue
);
228 let newvalue
= Proxmox
.Utils
.rgbToHex(newrgb
);
229 if (!rec
.get('color')) {
230 rec
.set('color', newvalue
);
231 } else if (oldValue
) {
232 let oldrgb
= Proxmox
.Utils
.stringToRGB(oldValue
);
233 let oldvalue
= Proxmox
.Utils
.rgbToHex(oldrgb
);
234 if (rec
.get('color') === oldvalue
) {
235 rec
.set('color', newvalue
);
239 me
.fieldChange(field
, newValue
, oldValue
);
242 backgroundChange: function(field
, newValue
, oldValue
) {
244 let rec
= field
.getWidgetRecord();
248 if (newValue
&& newValue
!== oldValue
) {
249 let newrgb
= Proxmox
.Utils
.hexToRGB(newValue
);
250 let newcls
= Proxmox
.Utils
.getTextContrastClass(newrgb
);
251 let hexvalue
= newcls
=== 'dark' ? '000000' : 'FFFFFF';
252 if (!rec
.get('text')) {
253 rec
.set('text', hexvalue
);
254 } else if (oldValue
) {
255 let oldrgb
= Proxmox
.Utils
.hexToRGB(oldValue
);
256 let oldcls
= Proxmox
.Utils
.getTextContrastClass(oldrgb
);
257 let oldvalue
= oldcls
=== 'dark' ? '000000' : 'FFFFFF';
258 if (rec
.get('text') === oldvalue
) {
259 rec
.set('text', hexvalue
);
263 me
.fieldChange(field
, newValue
, oldValue
);
266 fieldChange: function(field
, newValue
, oldValue
) {
268 let view
= me
.getView();
269 let rec
= field
.getWidgetRecord();
273 let column
= field
.getWidgetColumn();
274 rec
.set(column
.dataIndex
, newValue
);
281 text
: gettext('Add'),
285 xtype
: 'proxmoxButton',
286 text
: gettext('Remove'),
287 handler
: 'removeSelection',
296 xtype
: 'widgetcolumn',
297 onWidgetAttach: function(col
, widget
, rec
) {
298 widget
.getStore().setData(PVE
.UIOptions
.tagList
.map(v
=> ({ tag
: v
})));
303 maskRe
: PVE
.Utils
.tagCharRegex
,
316 header
: gettext('Background'),
317 xtype
: 'widgetcolumn',
321 xtype
: 'pveColorPicker',
324 change
: 'backgroundChange',
329 header
: gettext('Text'),
330 xtype
: 'widgetcolumn',
334 xtype
: 'pveColorPicker',
338 change
: 'fieldChange',
347 this.commitChanges();
352 initComponent: function() {