2018-03-27 19:54:05 +05:30
|
|
|
import DropdownUtils from './dropdown_utils';
|
|
|
|
import FilteredSearchDropdownManager from './filtered_search_dropdown_manager';
|
2020-03-13 15:44:24 +05:30
|
|
|
import FilteredSearchVisualTokens from './filtered_search_visual_tokens';
|
2020-05-24 23:13:21 +05:30
|
|
|
import { FILTER_TYPE } from './constants';
|
2018-03-27 19:54:05 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
const DATA_DROPDOWN_TRIGGER = 'data-dropdown-trigger';
|
|
|
|
|
2018-03-27 19:54:05 +05:30
|
|
|
export default class FilteredSearchDropdown {
|
2017-09-10 17:25:29 +05:30
|
|
|
constructor({ droplab, dropdown, input, filter }) {
|
2017-08-17 22:00:37 +05:30
|
|
|
this.droplab = droplab;
|
|
|
|
this.hookId = input && input.id;
|
|
|
|
this.input = input;
|
|
|
|
this.filter = filter;
|
|
|
|
this.dropdown = dropdown;
|
|
|
|
this.loadingTemplate = `<div class="filter-dropdown-loading">
|
2020-03-13 15:44:24 +05:30
|
|
|
<span class="spinner"></span>
|
2017-08-17 22:00:37 +05:30
|
|
|
</div>`;
|
|
|
|
this.bindEvents();
|
|
|
|
}
|
|
|
|
|
|
|
|
bindEvents() {
|
|
|
|
this.itemClickedWrapper = this.itemClicked.bind(this);
|
|
|
|
this.dropdown.addEventListener('click.dl', this.itemClickedWrapper);
|
|
|
|
}
|
|
|
|
|
|
|
|
unbindEvents() {
|
|
|
|
this.dropdown.removeEventListener('click.dl', this.itemClickedWrapper);
|
|
|
|
}
|
|
|
|
|
|
|
|
getCurrentHook() {
|
|
|
|
return this.droplab.hooks.filter(h => h.id === this.hookId)[0] || null;
|
|
|
|
}
|
|
|
|
|
|
|
|
itemClicked(e, getValueFunction) {
|
|
|
|
const { selected } = e.detail;
|
|
|
|
if (selected.tagName === 'LI' && selected.innerHTML) {
|
2020-03-13 15:44:24 +05:30
|
|
|
const {
|
|
|
|
lastVisualToken: visualToken,
|
|
|
|
} = FilteredSearchVisualTokens.getLastVisualTokenBeforeInput();
|
|
|
|
const { tokenOperator } = DropdownUtils.getVisualTokenValues(visualToken);
|
|
|
|
|
|
|
|
const dataValueSet = DropdownUtils.setDataValueIfSelected(
|
|
|
|
this.filter,
|
|
|
|
tokenOperator,
|
|
|
|
selected,
|
|
|
|
);
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
if (!dataValueSet) {
|
|
|
|
const value = getValueFunction(selected);
|
2020-03-13 15:44:24 +05:30
|
|
|
FilteredSearchDropdownManager.addWordToInput({
|
|
|
|
tokenName: this.filter,
|
|
|
|
tokenOperator,
|
|
|
|
tokenValue: value,
|
|
|
|
clicked: true,
|
|
|
|
});
|
2017-08-17 22:00:37 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
this.resetFilters();
|
|
|
|
this.dismissDropdown();
|
|
|
|
this.dispatchInputEvent();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
setAsDropdown() {
|
|
|
|
this.input.setAttribute(DATA_DROPDOWN_TRIGGER, `#${this.dropdown.id}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
setOffset(offset = 0) {
|
|
|
|
if (window.innerWidth > 480) {
|
|
|
|
this.dropdown.style.left = `${offset}px`;
|
|
|
|
} else {
|
|
|
|
this.dropdown.style.left = '0px';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
renderContent(forceShowList = false) {
|
|
|
|
const currentHook = this.getCurrentHook();
|
2020-05-24 23:13:21 +05:30
|
|
|
|
|
|
|
FilteredSearchDropdown.hideDropdownItemsforNotOperator(currentHook);
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
if (forceShowList && currentHook && currentHook.list.hidden) {
|
|
|
|
currentHook.list.show();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
render(forceRenderContent = false, forceShowList = false) {
|
|
|
|
this.setAsDropdown();
|
|
|
|
|
|
|
|
const currentHook = this.getCurrentHook();
|
|
|
|
const firstTimeInitialized = currentHook === null;
|
|
|
|
|
|
|
|
if (firstTimeInitialized || forceRenderContent) {
|
|
|
|
this.renderContent(forceShowList);
|
|
|
|
} else if (currentHook.list.list.id !== this.dropdown.id) {
|
|
|
|
this.renderContent(forceShowList);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
dismissDropdown() {
|
|
|
|
// Focusing on the input will dismiss dropdown
|
|
|
|
// (default droplab functionality)
|
|
|
|
this.input.focus();
|
|
|
|
}
|
|
|
|
|
|
|
|
dispatchInputEvent() {
|
2018-12-13 13:39:08 +05:30
|
|
|
// Propagate input change to FilteredSearchDropdownManager
|
2017-08-17 22:00:37 +05:30
|
|
|
// so that it can determine which dropdowns to open
|
2018-12-13 13:39:08 +05:30
|
|
|
this.input.dispatchEvent(
|
|
|
|
new CustomEvent('input', {
|
|
|
|
bubbles: true,
|
|
|
|
cancelable: true,
|
|
|
|
}),
|
|
|
|
);
|
2017-08-17 22:00:37 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
dispatchFormSubmitEvent() {
|
|
|
|
// dispatchEvent() is necessary as form.submit() does not
|
|
|
|
// trigger event handlers
|
|
|
|
this.input.form.dispatchEvent(new Event('submit'));
|
|
|
|
}
|
|
|
|
|
|
|
|
hideDropdown() {
|
|
|
|
const currentHook = this.getCurrentHook();
|
|
|
|
if (currentHook) {
|
|
|
|
currentHook.list.hide();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
resetFilters() {
|
|
|
|
const hook = this.getCurrentHook();
|
|
|
|
|
|
|
|
if (hook) {
|
|
|
|
const data = hook.list.data || [];
|
2018-03-27 19:54:05 +05:30
|
|
|
|
|
|
|
if (!data) return;
|
|
|
|
|
2018-12-13 13:39:08 +05:30
|
|
|
const results = data.map(o => {
|
2017-08-17 22:00:37 +05:30
|
|
|
const updated = o;
|
|
|
|
updated.droplab_hidden = false;
|
|
|
|
return updated;
|
|
|
|
});
|
|
|
|
hook.list.render(results);
|
|
|
|
}
|
|
|
|
}
|
2020-05-24 23:13:21 +05:30
|
|
|
|
|
|
|
/**
|
|
|
|
* Hide None & Any options from the current dropdown.
|
|
|
|
* Hiding happens only for NOT operator.
|
|
|
|
*/
|
|
|
|
static hideDropdownItemsforNotOperator(currentHook) {
|
|
|
|
const lastOperator = FilteredSearchVisualTokens.getLastTokenOperator();
|
|
|
|
|
|
|
|
if (lastOperator === '!=') {
|
|
|
|
const { list: dropdownEl } = currentHook.list;
|
|
|
|
|
|
|
|
let shouldHideDivider = true;
|
|
|
|
|
|
|
|
// Iterate over all the static dropdown values,
|
|
|
|
// then hide `None` and `Any` items.
|
|
|
|
Array.from(dropdownEl.querySelectorAll('li[data-value]')).forEach(itemEl => {
|
|
|
|
const {
|
|
|
|
dataset: { value },
|
|
|
|
} = itemEl;
|
|
|
|
|
|
|
|
if (value.toLowerCase() === FILTER_TYPE.none || value.toLowerCase() === FILTER_TYPE.any) {
|
|
|
|
itemEl.classList.add('hidden');
|
|
|
|
} else {
|
|
|
|
// If we encountered any element other than None/Any, then
|
|
|
|
// we shouldn't hide the divider
|
|
|
|
shouldHideDivider = false;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (shouldHideDivider) {
|
|
|
|
const divider = dropdownEl.querySelector('li.divider');
|
|
|
|
if (divider) {
|
|
|
|
divider.classList.add('hidden');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-08-17 22:00:37 +05:30
|
|
|
}
|