From 00d93e4b8777fc3437e8978f772ffb2688eb2528 Mon Sep 17 00:00:00 2001
From: Claas Kazzer <kazzer@uni-leipzig.de>
Date: Thu, 25 Mar 2021 13:08:18 +0100
Subject: [PATCH] refs #18277 [FINC: Adv Search] add / remove fields and groups

* corrects misplaced label
* sync meta attributes for inputs with js
* fix "for"-attribute of type label
* use human readable numeration for legend and aria-label - js version
* also set reset focus after deleting search field on last search field

co-authored by: Robert Lange <robert.lange@uni-leipzig.de>
---
 themes/finc/js/advanced_search.js                 | 12 ++++++++++--
 themes/finc/templates/search/advanced/limit.phtml |  2 +-
 2 files changed, 11 insertions(+), 3 deletions(-)

diff --git a/themes/finc/js/advanced_search.js b/themes/finc/js/advanced_search.js
index 5dad9556167..dde587e5f5a 100644
--- a/themes/finc/js/advanced_search.js
+++ b/themes/finc/js/advanced_search.js
@@ -10,10 +10,13 @@ function addSearch(group, _fieldValues) {
   var $newSearch = $($('#new_search_template').html());
 
   $newSearch.attr('id', 'search' + inputID);
+  $newSearch.find('fieldset legend').text($newSearch.find('fieldset legend').text().replace('1', (groupLength[group] + 1)));
   $newSearch.find('input.form-control')
     .attr('id', 'search_lookfor' + inputID)
     .attr('name', 'lookfor' + group + '[]')
     .val('');
+  $newSearch.find('.adv-input label').attr('for', 'search_lookfor' + inputID);
+  $newSearch.find('.adv-select label').attr('for', 'search_type' + inputID);
   $newSearch.find('select.adv-term-type option:first-child').attr('selected', 1);
   $newSearch.find('select.adv-term-type')
     .attr('id', 'search_type' + inputID)
@@ -44,7 +47,6 @@ function addSearch(group, _fieldValues) {
       .attr('name', 'op' + group + '[]')
       .removeClass('hidden');
     $newSearch.find('.first-op').remove();
-    $newSearch.find('label').remove();
     // Show x if we have more than one search inputs
     $('#group' + group + ' .adv-term-remove').removeClass('hidden');
   }
@@ -67,7 +69,12 @@ function deleteSearch(group, sindex) {
   }
   if (groupLength[group] > 1) {
     groupLength[group]--;
-    $('#search' + group + '_' + groupLength[group]).remove();
+    var toRemove = $('#search' + group + '_' + groupLength[group]);
+    var parent = toRemove.parent();
+    toRemove.remove();
+    if(parent.length) {
+      parent.find('.adv-search input.form-control').focus();
+    }
     if (groupLength[group] === 1) {
       $('#group' + group + ' .adv-term-remove').addClass('hidden'); // Hide x
     }
@@ -84,6 +91,7 @@ function addGroup(_firstTerm, _firstField, _join) {
   $newGroup.find('.adv-group-label') // update label
     .attr('for', 'search_lookfor' + nextGroup + '_0');
   $newGroup.attr('id', 'group' + nextGroup);
+  $newGroup.attr('aria-label', $newGroup.attr('aria-label').replace('0', nextGroup + 1));
   $newGroup.find('.search_place_holder')
     .attr('id', 'group' + nextGroup + 'Holder')
     .removeClass('hidden');
diff --git a/themes/finc/templates/search/advanced/limit.phtml b/themes/finc/templates/search/advanced/limit.phtml
index 3a48c286d3f..1143d26934e 100644
--- a/themes/finc/templates/search/advanced/limit.phtml
+++ b/themes/finc/templates/search/advanced/limit.phtml
@@ -10,12 +10,12 @@
 <?php if (count($limitList) > 1): ?>
   <fieldset class="limits">
     <legend><?=$this->transEsc('Results per page')?></legend>
+    <label for="limit"><?=$this->transEsc('Limit To')?></label>
     <select id="limit" name="limit" class="form-control">
       <?php foreach ($limitList as $limitVal): ?>
         <option value="<?=$this->escapeHtmlAttr($limitVal)?>" <?=($limitVal == $defaultLimit) ? 'selected="selected"' : ''?>><?=$this->escapeHtml($limitVal)?></option>
       <?php endforeach; ?>
     </select>
-    <label for="limit"><?=$this->transEsc('Limit To')?></label>
   </fieldset>
 <?php endif; ?>
 <!-- finc: search - advanced - limit - END -->
\ No newline at end of file
-- 
GitLab