gitlab-org--gitlab-foss/app/assets/javascripts/filtered_search/filtered_search_dropdown.js

125 lines
3.4 KiB
JavaScript
Raw Normal View History

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;
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
if (selected.tagName === 'LI' && selected.innerHTML) {
2017-01-05 19:56:23 +00:00
const dataValueSet = gl.DropdownUtils.setDataValueIfSelected(this.filter, selected);
if (!dataValueSet) {
const value = getValueFunction(selected);
2017-01-30 22:53:18 +00:00
gl.FilteredSearchDropdownManager.addWordToInput(this.filter, value, true);
}
2016-12-12 15:55:27 +00:00
2017-03-20 23:27:12 +00:00
this.resetFilters();
this.dismissDropdown();
this.dispatchInputEvent();
}
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
}
setOffset(offset = 0) {
if (window.innerWidth > 480) {
this.dropdown.style.left = `${offset}px`;
} else {
this.dropdown.style.left = '0px';
}
}
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-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
this.input.dispatchEvent(new CustomEvent('input', {
bubbles: true,
cancelable: true,
}));
2016-12-12 16:28:22 +00:00
}
dispatchFormSubmitEvent() {
// dispatchEvent() is necessary as form.submit() does not
// trigger event handlers
this.input.form.dispatchEvent(new Event('submit'));
}
hideDropdown() {
2017-02-15 22:13:53 +00:00
const currentHook = this.getCurrentHook();
if (currentHook) {
currentHook.list.hide();
}
}
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-02 21:04:10 +00:00
}
2016-12-13 14:52:43 +00:00
window.gl = window.gl || {};
gl.FilteredSearchDropdown = FilteredSearchDropdown;
})();