]>
git.proxmox.com Git - proxmox-widget-toolkit.git/blob - src/form/BandwidthSelector.js
1 Ext
.define('Proxmox.form.SizeField', {
2 extend
: 'Ext.form.FieldContainer',
3 alias
: 'widget.pmxSizeField',
5 mixins
: ['Proxmox.Mixin.CBind'],
13 unitlabel
: (get) => get('unit') + get('unitPostfix'),
24 // display unit (TODO: make (optionally) selectable)
28 // use this if the backend saves values in another unit than bytes, e.g.,
29 // for KiB set it to 'KiB'
30 backendUnit
: undefined,
32 // submit a canonical size unit, e.g., 20.5 MiB
33 submitAutoScaledSizeUnit
: false,
35 // allow setting 0 and using it as a submit value
45 emptyText
: '{emptyText}',
46 allowZero
: '{allowZero}',
47 emptyValue
: '{emptyValue}',
51 submitLocaleSeparator
: false,
52 fieldStyle
: 'text-align: right',
54 enableKeyEvents
: true,
55 setValue: function(v
) {
56 if (!this._transformed
) {
57 let fieldContainer
= this.up('fieldcontainer');
58 let vm
= fieldContainer
.getViewModel();
59 let unit
= vm
.get('unit');
61 if (typeof v
=== "string") {
62 v
= Proxmox
.Utils
.size_unit_to_bytes(v
);
64 v
/= Proxmox
.Utils
.SizeUnits
[unit
];
65 v
*= fieldContainer
.backendFactor
;
67 this._transformed
= true;
70 if (Number(v
) === 0 && !this.allowZero
) {
74 return Ext
.form
.field
.Text
.prototype.setValue
.call(this, v
);
76 getSubmitValue: function() {
77 let v
= this.processRawValue(this.getRawValue());
78 v
= v
.replace(this.decimalSeparator
, '.');
80 if (v
=== undefined || v
=== '') {
81 return this.emptyValue
;
84 if (Number(v
) === 0) {
85 return this.allowZero
? 0 : null;
88 let fieldContainer
= this.up('fieldcontainer');
89 let vm
= fieldContainer
.getViewModel();
90 let unit
= vm
.get('unit');
92 v
= parseFloat(v
) * Proxmox
.Utils
.SizeUnits
[unit
];
94 if (fieldContainer
.submitAutoScaledSizeUnit
) {
95 return Proxmox
.Utils
.format_size(v
, !unit
.endsWith('iB'));
97 return String(Math
.floor(v
/ fieldContainer
.backendFactor
));
101 // our setValue gets only called if we have a value, avoid
102 // transformation of the first user-entered value
103 keydown: function() { this._transformed
= true; },
107 xtype
: 'displayfield',
112 value
: '{unitlabel}',
123 initComponent: function() {
126 me
.unit
= me
.unit
|| 'MiB';
127 if (!(me
.unit
in Proxmox
.Utils
.SizeUnits
)) {
128 throw "unknown unit: " + me
.unit
;
131 me
.backendFactor
= 1;
132 if (me
.backendUnit
!== undefined) {
133 if (!(me
.unit
in Proxmox
.Utils
.SizeUnits
)) {
134 throw "unknown backend unit: " + me
.backendUnit
;
136 me
.backendFactor
= Proxmox
.Utils
.SizeUnits
[me
.backendUnit
];
139 me
.callParent(arguments
);
141 me
.getViewModel().set('unit', me
.unit
);
142 me
.getViewModel().set('unitPostfix', me
.unitPostfix
);
146 Ext
.define('Proxmox.form.BandwidthField', {
147 extend
: 'Proxmox.form.SizeField',
148 alias
: 'widget.pmxBandwidthField',