From d7c741c6526ac10dacab817a19724dd3fe47c053 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20B=C3=B6hmer?= Date: Sun, 16 Feb 2025 21:14:57 +0100 Subject: [PATCH] Disable create option for input selects if an entity with this name already exists --- .../structural_entity_select_controller.js | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/assets/controllers/elements/structural_entity_select_controller.js b/assets/controllers/elements/structural_entity_select_controller.js index 6d436676..a1114a97 100644 --- a/assets/controllers/elements/structural_entity_select_controller.js +++ b/assets/controllers/elements/structural_entity_select_controller.js @@ -48,6 +48,7 @@ export default class extends Controller { selectOnTab: true, maxOptions: null, create: allowAdd ? this.createItem.bind(this) : false, + createFilter: this.createFilter.bind(this), // This three options allow us to paste element names with commas: (see issue #538) maxItems: 1, @@ -128,6 +129,31 @@ export default class extends Controller { }); } + createFilter(input) { + + //Normalize the input (replace spacing around arrows) + if (input.includes("->")) { + const inputs = input.split("->"); + inputs.forEach((value, index) => { + inputs[index] = value.trim(); + }); + input = inputs.join("->"); + } else { + input = input.trim(); + } + + const options = this._tomSelect.options; + //Iterate over all options and check if the input is already present + for (let index in options) { + const option = options[index]; + if (option.path === input) { + return false; + } + } + + return true; + } + updateValidity() { //Mark this input as invalid, if the selected option is disabled