debian-mirror-gitlab/app/assets/javascripts/filtered_search/dropdown_user.js

83 lines
2.3 KiB
JavaScript
Raw Normal View History

2019-05-30 16:15:17 +05:30
import Flash from '../flash';
import AjaxFilter from '../droplab/plugins/ajax_filter';
import FilteredSearchDropdown from './filtered_search_dropdown';
2017-09-10 17:25:29 +05:30
import { addClassIfElementExists } from '../lib/utils/dom_utils';
2019-05-30 16:15:17 +05:30
import DropdownUtils from './dropdown_utils';
import FilteredSearchTokenizer from './filtered_search_tokenizer';
2017-08-17 22:00:37 +05:30
2019-05-30 16:15:17 +05:30
export default class DropdownUser extends FilteredSearchDropdown {
2017-09-10 17:25:29 +05:30
constructor(options = {}) {
2019-05-30 16:15:17 +05:30
const { tokenKeys } = options;
super(options);
this.config = {
AjaxFilter: {
endpoint: `${gon.relative_url_root || ''}/autocomplete/users.json`,
searchKey: 'search',
params: {
active: true,
group_id: this.getGroupId(),
project_id: this.getProjectId(),
current_user: true,
},
searchValueFunction: this.getSearchInput.bind(this),
loadingTemplate: this.loadingTemplate,
onLoadingFinished: () => {
this.hideCurrentUser();
},
onError() {
/* eslint-disable no-new */
new Flash('An error occurred fetching the dropdown data.');
/* eslint-enable no-new */
},
2017-08-17 22:00:37 +05:30
},
};
2019-05-30 16:15:17 +05:30
this.tokenKeys = tokenKeys;
2017-09-10 17:25:29 +05:30
}
hideCurrentUser() {
addClassIfElementExists(this.dropdown.querySelector('.js-current-user'), 'hidden');
2017-08-17 22:00:37 +05:30
}
2019-05-30 16:15:17 +05:30
itemClicked(e) {
super.itemClicked(e, selected =>
selected.querySelector('.dropdown-light-content').innerText.trim(),
);
}
renderContent(forceShowList = false) {
this.droplab.changeHookList(this.hookId, this.dropdown, [AjaxFilter], this.config);
super.renderContent(forceShowList);
}
2018-03-17 18:26:18 +05:30
getGroupId() {
return this.input.getAttribute('data-group-id');
}
2017-08-17 22:00:37 +05:30
getProjectId() {
return this.input.getAttribute('data-project-id');
}
2019-05-30 16:15:17 +05:30
getSearchInput() {
const query = DropdownUtils.getSearchInput(this.input);
const { lastToken } = FilteredSearchTokenizer.processTokens(query, this.tokenKeys.get());
let value = lastToken || '';
if (value[0] === '@') {
value = value.slice(1);
}
// 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);
}
return value;
}
init() {
this.droplab.addHook(this.input, this.dropdown, [AjaxFilter], this.config).init();
}
2017-08-17 22:00:37 +05:30
}