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';
|
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 {
|
|
|
|
const token = selected.querySelector('.js-filter-hint').innerText.trim();
|
|
|
|
const tag = selected.querySelector('.js-filter-tag').innerText.trim();
|
|
|
|
|
|
|
|
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);
|
|
|
|
FilteredSearchDropdownManager.addWordToInput(key, '', false, {
|
|
|
|
uppercaseTokenName,
|
|
|
|
});
|
2017-08-17 22:00:37 +05:30
|
|
|
}
|
|
|
|
this.dismissDropdown();
|
|
|
|
this.dispatchInputEvent();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
renderContent() {
|
2018-12-13 13:39:08 +05:30
|
|
|
const dropdownData = this.tokenKeys.get().map(tokenKey => ({
|
|
|
|
icon: `${gon.sprite_icons}#${tokenKey.icon}`,
|
|
|
|
hint: tokenKey.key,
|
|
|
|
tag: `:${tokenKey.tag}`,
|
|
|
|
type: tokenKey.type,
|
|
|
|
}));
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
this.droplab.changeHookList(this.hookId, this.dropdown, [Filter], this.config);
|
|
|
|
this.droplab.setData(this.hookId, dropdownData);
|
|
|
|
}
|
|
|
|
|
|
|
|
init() {
|
|
|
|
this.droplab.addHook(this.input, this.dropdown, [Filter], this.config).init();
|
|
|
|
}
|
|
|
|
}
|