From 1af88abc2f26bd817d707ce2a17c079580c3d9c5 Mon Sep 17 00:00:00 2001 From: Arlen Johnson Date: Thu, 21 Sep 2023 11:22:56 -0400 Subject: [PATCH] Change global search from POST to GET to allow better back-button behavior (CFM-323) (#126) --- app/src/Controller/DashboardsController.php | 13 ++++--------- app/templates/element/javascript.php | 6 +++--- app/templates/element/searchGlobal.php | 11 ++++++----- app/webroot/css/co-base.css | 2 +- 4 files changed, 14 insertions(+), 18 deletions(-) diff --git a/app/src/Controller/DashboardsController.php b/app/src/Controller/DashboardsController.php index 12562df3d..0e5ffb211 100644 --- a/app/src/Controller/DashboardsController.php +++ b/app/src/Controller/DashboardsController.php @@ -311,14 +311,9 @@ public function search() { // Gather our search string. $q = ''; - if(!empty($this->request->getData('global-search-q'))) { - // A search was passed in from the global search-bar. - $q = trim($this->request->getData('global-search-q')); - // Now pass the search string to the in-page search form and empty the global search bar. - $this->setRequest($this->getRequest()->withData('global-search-q', '')->withData('q', $q)); - } elseif(!empty($this->request->getData('q'))) { - // A search was passed in from the form on the Global Search page. - $q = trim($this->request->getData('q')); + if(!empty($this->request->getQuery('q'))) { + // A search was passed in from the form on the Global Search bar. + $q = trim($this->request->getQuery('q')); } // Only process the request if we have a string of non-space characters @@ -403,7 +398,7 @@ public function search() { $this->Flash->information(__d('result', 'search.exact', - [filter_var($this->request->getData('q'), FILTER_SANITIZE_SPECIAL_CHARS), + [filter_var($this->request->getQuery('q'), FILTER_SANITIZE_SPECIAL_CHARS), __d('controller', $matchClass, [1])])); // Redirect to the matchClass controller diff --git a/app/templates/element/javascript.php b/app/templates/element/javascript.php index a7c4185a7..b35bbd582 100644 --- a/app/templates/element/javascript.php +++ b/app/templates/element/javascript.php @@ -73,7 +73,7 @@ $('#global-search-clear').click(function(e) { e.stopPropagation(); $('#q').val(''); - $('#q').removeClass('hasValue'); + $('#q').removeClass('has-value'); $('#q').focus(); }); // Select search text on focus @@ -83,9 +83,9 @@ // Hide and reveal clear button $('#q').on('input', function(e) { if($(this).val() != '') { - $(this).addClass('hasValue'); + $(this).addClass('has-value'); } else { - $(this).removeClass('hasValue'); + $(this).removeClass('has-value'); } }); diff --git a/app/templates/element/searchGlobal.php b/app/templates/element/searchGlobal.php index faf8a3d17..3282f4e36 100644 --- a/app/templates/element/searchGlobal.php +++ b/app/templates/element/searchGlobal.php @@ -26,7 +26,7 @@ */ $options = [ - 'type' => 'post', + 'type' => 'get', 'url' => [ 'plugin' => null, 'controller' => 'dashboards', @@ -51,16 +51,17 @@ __d('field','search.global'), ['class' => 'visually-hidden'] ); - $globalSearchInputClass = ''; - if(!empty($this->request->getData('q'))) { - $globalSearchInputClass = 'hasValue'; + $globalSearchInputClass = 'global-search-query'; + if(!empty($this->request->getQuery('q'))) { + $globalSearchInputClass .= ' has-value'; } print $this->Form->input( 'q', [ 'id' => 'q', 'class' => $globalSearchInputClass, - 'placeholder' => __d('field','search.placeholder') + 'placeholder' => __d('field','search.placeholder'), + 'value' => $this->request->getQuery('q') ] ); print $this->Form->button( diff --git a/app/webroot/css/co-base.css b/app/webroot/css/co-base.css index d4ba15af4..f43c6aeb4 100644 --- a/app/webroot/css/co-base.css +++ b/app/webroot/css/co-base.css @@ -206,7 +206,7 @@ body.cos.select #top-bar { color: var(--cmg-color-txt-inverse); border-right: 1px dotted var(--cmg-color-highlight-005); } -#q.hasValue + #global-search-clear { +#q.has-value + #global-search-clear { display: inline-block; } #top-bar #global-search-toggle {