2021-09-06 21:02:14 -04:00
|
|
|
<script>
|
2021-09-09 23:10:59 -04:00
|
|
|
import { GlSearchBoxByType, GlOutsideDirective as Outside } from '@gitlab/ui';
|
2021-09-15 14:11:29 -04:00
|
|
|
import { mapState, mapActions, mapGetters } from 'vuex';
|
2021-12-13 16:14:32 -05:00
|
|
|
import { debounce } from 'lodash';
|
2021-09-15 14:11:29 -04:00
|
|
|
import { visitUrl } from '~/lib/utils/url_utility';
|
2021-12-13 16:14:32 -05:00
|
|
|
import { DEFAULT_DEBOUNCE_AND_THROTTLE_MS } from '~/lib/utils/constants';
|
2021-12-02 19:11:20 -05:00
|
|
|
import { s__, sprintf } from '~/locale';
|
2021-11-22 16:10:35 -05:00
|
|
|
import DropdownKeyboardNavigation from '~/vue_shared/components/dropdown_keyboard_navigation.vue';
|
2021-12-02 19:11:20 -05:00
|
|
|
import {
|
|
|
|
FIRST_DROPDOWN_INDEX,
|
|
|
|
SEARCH_BOX_INDEX,
|
|
|
|
SEARCH_INPUT_DESCRIPTION,
|
|
|
|
SEARCH_RESULTS_DESCRIPTION,
|
|
|
|
} from '../constants';
|
2021-09-21 14:11:18 -04:00
|
|
|
import HeaderSearchAutocompleteItems from './header_search_autocomplete_items.vue';
|
2021-09-09 23:10:59 -04:00
|
|
|
import HeaderSearchDefaultItems from './header_search_default_items.vue';
|
2021-09-15 14:11:29 -04:00
|
|
|
import HeaderSearchScopedItems from './header_search_scoped_items.vue';
|
2021-09-06 21:02:14 -04:00
|
|
|
|
|
|
|
export default {
|
|
|
|
name: 'HeaderSearchApp',
|
|
|
|
i18n: {
|
2022-01-05 13:13:30 -05:00
|
|
|
searchGitlab: s__('GlobalSearch|Search GitLab'),
|
2021-12-02 19:11:20 -05:00
|
|
|
searchInputDescribeByNoDropdown: s__(
|
|
|
|
'GlobalSearch|Type and press the enter key to submit search.',
|
|
|
|
),
|
|
|
|
searchInputDescribeByWithDropdown: s__(
|
|
|
|
'GlobalSearch|Type for new suggestions to appear below.',
|
|
|
|
),
|
|
|
|
searchDescribedByDefault: s__(
|
|
|
|
'GlobalSearch|%{count} default results provided. Use the up and down arrow keys to navigate search results list.',
|
|
|
|
),
|
|
|
|
searchDescribedByUpdated: s__(
|
|
|
|
'GlobalSearch|Results updated. %{count} results available. Use the up and down arrow keys to navigate search results list, or ENTER to submit.',
|
|
|
|
),
|
|
|
|
searchResultsLoading: s__('GlobalSearch|Search results are loading'),
|
2021-09-06 21:02:14 -04:00
|
|
|
},
|
2021-09-09 23:10:59 -04:00
|
|
|
directives: { Outside },
|
2021-09-06 21:02:14 -04:00
|
|
|
components: {
|
|
|
|
GlSearchBoxByType,
|
2021-09-09 23:10:59 -04:00
|
|
|
HeaderSearchDefaultItems,
|
2021-09-15 14:11:29 -04:00
|
|
|
HeaderSearchScopedItems,
|
2021-09-21 14:11:18 -04:00
|
|
|
HeaderSearchAutocompleteItems,
|
2021-11-22 16:10:35 -05:00
|
|
|
DropdownKeyboardNavigation,
|
2021-09-09 23:10:59 -04:00
|
|
|
},
|
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
showDropdown: false,
|
2021-11-22 16:10:35 -05:00
|
|
|
currentFocusIndex: SEARCH_BOX_INDEX,
|
2021-09-09 23:10:59 -04:00
|
|
|
};
|
|
|
|
},
|
|
|
|
computed: {
|
2021-12-02 19:11:20 -05:00
|
|
|
...mapState(['search', 'loading']),
|
2021-11-22 16:10:35 -05:00
|
|
|
...mapGetters(['searchQuery', 'searchOptions']),
|
2021-09-15 14:11:29 -04:00
|
|
|
searchText: {
|
|
|
|
get() {
|
|
|
|
return this.search;
|
|
|
|
},
|
|
|
|
set(value) {
|
|
|
|
this.setSearch(value);
|
|
|
|
},
|
|
|
|
},
|
2021-11-22 16:10:35 -05:00
|
|
|
currentFocusedOption() {
|
|
|
|
return this.searchOptions[this.currentFocusIndex];
|
|
|
|
},
|
2021-12-02 19:11:20 -05:00
|
|
|
currentFocusedId() {
|
|
|
|
return this.currentFocusedOption?.html_id;
|
|
|
|
},
|
2021-11-22 16:10:35 -05:00
|
|
|
isLoggedIn() {
|
|
|
|
return gon?.current_username;
|
|
|
|
},
|
2021-09-09 23:10:59 -04:00
|
|
|
showSearchDropdown() {
|
2021-11-22 16:10:35 -05:00
|
|
|
return this.showDropdown && this.isLoggedIn;
|
2021-09-09 23:10:59 -04:00
|
|
|
},
|
2021-09-15 14:11:29 -04:00
|
|
|
showDefaultItems() {
|
|
|
|
return !this.searchText;
|
|
|
|
},
|
2021-11-22 16:10:35 -05:00
|
|
|
defaultIndex() {
|
|
|
|
if (this.showDefaultItems) {
|
|
|
|
return SEARCH_BOX_INDEX;
|
|
|
|
}
|
|
|
|
|
|
|
|
return FIRST_DROPDOWN_INDEX;
|
|
|
|
},
|
2021-12-02 19:11:20 -05:00
|
|
|
searchInputDescribeBy() {
|
|
|
|
if (this.isLoggedIn) {
|
|
|
|
return this.$options.i18n.searchInputDescribeByWithDropdown;
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.$options.i18n.searchInputDescribeByNoDropdown;
|
|
|
|
},
|
|
|
|
dropdownResultsDescription() {
|
|
|
|
if (!this.showSearchDropdown) {
|
|
|
|
return ''; // This allows aria-live to see register an update when the dropdown is shown
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.showDefaultItems) {
|
|
|
|
return sprintf(this.$options.i18n.searchDescribedByDefault, {
|
|
|
|
count: this.searchOptions.length,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.loading
|
|
|
|
? this.$options.i18n.searchResultsLoading
|
|
|
|
: sprintf(this.$options.i18n.searchDescribedByUpdated, {
|
|
|
|
count: this.searchOptions.length,
|
|
|
|
});
|
|
|
|
},
|
2021-09-09 23:10:59 -04:00
|
|
|
},
|
|
|
|
methods: {
|
2021-12-13 16:14:32 -05:00
|
|
|
...mapActions(['setSearch', 'fetchAutocompleteOptions', 'clearAutocomplete']),
|
2021-09-09 23:10:59 -04:00
|
|
|
openDropdown() {
|
|
|
|
this.showDropdown = true;
|
|
|
|
},
|
|
|
|
closeDropdown() {
|
|
|
|
this.showDropdown = false;
|
|
|
|
},
|
2021-09-15 14:11:29 -04:00
|
|
|
submitSearch() {
|
2021-11-22 16:10:35 -05:00
|
|
|
return visitUrl(this.currentFocusedOption?.url || this.searchQuery);
|
2021-09-15 14:11:29 -04:00
|
|
|
},
|
2021-12-13 16:14:32 -05:00
|
|
|
getAutocompleteOptions: debounce(function debouncedSearch(searchTerm) {
|
2021-09-21 14:11:18 -04:00
|
|
|
if (!searchTerm) {
|
2021-12-13 16:14:32 -05:00
|
|
|
this.clearAutocomplete();
|
|
|
|
} else {
|
|
|
|
this.fetchAutocompleteOptions();
|
2021-09-21 14:11:18 -04:00
|
|
|
}
|
2021-12-13 16:14:32 -05:00
|
|
|
}, DEFAULT_DEBOUNCE_AND_THROTTLE_MS),
|
2021-09-06 21:02:14 -04:00
|
|
|
},
|
2021-11-22 16:10:35 -05:00
|
|
|
SEARCH_BOX_INDEX,
|
2021-12-02 19:11:20 -05:00
|
|
|
SEARCH_INPUT_DESCRIPTION,
|
|
|
|
SEARCH_RESULTS_DESCRIPTION,
|
2021-09-06 21:02:14 -04:00
|
|
|
};
|
|
|
|
</script>
|
|
|
|
|
|
|
|
<template>
|
2021-12-02 19:11:20 -05:00
|
|
|
<form
|
|
|
|
v-outside="closeDropdown"
|
|
|
|
role="search"
|
2022-01-05 13:13:30 -05:00
|
|
|
:aria-label="$options.i18n.searchGitlab"
|
2021-12-02 19:11:20 -05:00
|
|
|
class="header-search gl-relative"
|
|
|
|
>
|
2021-09-09 23:10:59 -04:00
|
|
|
<gl-search-box-by-type
|
2021-12-13 19:14:57 -05:00
|
|
|
id="search"
|
2021-09-15 14:11:29 -04:00
|
|
|
v-model="searchText"
|
2021-12-02 19:11:20 -05:00
|
|
|
role="searchbox"
|
2021-11-22 16:10:35 -05:00
|
|
|
class="gl-z-index-1"
|
2021-09-09 23:10:59 -04:00
|
|
|
autocomplete="off"
|
2022-01-05 13:13:30 -05:00
|
|
|
:placeholder="$options.i18n.searchGitlab"
|
2021-12-02 19:11:20 -05:00
|
|
|
:aria-activedescendant="currentFocusedId"
|
|
|
|
:aria-describedby="$options.SEARCH_INPUT_DESCRIPTION"
|
2021-09-09 23:10:59 -04:00
|
|
|
@focus="openDropdown"
|
|
|
|
@click="openDropdown"
|
2021-09-21 14:11:18 -04:00
|
|
|
@input="getAutocompleteOptions"
|
2021-11-22 16:10:35 -05:00
|
|
|
@keydown.enter.stop.prevent="submitSearch"
|
2021-09-09 23:10:59 -04:00
|
|
|
/>
|
2021-12-02 19:11:20 -05:00
|
|
|
<span :id="$options.SEARCH_INPUT_DESCRIPTION" role="region" class="gl-sr-only">{{
|
|
|
|
searchInputDescribeBy
|
|
|
|
}}</span>
|
|
|
|
<span
|
|
|
|
role="region"
|
|
|
|
:data-testid="$options.SEARCH_RESULTS_DESCRIPTION"
|
|
|
|
class="gl-sr-only"
|
|
|
|
aria-live="polite"
|
|
|
|
aria-atomic="true"
|
|
|
|
>
|
|
|
|
{{ dropdownResultsDescription }}
|
|
|
|
</span>
|
2021-09-09 23:10:59 -04:00
|
|
|
<div
|
|
|
|
v-if="showSearchDropdown"
|
|
|
|
data-testid="header-search-dropdown-menu"
|
2021-09-21 14:11:18 -04:00
|
|
|
class="header-search-dropdown-menu gl-absolute gl-w-full gl-bg-white gl-border-1 gl-rounded-base gl-border-solid gl-border-gray-200 gl-shadow-x0-y2-b4-s0"
|
2021-09-09 23:10:59 -04:00
|
|
|
>
|
|
|
|
<div class="header-search-dropdown-content gl-overflow-y-auto gl-py-2">
|
2021-11-22 16:10:35 -05:00
|
|
|
<dropdown-keyboard-navigation
|
|
|
|
v-model="currentFocusIndex"
|
|
|
|
:max="searchOptions.length - 1"
|
|
|
|
:min="$options.SEARCH_BOX_INDEX"
|
|
|
|
:default-index="defaultIndex"
|
|
|
|
@tab="closeDropdown"
|
|
|
|
/>
|
|
|
|
<header-search-default-items
|
|
|
|
v-if="showDefaultItems"
|
|
|
|
:current-focused-option="currentFocusedOption"
|
|
|
|
/>
|
2021-09-15 14:11:29 -04:00
|
|
|
<template v-else>
|
2021-11-22 16:10:35 -05:00
|
|
|
<header-search-scoped-items :current-focused-option="currentFocusedOption" />
|
|
|
|
<header-search-autocomplete-items :current-focused-option="currentFocusedOption" />
|
2021-09-15 14:11:29 -04:00
|
|
|
</template>
|
2021-09-09 23:10:59 -04:00
|
|
|
</div>
|
|
|
|
</div>
|
2021-12-02 19:11:20 -05:00
|
|
|
</form>
|
2021-09-06 21:02:14 -04:00
|
|
|
</template>
|