2017-04-07 13:57:03 +00:00
|
|
|
/* global Flash */
|
|
|
|
|
|
|
|
import AjaxFilter from '~/droplab/plugins/ajax_filter';
|
2017-05-16 20:50:55 +00:00
|
|
|
import './filtered_search_dropdown';
|
2017-06-27 18:37:30 +00:00
|
|
|
import { addClassIfElementExists } from '../lib/utils/dom_utils';
|
2016-12-14 03:55:25 +00:00
|
|
|
|
2017-04-17 07:01:11 +00:00
|
|
|
class DropdownUser extends gl.FilteredSearchDropdown {
|
2017-05-23 16:35:54 +00:00
|
|
|
constructor(droplab, dropdown, input, tokenKeys, filter) {
|
2017-04-17 07:01:11 +00:00
|
|
|
super(droplab, dropdown, input, filter);
|
|
|
|
this.config = {
|
|
|
|
AjaxFilter: {
|
|
|
|
endpoint: `${gon.relative_url_root || ''}/autocomplete/users.json`,
|
|
|
|
searchKey: 'search',
|
|
|
|
params: {
|
|
|
|
per_page: 20,
|
|
|
|
active: true,
|
|
|
|
project_id: this.getProjectId(),
|
|
|
|
current_user: true,
|
2016-12-12 00:05:55 +00:00
|
|
|
},
|
2017-04-17 07:01:11 +00:00
|
|
|
searchValueFunction: this.getSearchInput.bind(this),
|
|
|
|
loadingTemplate: this.loadingTemplate,
|
2017-06-02 07:42:18 +00:00
|
|
|
onLoadingFinished: () => {
|
|
|
|
this.hideCurrentUser();
|
|
|
|
},
|
2017-04-17 07:01:11 +00:00
|
|
|
onError() {
|
|
|
|
/* eslint-disable no-new */
|
|
|
|
new Flash('An error occured fetching the dropdown data.');
|
|
|
|
/* eslint-enable no-new */
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
2017-05-23 16:35:54 +00:00
|
|
|
this.tokenKeys = tokenKeys;
|
2017-04-17 07:01:11 +00:00
|
|
|
}
|
2016-12-07 18:51:25 +00:00
|
|
|
|
2017-06-02 07:42:18 +00:00
|
|
|
hideCurrentUser() {
|
2017-06-27 18:37:30 +00:00
|
|
|
addClassIfElementExists(this.dropdown.querySelector('.js-current-user'), 'hidden');
|
2017-06-02 07:42:18 +00:00
|
|
|
}
|
|
|
|
|
2017-04-17 07:01:11 +00:00
|
|
|
itemClicked(e) {
|
|
|
|
super.itemClicked(e,
|
|
|
|
selected => selected.querySelector('.dropdown-light-content').innerText.trim());
|
|
|
|
}
|
2016-12-12 00:11:58 +00:00
|
|
|
|
2017-04-17 07:01:11 +00:00
|
|
|
renderContent(forceShowList = false) {
|
|
|
|
this.droplab.changeHookList(this.hookId, this.dropdown, [AjaxFilter], this.config);
|
|
|
|
super.renderContent(forceShowList);
|
|
|
|
}
|
2017-01-30 22:53:18 +00:00
|
|
|
|
2017-04-17 07:01:11 +00:00
|
|
|
getProjectId() {
|
|
|
|
return this.input.getAttribute('data-project-id');
|
|
|
|
}
|
2017-01-30 22:53:18 +00:00
|
|
|
|
2017-04-17 07:01:11 +00:00
|
|
|
getSearchInput() {
|
|
|
|
const query = gl.DropdownUtils.getSearchInput(this.input);
|
2017-05-23 16:35:54 +00:00
|
|
|
const { lastToken } = gl.FilteredSearchTokenizer.processTokens(query, this.tokenKeys.get());
|
2016-12-07 18:51:25 +00:00
|
|
|
|
2017-04-17 07:01:11 +00:00
|
|
|
let value = lastToken || '';
|
2017-01-27 19:22:12 +00:00
|
|
|
|
2017-04-17 07:01:11 +00:00
|
|
|
if (value[0] === '@') {
|
|
|
|
value = value.slice(1);
|
2016-12-09 17:44:09 +00:00
|
|
|
}
|
2016-12-07 18:51:25 +00:00
|
|
|
|
2017-04-17 07:01:11 +00:00
|
|
|
// Removes the first character if it is a quotation so that we can search
|
|
|
|
// with multiple words
|
|
|
|
if (value[0] === '"' || value[0] === '\'') {
|
|
|
|
value = value.slice(1);
|
2016-12-07 18:51:25 +00:00
|
|
|
}
|
2017-04-17 07:01:11 +00:00
|
|
|
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
init() {
|
|
|
|
this.droplab.addHook(this.input, this.dropdown, [AjaxFilter], this.config).init();
|
2016-12-02 21:04:10 +00:00
|
|
|
}
|
2017-04-17 07:01:11 +00:00
|
|
|
}
|
2016-12-02 21:04:10 +00:00
|
|
|
|
2017-04-17 07:01:11 +00:00
|
|
|
window.gl = window.gl || {};
|
|
|
|
gl.DropdownUser = DropdownUser;
|