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

106 lines
2.9 KiB
JavaScript
Raw Normal View History

2016-12-02 21:04:10 +00:00
/* eslint-disable no-param-reassign */
((global) => {
const DATA_DROPDOWN_TRIGGER = 'data-dropdown-trigger';
class FilteredSearchDropdown {
2016-12-08 21:36:54 +00:00
constructor(droplab, dropdown, input) {
this.droplab = droplab;
2016-12-02 21:04:10 +00:00
this.hookId = 'filtered-search';
this.input = input;
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;
const dataValueSet = this.setDataValueIfSelected(selected);
2016-12-12 15:55:27 +00:00
if (!dataValueSet) {
2016-12-12 22:25:31 +00:00
const value = getValueFunction(selected);
2016-12-12 16:55:55 +00:00
gl.FilteredSearchDropdownManager.addWordToInput(value);
2016-12-12 15:55:27 +00:00
}
this.dismissDropdown();
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) {
this.dropdown.style.left = `${offset}px`;
}
setDataValueIfSelected(selected) {
const dataValue = selected.getAttribute('data-value');
if (dataValue) {
2016-12-12 15:21:38 +00:00
gl.FilteredSearchDropdownManager.addWordToInput(dataValue);
}
2016-12-12 16:28:22 +00:00
// Return boolean based on whether it was set
return dataValue !== null;
}
2016-12-12 16:28:22 +00:00
renderContent(forceShowList = false) {
if (forceShowList && this.getCurrentHook().list.hidden) {
this.getCurrentHook().list.show();
}
}
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-12 16:28:22 +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 Event('input'));
}
hideDropdown() {
this.getCurrentHook().list.hide();
}
resetFilters() {
const hook = this.getCurrentHook();
const data = hook.list.data;
2016-12-12 16:28:22 +00:00
const results = data.map(o => o.droplab_hidden = false);
hook.list.render(results);
}
2016-12-02 21:04:10 +00:00
}
global.FilteredSearchDropdown = FilteredSearchDropdown;
})(window.gl || (window.gl = {}));