2017-08-17 22:00:37 +05:30
|
|
|
import Filter from '~/droplab/plugins/filter';
|
2018-03-27 19:54:05 +05:30
|
|
|
import FilteredSearchDropdown from './filtered_search_dropdown';
|
|
|
|
import DropdownUtils from './dropdown_utils';
|
|
|
|
import FilteredSearchDropdownManager from './filtered_search_dropdown_manager';
|
|
|
|
import FilteredSearchVisualTokens from './filtered_search_visual_tokens';
|
2020-03-13 15:44:24 +05:30
|
|
|
import { __ } from '~/locale';
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2018-03-27 19:54:05 +05:30
|
|
|
export default class DropdownHint extends FilteredSearchDropdown {
|
2017-09-10 17:25:29 +05:30
|
|
|
constructor(options = {}) {
|
|
|
|
const { input, tokenKeys } = options;
|
|
|
|
super(options);
|
2017-08-17 22:00:37 +05:30
|
|
|
this.config = {
|
|
|
|
Filter: {
|
|
|
|
template: 'hint',
|
2018-03-27 19:54:05 +05:30
|
|
|
filterFunction: DropdownUtils.filterHint.bind(null, {
|
2017-09-10 17:25:29 +05:30
|
|
|
input,
|
|
|
|
allowedKeys: tokenKeys.getKeys(),
|
|
|
|
}),
|
2017-08-17 22:00:37 +05:30
|
|
|
},
|
|
|
|
};
|
2017-09-10 17:25:29 +05:30
|
|
|
this.tokenKeys = tokenKeys;
|
2017-08-17 22:00:37 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
itemClicked(e) {
|
|
|
|
const { selected } = e.detail;
|
|
|
|
|
|
|
|
if (selected.tagName === 'LI') {
|
|
|
|
if (selected.hasAttribute('data-value')) {
|
|
|
|
this.dismissDropdown();
|
|
|
|
} else if (selected.getAttribute('data-action') === 'submit') {
|
|
|
|
this.dismissDropdown();
|
|
|
|
this.dispatchFormSubmitEvent();
|
|
|
|
} else {
|
2020-03-13 15:44:24 +05:30
|
|
|
const filterItemEl = selected.closest('.filter-dropdown-item');
|
|
|
|
const { hint: token, tag } = filterItemEl.dataset;
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
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 = [];
|
|
|
|
|
|
|
|
previousInputValues.forEach((value, index) => {
|
|
|
|
searchTerms.push(value);
|
|
|
|
|
2018-12-13 13:39:08 +05:30
|
|
|
if (
|
|
|
|
index === previousInputValues.length - 1 &&
|
|
|
|
token.indexOf(value.toLowerCase()) !== -1
|
|
|
|
) {
|
2017-08-17 22:00:37 +05:30
|
|
|
searchTerms.pop();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (searchTerms.length > 0) {
|
2018-03-27 19:54:05 +05:30
|
|
|
FilteredSearchVisualTokens.addSearchVisualToken(searchTerms.join(' '));
|
2017-08-17 22:00:37 +05:30
|
|
|
}
|
|
|
|
|
2018-12-05 23:21:45 +05:30
|
|
|
const key = token.replace(':', '');
|
|
|
|
const { uppercaseTokenName } = this.tokenKeys.searchByKey(key);
|
2020-03-13 15:44:24 +05:30
|
|
|
|
|
|
|
FilteredSearchDropdownManager.addWordToInput({
|
|
|
|
tokenName: key,
|
|
|
|
clicked: false,
|
|
|
|
options: {
|
|
|
|
uppercaseTokenName,
|
|
|
|
},
|
2018-12-05 23:21:45 +05:30
|
|
|
});
|
2017-08-17 22:00:37 +05:30
|
|
|
}
|
|
|
|
this.dismissDropdown();
|
|
|
|
this.dispatchInputEvent();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
renderContent() {
|
2020-03-13 15:44:24 +05:30
|
|
|
const searchItem = [
|
|
|
|
{
|
|
|
|
hint: 'search',
|
|
|
|
tag: 'search',
|
|
|
|
formattedKey: __('Search for this text'),
|
|
|
|
icon: `${gon.sprite_icons}#search`,
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
const dropdownData = this.tokenKeys
|
|
|
|
.get()
|
|
|
|
.map(tokenKey => ({
|
|
|
|
icon: `${gon.sprite_icons}#${tokenKey.icon}`,
|
|
|
|
hint: tokenKey.key,
|
|
|
|
tag: `:${tokenKey.tag}`,
|
|
|
|
type: tokenKey.type,
|
|
|
|
formattedKey: tokenKey.formattedKey,
|
|
|
|
}))
|
|
|
|
.concat(searchItem);
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
this.droplab.changeHookList(this.hookId, this.dropdown, [Filter], this.config);
|
|
|
|
this.droplab.setData(this.hookId, dropdownData);
|
2020-03-13 15:44:24 +05:30
|
|
|
|
|
|
|
super.renderContent();
|
2017-08-17 22:00:37 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
init() {
|
|
|
|
this.droplab.addHook(this.input, this.dropdown, [Filter], this.config).init();
|
|
|
|
}
|
|
|
|
}
|