From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <d.csapak@proxmox.com>
Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68])
 (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
 key-exchange X25519 server-signature RSA-PSS (2048 bits))
 (No client certificate requested)
 by lists.proxmox.com (Postfix) with ESMTPS id 48F8980C7B
 for <pbs-devel@lists.proxmox.com>; Fri, 19 Nov 2021 15:43:06 +0100 (CET)
Received: from firstgate.proxmox.com (localhost [127.0.0.1])
 by firstgate.proxmox.com (Proxmox) with ESMTP id 37ACD2C849
 for <pbs-devel@lists.proxmox.com>; Fri, 19 Nov 2021 15:42:36 +0100 (CET)
Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com
 [94.136.29.106])
 (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
 key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256)
 (No client certificate requested)
 by firstgate.proxmox.com (Proxmox) with ESMTPS id 4F0AC2C828
 for <pbs-devel@lists.proxmox.com>; Fri, 19 Nov 2021 15:42:34 +0100 (CET)
Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1])
 by proxmox-new.maurer-it.com (Proxmox) with ESMTP id B9D1A43F79
 for <pbs-devel@lists.proxmox.com>; Fri, 19 Nov 2021 15:42:28 +0100 (CET)
From: Dominik Csapak <d.csapak@proxmox.com>
To: pbs-devel@lists.proxmox.com
Date: Fri, 19 Nov 2021 15:42:25 +0100
Message-Id: <20211119144227.1337999-2-d.csapak@proxmox.com>
X-Mailer: git-send-email 2.30.2
In-Reply-To: <20211119144227.1337999-1-d.csapak@proxmox.com>
References: <20211119144227.1337999-1-d.csapak@proxmox.com>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
X-SPAM-LEVEL: Spam detection results:  0
 AWL 0.211 Adjusted score from AWL reputation of From: address
 BAYES_00                 -1.9 Bayes spam probability is 0 to 1%
 KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment
 SPF_HELO_NONE           0.001 SPF: HELO does not publish an SPF Record
 SPF_PASS               -0.001 SPF: sender matches SPF record
Subject: [pbs-devel] [PATCH widget-toolkit 1/1] form: copy
 BandwidthSelector/SizeField from PVE
X-BeenThere: pbs-devel@lists.proxmox.com
X-Mailman-Version: 2.1.29
Precedence: list
List-Id: Proxmox Backup Server development discussion
 <pbs-devel.lists.proxmox.com>
List-Unsubscribe: <https://lists.proxmox.com/cgi-bin/mailman/options/pbs-devel>, 
 <mailto:pbs-devel-request@lists.proxmox.com?subject=unsubscribe>
List-Archive: <http://lists.proxmox.com/pipermail/pbs-devel/>
List-Post: <mailto:pbs-devel@lists.proxmox.com>
List-Help: <mailto:pbs-devel-request@lists.proxmox.com?subject=help>
List-Subscribe: <https://lists.proxmox.com/cgi-bin/mailman/listinfo/pbs-devel>, 
 <mailto:pbs-devel-request@lists.proxmox.com?subject=subscribe>
X-List-Received-Date: Fri, 19 Nov 2021 14:43:06 -0000

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