2018-05-09 12:01:36 +05:30
|
|
|
import $ from 'jquery';
|
2018-03-17 18:26:18 +05:30
|
|
|
import Flash from '~/flash';
|
|
|
|
import Api from '~/api';
|
2019-07-31 22:56:46 +05:30
|
|
|
import { __ } from '~/locale';
|
2018-03-17 18:26:18 +05:30
|
|
|
|
|
|
|
export default class Search {
|
|
|
|
constructor() {
|
|
|
|
const $groupDropdown = $('.js-search-group-dropdown');
|
|
|
|
const $projectDropdown = $('.js-search-project-dropdown');
|
|
|
|
|
|
|
|
this.searchInput = '.js-search-input';
|
|
|
|
this.searchClear = '.js-search-clear';
|
|
|
|
|
2018-03-27 19:54:05 +05:30
|
|
|
this.groupId = $groupDropdown.data('groupId');
|
2018-03-17 18:26:18 +05:30
|
|
|
this.eventListeners();
|
|
|
|
|
|
|
|
$groupDropdown.glDropdown({
|
|
|
|
selectable: true,
|
|
|
|
filterable: true,
|
|
|
|
filterRemote: true,
|
|
|
|
fieldName: 'group_id',
|
|
|
|
search: {
|
|
|
|
fields: ['full_name'],
|
|
|
|
},
|
|
|
|
data(term, callback) {
|
2018-12-13 13:39:08 +05:30
|
|
|
return Api.groups(term, {}, data => {
|
2018-03-17 18:26:18 +05:30
|
|
|
data.unshift({
|
2019-07-31 22:56:46 +05:30
|
|
|
full_name: __('Any'),
|
2018-03-17 18:26:18 +05:30
|
|
|
});
|
|
|
|
data.splice(1, 0, 'divider');
|
|
|
|
return callback(data);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
id(obj) {
|
|
|
|
return obj.id;
|
|
|
|
},
|
|
|
|
text(obj) {
|
|
|
|
return obj.full_name;
|
|
|
|
},
|
|
|
|
toggleLabel(obj) {
|
2018-12-13 13:39:08 +05:30
|
|
|
return `${$groupDropdown.data('defaultLabel')} ${obj.full_name}`;
|
2018-03-17 18:26:18 +05:30
|
|
|
},
|
|
|
|
clicked: () => Search.submitSearch(),
|
|
|
|
});
|
|
|
|
|
|
|
|
$projectDropdown.glDropdown({
|
|
|
|
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
|
|
|
});
|
|
|
|
data.splice(1, 0, 'divider');
|
|
|
|
|
|
|
|
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;
|
|
|
|
},
|
|
|
|
toggleLabel(obj) {
|
2018-12-13 13:39:08 +05:30
|
|
|
return `${$projectDropdown.data('defaultLabel')} ${obj.name_with_namespace}`;
|
2018-03-17 18:26:18 +05:30
|
|
|
},
|
|
|
|
clicked: () => Search.submitSearch(),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|