Commit efd54f00 authored by Kushal Pandya's avatar Kushal Pandya

Make class initialization configurable

parent 699908ea
...@@ -3,19 +3,15 @@ import DropLab from '~/droplab/drop_lab'; ...@@ -3,19 +3,15 @@ import DropLab from '~/droplab/drop_lab';
import FilteredSearchContainer from './container'; import FilteredSearchContainer from './container';
class FilteredSearchDropdownManager { class FilteredSearchDropdownManager {
constructor(baseEndpoint = '', tokenizer, page, isGroup) { constructor(baseEndpoint = '', tokenizer, page, isGroup, filteredSearchTokenKeys) {
this.container = FilteredSearchContainer.container; this.container = FilteredSearchContainer.container;
this.baseEndpoint = baseEndpoint.replace(/\/$/, ''); this.baseEndpoint = baseEndpoint.replace(/\/$/, '');
this.tokenizer = tokenizer; this.tokenizer = tokenizer;
this.filteredSearchTokenKeys = gl.FilteredSearchTokenKeys; this.filteredSearchTokenKeys = filteredSearchTokenKeys;
this.filteredSearchInput = this.container.querySelector('.filtered-search'); this.filteredSearchInput = this.container.querySelector('.filtered-search');
this.page = page; this.page = page;
this.groupsOnly = page === 'boards' && isGroup; this.groupsOnly = page === 'boards' && isGroup;
if (this.page === 'issues' || this.page === 'boards') {
this.filteredSearchTokenKeys = gl.FilteredSearchTokenKeysIssuesEE;
}
this.setupMapping(); this.setupMapping();
this.cleanupWrapper = this.cleanup.bind(this); this.cleanupWrapper = this.cleanup.bind(this);
...@@ -34,12 +30,30 @@ class FilteredSearchDropdownManager { ...@@ -34,12 +30,30 @@ class FilteredSearchDropdownManager {
} }
setupMapping() { setupMapping() {
this.mapping = { const supportedTokens = this.filteredSearchTokenKeys.getKeys();
const allowedMappings = {
hint: {
reference: null,
gl: 'DropdownHint',
element: this.container.querySelector('#js-dropdown-hint'),
},
};
const availableMappings = {
author: { author: {
reference: null, reference: null,
gl: 'DropdownUser', gl: 'DropdownUser',
element: this.container.querySelector('#js-dropdown-author'), element: this.container.querySelector('#js-dropdown-author'),
}, },
label: {
reference: null,
gl: 'DropdownNonUser',
extraArguments: {
endpoint: `${this.baseEndpoint}/labels.json${this.groupsOnly ? '?only_group_labels=true' : ''}`,
symbol: '~',
preprocessing: gl.DropdownUtils.duplicateLabelPreprocessing,
},
element: this.container.querySelector('#js-dropdown-label'),
},
assignee: { assignee: {
reference: null, reference: null,
gl: 'DropdownUser', gl: 'DropdownUser',
...@@ -54,35 +68,25 @@ class FilteredSearchDropdownManager { ...@@ -54,35 +68,25 @@ class FilteredSearchDropdownManager {
}, },
element: this.container.querySelector('#js-dropdown-milestone'), element: this.container.querySelector('#js-dropdown-milestone'),
}, },
label: {
reference: null,
gl: 'DropdownNonUser',
extraArguments: {
endpoint: `${this.baseEndpoint}/labels.json${this.groupsOnly ? '?only_group_labels=true' : ''}`,
symbol: '~',
preprocessing: gl.DropdownUtils.duplicateLabelPreprocessing,
},
element: this.container.querySelector('#js-dropdown-label'),
},
'my-reaction': { 'my-reaction': {
reference: null, reference: null,
gl: 'DropdownEmoji', gl: 'DropdownEmoji',
element: this.container.querySelector('#js-dropdown-my-reaction'), element: this.container.querySelector('#js-dropdown-my-reaction'),
}, },
hint: { weight: {
reference: null, reference: null,
gl: 'DropdownHint', gl: 'DropdownNonUser',
element: this.container.querySelector('#js-dropdown-hint'), element: this.container.querySelector('#js-dropdown-weight'),
}, },
}; };
if (this.page === 'issues' || this.page === 'boards') { supportedTokens.forEach((type) => {
this.mapping.weight = { if (availableMappings[type]) {
reference: null, allowedMappings[type] = availableMappings[type];
gl: 'DropdownNonUser', }
element: this.container.querySelector('#js-dropdown-weight'), });
};
} this.mapping = allowedMappings;
} }
static addWordToInput(tokenName, tokenValue = '', clicked = false) { static addWordToInput(tokenName, tokenValue = '', clicked = false) {
......
...@@ -3,28 +3,36 @@ import { visitUrl } from '../lib/utils/url_utility'; ...@@ -3,28 +3,36 @@ import { visitUrl } from '../lib/utils/url_utility';
import Flash from '../flash'; import Flash from '../flash';
import FilteredSearchContainer from './container'; import FilteredSearchContainer from './container';
import RecentSearchesRoot from './recent_searches_root'; import RecentSearchesRoot from './recent_searches_root';
import FilteredSearchTokenKeys from './filtered_search_token_keys';
import RecentSearchesStore from './stores/recent_searches_store'; import RecentSearchesStore from './stores/recent_searches_store';
import RecentSearchesService from './services/recent_searches_service'; import RecentSearchesService from './services/recent_searches_service';
import eventHub from './event_hub'; import eventHub from './event_hub';
import { addClassIfElementExists } from '../lib/utils/dom_utils'; import { addClassIfElementExists } from '../lib/utils/dom_utils';
class FilteredSearchManager { class FilteredSearchManager {
constructor(page) { constructor({
page,
filteredSearchTokenKeys = FilteredSearchTokenKeys,
stateFiltersSelector = '.issues-state-filters',
}) {
this.isGroup = false;
this.states = ['opened', 'closed', 'merged', 'all'];
this.page = page; this.page = page;
this.container = FilteredSearchContainer.container; this.container = FilteredSearchContainer.container;
this.filteredSearchInput = this.container.querySelector('.filtered-search'); this.filteredSearchInput = this.container.querySelector('.filtered-search');
this.filteredSearchInputForm = this.filteredSearchInput.form; this.filteredSearchInputForm = this.filteredSearchInput.form;
this.clearSearchButton = this.container.querySelector('.clear-search'); this.clearSearchButton = this.container.querySelector('.clear-search');
this.tokensContainer = this.container.querySelector('.tokens-container'); this.tokensContainer = this.container.querySelector('.tokens-container');
this.filteredSearchTokenKeys = gl.FilteredSearchTokenKeys; this.filteredSearchTokenKeys = filteredSearchTokenKeys;
this.stateFiltersSelector = stateFiltersSelector;
gl.FilteredSearchTokenKeysIssuesEE.init({ this.recentsStorageKeyNames = {
multipleAssignees: this.filteredSearchInput.dataset.multipleAssignees, issues: 'issue-recent-searches',
}); merge_requests: 'merge-request-recent-searches',
};
if (this.page === 'issues' || this.page === 'boards') { // EE specific setup
this.filteredSearchTokenKeys = gl.FilteredSearchTokenKeysIssuesEE; this.initEE();
}
this.recentSearchesStore = new RecentSearchesStore({ this.recentSearchesStore = new RecentSearchesStore({
isLocalStorageAvailable: RecentSearchesService.isAvailable(), isLocalStorageAvailable: RecentSearchesService.isAvailable(),
...@@ -33,14 +41,30 @@ class FilteredSearchManager { ...@@ -33,14 +41,30 @@ class FilteredSearchManager {
this.searchHistoryDropdownElement = document.querySelector('.js-filtered-search-history-dropdown'); this.searchHistoryDropdownElement = document.querySelector('.js-filtered-search-history-dropdown');
const fullPath = this.searchHistoryDropdownElement ? const fullPath = this.searchHistoryDropdownElement ?
this.searchHistoryDropdownElement.dataset.fullPath : 'project'; this.searchHistoryDropdownElement.dataset.fullPath : 'project';
let recentSearchesPagePrefix = 'issue-recent-searches'; const recentSearchesKey = `${fullPath}-${this.recentsStorageKeyNames[this.page]}`;
if (this.page === 'merge_requests') {
recentSearchesPagePrefix = 'merge-request-recent-searches';
}
const recentSearchesKey = `${fullPath}-${recentSearchesPagePrefix}`;
this.recentSearchesService = new RecentSearchesService(recentSearchesKey); this.recentSearchesService = new RecentSearchesService(recentSearchesKey);
} }
/**
* Do EE specific initializations
*/
initEE() {
// Setup token keys for multiple-assignees support
if (typeof this.filteredSearchTokenKeys.init === 'function') {
this.filteredSearchTokenKeys.init({
multipleAssignees: this.filteredSearchInput.dataset.multipleAssignees,
});
}
// Add localStorage key name for Epics recent searches
this.recentsStorageKeyNames.epics = 'epics-recent-searches';
// Update `isGroup` from DOM info
if (this.filteredSearchInput) {
this.isGroup = !!this.filteredSearchInput.getAttribute('data-group-id');
}
}
setup() { setup() {
// Fetch recent searches from localStorage // Fetch recent searches from localStorage
this.fetchingRecentSearchesPromise = this.recentSearchesService.fetch() this.fetchingRecentSearchesPromise = this.recentSearchesService.fetch()
...@@ -70,7 +94,8 @@ class FilteredSearchManager { ...@@ -70,7 +94,8 @@ class FilteredSearchManager {
this.filteredSearchInput.getAttribute('data-base-endpoint') || '', this.filteredSearchInput.getAttribute('data-base-endpoint') || '',
this.tokenizer, this.tokenizer,
this.page, this.page,
Boolean(this.filteredSearchInput.getAttribute('data-group-id')), this.isGroup,
this.filteredSearchTokenKeys,
); );
this.recentSearchesRoot = new RecentSearchesRoot( this.recentSearchesRoot = new RecentSearchesRoot(
...@@ -99,40 +124,33 @@ class FilteredSearchManager { ...@@ -99,40 +124,33 @@ class FilteredSearchManager {
} }
bindStateEvents() { bindStateEvents() {
this.stateFilters = document.querySelector('.container-fluid .issues-state-filters'); this.stateFilters = document.querySelector(`.container-fluid ${this.stateFiltersSelector}`);
if (this.stateFilters) { if (this.stateFilters) {
this.searchStateWrapper = this.searchState.bind(this); this.searchStateWrapper = this.searchState.bind(this);
this.stateFilters.querySelector('[data-state="opened"]') this.applyToStateFilters((filterEl) => {
.addEventListener('click', this.searchStateWrapper); filterEl.addEventListener('click', this.searchStateWrapper);
this.stateFilters.querySelector('[data-state="closed"]') });
.addEventListener('click', this.searchStateWrapper);
this.stateFilters.querySelector('[data-state="all"]')
.addEventListener('click', this.searchStateWrapper);
this.mergedState = this.stateFilters.querySelector('[data-state="merged"]');
if (this.mergedState) {
this.mergedState.addEventListener('click', this.searchStateWrapper);
}
} }
} }
unbindStateEvents() { unbindStateEvents() {
if (this.stateFilters) { if (this.stateFilters) {
this.stateFilters.querySelector('[data-state="opened"]') this.applyToStateFilters((filterEl) => {
.removeEventListener('click', this.searchStateWrapper); filterEl.removeEventListener('click', this.searchStateWrapper);
this.stateFilters.querySelector('[data-state="closed"]') });
.removeEventListener('click', this.searchStateWrapper);
this.stateFilters.querySelector('[data-state="all"]')
.removeEventListener('click', this.searchStateWrapper);
if (this.mergedState) {
this.mergedState.removeEventListener('click', this.searchStateWrapper);
}
} }
} }
applyToStateFilters(callback) {
this.stateFilters.querySelectorAll('a[data-state]').forEach((filterEl) => {
if (this.states.indexOf(filterEl.dataset.state) > -1) {
callback(filterEl);
}
});
}
bindEvents() { bindEvents() {
this.handleFormSubmit = this.handleFormSubmit.bind(this); this.handleFormSubmit = this.handleFormSubmit.bind(this);
this.setDropdownWrapper = this.dropdownManager.setDropdown.bind(this.dropdownManager); this.setDropdownWrapper = this.dropdownManager.setDropdown.bind(this.dropdownManager);
......
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