2017-04-17 07:01:11 +00:00
|
|
|
const DATA_DROPDOWN_TRIGGER = 'data-dropdown-trigger';
|
|
|
|
|
|
|
|
class FilteredSearchDropdown {
|
|
|
|
constructor(droplab, dropdown, input, filter) {
|
|
|
|
this.droplab = droplab;
|
|
|
|
this.hookId = input && input.id;
|
|
|
|
this.input = input;
|
|
|
|
this.filter = filter;
|
|
|
|
this.dropdown = dropdown;
|
|
|
|
this.loadingTemplate = `<div class="filter-dropdown-loading">
|
|
|
|
<i class="fa fa-spinner fa-spin"></i>
|
|
|
|
</div>`;
|
|
|
|
this.bindEvents();
|
|
|
|
}
|
2016-12-02 21:04:10 +00:00
|
|
|
|
2017-04-17 07:01:11 +00:00
|
|
|
bindEvents() {
|
|
|
|
this.itemClickedWrapper = this.itemClicked.bind(this);
|
|
|
|
this.dropdown.addEventListener('click.dl', this.itemClickedWrapper);
|
|
|
|
}
|
2016-12-09 17:44:09 +00:00
|
|
|
|
2017-04-17 07:01:11 +00:00
|
|
|
unbindEvents() {
|
|
|
|
this.dropdown.removeEventListener('click.dl', this.itemClickedWrapper);
|
|
|
|
}
|
2016-12-02 21:04:10 +00:00
|
|
|
|
2017-04-17 07:01:11 +00:00
|
|
|
getCurrentHook() {
|
|
|
|
return this.droplab.hooks.filter(h => h.id === this.hookId)[0] || null;
|
|
|
|
}
|
2016-12-12 15:55:27 +00:00
|
|
|
|
2017-04-17 07:01:11 +00:00
|
|
|
itemClicked(e, getValueFunction) {
|
|
|
|
const { selected } = e.detail;
|
2016-12-16 05:20:31 +00:00
|
|
|
|
2017-04-17 07:01:11 +00:00
|
|
|
if (selected.tagName === 'LI' && selected.innerHTML) {
|
|
|
|
const dataValueSet = gl.DropdownUtils.setDataValueIfSelected(this.filter, selected);
|
2016-12-12 15:55:27 +00:00
|
|
|
|
2017-04-17 07:01:11 +00:00
|
|
|
if (!dataValueSet) {
|
|
|
|
const value = getValueFunction(selected);
|
|
|
|
gl.FilteredSearchDropdownManager.addWordToInput(this.filter, value, true);
|
2016-12-16 05:20:31 +00:00
|
|
|
}
|
2016-12-02 21:04:10 +00:00
|
|
|
|
2017-04-17 07:01:11 +00:00
|
|
|
this.resetFilters();
|
|
|
|
this.dismissDropdown();
|
|
|
|
this.dispatchInputEvent();
|
2016-12-02 21:04:10 +00:00
|
|
|
}
|
2017-04-17 07:01:11 +00:00
|
|
|
}
|
2016-12-02 21:04:10 +00:00
|
|
|
|
2017-04-17 07:01:11 +00:00
|
|
|
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';
|
2016-12-05 21:12:34 +00:00
|
|
|
}
|
2017-04-17 07:01:11 +00:00
|
|
|
}
|
2016-12-05 21:12:34 +00:00
|
|
|
|
2017-04-17 07:01:11 +00:00
|
|
|
renderContent(forceShowList = false) {
|
|
|
|
const currentHook = this.getCurrentHook();
|
|
|
|
if (forceShowList && currentHook && currentHook.list.hidden) {
|
|
|
|
currentHook.list.show();
|
2016-12-09 19:15:09 +00:00
|
|
|
}
|
2017-04-17 07:01:11 +00:00
|
|
|
}
|
2016-12-02 21:04:10 +00:00
|
|
|
|
2017-04-17 07:01:11 +00:00
|
|
|
render(forceRenderContent = false, forceShowList = false) {
|
|
|
|
this.setAsDropdown();
|
2016-12-02 21:04:10 +00:00
|
|
|
|
2017-04-17 07:01:11 +00:00
|
|
|
const currentHook = this.getCurrentHook();
|
|
|
|
const firstTimeInitialized = currentHook === null;
|
2016-12-07 18:55:03 +00:00
|
|
|
|
2017-04-17 07:01:11 +00:00
|
|
|
if (firstTimeInitialized || forceRenderContent) {
|
|
|
|
this.renderContent(forceShowList);
|
|
|
|
} else if (currentHook.list.list.id !== this.dropdown.id) {
|
|
|
|
this.renderContent(forceShowList);
|
2016-12-02 21:04:10 +00:00
|
|
|
}
|
2017-04-17 07:01:11 +00:00
|
|
|
}
|
2016-12-08 21:36:54 +00:00
|
|
|
|
2017-04-17 07:01:11 +00:00
|
|
|
dismissDropdown() {
|
|
|
|
// Focusing on the input will dismiss dropdown
|
|
|
|
// (default droplab functionality)
|
|
|
|
this.input.focus();
|
|
|
|
}
|
2016-12-12 16:28:22 +00:00
|
|
|
|
2017-04-17 07:01:11 +00:00
|
|
|
dispatchInputEvent() {
|
|
|
|
// Propogate input change to FilteredSearchDropdownManager
|
|
|
|
// so that it can determine which dropdowns to open
|
|
|
|
this.input.dispatchEvent(new CustomEvent('input', {
|
|
|
|
bubbles: true,
|
|
|
|
cancelable: true,
|
|
|
|
}));
|
|
|
|
}
|
2016-12-12 16:28:22 +00:00
|
|
|
|
2017-04-17 07:01:11 +00:00
|
|
|
dispatchFormSubmitEvent() {
|
|
|
|
// dispatchEvent() is necessary as form.submit() does not
|
|
|
|
// trigger event handlers
|
|
|
|
this.input.form.dispatchEvent(new Event('submit'));
|
|
|
|
}
|
2017-01-21 07:44:36 +00:00
|
|
|
|
2017-04-17 07:01:11 +00:00
|
|
|
hideDropdown() {
|
|
|
|
const currentHook = this.getCurrentHook();
|
|
|
|
if (currentHook) {
|
|
|
|
currentHook.list.hide();
|
2016-12-09 21:47:56 +00:00
|
|
|
}
|
2017-04-17 07:01:11 +00:00
|
|
|
}
|
2016-12-09 22:02:43 +00:00
|
|
|
|
2017-04-17 07:01:11 +00:00
|
|
|
resetFilters() {
|
|
|
|
const hook = this.getCurrentHook();
|
|
|
|
|
|
|
|
if (hook) {
|
|
|
|
const data = hook.list.data || [];
|
|
|
|
const results = data.map((o) => {
|
|
|
|
const updated = o;
|
|
|
|
updated.droplab_hidden = false;
|
|
|
|
return updated;
|
|
|
|
});
|
|
|
|
hook.list.render(results);
|
2016-12-09 22:02:43 +00:00
|
|
|
}
|
2016-12-02 21:04:10 +00:00
|
|
|
}
|
2017-04-17 07:01:11 +00:00
|
|
|
}
|
2016-12-02 21:04:10 +00:00
|
|
|
|
2017-04-17 07:01:11 +00:00
|
|
|
window.gl = window.gl || {};
|
|
|
|
gl.FilteredSearchDropdown = FilteredSearchDropdown;
|