From f6577a8f33ac4fe1483585ee632cc6150e879027 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20B=C3=B6hmer?= Date: Sat, 24 Aug 2024 15:31:44 +0200 Subject: [PATCH] Allow to create sub elements for existing elements, by typing "->" This fixes issue #666 and #560 --- .../structural_entity_select_controller.js | 27 +++++++++++++++++-- .../extend_existing_selection.js | 0 2 files changed, 25 insertions(+), 2 deletions(-) create mode 100644 assets/tomselect/extend_existing_selection/extend_existing_selection.js diff --git a/assets/controllers/elements/structural_entity_select_controller.js b/assets/controllers/elements/structural_entity_select_controller.js index 5280c339..48dce186 100644 --- a/assets/controllers/elements/structural_entity_select_controller.js +++ b/assets/controllers/elements/structural_entity_select_controller.js @@ -24,7 +24,6 @@ import {Controller} from "@hotwired/stimulus"; import {trans, ENTITY_SELECT_GROUP_NEW_NOT_ADDED_TO_DB} from '../../translator.js' - export default class extends Controller { _tomSelect; @@ -58,7 +57,18 @@ export default class extends Controller { render: { item: this.renderItem.bind(this), option: this.renderOption.bind(this), - option_create: function(data, escape) { + option_create: (data, escape) => { + //If the input starts with "->", we prepend the current selected value, for easier extension of existing values + //This here handles the display part, while the createItem function handles the actual creation + if (data.input.startsWith("->")) { + //Get current selected value + const current = this._tomSelect.getItem(this._tomSelect.getValue()).textContent.replaceAll("→", "->").trim(); + //Prepend it to the input + if (current) { + data.input = current + " " + data.input; + } + } + return '
 ' + escape(data.input) + '… ' + '(' + addHint +')' + '
'; @@ -76,6 +86,19 @@ export default class extends Controller { } createItem(input, callback) { + + //If the input starts with "->", we prepend the current selected value, for easier extension of existing values + if (input.startsWith("->")) { + //Get current selected value + let current = this._tomSelect.getItem(this._tomSelect.getValue()).textContent.replaceAll("→", "->").trim(); + //Replace no break spaces with normal spaces + current = current.replaceAll("\u00A0", " "); + //Prepend it to the input + if (current) { + input = current + " " + input; + } + } + callback({ //$%$ is a special value prefix, that is used to identify items, that are not yet in the DB value: '$%$' + input, diff --git a/assets/tomselect/extend_existing_selection/extend_existing_selection.js b/assets/tomselect/extend_existing_selection/extend_existing_selection.js new file mode 100644 index 00000000..e69de29b