]> git.proxmox.com Git - proxmox-widget-toolkit.git/commitdiff
form: copy BandwidthSelector/SizeField from PVE
authorDominik Csapak <d.csapak@proxmox.com>
Fri, 19 Nov 2021 14:42:25 +0000 (15:42 +0100)
committerThomas Lamprecht <t.lamprecht@proxmox.com>
Fri, 19 Nov 2021 15:04:55 +0000 (16:04 +0100)
and replace pve with pmx

Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
src/Makefile
src/form/BandwidthSelector.js [new file with mode: 0644]

index c245a53254ee68c203e87525f68273ddd4713972..de34531f25ae6d46e1d1bd5708ec1582817ea5b4 100644 (file)
@@ -23,6 +23,7 @@ JSSRC=                                        \
        data/model/Realm.js             \
        data/model/Certificates.js      \
        data/model/ACME.js              \
+       form/BandwidthSelector.js       \
        form/DisplayEdit.js             \
        form/ExpireDate.js              \
        form/IntegerField.js            \
diff --git a/src/form/BandwidthSelector.js b/src/form/BandwidthSelector.js
new file mode 100644 (file)
index 0000000..ce941cd
--- /dev/null
@@ -0,0 +1,154 @@
+Ext.define('Proxmox.form.SizeField', {
+    extend: 'Ext.form.FieldContainer',
+    alias: 'widget.pmxSizeField',
+
+    mixins: ['Proxmox.Mixin.CBind'],
+
+    viewModel: {
+       data: {
+           unit: 'MiB',
+           unitPostfix: '',
+       },
+       formulas: {
+           unitlabel: (get) => get('unit') + get('unitPostfix'),
+       },
+    },
+
+    emptyText: '',
+
+    layout: 'hbox',
+    defaults: {
+       hideLabel: true,
+    },
+
+    units: {
+       'B': 1,
+       'KiB': 1024,
+       'MiB': 1024*1024,
+       'GiB': 1024*1024*1024,
+       'TiB': 1024*1024*1024*1024,
+       'KB': 1000,
+       'MB': 1000*1000,
+       'GB': 1000*1000*1000,
+       'TB': 1000*1000*1000*1000,
+    },
+
+    // display unit (TODO: make (optionally) selectable)
+    unit: 'MiB',
+    unitPostfix: '',
+
+    // use this if the backend saves values in another unit tha bytes, e.g.,
+    // for KiB set it to 'KiB'
+    backendUnit: undefined,
+
+    // allow setting 0 and using it as a submit value
+    allowZero: false,
+
+    emptyValue: null,
+
+    items: [
+       {
+           xtype: 'numberfield',
+           cbind: {
+               name: '{name}',
+               emptyText: '{emptyText}',
+               allowZero: '{allowZero}',
+               emptyValue: '{emptyValue}',
+           },
+           minValue: 0,
+           step: 1,
+           submitLocaleSeparator: false,
+           fieldStyle: 'text-align: right',
+           flex: 1,
+           enableKeyEvents: true,
+           setValue: function(v) {
+               if (!this._transformed) {
+                   let fieldContainer = this.up('fieldcontainer');
+                   let vm = fieldContainer.getViewModel();
+                   let unit = vm.get('unit');
+
+                   v /= fieldContainer.units[unit];
+                   v *= fieldContainer.backendFactor;
+
+                   this._transformed = true;
+               }
+
+               if (Number(v) === 0 && !this.allowZero) {
+                   v = undefined;
+               }
+
+               return Ext.form.field.Text.prototype.setValue.call(this, v);
+           },
+           getSubmitValue: function() {
+               let v = this.processRawValue(this.getRawValue());
+               v = v.replace(this.decimalSeparator, '.');
+
+               if (v === undefined || v === '') {
+                   return this.emptyValue;
+               }
+
+               if (Number(v) === 0) {
+                   return this.allowZero ? 0 : null;
+               }
+
+               let fieldContainer = this.up('fieldcontainer');
+               let vm = fieldContainer.getViewModel();
+               let unit = vm.get('unit');
+
+               v = parseFloat(v) * fieldContainer.units[unit];
+               v /= fieldContainer.backendFactor;
+
+               return String(Math.floor(v));
+           },
+           listeners: {
+               // our setValue gets only called if we have a value, avoid
+               // transformation of the first user-entered value
+               keydown: function() { this._transformed = true; },
+           },
+       },
+       {
+           xtype: 'displayfield',
+           name: 'unit',
+           submitValue: false,
+           padding: '0 0 0 10',
+           bind: {
+               value: '{unitlabel}',
+           },
+           listeners: {
+               change: (f, v) => {
+                   f.originalValue = v;
+               },
+           },
+           width: 40,
+       },
+    ],
+
+    initComponent: function() {
+       let me = this;
+
+       me.unit = me.unit || 'MiB';
+       if (!(me.unit in me.units)) {
+           throw "unknown unit: " + me.unit;
+       }
+
+       me.backendFactor = 1;
+       if (me.backendUnit !== undefined) {
+           if (!(me.unit in me.units)) {
+               throw "unknown backend unit: " + me.backendUnit;
+           }
+           me.backendFactor = me.units[me.backendUnit];
+       }
+
+       me.callParent(arguments);
+
+       me.getViewModel().set('unit', me.unit);
+       me.getViewModel().set('unitPostfix', me.unitPostfix);
+    },
+});
+
+Ext.define('Proxmox.form.BandwidthField', {
+    extend: 'Proxmox.form.SizeField',
+    alias: 'widget.pmxBandwidthField',
+
+    unitPostfix: '/s',
+});