gitlab-org--gitlab-foss/app/assets/javascripts/filtered_search/filtered_search_manager.js.es6

160 lines
5.7 KiB
JavaScript
Raw Normal View History

2016-11-09 20:31:58 +00:00
/* eslint-disable no-param-reassign */
2016-11-04 21:27:11 +00:00
((global) => {
class FilteredSearchManager {
constructor() {
2016-11-30 18:30:52 +00:00
this.tokenizer = gl.FilteredSearchTokenizer;
2016-12-09 17:44:09 +00:00
this.filteredSearchInput = document.querySelector('.filtered-search');
this.clearSearchButton = document.querySelector('.clear-search');
2016-12-12 15:21:38 +00:00
this.dropdownManager = new gl.FilteredSearchDropdownManager();
2016-12-09 17:44:09 +00:00
2016-11-04 21:27:11 +00:00
this.bindEvents();
2016-12-12 22:37:49 +00:00
this.loadSearchParamsFromURL();
2016-12-12 15:21:38 +00:00
this.dropdownManager.setDropdown();
2016-12-09 17:44:09 +00:00
this.cleanupWrapper = this.cleanup.bind(this);
document.addEventListener('page:fetch', this.cleanupWrapper);
}
cleanup() {
this.unbindEvents();
2016-12-09 17:44:09 +00:00
document.removeEventListener('page:fetch', this.cleanupWrapper);
}
2016-11-04 21:27:11 +00:00
bindEvents() {
2016-12-12 15:21:38 +00:00
this.setDropdownWrapper = this.dropdownManager.setDropdown.bind(this.dropdownManager);
2016-12-12 22:37:49 +00:00
this.toggleClearSearchButtonWrapper = this.toggleClearSearchButton.bind(this);
this.checkForEnterWrapper = this.checkForEnter.bind(this);
this.clearSearchWrapper = this.clearSearch.bind(this);
this.checkForBackspaceWrapper = this.checkForBackspace.bind(this);
this.filteredSearchInput.addEventListener('input', this.setDropdownWrapper);
2016-12-12 22:37:49 +00:00
this.filteredSearchInput.addEventListener('input', this.toggleClearSearchButtonWrapper);
this.filteredSearchInput.addEventListener('keydown', this.checkForEnterWrapper);
this.filteredSearchInput.addEventListener('keyup', this.checkForBackspaceWrapper);
this.clearSearchButton.addEventListener('click', this.clearSearchWrapper);
}
unbindEvents() {
this.filteredSearchInput.removeEventListener('input', this.setDropdownWrapper);
2016-12-12 22:37:49 +00:00
this.filteredSearchInput.removeEventListener('input', this.toggleClearSearchButtonWrapper);
this.filteredSearchInput.removeEventListener('keydown', this.checkForEnterWrapper);
this.filteredSearchInput.removeEventListener('keyup', this.checkForBackspaceWrapper);
this.clearSearchButton.removeEventListener('click', this.clearSearchWrapper);
2016-12-07 18:55:03 +00:00
}
checkForBackspace(e) {
2016-12-12 22:27:10 +00:00
// 8 = Backspace Key
// 46 = Delete Key
if (e.keyCode === 8 || e.keyCode === 46) {
// Reposition dropdown so that it is aligned with cursor
2016-12-12 15:21:38 +00:00
this.dropdownManager.updateCurrentDropdownOffset();
}
}
2016-11-14 16:37:55 +00:00
checkForEnter(e) {
if (e.keyCode === 13) {
e.preventDefault();
// Prevent droplab from opening dropdown
2016-12-12 15:21:38 +00:00
this.dropdownManager.destroyDroplab();
2016-11-04 21:27:11 +00:00
this.search();
}
}
2016-12-12 22:37:49 +00:00
toggleClearSearchButton(e) {
if (e.target.value) {
this.clearSearchButton.classList.remove('hidden');
} else {
this.clearSearchButton.classList.add('hidden');
}
}
clearSearch(e) {
e.preventDefault();
this.filteredSearchInput.value = '';
this.clearSearchButton.classList.add('hidden');
this.dropdownManager.resetDropdowns();
}
loadSearchParamsFromURL() {
2016-12-13 03:24:55 +00:00
const params = gl.utils.getUrlParamsArray();
let inputValues = [];
2016-12-12 22:37:49 +00:00
params.forEach((p) => {
const split = p.split('=');
2016-12-13 04:15:31 +00:00
const keyParam = decodeURIComponent(split[0]);
2016-12-12 22:37:49 +00:00
const value = split[1];
2016-12-13 04:15:31 +00:00
// Check if it matches edge conditions listed in gl.FilteredSearchTokenKeys
const condition = gl.FilteredSearchTokenKeys.searchByConditionUrl(p);
2016-12-12 22:37:49 +00:00
2016-12-13 04:15:31 +00:00
if (condition) {
inputValues.push(`${condition.tokenKey}:${condition.value}`);
2016-12-12 22:37:49 +00:00
} else {
// Sanitize value since URL converts spaces into +
// Replace before decode so that we know what was originally + versus the encoded +
const sanitizedValue = value ? decodeURIComponent(value.replace(/\+/g, ' ')) : value;
2016-12-13 04:15:31 +00:00
const match = gl.FilteredSearchTokenKeys.searchByKeyParam(keyParam);
2016-12-12 22:37:49 +00:00
if (match) {
2016-12-13 04:15:31 +00:00
const sanitizedKey = keyParam.slice(0, keyParam.indexOf('_'));
2016-12-12 22:37:49 +00:00
const symbol = match.symbol;
2016-12-13 04:15:31 +00:00
let quotationsToUse = '';
2016-12-12 22:37:49 +00:00
2016-12-13 04:15:31 +00:00
if (sanitizedValue.indexOf(' ') !== -1) {
2016-12-12 22:37:49 +00:00
// Prefer ", but use ' if required
quotationsToUse = sanitizedValue.indexOf('"') === -1 ? '"' : '\'';
}
2016-12-13 04:15:31 +00:00
inputValues.push(`${sanitizedKey}:${symbol}${quotationsToUse}${sanitizedValue}${quotationsToUse}`);
} else if (!match && keyParam === 'search') {
inputValues.push(sanitizedValue);
2016-12-12 22:37:49 +00:00
}
}
});
// Trim the last space value
this.filteredSearchInput.value = inputValues.join(' ');
2016-12-12 22:37:49 +00:00
if (inputValues.length > 0) {
2016-12-12 22:37:49 +00:00
this.clearSearchButton.classList.remove('hidden');
}
}
2016-11-04 21:27:11 +00:00
search() {
let paths = [];
2016-12-12 16:28:22 +00:00
const { tokens, searchToken } = this.tokenizer.processTokens(this.filteredSearchInput.value);
2016-12-13 03:15:50 +00:00
const currentState = gl.utils.getParameterByName('state') || 'opened';
paths.push(`state=${currentState}`);
2016-12-13 03:15:50 +00:00
2016-11-09 23:07:30 +00:00
tokens.forEach((token) => {
2016-12-13 04:15:31 +00:00
const condition = gl.FilteredSearchTokenKeys.searchByConditionKeyValue(token.key, token.value.toLowerCase());
const { param } = gl.FilteredSearchTokenKeys.searchByKey(token.key);
2016-11-14 19:22:32 +00:00
let tokenPath = '';
2016-12-13 04:15:31 +00:00
if (token.wildcard && condition) {
tokenPath = condition.url;
2016-11-14 19:22:32 +00:00
} else if (!token.wildcard) {
// Remove the wildcard token
2016-12-13 04:15:31 +00:00
tokenPath = `${token.key}_${param}=${encodeURIComponent(token.value.slice(1))}`;
2016-11-14 19:22:32 +00:00
} else {
2016-12-13 04:15:31 +00:00
tokenPath = `${token.key}_${param}=${encodeURIComponent(token.value)}`;
2016-11-14 19:22:32 +00:00
}
paths.push(tokenPath);
2016-11-04 21:27:11 +00:00
});
2016-11-09 23:07:30 +00:00
if (searchToken) {
paths.push(`search=${encodeURIComponent(searchToken)}`);
2016-11-04 21:27:11 +00:00
}
Turbolinks.visit(`?scope=all&utf8=✓&${paths.join('&')}`);
2016-11-04 21:27:11 +00:00
}
}
global.FilteredSearchManager = FilteredSearchManager;
2016-11-08 17:35:28 +00:00
})(window.gl || (window.gl = {}));