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