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