debian-mirror-gitlab/app/assets/javascripts/global_search_input.js

426 lines
12 KiB
JavaScript
Raw Normal View History

2019-12-26 22:10:19 +05:30
/* eslint-disable no-return-assign, consistent-return, class-methods-use-this */
2018-05-09 12:01:36 +05:30
import $ from 'jquery';
2020-06-23 00:09:42 +05:30
import { throttle } from 'lodash';
import { s__, __, sprintf } from '~/locale';
2018-11-18 11:00:15 +05:30
import {
isInGroupsPage,
isInProjectPage,
getGroupSlug,
getProjectSlug,
spriteIcon,
} from './lib/utils/common_utils';
2018-03-17 18:26:18 +05:30
/**
* 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.
*/
const KEYCODE = {
ESCAPE: 27,
BACKSPACE: 8,
ENTER: 13,
UP: 38,
DOWN: 40,
};
function setSearchOptions() {
2019-12-26 22:10:19 +05:30
const $projectOptionsDataEl = $('.js-search-project-options');
const $groupOptionsDataEl = $('.js-search-group-options');
const $dashboardOptionsDataEl = $('.js-search-dashboard-options');
2018-03-17 18:26:18 +05:30
if ($projectOptionsDataEl.length) {
gl.projectOptions = gl.projectOptions || {};
2019-12-26 22:10:19 +05:30
const projectPath = $projectOptionsDataEl.data('projectPath');
2018-03-17 18:26:18 +05:30
gl.projectOptions[projectPath] = {
name: $projectOptionsDataEl.data('name'),
2018-03-27 19:54:05 +05:30
issuesPath: $projectOptionsDataEl.data('issuesPath'),
issuesDisabled: $projectOptionsDataEl.data('issuesDisabled'),
mrPath: $projectOptionsDataEl.data('mrPath'),
2018-03-17 18:26:18 +05:30
};
}
2016-09-13 17:45:13 +05:30
2018-03-17 18:26:18 +05:30
if ($groupOptionsDataEl.length) {
gl.groupOptions = gl.groupOptions || {};
2016-09-13 17:45:13 +05:30
2019-12-26 22:10:19 +05:30
const groupPath = $groupOptionsDataEl.data('groupPath');
2016-09-13 17:45:13 +05:30
2018-03-17 18:26:18 +05:30
gl.groupOptions[groupPath] = {
name: $groupOptionsDataEl.data('name'),
2018-03-27 19:54:05 +05:30
issuesPath: $groupOptionsDataEl.data('issuesPath'),
mrPath: $groupOptionsDataEl.data('mrPath'),
2018-03-17 18:26:18 +05:30
};
}
2016-09-13 17:45:13 +05:30
2018-03-17 18:26:18 +05:30
if ($dashboardOptionsDataEl.length) {
gl.dashboardOptions = {
2018-11-18 11:00:15 +05:30
name: s__('SearchAutocomplete|All GitLab'),
2018-03-27 19:54:05 +05:30
issuesPath: $dashboardOptionsDataEl.data('issuesPath'),
mrPath: $dashboardOptionsDataEl.data('mrPath'),
2018-03-17 18:26:18 +05:30
};
}
}
2020-06-23 00:09:42 +05:30
export class GlobalSearchInput {
constructor({ wrap } = {}) {
2018-03-17 18:26:18 +05:30
setSearchOptions();
this.bindEventContext();
this.wrap = wrap || $('.search');
this.dropdown = this.wrap.find('.dropdown');
this.dropdownToggle = this.wrap.find('.js-dropdown-search-toggle');
2018-11-18 11:00:15 +05:30
this.dropdownMenu = this.dropdown.find('.dropdown-menu');
2018-03-17 18:26:18 +05:30
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-11-18 11:00:15 +05:30
this.scrollFadeInitialized = false;
2018-03-17 18:26:18 +05:30
this.saveOriginalState();
// Only when user is logged in
if (gon.current_user_id) {
2020-06-23 00:09:42 +05:30
this.createGlobalSearchInput();
2016-11-03 12:29:30 +05:30
}
2016-09-13 17:45:13 +05:30
2018-03-17 18:26:18 +05:30
this.bindEvents();
this.dropdownToggle.dropdown();
2019-12-26 22:10:19 +05:30
this.searchInput.addClass('js-autocomplete-disabled');
2018-03-17 18:26:18 +05:30
}
2017-08-17 22:00:37 +05:30
2018-03-17 18:26:18 +05:30
// 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-12-26 22:10:19 +05:30
this.onSearchInputChange = this.onSearchInputChange.bind(this);
2018-11-18 11:00:15 +05:30
this.setScrollFade = this.setScrollFade.bind(this);
2018-03-17 18:26:18 +05:30
}
getElement(selector) {
return this.wrap.find(selector);
}
2016-09-13 17:45:13 +05:30
2018-03-17 18:26:18 +05:30
saveOriginalState() {
2018-11-18 11:00:15 +05:30
return (this.originalState = this.serializeState());
2018-03-17 18:26:18 +05:30
}
2020-06-23 00:09:42 +05:30
createGlobalSearchInput() {
2018-03-17 18:26:18 +05:30
return this.searchInput.glDropdown({
filterInputBlur: false,
filterable: true,
filterRemote: true,
highlight: true,
2018-11-18 11:00:15 +05:30
icon: true,
2018-03-17 18:26:18 +05:30
enterCallback: false,
filterInput: 'input#search',
search: {
fields: ['text'],
},
id: this.getSearchText,
data: this.getData.bind(this),
selectable: true,
clicked: this.onClick.bind(this),
});
}
2019-12-04 20:38:33 +05:30
getSearchText(selectedObject) {
2018-03-17 18:26:18 +05:30
return selectedObject.id ? selectedObject.text : '';
}
getData(term, callback) {
if (!term) {
const contents = this.getCategoryContents();
if (contents) {
2018-11-18 11:00:15 +05:30
const glDropdownInstance = this.searchInput.data('glDropdown');
if (glDropdownInstance) {
glDropdownInstance.filter.options.callback(contents);
}
2020-06-23 00:09:42 +05:30
this.enableDropdown();
2016-09-13 17:45:13 +05:30
}
2018-03-17 18:26:18 +05:30
return;
2016-11-03 12:29:30 +05:30
}
2016-09-13 17:45:13 +05:30
2020-06-23 00:09:42 +05:30
const options = this.scopedSearchOptions(term);
2018-11-18 11:00:15 +05:30
2020-06-23 00:09:42 +05:30
callback(options);
2018-11-18 11:00:15 +05:30
2020-06-23 00:09:42 +05:30
this.highlightFirstRow();
this.setScrollFade();
2018-03-17 18:26:18 +05:30
}
2016-09-13 17:45:13 +05:30
2020-06-23 00:09:42 +05:30
// 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
? `${gon.relative_url_root}/search?search=${term}&project_id=${projectId}&group_id=${groupId}`
: `${gon.relative_url_root}/search?search=${term}&project_id=${projectId}`;
options.push({
icon,
text: term,
template: sprintf(
s__(`SearchAutocomplete|in project %{projectName}`),
{
projectName: `<i>${projectOptions.name}</i>`,
},
false,
),
url,
});
2016-11-03 12:29:30 +05:30
}
2016-09-13 17:45:13 +05:30
2020-06-23 00:09:42 +05:30
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,
),
url: `${gon.relative_url_root}/search?search=${term}&group_id=${groupId}`,
2018-03-17 18:26:18 +05:30
});
2016-11-03 12:29:30 +05:30
}
2016-09-13 17:45:13 +05:30
2020-06-23 00:09:42 +05:30
options.push({
icon,
text: term,
template: s__('SearchAutocomplete|in all GitLab'),
url: `${gon.relative_url_root}/search?search=${term}`,
});
2018-03-17 18:26:18 +05:30
2020-06-23 00:09:42 +05:30
return options;
2018-03-17 18:26:18 +05:30
}
2016-09-13 17:45:13 +05:30
2018-03-17 18:26:18 +05:30
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(),
};
}
2016-09-13 17:45:13 +05:30
2018-03-17 18:26:18 +05:30
bindEvents() {
2019-12-26 22:10:19 +05:30
this.searchInput.on('input', this.onSearchInputChange);
2018-03-17 18:26:18 +05:30
this.searchInput.on('keyup', this.onSearchInputKeyUp);
this.searchInput.on('focus', this.onSearchInputFocus);
this.searchInput.on('blur', this.onSearchInputBlur);
this.clearInput.on('click', this.onClearInputClick);
2018-11-18 11:00:15 +05:30
this.dropdownContent.on('scroll', throttle(this.setScrollFade, 250));
2019-12-26 22:10:19 +05:30
this.searchInput.on('click', e => {
e.stopPropagation();
});
2018-03-17 18:26:18 +05:30
}
2020-06-23 00:09:42 +05:30
enableDropdown() {
2018-11-18 11:00:15 +05:30
this.setScrollFade();
2018-03-17 18:26:18 +05:30
// No need to enable anything if user is not logged in
if (!gon.current_user_id) {
return;
2016-11-03 12:29:30 +05:30
}
2016-09-13 17:45:13 +05:30
2018-03-17 18:26:18 +05:30
// If the dropdown is closed, we'll open it
2018-11-08 19:23:39 +05:30
if (!this.dropdown.hasClass('show')) {
2018-03-17 18:26:18 +05:30
this.loadingSuggestions = false;
this.dropdownToggle.dropdown('toggle');
2019-12-26 22:10:19 +05:30
return this.searchInput.removeClass('js-autocomplete-disabled');
2016-11-03 12:29:30 +05:30
}
2018-03-17 18:26:18 +05:30
}
2016-09-13 17:45:13 +05:30
2019-12-26 22:10:19 +05:30
onSearchInputChange() {
2020-06-23 00:09:42 +05:30
this.enableDropdown();
2018-03-17 18:26:18 +05:30
}
onSearchInputKeyUp(e) {
switch (e.keyCode) {
case KEYCODE.ESCAPE:
this.restoreOriginalState();
break;
case KEYCODE.ENTER:
2020-06-23 00:09:42 +05:30
this.disableDropdown();
2018-03-17 18:26:18 +05:30
break;
default:
2016-11-03 12:29:30 +05:30
}
2019-09-04 21:01:54 +05:30
this.wrap.toggleClass('has-value', Boolean(e.target.value));
2018-03-17 18:26:18 +05:30
}
2016-09-13 17:45:13 +05:30
2018-03-17 18:26:18 +05:30
onSearchInputFocus() {
this.isFocused = true;
this.wrap.addClass('search-active');
if (this.getValue() === '') {
return this.getData();
2016-11-03 12:29:30 +05:30
}
2018-03-17 18:26:18 +05:30
}
2016-09-13 17:45:13 +05:30
2018-03-17 18:26:18 +05:30
getValue() {
return this.searchInput.val();
}
2016-09-13 17:45:13 +05:30
2018-03-17 18:26:18 +05:30
onClearInputClick(e) {
e.preventDefault();
2019-09-04 21:01:54 +05:30
this.wrap.toggleClass('has-value', Boolean(e.target.value));
2018-03-17 18:26:18 +05:30
return this.searchInput.val('').focus();
}
2016-09-13 17:45:13 +05:30
2019-12-04 20:38:33 +05:30
onSearchInputBlur() {
2018-03-17 18:26:18 +05:30
this.isFocused = false;
this.wrap.removeClass('search-active');
// If input is blank then restore state
if (this.searchInput.val() === '') {
2019-09-04 21:01:54 +05:30
this.restoreOriginalState();
2017-08-17 22:00:37 +05:30
}
2019-09-04 21:01:54 +05:30
this.dropdownMenu.removeClass('show');
2016-11-03 12:29:30 +05:30
}
2018-03-17 18:26:18 +05:30
restoreOriginalState() {
2019-12-26 22:10:19 +05:30
const inputs = Object.keys(this.originalState);
for (let i = 0, len = inputs.length; i < len; i += 1) {
const input = inputs[i];
2019-12-21 20:55:43 +05:30
this.getElement(`#${input}`).val(this.originalState[input]);
2018-03-17 18:26:18 +05:30
}
}
2016-11-03 12:29:30 +05:30
2018-03-17 18:26:18 +05:30
resetSearchState() {
2019-12-26 22:10:19 +05:30
const inputs = Object.keys(this.originalState);
const results = [];
for (let i = 0, len = inputs.length; i < len; i += 1) {
const input = inputs[i];
2019-12-21 20:55:43 +05:30
results.push(this.getElement(`#${input}`).val(''));
2016-11-03 12:29:30 +05:30
}
2018-03-17 18:26:18 +05:30
return results;
}
2016-09-13 17:45:13 +05:30
2020-06-23 00:09:42 +05:30
disableDropdown() {
2019-12-26 22:10:19 +05:30
if (!this.searchInput.hasClass('js-autocomplete-disabled') && this.dropdown.hasClass('show')) {
this.searchInput.addClass('js-autocomplete-disabled');
2020-05-24 23:13:21 +05:30
this.dropdownToggle.dropdown('toggle');
2018-03-17 18:26:18 +05:30
this.restoreMenu();
2016-11-03 12:29:30 +05:30
}
2018-03-17 18:26:18 +05:30
}
restoreMenu() {
2019-12-26 22:10:19 +05:30
const html = `<ul><li class="dropdown-menu-empty-item"><a>${__('Loading...')}</a></li></ul>`;
2018-03-17 18:26:18 +05:30
return this.dropdownContent.html(html);
}
2016-11-03 12:29:30 +05:30
2018-03-17 18:26:18 +05:30
onClick(item, $el, e) {
2018-11-08 19:23:39 +05:30
if (window.location.pathname.indexOf(item.url) !== -1) {
2018-03-17 18:26:18 +05:30
if (!e.metaKey) e.preventDefault();
$el.removeClass('is-active');
2020-06-23 00:09:42 +05:30
this.disableDropdown();
2018-03-17 18:26:18 +05:30
return this.searchInput.val('').focus();
2016-11-03 12:29:30 +05:30
}
2018-03-17 18:26:18 +05:30
}
2018-11-18 11:00:15 +05:30
highlightFirstRow() {
this.searchInput.data('glDropdown').highlightRowAtIndex(null, 0);
}
2020-06-23 00:09:42 +05:30
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}`,
});
2018-11-18 11:00:15 +05:30
}
2020-06-23 00:09:42 +05:30
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}`,
},
{
text: s__("SearchAutocomplete|Merge requests I've created"),
url: `${mrPath}/?author_username=${userName}`,
},
];
2018-11-18 11:00:15 +05:30
2020-06-23 00:09:42 +05:30
let items;
if (issuesDisabled) {
items = baseItems.concat(mergeRequestItems);
} else {
items = baseItems.concat(...issueItems, ...mergeRequestItems);
}
return items;
2018-11-18 11:00:15 +05:30
}
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());
}
2018-03-17 18:26:18 +05:30
}
2018-12-05 23:21:45 +05:30
2020-06-23 00:09:42 +05:30
export default function initGlobalSearchInput(opts) {
return new GlobalSearchInput(opts);
2018-12-05 23:21:45 +05:30
}