From 8032d0fad866c02df716e4bc9981f4680a23e8c3 Mon Sep 17 00:00:00 2001 From: mokaddem Date: Mon, 13 Sep 2021 15:56:51 +0200 Subject: [PATCH] chg: [instance:search_all] Support of limit and per-model-searches --- src/Controller/InstanceController.php | 9 ++++++-- src/Model/Table/InstanceTable.php | 12 +++++++++-- templates/Instance/search_all.php | 31 +++++++++++++++++++++++++-- webroot/css/layout.css | 4 ++++ webroot/js/main.js | 20 +++++++++++++++-- 5 files changed, 68 insertions(+), 8 deletions(-) diff --git a/src/Controller/InstanceController.php b/src/Controller/InstanceController.php index c84ee1c..482c59d 100644 --- a/src/Controller/InstanceController.php +++ b/src/Controller/InstanceController.php @@ -30,12 +30,17 @@ class InstanceController extends AppController return $this->RestResponse->viewData($data, 'json'); } - public function searchAll($limit = 5) + public function searchAll() { $searchValue = $this->request->getQuery('search'); + $model = $this->request->getQuery('model', null); + $limit = $this->request->getQuery('limit', 5); + if (!empty($this->request->getQuery('show_all', false))) { + $limit = null; + } $data = []; if (!empty($searchValue)) { - $data = $this->Instance->searchAll($searchValue, $limit); + $data = $this->Instance->searchAll($searchValue, $limit, $model); } if ($this->ParamHandler->isRest()) { return $this->RestResponse->viewData($data, 'json'); diff --git a/src/Model/Table/InstanceTable.php b/src/Model/Table/InstanceTable.php index 8c23731..11e894b 100644 --- a/src/Model/Table/InstanceTable.php +++ b/src/Model/Table/InstanceTable.php @@ -24,10 +24,18 @@ class InstanceTable extends AppTable return $validator; } - public function searchAll($value, $limit=5) + public function searchAll($value, $limit=5, $model=null) { $results = []; - foreach ($this->seachAllTables as $tableName) { + $models = $this->seachAllTables; + if (!is_null($model)) { + if (in_array($model, $this->seachAllTables)) { + $models = [$model]; + } else { + return $results; // Cannot search in this model + } + } + foreach ($models as $tableName) { $controller = $this->getController($tableName); $table = TableRegistry::get($tableName); $query = $table->find(); diff --git a/templates/Instance/search_all.php b/templates/Instance/search_all.php index d49a3e3..fb1973b 100644 --- a/templates/Instance/search_all.php +++ b/templates/Instance/search_all.php @@ -12,7 +12,8 @@
- ', h($tableName)); + %s + ', h($tableName), $tableResult['amount']); foreach ($tableResult['entries'] as $entry) { $section .= sprintf('%s', @@ -26,7 +27,17 @@ } $remaining = $tableResult['amount'] - count($tableResult['entries']); if ($remaining > 0) { - $section .= sprintf('%s%s', + $section .= sprintf('%s %s%s', + Cake\Routing\Router::URL([ + 'controller' => 'instance', + 'action' => 'search_all', + '?' => [ + 'model' => h($tableName), + 'search' => h($this->request->getParam('?')['search'] ?? ''), + 'show_all' => 1 + ] + ]), + __('Load'), $remaining, __('more results') ); @@ -34,6 +45,22 @@ $sections[] = $section; } + if (!empty($ajax)) { + $sections[] = sprintf('%s', + Cake\Routing\Router::URL([ + 'controller' => 'instance', + 'action' => 'search_all', + '?' => [ + 'search' => h($this->request->getParam('?')['search'] ?? '') + ] + ]), + $this->FontAwesome->getClass('search-plus'), + __('View all results') + ); + } else { + echo sprintf('

%s %s

', __('Global search results for:'), h($this->request->getParam('?')['search'] ?? '')); + } + if (!empty($sections)) { echo implode('', $sections); } else { diff --git a/webroot/css/layout.css b/webroot/css/layout.css index 1245fca..590663c 100644 --- a/webroot/css/layout.css +++ b/webroot/css/layout.css @@ -149,6 +149,10 @@ main.content { margin: auto 0; } +.global-search-result-container { + min-width: 280px; +} + .global-search-result-container .total-found { font-size: 0.75rem; padding-top: 0rem; diff --git a/webroot/js/main.js b/webroot/js/main.js index 809adcc..b7ce49f 100644 --- a/webroot/js/main.js +++ b/webroot/js/main.js @@ -103,6 +103,14 @@ function performGlobalSearch(evt) { const $input = $('#globalSearch') const $resultContainer = $('.global-search-result-container') const value = $input.val() + const leftKey = 37, + upKey = 38, + rightKey = 39, + downKey = 40, + ingoredKeys = [leftKey, upKey, rightKey, downKey] + if (ingoredKeys.indexOf(evt.keyCode) != -1) { + return; + } if (value.length < 3 && evt.keyCode != 13) { $('#dropdownMenuSearchAll').dropdown('hide') return; @@ -117,9 +125,15 @@ function performGlobalSearch(evt) { $('#dropdownMenuSearchAll').dropdown('show') AJAXApi.quickFetchURL(url, options).then((theHTML) => { $resultContainer.html(theHTML) - $input.focus() }) +} +function focusSearchResults(evt) { + const upKey = 38, + downKey = 40 + if ([upKey, downKey].indexOf(evt.keyCode) != -1) { + $('.global-search-result-container').find('.dropdown-item').first().focus() + } } var UI @@ -129,5 +143,7 @@ $(document).ready(() => { } const debouncedGlobalSearch = debounce(performGlobalSearch, 400) - $('#globalSearch').keydown(debouncedGlobalSearch); + $('#globalSearch') + .keydown(debouncedGlobalSearch) + .keydown(focusSearchResults); })