chg: [instance:search_all] Support of limit and per-model-searches

pull/72/head
mokaddem 2021-09-13 15:56:51 +02:00
parent 0c2c402aa2
commit 8032d0fad8
5 changed files with 68 additions and 8 deletions

View File

@ -30,12 +30,17 @@ class InstanceController extends AppController
return $this->RestResponse->viewData($data, 'json'); return $this->RestResponse->viewData($data, 'json');
} }
public function searchAll($limit = 5) public function searchAll()
{ {
$searchValue = $this->request->getQuery('search'); $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 = []; $data = [];
if (!empty($searchValue)) { if (!empty($searchValue)) {
$data = $this->Instance->searchAll($searchValue, $limit); $data = $this->Instance->searchAll($searchValue, $limit, $model);
} }
if ($this->ParamHandler->isRest()) { if ($this->ParamHandler->isRest()) {
return $this->RestResponse->viewData($data, 'json'); return $this->RestResponse->viewData($data, 'json');

View File

@ -24,10 +24,18 @@ class InstanceTable extends AppTable
return $validator; return $validator;
} }
public function searchAll($value, $limit=5) public function searchAll($value, $limit=5, $model=null)
{ {
$results = []; $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); $controller = $this->getController($tableName);
$table = TableRegistry::get($tableName); $table = TableRegistry::get($tableName);
$query = $table->find(); $query = $table->find();

View File

@ -12,7 +12,8 @@
<span class="d-flex align-items-center search-container-divider"> <span class="d-flex align-items-center search-container-divider">
<hr class="m-0"/> <hr class="m-0"/>
</span> </span>
</span>', h($tableName)); <span class="font-weight-light text-muted ml-3 model-text">%s</span>
</span>', h($tableName), $tableResult['amount']);
foreach ($tableResult['entries'] as $entry) { foreach ($tableResult['entries'] as $entry) {
$section .= sprintf('<a class="dropdown-item" href="%s">%s</a>', $section .= sprintf('<a class="dropdown-item" href="%s">%s</a>',
@ -26,7 +27,17 @@
} }
$remaining = $tableResult['amount'] - count($tableResult['entries']); $remaining = $tableResult['amount'] - count($tableResult['entries']);
if ($remaining > 0) { if ($remaining > 0) {
$section .= sprintf('<span class="total-found d-block pr-2"><strong class="total-found-number text-primary">%s</strong><span class="total-found-text d-inline ml-1" href="#">%s</span></span>', $section .= sprintf('<a href="%s" class="dropdown-item total-found d-block pr-2">%s <strong class="total-found-number text-primary">%s</strong><span class="total-found-text d-inline ml-1" href="#">%s</span></a>',
Cake\Routing\Router::URL([
'controller' => 'instance',
'action' => 'search_all',
'?' => [
'model' => h($tableName),
'search' => h($this->request->getParam('?')['search'] ?? ''),
'show_all' => 1
]
]),
__('Load'),
$remaining, $remaining,
__('more results') __('more results')
); );
@ -34,6 +45,22 @@
$sections[] = $section; $sections[] = $section;
} }
if (!empty($ajax)) {
$sections[] = sprintf('<a class="dropdown-item border-top text-center text-muted p-2" href="%s"><i class="%s mr-2"></i>%s</a>',
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('<h2 class="font-weight-light mb-4">%s <span class="text-monospace">%s</span></h2>', __('Global search results for:'), h($this->request->getParam('?')['search'] ?? ''));
}
if (!empty($sections)) { if (!empty($sections)) {
echo implode('', $sections); echo implode('', $sections);
} else { } else {

View File

@ -149,6 +149,10 @@ main.content {
margin: auto 0; margin: auto 0;
} }
.global-search-result-container {
min-width: 280px;
}
.global-search-result-container .total-found { .global-search-result-container .total-found {
font-size: 0.75rem; font-size: 0.75rem;
padding-top: 0rem; padding-top: 0rem;

View File

@ -103,6 +103,14 @@ function performGlobalSearch(evt) {
const $input = $('#globalSearch') const $input = $('#globalSearch')
const $resultContainer = $('.global-search-result-container') const $resultContainer = $('.global-search-result-container')
const value = $input.val() 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) { if (value.length < 3 && evt.keyCode != 13) {
$('#dropdownMenuSearchAll').dropdown('hide') $('#dropdownMenuSearchAll').dropdown('hide')
return; return;
@ -117,9 +125,15 @@ function performGlobalSearch(evt) {
$('#dropdownMenuSearchAll').dropdown('show') $('#dropdownMenuSearchAll').dropdown('show')
AJAXApi.quickFetchURL(url, options).then((theHTML) => { AJAXApi.quickFetchURL(url, options).then((theHTML) => {
$resultContainer.html(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 var UI
@ -129,5 +143,7 @@ $(document).ready(() => {
} }
const debouncedGlobalSearch = debounce(performGlobalSearch, 400) const debouncedGlobalSearch = debounce(performGlobalSearch, 400)
$('#globalSearch').keydown(debouncedGlobalSearch); $('#globalSearch')
.keydown(debouncedGlobalSearch)
.keydown(focusSearchResults);
}) })