From 29b341cd2b3e58f3f4033a8c3639257cb6ec8d41 Mon Sep 17 00:00:00 2001 From: Jim Graham Date: Fri, 19 Apr 2024 00:05:20 -0400 Subject: [PATCH] Update Listbox (Multiple) TV --- core/lexicon/en/tv_widget.inc.php | 2 ++ .../mgr/inputproperties/listbox-multiple.php | 27 +++++++++++++++++-- .../mgr/input/listbox-multiple.class.php | 19 +++++++++++++ 3 files changed, 46 insertions(+), 2 deletions(-) diff --git a/core/lexicon/en/tv_widget.inc.php b/core/lexicon/en/tv_widget.inc.php index 9166a8d5b3c..75e820bff09 100644 --- a/core/lexicon/en/tv_widget.inc.php +++ b/core/lexicon/en/tv_widget.inc.php @@ -34,6 +34,8 @@ $_lang['combo_listwidth_desc'] = 'The width, in % or px, of the dropdown list itself. Defaults to the width of the combobox.'; $_lang['combo_maxheight'] = 'Max Height'; $_lang['combo_maxheight_desc'] = 'The maximum height in pixels of the dropdown list before scrollbars are shown. (Default: 300)'; +$_lang['combo_preserve_selectionorder'] = 'Preserve Entry Order'; +$_lang['combo_preserve_selectionorder_desc'] = 'When set to Yes, saved items display in the order they were originally selected. Otherwise, items follow the order defined in the list options. (Default: No)'; $_lang['combo_stackitems'] = 'Stack Selected Items'; $_lang['combo_stackitems_desc'] = 'When set to Yes, the items will be stacked 1 per line. Defaults to No, which displays the items inline.'; $_lang['combo_title'] = 'List Header'; diff --git a/core/src/Revolution/Processors/Element/TemplateVar/Configs/mgr/inputproperties/listbox-multiple.php b/core/src/Revolution/Processors/Element/TemplateVar/Configs/mgr/inputproperties/listbox-multiple.php index b021b77fd64..09e68071e31 100644 --- a/core/src/Revolution/Processors/Element/TemplateVar/Configs/mgr/inputproperties/listbox-multiple.php +++ b/core/src/Revolution/Processors/Element/TemplateVar/Configs/mgr/inputproperties/listbox-multiple.php @@ -16,6 +16,7 @@ # Set values $forceSelection = $params['forceSelection'] === 'true' || $params['forceSelection'] == 1 ? 'true' : 'false' ; $stackItems = $params['stackItems'] === 'true' || $params['stackItems'] == 1 ? 'true' : 'false' ; +$preserveSelectionOrder = $params['preserveSelectionOrder'] === 'true' || $params['preserveSelectionOrder'] == 1 ? 'true' : 'false' ; $typeAhead = $params['typeAhead'] === 'true' || $params['typeAhead'] == 1 ? 'true' : 'false' ; $typeAheadDelay = !empty($params['typeAheadDelay']) ? $params['typeAheadDelay'] : 250 ; $listHeader = !empty($params['title']) ? json_encode($params['title']) : 'null' ; @@ -30,7 +31,8 @@ 'combo_typeahead_delay_desc', 'combo_forceselection_desc', 'combo_listempty_text_desc', - 'combo_stackitems_desc' + 'combo_stackitems_desc', + 'combo_preserve_selectionorder_desc' ]; $this->setHelpContent($descKeys, $expandHelp); @@ -88,7 +90,28 @@ }] }, { - columnWidth: 0.5, + columnWidth: 0.25, + defaults: { + anchor: '100%', + msgTarget: 'under' + }, + items: [{ + xtype: 'combo-boolean', + fieldLabel: _('combo_preserve_selectionorder'), + description: {$this->helpContent['eh_combo_preserve_selectionorder_desc']}, + name: 'inopt_preserveSelectionOrder', + hiddenName: 'inopt_preserveSelectionOrder', + id: 'inopt_preserveSelectionOrder{$tvId}', + value: {$preserveSelectionOrder} + },{ + xtype: '{$helpXtype}', + forId: 'inopt_preserveSelectionOrder{$tvId}', + html: {$this->helpContent['combo_preserve_selectionorder_desc']}, + cls: 'desc-under' + }] + }, + { + columnWidth: 0.25, defaults: { anchor: '100%', msgTarget: 'under' diff --git a/core/src/Revolution/Processors/Element/TemplateVar/Renders/mgr/input/listbox-multiple.class.php b/core/src/Revolution/Processors/Element/TemplateVar/Renders/mgr/input/listbox-multiple.class.php index 4ee53e8428c..b6ef8bab4fa 100644 --- a/core/src/Revolution/Processors/Element/TemplateVar/Renders/mgr/input/listbox-multiple.class.php +++ b/core/src/Revolution/Processors/Element/TemplateVar/Renders/mgr/input/listbox-multiple.class.php @@ -77,7 +77,26 @@ public function process($value, array $params = []) } $items = array_merge($selections, $items); + if ($params['preserveSelectionOrder']) { + $this->reorderBySelectionOrder($savedValues, $items); + } + $this->setPlaceholder('opts', $items); } + + private function reorderBySelectionOrder(array $selections, array &$items) + { + $itemsValues = array_column($items, 'value'); + $orderedSelections = []; + + foreach ($selections as $selection) { + $index = array_search($selection, $itemsValues); + if ($index !== false) { + $orderedSelections[] = $items[$index]; + unset($items[$index]); + } + } + $items = array_merge($orderedSelections, $items); + } } return 'modTemplateVarInputRenderListboxMultiple';