public inbox for pbs-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Dominik Csapak <d.csapak@proxmox.com>
To: pbs-devel@lists.proxmox.com
Subject: [pbs-devel] [PATCH widget-toolkit 1/1] form: copy BandwidthSelector/SizeField from PVE
Date: Fri, 19 Nov 2021 15:42:25 +0100	[thread overview]
Message-ID: <20211119144227.1337999-2-d.csapak@proxmox.com> (raw)
In-Reply-To: <20211119144227.1337999-1-d.csapak@proxmox.com>

and replace pve with pmx

Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
---
 src/Makefile                  |   1 +
 src/form/BandwidthSelector.js | 154 ++++++++++++++++++++++++++++++++++
 2 files changed, 155 insertions(+)
 create mode 100644 src/form/BandwidthSelector.js

diff --git a/src/Makefile b/src/Makefile
index c245a53..de34531 100644
--- a/src/Makefile
+++ b/src/Makefile
@@ -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
index 0000000..ce941cd
--- /dev/null
+++ b/src/form/BandwidthSelector.js
@@ -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',
+});
-- 
2.30.2





  reply	other threads:[~2021-11-19 14:43 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-11-19 14:42 [pbs-devel] [PATCH widget-toolkit/proxmox-backup] implement traffic-control ui Dominik Csapak
2021-11-19 14:42 ` Dominik Csapak [this message]
2021-11-19 15:12   ` [pbs-devel] applied: [PATCH widget-toolkit 1/1] form: copy BandwidthSelector/SizeField from PVE Thomas Lamprecht
2021-11-19 14:42 ` [pbs-devel] [PATCH proxmox-backup 1/2] api: traffic_control: add missing rename to 'kebab-case' Dominik Csapak
2021-11-20 21:47   ` [pbs-devel] applied: " Thomas Lamprecht
2021-11-19 14:42 ` [pbs-devel] [PATCH proxmox-backup 2/2] ui: add Traffic Control UI Dominik Csapak
2021-11-20 21:57   ` [pbs-devel] applied: " Thomas Lamprecht

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20211119144227.1337999-2-d.csapak@proxmox.com \
    --to=d.csapak@proxmox.com \
    --cc=pbs-devel@lists.proxmox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal