2019-11-18 10:06:27 -05:00
|
|
|
/* eslint-disable no-return-assign, consistent-return, class-methods-use-this */
|
2018-03-09 15:18:59 -05:00
|
|
|
|
|
|
|
import $ from 'jquery';
|
2020-07-10 05:09:01 -04:00
|
|
|
import { escape, throttle } from 'lodash';
|
2021-02-14 13:09:20 -05:00
|
|
|
import initDeprecatedJQueryDropdown from '~/deprecated_jquery_dropdown';
|
2020-07-10 05:09:01 -04:00
|
|
|
import { getIdenticonBackgroundClass, getIdenticonTitle } from '~/helpers/avatar_helper';
|
2021-07-01 08:08:37 -04:00
|
|
|
import { DEFAULT_DEBOUNCE_AND_THROTTLE_MS } from '~/lib/utils/constants';
|
2021-02-14 13:09:20 -05:00
|
|
|
import { s__, __, sprintf } from '~/locale';
|
2021-02-01 10:08:56 -05:00
|
|
|
import Tracking from '~/tracking';
|
2020-07-10 05:09:01 -04:00
|
|
|
import axios from './lib/utils/axios_utils';
|
2021-07-09 11:09:47 -04:00
|
|
|
import { spriteIcon } from './lib/utils/common_utils';
|
2018-08-07 05:59:01 -04:00
|
|
|
import {
|
|
|
|
isInGroupsPage,
|
|
|
|
isInProjectPage,
|
|
|
|
getGroupSlug,
|
|
|
|
getProjectSlug,
|
2021-07-09 11:09:47 -04:00
|
|
|
} from './search_autocomplete_utils';
|
2016-12-13 22:01:05 -05:00
|
|
|
|
2017-12-06 11:41:24 -05:00
|
|
|
/**
|
|
|
|
* Search input in top navigation bar.
|
|
|
|
* On click, opens a dropdown
|
|
|
|
* As the user types it filters the results
|
|
|
|
* When the user clicks `x` button it cleans the input and closes the dropdown.
|
|
|
|
*/
|
|
|
|
|
2017-12-13 04:26:44 -05:00
|
|
|
const KEYCODE = {
|
|
|
|
ESCAPE: 27,
|
|
|
|
BACKSPACE: 8,
|
|
|
|
ENTER: 13,
|
|
|
|
UP: 38,
|
|
|
|
DOWN: 40,
|
|
|
|
};
|
|
|
|
|
|
|
|
function setSearchOptions() {
|
2019-11-18 10:06:27 -05:00
|
|
|
const $projectOptionsDataEl = $('.js-search-project-options');
|
|
|
|
const $groupOptionsDataEl = $('.js-search-group-options');
|
|
|
|
const $dashboardOptionsDataEl = $('.js-search-dashboard-options');
|
2017-12-13 04:26:44 -05:00
|
|
|
|
|
|
|
if ($projectOptionsDataEl.length) {
|
|
|
|
gl.projectOptions = gl.projectOptions || {};
|
|
|
|
|
2019-11-18 10:06:27 -05:00
|
|
|
const projectPath = $projectOptionsDataEl.data('projectPath');
|
2017-12-13 04:26:44 -05:00
|
|
|
|
|
|
|
gl.projectOptions[projectPath] = {
|
|
|
|
name: $projectOptionsDataEl.data('name'),
|
2018-02-20 17:20:48 -05:00
|
|
|
issuesPath: $projectOptionsDataEl.data('issuesPath'),
|
|
|
|
issuesDisabled: $projectOptionsDataEl.data('issuesDisabled'),
|
|
|
|
mrPath: $projectOptionsDataEl.data('mrPath'),
|
2017-12-13 04:26:44 -05:00
|
|
|
};
|
|
|
|
}
|
2017-12-06 11:41:24 -05:00
|
|
|
|
2017-12-13 04:26:44 -05:00
|
|
|
if ($groupOptionsDataEl.length) {
|
|
|
|
gl.groupOptions = gl.groupOptions || {};
|
2016-07-24 16:45:11 -04:00
|
|
|
|
2019-11-18 10:06:27 -05:00
|
|
|
const groupPath = $groupOptionsDataEl.data('groupPath');
|
2016-07-24 16:45:11 -04:00
|
|
|
|
2017-12-13 04:26:44 -05:00
|
|
|
gl.groupOptions[groupPath] = {
|
|
|
|
name: $groupOptionsDataEl.data('name'),
|
2018-02-20 17:20:48 -05:00
|
|
|
issuesPath: $groupOptionsDataEl.data('issuesPath'),
|
|
|
|
mrPath: $groupOptionsDataEl.data('mrPath'),
|
2017-12-13 04:26:44 -05:00
|
|
|
};
|
|
|
|
}
|
2016-07-24 16:45:11 -04:00
|
|
|
|
2017-12-13 04:26:44 -05:00
|
|
|
if ($dashboardOptionsDataEl.length) {
|
|
|
|
gl.dashboardOptions = {
|
2018-08-07 05:59:01 -04:00
|
|
|
name: s__('SearchAutocomplete|All GitLab'),
|
2018-02-20 17:20:48 -05:00
|
|
|
issuesPath: $dashboardOptionsDataEl.data('issuesPath'),
|
|
|
|
mrPath: $dashboardOptionsDataEl.data('mrPath'),
|
2017-12-13 04:26:44 -05:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-10 05:09:01 -04:00
|
|
|
export class SearchAutocomplete {
|
|
|
|
constructor({ wrap, optsEl, autocompletePath, projectId, projectRef } = {}) {
|
2017-12-13 04:26:44 -05:00
|
|
|
setSearchOptions();
|
|
|
|
this.bindEventContext();
|
|
|
|
this.wrap = wrap || $('.search');
|
2020-07-10 05:09:01 -04:00
|
|
|
this.optsEl = optsEl || this.wrap.find('.search-autocomplete-opts');
|
|
|
|
this.autocompletePath = autocompletePath || this.optsEl.data('autocompletePath');
|
2020-12-23 07:10:26 -05:00
|
|
|
this.projectId = projectId || this.optsEl.data('autocompleteProjectId') || '';
|
|
|
|
this.projectRef = projectRef || this.optsEl.data('autocompleteProjectRef') || '';
|
2017-12-13 04:26:44 -05:00
|
|
|
this.dropdown = this.wrap.find('.dropdown');
|
|
|
|
this.dropdownToggle = this.wrap.find('.js-dropdown-search-toggle');
|
2018-08-07 05:59:01 -04:00
|
|
|
this.dropdownMenu = this.dropdown.find('.dropdown-menu');
|
2017-12-13 04:26:44 -05:00
|
|
|
this.dropdownContent = this.dropdown.find('.dropdown-content');
|
|
|
|
this.scopeInputEl = this.getElement('#scope');
|
|
|
|
this.searchInput = this.getElement('.search-input');
|
|
|
|
this.projectInputEl = this.getElement('#search_project_id');
|
|
|
|
this.groupInputEl = this.getElement('#group_id');
|
|
|
|
this.searchCodeInputEl = this.getElement('#search_code');
|
|
|
|
this.repositoryInputEl = this.getElement('#repository_ref');
|
|
|
|
this.clearInput = this.getElement('.js-clear-input');
|
2018-08-07 05:59:01 -04:00
|
|
|
this.scrollFadeInitialized = false;
|
2017-12-13 04:26:44 -05:00
|
|
|
this.saveOriginalState();
|
|
|
|
|
|
|
|
// Only when user is logged in
|
|
|
|
if (gon.current_user_id) {
|
2020-07-10 05:09:01 -04:00
|
|
|
this.createAutocomplete();
|
2016-09-08 09:59:33 -04:00
|
|
|
}
|
2016-07-24 16:45:11 -04:00
|
|
|
|
2017-12-13 04:26:44 -05:00
|
|
|
this.bindEvents();
|
|
|
|
this.dropdownToggle.dropdown();
|
2019-10-21 14:06:29 -04:00
|
|
|
this.searchInput.addClass('js-autocomplete-disabled');
|
2017-12-13 04:26:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Finds an element inside wrapper element
|
|
|
|
bindEventContext() {
|
|
|
|
this.onSearchInputBlur = this.onSearchInputBlur.bind(this);
|
|
|
|
this.onClearInputClick = this.onClearInputClick.bind(this);
|
|
|
|
this.onSearchInputFocus = this.onSearchInputFocus.bind(this);
|
|
|
|
this.onSearchInputKeyUp = this.onSearchInputKeyUp.bind(this);
|
2019-10-30 08:06:34 -04:00
|
|
|
this.onSearchInputChange = this.onSearchInputChange.bind(this);
|
2018-08-07 05:59:01 -04:00
|
|
|
this.setScrollFade = this.setScrollFade.bind(this);
|
2017-12-13 04:26:44 -05:00
|
|
|
}
|
|
|
|
getElement(selector) {
|
|
|
|
return this.wrap.find(selector);
|
|
|
|
}
|
|
|
|
|
|
|
|
saveOriginalState() {
|
2018-08-07 05:59:01 -04:00
|
|
|
return (this.originalState = this.serializeState());
|
2017-12-13 04:26:44 -05:00
|
|
|
}
|
|
|
|
|
2020-07-10 05:09:01 -04:00
|
|
|
createAutocomplete() {
|
2020-08-24 14:10:19 -04:00
|
|
|
return initDeprecatedJQueryDropdown(this.searchInput, {
|
2017-12-13 04:26:44 -05:00
|
|
|
filterInputBlur: false,
|
|
|
|
filterable: true,
|
|
|
|
filterRemote: true,
|
|
|
|
highlight: true,
|
2018-08-07 05:59:01 -04:00
|
|
|
icon: true,
|
2017-12-13 04:26:44 -05:00
|
|
|
enterCallback: false,
|
|
|
|
filterInput: 'input#search',
|
|
|
|
search: {
|
|
|
|
fields: ['text'],
|
|
|
|
},
|
|
|
|
id: this.getSearchText,
|
|
|
|
data: this.getData.bind(this),
|
|
|
|
selectable: true,
|
|
|
|
clicked: this.onClick.bind(this),
|
2020-09-03 05:08:20 -04:00
|
|
|
trackSuggestionClickedLabel: 'search_autocomplete_suggestion',
|
2017-12-13 04:26:44 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-09-16 08:06:26 -04:00
|
|
|
getSearchText(selectedObject) {
|
2017-12-13 04:26:44 -05:00
|
|
|
return selectedObject.id ? selectedObject.text : '';
|
|
|
|
}
|
|
|
|
|
|
|
|
getData(term, callback) {
|
|
|
|
if (!term) {
|
|
|
|
const contents = this.getCategoryContents();
|
|
|
|
if (contents) {
|
2020-08-24 14:10:19 -04:00
|
|
|
const deprecatedJQueryDropdownInstance = this.searchInput.data('deprecatedJQueryDropdown');
|
2018-08-02 10:18:17 -04:00
|
|
|
|
2020-08-24 14:10:19 -04:00
|
|
|
if (deprecatedJQueryDropdownInstance) {
|
|
|
|
deprecatedJQueryDropdownInstance.filter.options.callback(contents);
|
2018-08-02 10:18:17 -04:00
|
|
|
}
|
2020-07-10 05:09:01 -04:00
|
|
|
this.enableAutocomplete();
|
2017-12-13 04:26:44 -05:00
|
|
|
}
|
|
|
|
return;
|
2016-09-08 09:59:33 -04:00
|
|
|
}
|
2016-07-24 16:45:11 -04:00
|
|
|
|
2020-07-10 05:09:01 -04:00
|
|
|
// Prevent multiple ajax calls
|
|
|
|
if (this.loadingSuggestions) {
|
|
|
|
return;
|
|
|
|
}
|
2016-07-24 16:45:11 -04:00
|
|
|
|
2020-07-10 05:09:01 -04:00
|
|
|
this.loadingSuggestions = true;
|
|
|
|
|
|
|
|
return axios
|
|
|
|
.get(this.autocompletePath, {
|
|
|
|
params: {
|
|
|
|
project_id: this.projectId,
|
|
|
|
project_ref: this.projectRef,
|
|
|
|
term,
|
|
|
|
},
|
|
|
|
})
|
2020-12-23 19:10:25 -05:00
|
|
|
.then((response) => {
|
2020-07-10 05:09:01 -04:00
|
|
|
const options = this.scopedSearchOptions(term);
|
|
|
|
|
|
|
|
// List results
|
|
|
|
let lastCategory = null;
|
|
|
|
for (let i = 0, len = response.data.length; i < len; i += 1) {
|
|
|
|
const suggestion = response.data[i];
|
|
|
|
// Add group header before list each group
|
|
|
|
if (lastCategory !== suggestion.category) {
|
|
|
|
options.push({ type: 'separator' });
|
|
|
|
options.push({
|
|
|
|
type: 'header',
|
|
|
|
content: suggestion.category,
|
|
|
|
});
|
|
|
|
lastCategory = suggestion.category;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the suggestion
|
|
|
|
options.push({
|
|
|
|
id: `${suggestion.category.toLowerCase()}-${suggestion.id}`,
|
|
|
|
icon: this.getAvatar(suggestion),
|
|
|
|
category: suggestion.category,
|
|
|
|
text: suggestion.label,
|
|
|
|
url: suggestion.url,
|
|
|
|
});
|
|
|
|
}
|
2017-12-13 04:26:44 -05:00
|
|
|
|
2020-07-10 05:09:01 -04:00
|
|
|
callback(options);
|
|
|
|
|
|
|
|
this.loadingSuggestions = false;
|
|
|
|
this.highlightFirstRow();
|
|
|
|
this.setScrollFade();
|
|
|
|
})
|
|
|
|
.catch(() => {
|
|
|
|
this.loadingSuggestions = false;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
getCategoryContents() {
|
|
|
|
const userName = gon.current_username;
|
|
|
|
const { projectOptions, groupOptions, dashboardOptions } = gl;
|
|
|
|
|
|
|
|
// Get options
|
|
|
|
let options;
|
|
|
|
if (isInProjectPage() && projectOptions) {
|
|
|
|
options = projectOptions[getProjectSlug()];
|
|
|
|
} else if (isInGroupsPage() && groupOptions) {
|
|
|
|
options = groupOptions[getGroupSlug()];
|
|
|
|
} else if (dashboardOptions) {
|
|
|
|
options = dashboardOptions;
|
|
|
|
}
|
|
|
|
|
|
|
|
const { issuesPath, mrPath, name, issuesDisabled } = options;
|
|
|
|
const baseItems = [];
|
|
|
|
|
|
|
|
if (name) {
|
|
|
|
baseItems.push({
|
|
|
|
type: 'header',
|
|
|
|
content: `${name}`,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
const issueItems = [
|
|
|
|
{
|
|
|
|
text: s__('SearchAutocomplete|Issues assigned to me'),
|
|
|
|
url: `${issuesPath}/?assignee_username=${userName}`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
text: s__("SearchAutocomplete|Issues I've created"),
|
|
|
|
url: `${issuesPath}/?author_username=${userName}`,
|
|
|
|
},
|
|
|
|
];
|
|
|
|
const mergeRequestItems = [
|
|
|
|
{
|
|
|
|
text: s__('SearchAutocomplete|Merge requests assigned to me'),
|
|
|
|
url: `${mrPath}/?assignee_username=${userName}`,
|
|
|
|
},
|
2020-12-04 13:09:55 -05:00
|
|
|
{
|
|
|
|
text: s__("SearchAutocomplete|Merge requests that I'm a reviewer"),
|
|
|
|
url: `${mrPath}/?reviewer_username=${userName}`,
|
|
|
|
},
|
2020-07-10 05:09:01 -04:00
|
|
|
{
|
|
|
|
text: s__("SearchAutocomplete|Merge requests I've created"),
|
|
|
|
url: `${mrPath}/?author_username=${userName}`,
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
let items;
|
|
|
|
if (issuesDisabled) {
|
|
|
|
items = baseItems.concat(mergeRequestItems);
|
|
|
|
} else {
|
|
|
|
items = baseItems.concat(...issueItems, ...mergeRequestItems);
|
|
|
|
}
|
|
|
|
return items;
|
2017-12-13 04:26:44 -05:00
|
|
|
}
|
2016-07-24 16:45:11 -04:00
|
|
|
|
2020-06-01 20:08:07 -04:00
|
|
|
// Add option to proceed with the search for each
|
|
|
|
// scope that is currently available, namely:
|
|
|
|
//
|
|
|
|
// - Search in this project
|
|
|
|
// - Search in this group (or project's group)
|
|
|
|
// - Search in all GitLab
|
|
|
|
scopedSearchOptions(term) {
|
|
|
|
const icon = spriteIcon('search', 's16 inline-search-icon');
|
|
|
|
const projectId = this.projectInputEl.val();
|
|
|
|
const groupId = this.groupInputEl.val();
|
|
|
|
const options = [];
|
|
|
|
|
|
|
|
if (projectId) {
|
|
|
|
const projectOptions = gl.projectOptions[getProjectSlug()];
|
|
|
|
const url = groupId
|
2021-08-05 02:09:25 -04:00
|
|
|
? `${gon.relative_url_root}/search?search=${term}&project_id=${projectId}&group_id=${groupId}&nav_source=navbar`
|
|
|
|
: `${gon.relative_url_root}/search?search=${term}&project_id=${projectId}&nav_source=navbar`;
|
2020-06-01 20:08:07 -04:00
|
|
|
|
|
|
|
options.push({
|
|
|
|
icon,
|
|
|
|
text: term,
|
|
|
|
template: sprintf(
|
|
|
|
s__(`SearchAutocomplete|in project %{projectName}`),
|
|
|
|
{
|
|
|
|
projectName: `<i>${projectOptions.name}</i>`,
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
),
|
|
|
|
url,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (groupId) {
|
|
|
|
const groupOptions = gl.groupOptions[getGroupSlug()];
|
|
|
|
options.push({
|
|
|
|
icon,
|
|
|
|
text: term,
|
|
|
|
template: sprintf(
|
|
|
|
s__(`SearchAutocomplete|in group %{groupName}`),
|
|
|
|
{
|
|
|
|
groupName: `<i>${groupOptions.name}</i>`,
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
),
|
2021-08-05 02:09:25 -04:00
|
|
|
url: `${gon.relative_url_root}/search?search=${term}&group_id=${groupId}&nav_source=navbar`,
|
2020-06-01 20:08:07 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
options.push({
|
|
|
|
icon,
|
|
|
|
text: term,
|
|
|
|
template: s__('SearchAutocomplete|in all GitLab'),
|
2021-08-05 02:09:25 -04:00
|
|
|
url: `${gon.relative_url_root}/search?search=${term}&nav_source=navbar`,
|
2020-06-01 20:08:07 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
return options;
|
|
|
|
}
|
|
|
|
|
2017-12-13 04:26:44 -05:00
|
|
|
serializeState() {
|
|
|
|
return {
|
|
|
|
// Search Criteria
|
|
|
|
search_project_id: this.projectInputEl.val(),
|
|
|
|
group_id: this.groupInputEl.val(),
|
|
|
|
search_code: this.searchCodeInputEl.val(),
|
|
|
|
repository_ref: this.repositoryInputEl.val(),
|
|
|
|
scope: this.scopeInputEl.val(),
|
|
|
|
};
|
|
|
|
}
|
2017-12-06 11:41:24 -05:00
|
|
|
|
2017-12-13 04:26:44 -05:00
|
|
|
bindEvents() {
|
2019-10-30 08:06:34 -04:00
|
|
|
this.searchInput.on('input', this.onSearchInputChange);
|
2017-12-13 04:26:44 -05:00
|
|
|
this.searchInput.on('keyup', this.onSearchInputKeyUp);
|
|
|
|
this.searchInput.on('focus', this.onSearchInputFocus);
|
|
|
|
this.searchInput.on('blur', this.onSearchInputBlur);
|
|
|
|
this.clearInput.on('click', this.onClearInputClick);
|
2021-07-01 08:08:37 -04:00
|
|
|
this.dropdownContent.on(
|
|
|
|
'scroll',
|
|
|
|
throttle(this.setScrollFade, DEFAULT_DEBOUNCE_AND_THROTTLE_MS),
|
|
|
|
);
|
2019-10-30 08:06:34 -04:00
|
|
|
|
2020-12-23 19:10:25 -05:00
|
|
|
this.searchInput.on('click', (e) => {
|
2019-10-30 08:06:34 -04:00
|
|
|
e.stopPropagation();
|
|
|
|
});
|
2017-12-13 04:26:44 -05:00
|
|
|
}
|
|
|
|
|
2020-07-10 05:09:01 -04:00
|
|
|
enableAutocomplete() {
|
2018-08-07 05:59:01 -04:00
|
|
|
this.setScrollFade();
|
|
|
|
|
2017-12-13 04:26:44 -05:00
|
|
|
// No need to enable anything if user is not logged in
|
|
|
|
if (!gon.current_user_id) {
|
|
|
|
return;
|
2017-01-10 17:54:56 -05:00
|
|
|
}
|
2016-07-24 16:45:11 -04:00
|
|
|
|
2017-12-13 04:26:44 -05:00
|
|
|
// If the dropdown is closed, we'll open it
|
2018-06-27 03:00:11 -04:00
|
|
|
if (!this.dropdown.hasClass('show')) {
|
2017-12-13 04:26:44 -05:00
|
|
|
this.loadingSuggestions = false;
|
|
|
|
this.dropdownToggle.dropdown('toggle');
|
2020-08-19 20:10:11 -04:00
|
|
|
|
|
|
|
const trackEvent = 'click_search_bar';
|
|
|
|
const trackCategory = undefined; // will be default set in event method
|
|
|
|
|
|
|
|
Tracking.event(trackCategory, trackEvent, {
|
|
|
|
label: 'main_navigation',
|
|
|
|
property: 'navigation',
|
|
|
|
});
|
|
|
|
|
2019-10-21 14:06:29 -04:00
|
|
|
return this.searchInput.removeClass('js-autocomplete-disabled');
|
2016-09-08 09:59:33 -04:00
|
|
|
}
|
2017-12-13 04:26:44 -05:00
|
|
|
}
|
2016-07-24 16:45:11 -04:00
|
|
|
|
2019-10-30 08:06:34 -04:00
|
|
|
onSearchInputChange() {
|
2020-07-10 05:09:01 -04:00
|
|
|
this.enableAutocomplete();
|
2017-12-13 04:26:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
onSearchInputKeyUp(e) {
|
|
|
|
switch (e.keyCode) {
|
|
|
|
case KEYCODE.ESCAPE:
|
|
|
|
this.restoreOriginalState();
|
|
|
|
break;
|
|
|
|
case KEYCODE.ENTER:
|
2020-07-10 05:09:01 -04:00
|
|
|
this.disableAutocomplete();
|
2017-12-13 04:26:44 -05:00
|
|
|
break;
|
|
|
|
default:
|
2016-09-08 09:59:33 -04:00
|
|
|
}
|
2019-06-03 18:51:02 -04:00
|
|
|
this.wrap.toggleClass('has-value', Boolean(e.target.value));
|
2017-12-13 04:26:44 -05:00
|
|
|
}
|
2016-07-24 16:45:11 -04:00
|
|
|
|
2017-12-13 04:26:44 -05:00
|
|
|
onSearchInputFocus() {
|
|
|
|
this.isFocused = true;
|
|
|
|
this.wrap.addClass('search-active');
|
|
|
|
if (this.getValue() === '') {
|
|
|
|
return this.getData();
|
2016-09-08 09:59:33 -04:00
|
|
|
}
|
2017-12-13 04:26:44 -05:00
|
|
|
}
|
2016-07-24 16:45:11 -04:00
|
|
|
|
2017-12-13 04:26:44 -05:00
|
|
|
getValue() {
|
|
|
|
return this.searchInput.val();
|
|
|
|
}
|
2016-07-24 16:45:11 -04:00
|
|
|
|
2017-12-13 04:26:44 -05:00
|
|
|
onClearInputClick(e) {
|
|
|
|
e.preventDefault();
|
2019-06-03 18:51:02 -04:00
|
|
|
this.wrap.toggleClass('has-value', Boolean(e.target.value));
|
2017-12-13 04:26:44 -05:00
|
|
|
return this.searchInput.val('').focus();
|
|
|
|
}
|
2016-07-24 16:45:11 -04:00
|
|
|
|
2019-09-16 08:06:26 -04:00
|
|
|
onSearchInputBlur() {
|
2017-12-13 04:26:44 -05:00
|
|
|
this.isFocused = false;
|
|
|
|
this.wrap.removeClass('search-active');
|
|
|
|
// If input is blank then restore state
|
|
|
|
if (this.searchInput.val() === '') {
|
2019-05-24 15:20:05 -04:00
|
|
|
this.restoreOriginalState();
|
2016-09-08 09:59:33 -04:00
|
|
|
}
|
2019-05-22 15:49:12 -04:00
|
|
|
this.dropdownMenu.removeClass('show');
|
2017-12-13 04:26:44 -05:00
|
|
|
}
|
2016-07-24 16:45:11 -04:00
|
|
|
|
2017-12-13 04:26:44 -05:00
|
|
|
restoreOriginalState() {
|
2019-11-18 10:06:27 -05:00
|
|
|
const inputs = Object.keys(this.originalState);
|
|
|
|
for (let i = 0, len = inputs.length; i < len; i += 1) {
|
|
|
|
const input = inputs[i];
|
2019-09-24 08:06:20 -04:00
|
|
|
this.getElement(`#${input}`).val(this.originalState[input]);
|
2016-09-08 09:59:33 -04:00
|
|
|
}
|
2017-12-13 04:26:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
resetSearchState() {
|
2019-11-18 10:06:27 -05:00
|
|
|
const inputs = Object.keys(this.originalState);
|
|
|
|
const results = [];
|
|
|
|
for (let i = 0, len = inputs.length; i < len; i += 1) {
|
|
|
|
const input = inputs[i];
|
2019-09-24 08:06:20 -04:00
|
|
|
results.push(this.getElement(`#${input}`).val(''));
|
2016-09-08 09:59:33 -04:00
|
|
|
}
|
2017-12-13 04:26:44 -05:00
|
|
|
return results;
|
|
|
|
}
|
2016-07-24 16:45:11 -04:00
|
|
|
|
2020-07-10 05:09:01 -04:00
|
|
|
disableAutocomplete() {
|
2019-10-21 14:06:29 -04:00
|
|
|
if (!this.searchInput.hasClass('js-autocomplete-disabled') && this.dropdown.hasClass('show')) {
|
|
|
|
this.searchInput.addClass('js-autocomplete-disabled');
|
2020-04-22 17:10:00 -04:00
|
|
|
this.dropdownToggle.dropdown('toggle');
|
2017-12-13 04:26:44 -05:00
|
|
|
this.restoreMenu();
|
2016-09-08 09:59:33 -04:00
|
|
|
}
|
2017-12-13 04:26:44 -05:00
|
|
|
}
|
2016-07-24 16:45:11 -04:00
|
|
|
|
2017-12-13 04:26:44 -05:00
|
|
|
restoreMenu() {
|
2019-11-18 10:06:27 -05:00
|
|
|
const html = `<ul><li class="dropdown-menu-empty-item"><a>${__('Loading...')}</a></li></ul>`;
|
2017-12-13 04:26:44 -05:00
|
|
|
return this.dropdownContent.html(html);
|
|
|
|
}
|
2016-07-24 16:45:11 -04:00
|
|
|
|
2017-12-13 04:26:44 -05:00
|
|
|
onClick(item, $el, e) {
|
2018-06-15 11:58:27 -04:00
|
|
|
if (window.location.pathname.indexOf(item.url) !== -1) {
|
2017-12-13 04:26:44 -05:00
|
|
|
if (!e.metaKey) e.preventDefault();
|
2020-07-10 05:09:01 -04:00
|
|
|
/* eslint-disable-next-line @gitlab/require-i18n-strings */
|
|
|
|
if (item.category === 'Projects') {
|
|
|
|
this.projectInputEl.val(item.id);
|
|
|
|
}
|
|
|
|
// eslint-disable-next-line @gitlab/require-i18n-strings
|
|
|
|
if (item.category === 'Groups') {
|
|
|
|
this.groupInputEl.val(item.id);
|
|
|
|
}
|
2017-12-13 04:26:44 -05:00
|
|
|
$el.removeClass('is-active');
|
2020-07-10 05:09:01 -04:00
|
|
|
this.disableAutocomplete();
|
2017-12-13 04:26:44 -05:00
|
|
|
return this.searchInput.val('').focus();
|
2017-01-10 17:54:56 -05:00
|
|
|
}
|
2016-09-08 09:59:33 -04:00
|
|
|
}
|
2018-08-07 05:59:01 -04:00
|
|
|
|
|
|
|
highlightFirstRow() {
|
2020-08-24 14:10:19 -04:00
|
|
|
this.searchInput.data('deprecatedJQueryDropdown').highlightRowAtIndex(null, 0);
|
2018-08-07 05:59:01 -04:00
|
|
|
}
|
|
|
|
|
2020-07-10 05:09:01 -04:00
|
|
|
getAvatar(item) {
|
2022-06-10 23:09:17 -04:00
|
|
|
if (!Object.prototype.hasOwnProperty.call(item, 'avatar_url')) {
|
2020-07-10 05:09:01 -04:00
|
|
|
return false;
|
2018-08-07 05:59:01 -04:00
|
|
|
}
|
|
|
|
|
2020-07-10 05:09:01 -04:00
|
|
|
const { label, id } = item;
|
|
|
|
const avatarUrl = item.avatar_url;
|
|
|
|
const avatar = avatarUrl
|
|
|
|
? `<img class="search-item-avatar" src="${avatarUrl}" />`
|
|
|
|
: `<div class="s16 avatar identicon ${getIdenticonBackgroundClass(id)}">${getIdenticonTitle(
|
|
|
|
escape(label),
|
|
|
|
)}</div>`;
|
2018-08-07 05:59:01 -04:00
|
|
|
|
2020-07-10 05:09:01 -04:00
|
|
|
return avatar;
|
2018-08-07 05:59:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
isScrolledUp() {
|
|
|
|
const el = this.dropdownContent[0];
|
|
|
|
const currentPosition = this.contentClientHeight + el.scrollTop;
|
|
|
|
|
|
|
|
return currentPosition < this.maxPosition;
|
|
|
|
}
|
|
|
|
|
|
|
|
initScrollFade() {
|
|
|
|
const el = this.dropdownContent[0];
|
|
|
|
this.scrollFadeInitialized = true;
|
|
|
|
|
|
|
|
this.contentClientHeight = el.clientHeight;
|
|
|
|
this.maxPosition = el.scrollHeight;
|
|
|
|
this.dropdownMenu.addClass('dropdown-content-faded-mask');
|
|
|
|
}
|
|
|
|
|
|
|
|
setScrollFade() {
|
|
|
|
this.initScrollFade();
|
|
|
|
|
|
|
|
this.dropdownMenu.toggleClass('fade-out', !this.isScrolledUp());
|
|
|
|
}
|
2017-12-13 04:26:44 -05:00
|
|
|
}
|
2018-09-13 17:59:09 -04:00
|
|
|
|
2020-07-10 05:09:01 -04:00
|
|
|
export default function initSearchAutocomplete(opts) {
|
|
|
|
return new SearchAutocomplete(opts);
|
2018-09-13 17:59:09 -04:00
|
|
|
}
|