]> git.proxmox.com Git - proxmox-widget-toolkit.git/blob - form/KVComboBox.js
KVComboBox: add setComboItems function
[proxmox-widget-toolkit.git] / form / KVComboBox.js
1 /* Key-Value ComboBox
2 *
3 * config properties:
4 * comboItems: an array of Key - Value pairs
5 * deleteEmpty: if set to true (default), an empty value received from the
6 * comboBox will reset the property to its default value
7 */
8 Ext.define('Proxmox.form.KVComboBox', {
9 extend: 'Ext.form.field.ComboBox',
10 alias: 'widget.proxmoxKVComboBox',
11
12 config: {
13 deleteEmpty: true
14 },
15
16 comboItems: undefined,
17 displayField: 'value',
18 valueField: 'key',
19 queryMode: 'local',
20
21 // overide framework function to implement deleteEmpty behaviour
22 getSubmitData: function() {
23 var me = this,
24 data = null,
25 val;
26 if (!me.disabled && me.submitValue) {
27 val = me.getSubmitValue();
28 if (val !== null && val !== '' && val !== '__default__') {
29 data = {};
30 data[me.getName()] = val;
31 } else if (me.getDeleteEmpty()) {
32 data = {};
33 data['delete'] = me.getName();
34 }
35 }
36 return data;
37 },
38
39 validator: function(val) {
40 var me = this;
41
42 if (me.editable || val === null || val === '') {
43 return true;
44 }
45
46 if (me.store.getCount() > 0) {
47 var values = me.multiSelect ? val.split(me.delimiter) : [val];
48 var items = me.store.getData().collect('value', 'data');
49 if (Ext.Array.every(values, function(value) {
50 return Ext.Array.contains(items, value);
51 })) {
52 return true;
53 }
54 }
55
56 // returns a boolean or string
57 /*jslint confusion: true */
58 return "value '" + val + "' not allowed!";
59 },
60
61 initComponent: function() {
62 var me = this;
63
64 me.store = Ext.create('Ext.data.ArrayStore', {
65 model: 'KeyValue',
66 data : me.comboItems
67 });
68
69 if (me.initialConfig.editable === undefined) {
70 me.editable = false;
71 }
72
73 me.callParent();
74 },
75
76 setComboItems: function(items) {
77 var me = this;
78
79 me.getStore().setData(items);
80 }
81
82 });