ui: dc/corosync-link: eslint fixes and code cleanup/refactoring

Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
This commit is contained in:
Thomas Lamprecht 2021-05-18 11:34:53 +02:00
parent 4ed38e2569
commit 95f59d0b89

View File

@ -4,14 +4,13 @@ Ext.define('PVE.form.CorosyncLinkEditorController', {
addLinkIfEmpty: function() { addLinkIfEmpty: function() {
let view = this.getView(); let view = this.getView();
if (view.items || view.items.length == 0) { if (view.items || view.items.length === 0) {
this.addLink(); this.addLink();
} }
}, },
addEmptyLink: function() { addEmptyLink: function() {
// discard parameters to allow being called from 'handler' this.addLink(); // discard parameters to allow being called from 'handler'
this.addLink();
}, },
addLink: function(link) { addLink: function(link) {
@ -83,37 +82,29 @@ Ext.define('PVE.form.CorosyncLinkEditorController', {
getNextFreeNetwork: function() { getNextFreeNetwork: function() {
let view = this.getView(); let view = this.getView();
let vm = view.getViewModel(); let vm = view.getViewModel();
let netsInUse = Ext.Array.map(
view.query('proxmoxNetworkSelector'), selector => selector.value);
// default to empty field, user has to set up link manually let networksInUse = view.query('proxmoxNetworkSelector').map(selector => selector.value);
let retval = undefined;
let nets = vm.get('networks'); for (const network of vm.get('networks')) {
Ext.Array.each(nets, net => { if (!networksInUse.includes(network)) {
if (!Ext.Array.contains(netsInUse, net)) { return network;
retval = net;
return false; // break
} }
}); }
return undefined; // default to empty field, user has to set up link manually
return retval;
}, },
getNextFreeNumber: function() { getNextFreeNumber: function() {
let view = this.getView(); let view = this.getView();
let vm = view.getViewModel(); let vm = view.getViewModel();
let numbersInUse = Ext.Array.map(
view.query('numberfield'), field => field.value); let numbersInUse = view.query('numberfield').map(field => field.value);
for (let i = 0; i < vm.get('maxLinkCount'); i++) { for (let i = 0; i < vm.get('maxLinkCount'); i++) {
if (!Ext.Array.contains(numbersInUse, i)) { if (!numbersInUse.includes(i)) {
return i; return i;
} }
} }
// all numbers in use, this should never happen since add button is disabled automatically
// all numbers in use, this should never happen since add button is
// disabled automatically
return 0; return 0;
}, },
}); });
@ -180,11 +171,9 @@ Ext.define('PVE.form.CorosyncLinkSelector', {
width: 220, width: 220,
margin: '0 5px 0 5px', margin: '0 5px 0 5px',
getSubmitValue: function() { getSubmitValue: function() {
// link number is encoded into key, so we need to set field
// name before value retrieval
let me = this; let me = this;
let numSelect = me.prev('numberfield'); // always the correct one // link number is encoded into key, so we need to set field name before value retrieval
let linkNumber = numSelect.getValue(); let linkNumber = me.prev('numberfield').getValue(); // always the correct one
me.name = 'link' + linkNumber; me.name = 'link' + linkNumber;
return me.getValue(); return me.getValue();
}, },
@ -225,29 +214,28 @@ Ext.define('PVE.form.CorosyncLinkSelector', {
let numSelect = me.down('numberfield'); let numSelect = me.down('numberfield');
let netSelect = me.down('proxmoxNetworkSelector'); let netSelect = me.down('proxmoxNetworkSelector');
numSelect.validator = this.createNoDuplicatesValidator( numSelect.validator = me.createNoDuplicatesValidator(
'numberfield', 'numberfield',
gettext("Duplicate link number not allowed."), gettext("Duplicate link number not allowed."),
); );
netSelect.validator = this.createNoDuplicatesValidator( netSelect.validator = me.createNoDuplicatesValidator(
'proxmoxNetworkSelector', 'proxmoxNetworkSelector',
gettext("Duplicate link address not allowed."), gettext("Duplicate link address not allowed."),
); );
}, },
createNoDuplicatesValidator: function(queryString, errorMsg) { createNoDuplicatesValidator: function(queryString, errorMsg) { // linkSelector generator
// linkSelector let view = this; // eslint-disable-line consistent-this
let me = this; /** @this is the field itself, as the validator this is called from scopes it that way */
return function(val) { return function(val) {
let curField = this; let me = this;
let form = me.up('form'); let form = view.up('form');
let linkEditor = me.up('pveCorosyncLinkEditor'); let linkEditor = view.up('pveCorosyncLinkEditor');
if (!form.validating) { if (!form.validating) {
// avoid recursion/double validation by setting temporary states // avoid recursion/double validation by setting temporary states
curField.validating = true; me.validating = true;
form.validating = true; form.validating = true;
// validate all other fields as well, to always mark both // validate all other fields as well, to always mark both
@ -255,28 +243,23 @@ Ext.define('PVE.form.CorosyncLinkSelector', {
form.isValid(); form.isValid();
form.validating = false; form.validating = false;
curField.validating = false; me.validating = false;
} else if (curField.validating) { } else if (me.validating) {
// we'll be validated by the original call in the other // we'll be validated by the original call in the other if-branch, avoid double work
// if-branch, avoid double work
return true; return true;
} }
if (val === undefined || val instanceof String && val.length === 0) { if (val === undefined || (val instanceof String && val.length === 0)) {
// let this be caught by allowBlank, if at all return true; // let this be caught by allowBlank, if at all
return true;
} }
let allFields = linkEditor.query(queryString); let allFields = linkEditor.query(queryString);
let err = undefined; for (const field of allFields) {
Ext.Array.each(allFields, field => { if (field !== me && String(field.getValue()) === String(val)) {
if (field != curField && field.getValue() == val) { return errorMsg;
err = errorMsg;
return false; // break
} }
}); }
return true;
return err || true;
}; };
}, },
}); });