2021-02-01 15:08:56 +00:00
|
|
|
import { __ } from '~/locale';
|
2021-10-08 15:12:51 +00:00
|
|
|
import Filter from './droplab/plugins/filter';
|
2018-02-21 20:22:56 +00:00
|
|
|
import DropdownUtils from './dropdown_utils';
|
2021-02-14 18:09:20 +00:00
|
|
|
import FilteredSearchDropdown from './filtered_search_dropdown';
|
2018-02-21 20:22:56 +00:00
|
|
|
import FilteredSearchDropdownManager from './filtered_search_dropdown_manager';
|
|
|
|
import FilteredSearchVisualTokens from './filtered_search_visual_tokens';
|
2016-12-14 03:55:25 +00:00
|
|
|
|
2018-02-21 20:22:56 +00:00
|
|
|
export default class DropdownHint extends FilteredSearchDropdown {
|
2017-07-20 19:17:34 +00:00
|
|
|
constructor(options = {}) {
|
|
|
|
const { input, tokenKeys } = options;
|
|
|
|
super(options);
|
2017-04-17 07:01:11 +00:00
|
|
|
this.config = {
|
|
|
|
Filter: {
|
|
|
|
template: 'hint',
|
2018-02-21 20:22:56 +00:00
|
|
|
filterFunction: DropdownUtils.filterHint.bind(null, {
|
2017-05-23 16:35:54 +00:00
|
|
|
input,
|
|
|
|
allowedKeys: tokenKeys.getKeys(),
|
|
|
|
}),
|
2017-04-17 07:01:11 +00:00
|
|
|
},
|
|
|
|
};
|
2017-05-23 16:35:54 +00:00
|
|
|
this.tokenKeys = tokenKeys;
|
2017-04-17 07:01:11 +00:00
|
|
|
}
|
2016-12-09 19:15:09 +00:00
|
|
|
|
2017-04-17 07:01:11 +00:00
|
|
|
itemClicked(e) {
|
|
|
|
const { selected } = e.detail;
|
2016-11-30 21:25:10 +00:00
|
|
|
|
2017-04-17 07:01:11 +00:00
|
|
|
if (selected.tagName === 'LI') {
|
2022-06-17 15:08:29 +00:00
|
|
|
if (Object.prototype.hasOwnProperty.call(selected.dataset, 'value')) {
|
2017-04-17 07:01:11 +00:00
|
|
|
this.dismissDropdown();
|
2022-06-17 15:08:29 +00:00
|
|
|
} else if (selected.dataset.action === 'submit') {
|
2017-04-17 07:01:11 +00:00
|
|
|
this.dismissDropdown();
|
|
|
|
this.dispatchFormSubmitEvent();
|
|
|
|
} else {
|
2020-01-02 13:03:23 +00:00
|
|
|
const filterItemEl = selected.closest('.filter-dropdown-item');
|
|
|
|
const { hint: token, tag } = filterItemEl.dataset;
|
2017-01-30 22:53:18 +00:00
|
|
|
|
2017-04-17 07:01:11 +00:00
|
|
|
if (tag.length) {
|
|
|
|
// Get previous input values in the input field and convert them into visual tokens
|
|
|
|
const previousInputValues = this.input.value.split(' ');
|
|
|
|
const searchTerms = [];
|
2017-01-30 22:53:18 +00:00
|
|
|
|
2017-04-17 07:01:11 +00:00
|
|
|
previousInputValues.forEach((value, index) => {
|
|
|
|
searchTerms.push(value);
|
2017-01-30 22:53:18 +00:00
|
|
|
|
2018-10-10 06:18:49 +00:00
|
|
|
if (
|
|
|
|
index === previousInputValues.length - 1 &&
|
|
|
|
token.indexOf(value.toLowerCase()) !== -1
|
|
|
|
) {
|
2017-04-17 07:01:11 +00:00
|
|
|
searchTerms.pop();
|
2017-01-30 22:53:18 +00:00
|
|
|
}
|
2017-04-17 07:01:11 +00:00
|
|
|
});
|
2017-01-30 22:53:18 +00:00
|
|
|
|
2017-04-17 07:01:11 +00:00
|
|
|
if (searchTerms.length > 0) {
|
2018-02-21 20:22:56 +00:00
|
|
|
FilteredSearchVisualTokens.addSearchVisualToken(searchTerms.join(' '));
|
2016-12-16 05:20:31 +00:00
|
|
|
}
|
2017-04-17 07:01:11 +00:00
|
|
|
|
2018-10-03 08:15:00 +00:00
|
|
|
const key = token.replace(':', '');
|
|
|
|
const { uppercaseTokenName } = this.tokenKeys.searchByKey(key);
|
2020-01-02 13:03:23 +00:00
|
|
|
|
|
|
|
FilteredSearchDropdownManager.addWordToInput({
|
|
|
|
tokenName: key,
|
|
|
|
clicked: false,
|
|
|
|
options: {
|
|
|
|
uppercaseTokenName,
|
|
|
|
},
|
2018-10-03 08:15:00 +00:00
|
|
|
});
|
2016-12-09 17:44:09 +00:00
|
|
|
}
|
2017-04-17 07:01:11 +00:00
|
|
|
this.dismissDropdown();
|
|
|
|
this.dispatchInputEvent();
|
2016-11-30 21:25:10 +00:00
|
|
|
}
|
|
|
|
}
|
2017-04-17 07:01:11 +00:00
|
|
|
}
|
2016-11-30 21:25:10 +00:00
|
|
|
|
2017-04-17 07:01:11 +00:00
|
|
|
renderContent() {
|
2020-01-02 13:03:23 +00:00
|
|
|
const searchItem = [
|
|
|
|
{
|
|
|
|
hint: 'search',
|
|
|
|
tag: 'search',
|
|
|
|
formattedKey: __('Search for this text'),
|
|
|
|
icon: `${gon.sprite_icons}#search`,
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
const dropdownData = this.tokenKeys
|
|
|
|
.get()
|
2020-12-23 21:10:24 +00:00
|
|
|
.map((tokenKey) => ({
|
2020-01-02 13:03:23 +00:00
|
|
|
icon: `${gon.sprite_icons}#${tokenKey.icon}`,
|
|
|
|
hint: tokenKey.key,
|
|
|
|
tag: `:${tokenKey.tag}`,
|
|
|
|
type: tokenKey.type,
|
|
|
|
formattedKey: tokenKey.formattedKey,
|
|
|
|
}))
|
|
|
|
.concat(searchItem);
|
2016-12-16 05:08:49 +00:00
|
|
|
|
2017-04-17 07:01:11 +00:00
|
|
|
this.droplab.changeHookList(this.hookId, this.dropdown, [Filter], this.config);
|
|
|
|
this.droplab.setData(this.hookId, dropdownData);
|
2020-01-02 13:03:23 +00:00
|
|
|
|
|
|
|
super.renderContent();
|
2017-04-17 07:01:11 +00:00
|
|
|
}
|
2016-12-06 22:23:04 +00:00
|
|
|
|
2017-04-17 07:01:11 +00:00
|
|
|
init() {
|
|
|
|
this.droplab.addHook(this.input, this.dropdown, [Filter], this.config).init();
|
2016-11-30 21:25:10 +00:00
|
|
|
}
|
2017-04-17 07:01:11 +00:00
|
|
|
}
|