From dd139ee89e45a343467de95493e59a4f049d233f Mon Sep 17 00:00:00 2001 From: Anya Lin Date: Sat, 11 Jan 2025 17:34:19 +0800 Subject: [PATCH] luci-base: add member `allowduplicates` to `DynamicList` Add member `allowduplicates` to `DynamicList` to allow duplicate underlying form values. `allowduplicates` defaults is `null`, `DynamicList` will keep the same behavior as before. If `true`, the underlying form value will not be checked for duplication. Signed-off-by: Anya Lin --- .../luci-base/htdocs/luci-static/resources/form.js | 12 ++++++++++++ modules/luci-base/htdocs/luci-static/resources/ui.js | 5 +++-- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/modules/luci-base/htdocs/luci-static/resources/form.js b/modules/luci-base/htdocs/luci-static/resources/form.js index e1fc3ff3e9dd..2d0304003cd7 100644 --- a/modules/luci-base/htdocs/luci-static/resources/form.js +++ b/modules/luci-base/htdocs/luci-static/resources/form.js @@ -3863,6 +3863,17 @@ const CBIValue = CBIAbstractValue.extend(/** @lends LuCI.form.Value.prototype */ const CBIDynamicList = CBIValue.extend(/** @lends LuCI.form.DynamicList.prototype */ { __name__: 'CBI.DynamicList', + /** + * Allows the underlying form controls to have multiple identical values. + * + * Default is `null`. If `true`, the underlying form value will + * not be checked for duplication. + * + * @name LuCI.form.DynamicList.prototype#allowduplicates + * @type boolean + * @default null + */ + /** @private */ renderWidget(section_id, option_index, cfgvalue) { const value = (cfgvalue != null) ? cfgvalue : this.default; @@ -3872,6 +3883,7 @@ const CBIDynamicList = CBIValue.extend(/** @lends LuCI.form.DynamicList.prototyp const widget = new ui.DynamicList(items, choices, { id: this.cbid(section_id), sort: this.keylist, + allowduplicates: this.allowduplicates, optional: this.optional || this.rmempty, datatype: this.datatype, placeholder: this.placeholder, diff --git a/modules/luci-base/htdocs/luci-static/resources/ui.js b/modules/luci-base/htdocs/luci-static/resources/ui.js index 391d64a35449..1dc5ee15bfe7 100644 --- a/modules/luci-base/htdocs/luci-static/resources/ui.js +++ b/modules/luci-base/htdocs/luci-static/resources/ui.js @@ -2424,7 +2424,7 @@ const UIDynamicList = UIElement.extend(/** @lends LuCI.ui.DynamicList.prototype exists = true; }); - if (!exists) { + if (this.options.allowduplicates || !exists) { const ai = dl.querySelector('.add-item'); ai.parentNode.insertBefore(new_item, ai); } @@ -2505,7 +2505,8 @@ const UIDynamicList = UIElement.extend(/** @lends LuCI.ui.DynamicList.prototype return; sbIn.setValues(sbEl, null); - sbVal.element.setAttribute('unselectable', ''); + if (!this.options.allowduplicates) + sbVal.element.setAttribute('unselectable', ''); if (sbVal.element.hasAttribute('created')) { sbVal.element.removeAttribute('created');