luci-base: ui.js: fix multi vs. multiple keyword confusion

The "luci.form" and "luci.tools.widgets" classes use the "multiple"
keyword while ui.js uses "multi" internally, leading to single-value
dropdowns widget values getting stored as uci lists.

Fix the issue by using "multiple" everywhere.

Fixes: #2871
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
This commit is contained in:
Jo-Philipp Wich 2019-07-19 10:39:54 +02:00
parent 776e6d5409
commit dc0211803e

View file

@ -213,13 +213,13 @@ var UISelect = UIElement.extend({
if (!Array.isArray(value)) if (!Array.isArray(value))
value = (value != null && value != '') ? [ value ] : []; value = (value != null && value != '') ? [ value ] : [];
if (!options.multi && value.length > 1) if (!options.multiple && value.length > 1)
value.length = 1; value.length = 1;
this.values = value; this.values = value;
this.choices = choices; this.choices = choices;
this.options = Object.assign({ this.options = Object.assign({
multi: false, multiple: false,
widget: 'select', widget: 'select',
orientation: 'horizontal' orientation: 'horizontal'
}, options); }, options);
@ -240,7 +240,7 @@ var UISelect = UIElement.extend({
'name': this.options.name, 'name': this.options.name,
'size': this.options.size, 'size': this.options.size,
'class': 'cbi-input-select', 'class': 'cbi-input-select',
'multiple': this.options.multi ? '' : null 'multiple': this.options.multiple ? '' : null
})); }));
if (this.options.optional) if (this.options.optional)
@ -267,8 +267,8 @@ var UISelect = UIElement.extend({
E('input', { E('input', {
'id': this.options.id ? 'widget.' + this.options.id : null, 'id': this.options.id ? 'widget.' + this.options.id : null,
'name': this.options.id || this.options.name, 'name': this.options.id || this.options.name,
'type': this.options.multi ? 'checkbox' : 'radio', 'type': this.options.multiple ? 'checkbox' : 'radio',
'class': this.options.multi ? 'cbi-input-checkbox' : 'cbi-input-radio', 'class': this.options.multiple ? 'cbi-input-checkbox' : 'cbi-input-radio',
'value': keys[i], 'value': keys[i],
'checked': (this.values.indexOf(keys[i]) > -1) ? '' : null 'checked': (this.values.indexOf(keys[i]) > -1) ? '' : null
}), }),
@ -345,7 +345,7 @@ var UIDropdown = UIElement.extend({
this.choices = choices; this.choices = choices;
this.options = Object.assign({ this.options = Object.assign({
sort: true, sort: true,
multi: Array.isArray(value), multiple: Array.isArray(value),
optional: true, optional: true,
select_placeholder: _('-- Please choose --'), select_placeholder: _('-- Please choose --'),
custom_placeholder: _('-- custom --'), custom_placeholder: _('-- custom --'),
@ -361,7 +361,7 @@ var UIDropdown = UIElement.extend({
var sb = E('div', { var sb = E('div', {
'id': this.options.id, 'id': this.options.id,
'class': 'cbi-dropdown', 'class': 'cbi-dropdown',
'multiple': this.options.multi ? '' : null, 'multiple': this.options.multiple ? '' : null,
'optional': this.options.optional ? '' : null, 'optional': this.options.optional ? '' : null,
}, E('ul')); }, E('ul'));
@ -409,7 +409,7 @@ var UIDropdown = UIElement.extend({
bind: function(sb) { bind: function(sb) {
var o = this.options; var o = this.options;
o.multi = sb.hasAttribute('multiple'); o.multiple = sb.hasAttribute('multiple');
o.optional = sb.hasAttribute('optional'); o.optional = sb.hasAttribute('optional');
o.placeholder = sb.getAttribute('placeholder') || o.placeholder; o.placeholder = sb.getAttribute('placeholder') || o.placeholder;
o.display_items = parseInt(sb.getAttribute('display-items') || o.display_items); o.display_items = parseInt(sb.getAttribute('display-items') || o.display_items);
@ -425,7 +425,7 @@ var UIDropdown = UIElement.extend({
ndisplay = this.options.display_items, ndisplay = this.options.display_items,
n = 0; n = 0;
if (this.options.multi) { if (this.options.multiple) {
var items = ul.querySelectorAll('li'); var items = ul.querySelectorAll('li');
for (var i = 0; i < items.length; i++) { for (var i = 0; i < items.length; i++) {
@ -657,7 +657,7 @@ var UIDropdown = UIElement.extend({
if (li.hasAttribute('unselectable')) if (li.hasAttribute('unselectable'))
return; return;
if (this.options.multi) { if (this.options.multiple) {
var cbox = li.querySelector('input[type="checkbox"]'), var cbox = li.querySelector('input[type="checkbox"]'),
items = li.parentNode.querySelectorAll('li'), items = li.parentNode.querySelectorAll('li'),
label = sb.querySelector('ul.preview'), label = sb.querySelector('ul.preview'),
@ -780,7 +780,7 @@ var UIDropdown = UIElement.extend({
element: sb element: sb
}; };
if (this.options.multi) if (this.options.multiple)
detail.values = values; detail.values = values;
else else
detail.value = values.length ? values[0] : null; detail.value = values.length ? values[0] : null;
@ -800,12 +800,12 @@ var UIDropdown = UIElement.extend({
for (var value in values) { for (var value in values) {
this.createItems(sb, value); this.createItems(sb, value);
if (!this.options.multi) if (!this.options.multiple)
break; break;
} }
} }
if (this.options.multi) { if (this.options.multiple) {
var lis = ul.querySelectorAll('li[data-value]'); var lis = ul.querySelectorAll('li[data-value]');
for (var i = 0; i < lis.length; i++) { for (var i = 0; i < lis.length; i++) {
var value = lis[i].getAttribute('data-value'); var value = lis[i].getAttribute('data-value');
@ -857,7 +857,7 @@ var UIDropdown = UIElement.extend({
val = (value || '').trim(), val = (value || '').trim(),
ul = sb.querySelector('ul'); ul = sb.querySelector('ul');
if (!sbox.options.multi) if (!sbox.options.multiple)
val = val.length ? [ val ] : []; val = val.length ? [ val ] : [];
else else
val = val.length ? val.split(/\s+/) : []; val = val.length ? val.split(/\s+/) : [];
@ -881,7 +881,7 @@ var UIDropdown = UIElement.extend({
new_item = E(markup.replace(/{{value}}/g, item)); new_item = E(markup.replace(/{{value}}/g, item));
if (sbox.options.multi) { if (sbox.options.multiple) {
sbox.transformItem(sb, new_item); sbox.transformItem(sb, new_item);
} }
else { else {
@ -1071,7 +1071,7 @@ var UIDropdown = UIElement.extend({
}, },
setValue: function(values) { setValue: function(values) {
if (this.options.multi) { if (this.options.multiple) {
if (!Array.isArray(values)) if (!Array.isArray(values))
values = (values != null && values != '') ? [ values ] : []; values = (values != null && values != '') ? [ values ] : [];
@ -1104,7 +1104,7 @@ var UIDropdown = UIElement.extend({
for (var i = 0; i < h.length; i++) for (var i = 0; i < h.length; i++)
v.push(h[i].value); v.push(h[i].value);
return this.options.multi ? v : v[0]; return this.options.multiple ? v : v[0];
} }
}); });
@ -1116,7 +1116,7 @@ var UICombobox = UIDropdown.extend({
dropdown_items: -1, dropdown_items: -1,
sort: true sort: true
}, options, { }, options, {
multi: false, multiple: false,
create: true, create: true,
optional: true optional: true
}) ]); }) ]);
@ -1134,7 +1134,7 @@ var UIDynamicList = UIElement.extend({
this.values = values; this.values = values;
this.choices = choices; this.choices = choices;
this.options = Object.assign({}, options, { this.options = Object.assign({}, options, {
multi: false, multiple: false,
optional: true optional: true
}); });
}, },