]> git.proxmox.com Git - pve-manager.git/blame - www/manager6/form/BandwidthSelector.js
ui: transform bandwidth-field to generic size-field & derive bandwidth from that
[pve-manager.git] / www / manager6 / form / BandwidthSelector.js
CommitLineData
635163b0 1Ext.define('PVE.form.SizeField', {
56b14a54 2 extend: 'Ext.form.FieldContainer',
635163b0 3 alias: 'widget.pveSizeField',
56b14a54 4
8058410f 5 mixins: ['Proxmox.Mixin.CBind'],
56b14a54
TL
6
7 viewModel: {
8 data: {
9 unit: 'MiB',
635163b0 10 unitPostfix: '',
56b14a54
TL
11 },
12 formulas: {
d43aec90 13 unitlabel: (get) => get('unit') + get('unitPostfix'),
f6710aac 14 },
56b14a54
TL
15 },
16
17 emptyText: '',
18
19 layout: 'hbox',
20 defaults: {
f6710aac 21 hideLabel: true,
56b14a54
TL
22 },
23
24 units: {
25 'KiB': 1024,
26 'MiB': 1024*1024,
27 'GiB': 1024*1024*1024,
28 'KB': 1000,
29 'MB': 1000*1000,
30 'GB': 1000*1000*1000,
31 },
32
33 // display unit (TODO: make (optionally) selectable)
34 unit: 'MiB',
635163b0 35 unitPostfix: '',
56b14a54
TL
36
37 // use this if the backend saves values in another unit tha bytes, e.g.,
38 // for KiB set it to 'KiB'
39 backendUnit: undefined,
40
dcbc3b3e
FE
41 // allow setting 0 and using it as a submit value
42 allowZero: false,
43
56b14a54
TL
44 items: [
45 {
46 xtype: 'numberfield',
47 cbind: {
48 name: '{name}',
49 emptyText: '{emptyText}',
dcbc3b3e 50 allowZero: '{allowZero}',
56b14a54
TL
51 },
52 minValue: 0,
53 step: 1,
54 submitLocaleSeparator: false,
55 fieldStyle: 'text-align: right',
56 flex: 1,
57 enableKeyEvents: true,
58 setValue: function(v) {
59 if (!this._transformed) {
635163b0 60 let fieldContainer = this.up('fieldcontainer');
e1e930c3 61 let vm = fieldContainer.getViewModel();
56b14a54
TL
62 let unit = vm.get('unit');
63
e1e930c3
TL
64 v /= fieldContainer.units[unit];
65 v *= fieldContainer.backendFactor;
56b14a54
TL
66
67 this._transformed = true;
68 }
69
dcbc3b3e
FE
70 if (Number(v) === 0 && !this.allowZero) {
71 v = undefined;
72 }
56b14a54
TL
73
74 return Ext.form.field.Text.prototype.setValue.call(this, v);
75 },
76 getSubmitValue: function() {
77 let v = this.processRawValue(this.getRawValue());
8058410f 78 v = v.replace(this.decimalSeparator, '.');
56b14a54 79
dcbc3b3e
FE
80 if (v === undefined || v === '') {
81 return null;
82 }
83
84 if (Number(v) === 0) {
85 return this.allowZero ? 0 : null;
86 }
56b14a54 87
635163b0 88 let fieldContainer = this.up('fieldcontainer');
e1e930c3 89 let vm = fieldContainer.getViewModel();
56b14a54
TL
90 let unit = vm.get('unit');
91
e1e930c3
TL
92 v = parseFloat(v) * fieldContainer.units[unit];
93 v /= fieldContainer.backendFactor;
56b14a54 94
e1e930c3 95 return String(Math.floor(v));
56b14a54
TL
96 },
97 listeners: {
98 // our setValue gets only called if we have a value, avoid
99 // transformation of the first user-entered value
8058410f 100 keydown: function() { this._transformed = true; },
56b14a54
TL
101 },
102 },
103 {
104 xtype: 'displayfield',
105 name: 'unit',
106 submitValue: false,
107 padding: '0 0 0 10',
108 bind: {
109 value: '{unitlabel}',
110 },
111 listeners: {
e1e930c3
TL
112 change: (f, v) => {
113 f.originalValue = v;
114 },
56b14a54
TL
115 },
116 width: 40,
117 },
118 ],
119
120 initComponent: function() {
121 let me = this;
122
123 me.unit = me.unit || 'MiB';
124 if (!(me.unit in me.units)) {
125 throw "unknown unit: " + me.unit;
126 }
127
128 me.backendFactor = 1;
129 if (me.backendUnit !== undefined) {
130 if (!(me.unit in me.units)) {
131 throw "unknown backend unit: " + me.backendUnit;
132 }
133 me.backendFactor = me.units[me.backendUnit];
134 }
135
56b14a54
TL
136 me.callParent(arguments);
137
138 me.getViewModel().set('unit', me.unit);
d43aec90 139 me.getViewModel().set('unitPostfix', me.unitPostfix);
56b14a54
TL
140 },
141});
142
635163b0
TL
143Ext.define('PVE.form.BandwidthField', {
144 extend: 'PVE.form.SizeField',
145 alias: 'widget.pveBandwidthField',
146
147 unitPostfix: '/s',
148});