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

219 lines
6.3 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) => {
const validTokenKeys = [{
key: 'author',
type: 'string',
param: 'username',
2016-11-09 20:31:58 +00:00
}, {
2016-11-04 21:27:11 +00:00
key: 'assignee',
2016-11-07 22:18:50 +00:00
type: 'string',
param: 'username',
2016-11-09 20:31:58 +00:00
}, {
2016-11-04 21:27:11 +00:00
key: 'milestone',
2016-11-07 22:18:50 +00:00
type: 'string',
param: 'title',
2016-11-09 20:31:58 +00:00
}, {
2016-11-04 21:27:11 +00:00
key: 'label',
2016-11-07 22:18:50 +00:00
type: 'array',
2016-11-08 21:42:15 +00:00
param: 'name[]',
2016-11-09 20:31:58 +00:00
}];
function toggleClearSearchButton(event) {
const clearSearch = document.querySelector('.clear-search');
if (event.target.value) {
clearSearch.classList.remove('hidden');
} else {
clearSearch.classList.add('hidden');
}
}
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];
// 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;
const match = validTokenKeys.find(t => key === `${t.key}_${t.param}`);
if (match) {
const sanitizedKey = key.slice(0, key.indexOf('_'));
const valueHasSpace = sanitizedValue.indexOf(' ') !== -1;
const preferredQuotations = '"';
let quotationsToUse = preferredQuotations;
if (valueHasSpace) {
// Prefer ", but use ' if required
quotationsToUse = sanitizedValue.indexOf(preferredQuotations) === -1 ? preferredQuotations : '\'';
}
inputValue += valueHasSpace ? `${sanitizedKey}:${quotationsToUse}${sanitizedValue}${quotationsToUse}` : `${sanitizedKey}:${sanitizedValue}`;
inputValue += ' ';
} else if (!match && key === 'search') {
inputValue += sanitizedValue;
inputValue += ' ';
}
});
// 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() {
this.bindEvents();
2016-11-09 20:31:58 +00:00
loadSearchParamsFromURL();
2016-11-04 21:27:11 +00:00
this.clearTokens();
}
bindEvents() {
const input = document.querySelector('.filtered-search');
2016-11-08 18:47:53 +00:00
const clearSearch = document.querySelector('.clear-search');
2016-11-04 21:27:11 +00:00
input.addEventListener('input', this.tokenize.bind(this));
2016-11-09 20:31:58 +00:00
input.addEventListener('input', toggleClearSearchButton);
2016-11-04 21:27:11 +00:00
input.addEventListener('keydown', this.checkForEnter.bind(this));
2016-11-08 18:47:53 +00:00
clearSearch.addEventListener('click', this.clearSearch.bind(this));
}
clearSearch(event) {
event.stopPropagation();
event.preventDefault();
this.clearTokens();
document.querySelector('.filtered-search').value = '';
document.querySelector('.clear-search').classList.add('hidden');
2016-11-04 21:27:11 +00:00
}
clearTokens() {
this.tokens = [];
this.searchToken = '';
}
tokenize(event) {
// Re-calculate tokens
this.clearTokens();
const input = event.target.value;
const inputs = input.split(' ');
let searchTerms = '';
2016-11-08 20:18:46 +00:00
let lastQuotation = '';
let incompleteToken = false;
2016-11-04 21:27:11 +00:00
const addSearchTerm = function addSearchTerm(term) {
2016-11-09 20:31:58 +00:00
// Add space for next term
searchTerms += `${term} `;
};
2016-11-04 21:27:11 +00:00
inputs.forEach((i) => {
2016-11-08 20:18:46 +00:00
if (incompleteToken) {
const prevToken = this.tokens[this.tokens.length - 1];
prevToken.value += ` ${i}`;
// Remove last quotation
const lastQuotationRegex = new RegExp(lastQuotation, 'g');
prevToken.value = prevToken.value.replace(lastQuotationRegex, '');
this.tokens[this.tokens.length - 1] = prevToken;
// Check to see if this quotation completes the token value
if (i.indexOf(lastQuotation)) {
incompleteToken = !incompleteToken;
}
return;
}
2016-11-04 21:27:11 +00:00
const colonIndex = i.indexOf(':');
if (colonIndex !== -1) {
const tokenKey = i.slice(0, colonIndex).toLowerCase();
const tokenValue = i.slice(colonIndex + 1);
2016-11-09 20:31:58 +00:00
const match = validTokenKeys.find(v => v.key === tokenKey);
2016-11-08 20:18:46 +00:00
if (tokenValue.indexOf('"') !== -1) {
lastQuotation = '"';
incompleteToken = true;
} else if (tokenValue.indexOf('\'') !== -1) {
lastQuotation = '\'';
incompleteToken = true;
}
2016-11-04 21:27:11 +00:00
2016-11-07 22:18:50 +00:00
if (match && tokenValue.length > 0) {
this.tokens.push({
key: match.key,
value: tokenValue,
});
} else {
addSearchTerm(i);
2016-11-04 21:27:11 +00:00
}
} else {
addSearchTerm(i);
2016-11-04 21:27:11 +00:00
}
}, this);
this.searchToken = searchTerms.trim();
this.printTokens();
}
printTokens() {
2016-11-09 20:31:58 +00:00
console.log('tokens:');
this.tokens.forEach(token => console.log(token));
console.log(`search: ${this.searchToken}`);
2016-11-04 21:27:11 +00:00
}
checkForEnter(event) {
if (event.key === 'Enter') {
event.stopPropagation();
event.preventDefault();
this.search();
}
}
search() {
console.log('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;
if (stateIndex !== -1) {
const remaining = currentPath.slice(stateIndex + 6);
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-07 22:18:50 +00:00
this.tokens.forEach((token) => {
2016-11-09 20:31:58 +00:00
const param = validTokenKeys.find(t => t.key === token.key).param;
2016-11-08 21:42:15 +00:00
path += `&${token.key}_${param}=${encodeURIComponent(token.value)}`;
2016-11-04 21:27:11 +00:00
});
if (this.searchToken) {
2016-11-09 20:31:58 +00:00
path += `&search=${encodeURIComponent(this.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 = {}));