debian-mirror-gitlab/app/assets/javascripts/pages/search/show/search.js

117 lines
3 KiB
JavaScript
Raw Normal View History

2018-05-09 12:01:36 +05:30
import $ from 'jquery';
2021-01-29 00:20:46 +05:30
import setHighlightClass from 'ee_else_ce/search/highlight_blob_search_result';
2020-11-24 15:15:51 +05:30
import initDeprecatedJQueryDropdown from '~/deprecated_jquery_dropdown';
2020-10-24 23:57:45 +05:30
import { deprecatedCreateFlash as Flash } from '~/flash';
2018-03-17 18:26:18 +05:30
import Api from '~/api';
2019-07-31 22:56:46 +05:30
import { __ } from '~/locale';
2019-10-12 21:52:04 +05:30
import Project from '~/pages/projects/project';
2021-01-29 00:20:46 +05:30
import { visitUrl, queryToObject } from '~/lib/utils/url_utility';
2019-10-12 21:52:04 +05:30
import refreshCounts from './refresh_counts';
2018-03-17 18:26:18 +05:30
export default class Search {
constructor() {
2021-01-29 00:20:46 +05:30
setHighlightClass(); // Code Highlighting
2018-03-17 18:26:18 +05:30
const $projectDropdown = $('.js-search-project-dropdown');
this.searchInput = '.js-search-input';
this.searchClear = '.js-search-clear';
2021-01-29 00:20:46 +05:30
const query = queryToObject(window.location.search);
this.groupId = query?.group_id;
2018-03-17 18:26:18 +05:30
this.eventListeners();
2019-10-12 21:52:04 +05:30
refreshCounts();
2018-03-17 18:26:18 +05:30
2020-11-24 15:15:51 +05:30
initDeprecatedJQueryDropdown($projectDropdown, {
2018-03-17 18:26:18 +05:30
selectable: true,
filterable: true,
filterRemote: true,
fieldName: 'project_id',
search: {
fields: ['name'],
},
data: (term, callback) => {
this.getProjectsData(term)
2018-12-13 13:39:08 +05:30
.then(data => {
2018-03-17 18:26:18 +05:30
data.unshift({
2019-07-31 22:56:46 +05:30
name_with_namespace: __('Any'),
2018-03-17 18:26:18 +05:30
});
2019-12-04 20:38:33 +05:30
data.splice(1, 0, { type: 'divider' });
2018-03-17 18:26:18 +05:30
return data;
})
.then(data => callback(data))
2019-07-31 22:56:46 +05:30
.catch(() => new Flash(__('Error fetching projects')));
2018-03-17 18:26:18 +05:30
},
id(obj) {
return obj.id;
},
text(obj) {
return obj.name_with_namespace;
},
clicked: () => Search.submitSearch(),
});
2019-10-12 21:52:04 +05:30
Project.initRefSwitcher();
2018-03-17 18:26:18 +05:30
}
eventListeners() {
$(document)
.off('keyup', this.searchInput)
.on('keyup', this.searchInput, this.searchKeyUp);
$(document)
.off('click', this.searchClear)
.on('click', this.searchClear, this.clearSearchField.bind(this));
2021-01-03 14:25:43 +05:30
$('a.js-search-clear')
.off('click', this.clearSearchFilter)
.on('click', this.clearSearchFilter);
2018-03-17 18:26:18 +05:30
}
static submitSearch() {
return $('.js-search-form').submit();
}
searchKeyUp() {
const $input = $(this);
if ($input.val() === '') {
$('.js-search-clear').addClass('hidden');
} else {
$('.js-search-clear').removeClass('hidden');
}
}
clearSearchField() {
2018-12-13 13:39:08 +05:30
return $(this.searchInput)
.val('')
.trigger('keyup')
.focus();
2018-03-17 18:26:18 +05:30
}
2021-01-03 14:25:43 +05:30
// We need to manually follow the link on the anchors
// that have this event bound, as their `click` default
// behavior is prevented by the toggle logic.
/* eslint-disable-next-line class-methods-use-this */
clearSearchFilter(ev) {
const $target = $(ev.currentTarget);
visitUrl($target.href);
ev.stopPropagation();
}
2018-03-17 18:26:18 +05:30
getProjectsData(term) {
2018-12-13 13:39:08 +05:30
return new Promise(resolve => {
2018-03-17 18:26:18 +05:30
if (this.groupId) {
2018-11-08 19:23:39 +05:30
Api.groupProjects(this.groupId, term, {}, resolve);
2018-03-17 18:26:18 +05:30
} else {
2018-12-13 13:39:08 +05:30
Api.projects(
term,
{
order_by: 'id',
},
resolve,
);
2018-03-17 18:26:18 +05:30
}
});
}
}