chg: [instance:search_all] Support of limit and per-model-searches
parent
0c2c402aa2
commit
8032d0fad8
|
@ -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');
|
||||
|
|
|
@ -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();
|
||||
|
|
|
@ -12,7 +12,8 @@
|
|||
<span class="d-flex align-items-center search-container-divider">
|
||||
<hr class="m-0"/>
|
||||
</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) {
|
||||
$section .= sprintf('<a class="dropdown-item" href="%s">%s</a>',
|
||||
|
@ -26,7 +27,17 @@
|
|||
}
|
||||
$remaining = $tableResult['amount'] - count($tableResult['entries']);
|
||||
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,
|
||||
__('more results')
|
||||
);
|
||||
|
@ -34,6 +45,22 @@
|
|||
$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)) {
|
||||
echo implode('', $sections);
|
||||
} else {
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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);
|
||||
})
|
||||
|
|
Loading…
Reference in New Issue