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

195 lines
6.9 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) => {
2016-12-12 16:28:22 +00:00
// TODO: Encapsulate inside class?
2016-11-14 16:37:55 +00:00
function toggleClearSearchButton(e) {
2016-11-09 23:07:30 +00:00
const clearSearchButton = document.querySelector('.clear-search');
2016-11-09 20:31:58 +00:00
2016-11-14 23:45:26 +00:00
if (e.target.value) {
clearSearchButton.classList.remove('hidden');
} else {
clearSearchButton.classList.add('hidden');
}
2016-11-09 20:31:58 +00:00
}
function loadSearchParamsFromURL() {
// We can trust that each param has one & since values containing & will be encoded
// Remove the first character of search as it is always ?
const params = window.location.search.slice(1).split('&');
let inputValue = '';
params.forEach((p) => {
const split = p.split('=');
const key = decodeURIComponent(split[0]);
const value = split[1];
2016-11-30 18:30:52 +00:00
// Check if it matches edge conditions listed in gl.FilteredSearchTokenKeys.get()
2016-11-14 19:22:32 +00:00
let conditionIndex = 0;
2016-11-30 18:30:52 +00:00
const validCondition = gl.FilteredSearchTokenKeys.get()
2016-11-14 23:45:26 +00:00
.filter(v => v.conditions && v.conditions.filter((c, index) => {
2016-12-12 17:06:45 +00:00
// Return TokenKeys that have conditions that much the URL
2016-11-14 23:45:26 +00:00
if (c.url === p) {
conditionIndex = index;
}
return c.url === p;
})[0])[0];
2016-11-14 19:22:32 +00:00
if (validCondition) {
2016-12-12 16:28:22 +00:00
// Parse params based on rules provided in the conditions key of gl.FilteredSearchTokenKeys.get()
2016-11-14 19:22:32 +00:00
inputValue += `${validCondition.key}:${validCondition.conditions[conditionIndex].keyword}`;
inputValue += ' ';
2016-11-14 19:22:32 +00:00
} else {
// Sanitize value since URL converts spaces into +
// Replace before decode so that we know what was originally + versus the encoded +
2016-12-12 22:29:51 +00:00
const sanitizedValue = value ? decodeURIComponent(value.replace(/\+/g, ' ')) : value;
2016-11-30 18:30:52 +00:00
const match = gl.FilteredSearchTokenKeys.get().filter(t => key === `${t.key}_${t.param}`)[0];
2016-11-14 19:22:32 +00:00
if (match) {
const sanitizedKey = key.slice(0, key.indexOf('_'));
const valueHasSpace = sanitizedValue.indexOf(' ') !== -1;
const symbol = match.symbol;
2016-12-12 22:31:22 +00:00
let quotationsToUse;
2016-11-14 19:22:32 +00:00
if (valueHasSpace) {
// Prefer ", but use ' if required
2016-12-12 22:31:22 +00:00
quotationsToUse = sanitizedValue.indexOf('"') === -1 ? '"' : '\'';
2016-11-14 19:22:32 +00:00
}
inputValue += valueHasSpace ? `${sanitizedKey}:${symbol}${quotationsToUse}${sanitizedValue}${quotationsToUse}` : `${sanitizedKey}:${symbol}${sanitizedValue}`;
inputValue += ' ';
} else if (!match && key === 'search') {
inputValue += sanitizedValue;
inputValue += ' ';
2016-11-09 20:31:58 +00:00
}
}
});
// Trim the last space value
document.querySelector('.filtered-search').value = inputValue.trim();
if (inputValue.trim()) {
document.querySelector('.clear-search').classList.remove('hidden');
}
}
2016-11-04 21:27:11 +00:00
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-11-09 20:31:58 +00:00
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);
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-09 17:44:09 +00:00
this.filteredSearchInput.addEventListener('input', toggleClearSearchButton);
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);
this.filteredSearchInput.removeEventListener('input', toggleClearSearchButton);
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
}
clearSearch(e) {
e.preventDefault();
2016-12-09 17:44:09 +00:00
this.filteredSearchInput.value = '';
this.clearSearchButton.classList.add('hidden');
2016-12-12 15:21:38 +00:00
this.dropdownManager.resetDropdowns();
2016-11-04 21:27:11 +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();
}
}
search() {
2016-11-08 19:20:37 +00:00
let path = '?scope=all&utf8=✓';
// Check current state
const currentPath = window.location.search;
const stateIndex = currentPath.indexOf('state=');
const defaultState = 'opened';
let currentState = defaultState;
2016-12-12 16:28:22 +00:00
const { tokens, searchToken } = this.tokenizer.processTokens(this.filteredSearchInput.value);
2016-11-09 23:07:30 +00:00
2016-11-08 19:20:37 +00:00
if (stateIndex !== -1) {
2016-12-12 17:06:45 +00:00
// Get currentState from url params if available
const remaining = currentPath.slice(stateIndex + 'state='.length);
2016-11-08 19:20:37 +00:00
const separatorIndex = remaining.indexOf('&');
currentState = separatorIndex === -1 ? remaining : remaining.slice(0, separatorIndex);
}
2016-11-09 20:31:58 +00:00
path += `&state=${currentState}`;
2016-11-09 23:07:30 +00:00
tokens.forEach((token) => {
2016-11-30 18:30:52 +00:00
const match = gl.FilteredSearchTokenKeys.get().filter(t => t.key === token.key)[0];
2016-11-14 19:22:32 +00:00
let tokenPath = '';
if (token.wildcard && match.conditions) {
2016-11-14 23:45:26 +00:00
const condition = match.conditions
.filter(c => c.keyword === token.value.toLowerCase())[0];
2016-11-14 19:22:32 +00:00
if (condition) {
tokenPath = `${condition.url}`;
}
} else if (!token.wildcard) {
// Remove the wildcard token
tokenPath = `${token.key}_${match.param}=${encodeURIComponent(token.value.slice(1))}`;
} else {
tokenPath = `${token.key}_${match.param}=${encodeURIComponent(token.value)}`;
}
path += `&${tokenPath}`;
2016-11-04 21:27:11 +00:00
});
2016-11-09 23:07:30 +00:00
if (searchToken) {
path += `&search=${encodeURIComponent(searchToken)}`;
2016-11-04 21:27:11 +00:00
}
window.location = path;
}
}
global.FilteredSearchManager = FilteredSearchManager;
2016-11-08 17:35:28 +00:00
})(window.gl || (window.gl = {}));