Commit 3e89960d authored by Clement Ho's avatar Clement Ho Committed by Jacob Schatz

Refactor filtered search dropdown parameters

parent 01e02988
...@@ -2,8 +2,9 @@ import Filter from '~/droplab/plugins/filter'; ...@@ -2,8 +2,9 @@ import Filter from '~/droplab/plugins/filter';
import './filtered_search_dropdown'; import './filtered_search_dropdown';
class DropdownHint extends gl.FilteredSearchDropdown { class DropdownHint extends gl.FilteredSearchDropdown {
constructor(droplab, dropdown, input, tokenKeys, filter) { constructor(options = {}) {
super(droplab, dropdown, input, filter); const { input, tokenKeys } = options;
super(options);
this.config = { this.config = {
Filter: { Filter: {
template: 'hint', template: 'hint',
......
...@@ -5,8 +5,9 @@ import Filter from '~/droplab/plugins/filter'; ...@@ -5,8 +5,9 @@ import Filter from '~/droplab/plugins/filter';
import './filtered_search_dropdown'; import './filtered_search_dropdown';
class DropdownNonUser extends gl.FilteredSearchDropdown { class DropdownNonUser extends gl.FilteredSearchDropdown {
constructor(droplab, dropdown, input, tokenKeys, filter, endpoint, symbol) { constructor(options = {}) {
super(droplab, dropdown, input, filter); const { input, endpoint, symbol } = options;
super(options);
this.symbol = symbol; this.symbol = symbol;
this.config = { this.config = {
Ajax: { Ajax: {
......
...@@ -5,8 +5,9 @@ import './filtered_search_dropdown'; ...@@ -5,8 +5,9 @@ import './filtered_search_dropdown';
import { addClassIfElementExists } from '../lib/utils/dom_utils'; import { addClassIfElementExists } from '../lib/utils/dom_utils';
class DropdownUser extends gl.FilteredSearchDropdown { class DropdownUser extends gl.FilteredSearchDropdown {
constructor(droplab, dropdown, input, tokenKeys, filter) { constructor(options = {}) {
super(droplab, dropdown, input, filter); const { tokenKeys } = options;
super(options);
this.config = { this.config = {
AjaxFilter: { AjaxFilter: {
endpoint: `${gon.relative_url_root || ''}/autocomplete/users.json`, endpoint: `${gon.relative_url_root || ''}/autocomplete/users.json`,
......
const DATA_DROPDOWN_TRIGGER = 'data-dropdown-trigger'; const DATA_DROPDOWN_TRIGGER = 'data-dropdown-trigger';
class FilteredSearchDropdown { class FilteredSearchDropdown {
constructor(droplab, dropdown, input, filter) { constructor({ droplab, dropdown, input, filter }) {
this.droplab = droplab; this.droplab = droplab;
this.hookId = input && input.id; this.hookId = input && input.id;
this.input = input; this.input = input;
......
...@@ -42,13 +42,19 @@ class FilteredSearchDropdownManager { ...@@ -42,13 +42,19 @@ class FilteredSearchDropdownManager {
milestone: { milestone: {
reference: null, reference: null,
gl: 'DropdownNonUser', gl: 'DropdownNonUser',
extraArguments: [`${this.baseEndpoint}/milestones.json`, '%'], extraArguments: {
endpoint: `${this.baseEndpoint}/milestones.json`,
symbol: '%',
},
element: this.container.querySelector('#js-dropdown-milestone'), element: this.container.querySelector('#js-dropdown-milestone'),
}, },
label: { label: {
reference: null, reference: null,
gl: 'DropdownNonUser', gl: 'DropdownNonUser',
extraArguments: [`${this.baseEndpoint}/labels.json`, '~'], extraArguments: {
endpoint: `${this.baseEndpoint}/labels.json`,
symbol: '~',
},
element: this.container.querySelector('#js-dropdown-label'), element: this.container.querySelector('#js-dropdown-label'),
}, },
hint: { hint: {
...@@ -97,13 +103,19 @@ class FilteredSearchDropdownManager { ...@@ -97,13 +103,19 @@ class FilteredSearchDropdownManager {
let forceShowList = false; let forceShowList = false;
if (!mappingKey.reference) { if (!mappingKey.reference) {
const dl = this.droplab; const defaultArguments = {
const defaultArguments = droplab: this.droplab,
[null, dl, element, this.filteredSearchInput, this.filteredSearchTokenKeys, key]; dropdown: element,
const glArguments = defaultArguments.concat(mappingKey.extraArguments || []); input: this.filteredSearchInput,
tokenKeys: this.filteredSearchTokenKeys,
filter: key,
};
const extraArguments = mappingKey.extraArguments || {};
const glArguments = Object.assign({}, defaultArguments, extraArguments);
// Passing glArguments to `new gl[glClass](<arguments>)` // Passing glArguments to `new gl[glClass](<arguments>)`
mappingKey.reference = new (Function.prototype.bind.apply(gl[glClass], glArguments))(); mappingKey.reference =
new (Function.prototype.bind.apply(gl[glClass], [null, glArguments]))();
} }
if (firstLoad) { if (firstLoad) {
......
...@@ -12,7 +12,9 @@ describe('Dropdown User', () => { ...@@ -12,7 +12,9 @@ describe('Dropdown User', () => {
spyOn(gl.DropdownUser.prototype, 'getProjectId').and.callFake(() => {}); spyOn(gl.DropdownUser.prototype, 'getProjectId').and.callFake(() => {});
spyOn(gl.DropdownUtils, 'getSearchInput').and.callFake(() => {}); spyOn(gl.DropdownUtils, 'getSearchInput').and.callFake(() => {});
dropdownUser = new gl.DropdownUser(null, null, null, gl.FilteredSearchTokenKeys); dropdownUser = new gl.DropdownUser({
tokenKeys: gl.FilteredSearchTokenKeys,
});
}); });
it('should not return the double quote found in value', () => { it('should not return the double quote found in value', () => {
...@@ -78,7 +80,10 @@ describe('Dropdown User', () => { ...@@ -78,7 +80,10 @@ describe('Dropdown User', () => {
loadFixtures(fixtureTemplate); loadFixtures(fixtureTemplate);
authorFilterDropdownElement = document.querySelector('#js-dropdown-author'); authorFilterDropdownElement = document.querySelector('#js-dropdown-author');
const dummyInput = document.createElement('div'); const dummyInput = document.createElement('div');
dropdown = new gl.DropdownUser(null, authorFilterDropdownElement, dummyInput); dropdown = new gl.DropdownUser({
dropdown: authorFilterDropdownElement,
input: dummyInput,
});
}); });
const findCurrentUserElement = () => authorFilterDropdownElement.querySelector('.js-current-user'); const findCurrentUserElement = () => authorFilterDropdownElement.querySelector('.js-current-user');
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment