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

233 lines
6.5 KiB
JavaScript
Raw Normal View History

2016-11-04 21:27:11 +00:00
((global) => {
const TOKEN_TYPE_STRING = 'string';
const TOKEN_TYPE_ARRAY = 'array';
const validTokenKeys = [{
key: 'author',
type: 'string',
param: 'username',
2016-11-04 21:27:11 +00:00
},{
key: 'assignee',
2016-11-07 22:18:50 +00:00
type: 'string',
param: 'username',
2016-11-04 21:27:11 +00:00
},{
key: 'milestone',
2016-11-07 22:18:50 +00:00
type: 'string',
param: 'title',
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-04 21:27:11 +00:00
},];
class FilteredSearchManager {
constructor() {
this.bindEvents();
2016-11-07 22:19:15 +00:00
this.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));
input.addEventListener('input', this.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 = '';
}
2016-11-07 22:19:15 +00:00
loadSearchParamsFromURL() {
2016-11-08 21:42:15 +00:00
// 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('&');
2016-11-07 22:19:15 +00:00
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;
2016-11-07 22:19:15 +00:00
const match = validTokenKeys.find((t) => {
return key === `${t.key}_${t.param}`;
});
if (match) {
const sanitizedKey = key.slice(0, key.indexOf('_'));
const valueHasSpace = sanitizedValue.indexOf(' ') !== -1;
2016-11-08 20:18:46 +00:00
const preferredQuotations = '"';
let quotationsToUse = preferredQuotations;
if (valueHasSpace) {
// Prefer ", but use ' if required
quotationsToUse = sanitizedValue.indexOf(preferredQuotations) === -1 ? preferredQuotations : '\'';
2016-11-08 20:18:46 +00:00
}
inputValue += valueHasSpace ? `${sanitizedKey}:${quotationsToUse}${sanitizedValue}${quotationsToUse}` : `${sanitizedKey}:${sanitizedValue}`;
inputValue += ' ';
2016-11-07 22:19:15 +00:00
} else if (!match && key === 'search') {
inputValue += sanitizedValue;
inputValue += ' ';
2016-11-07 22:19:15 +00:00
}
});
// Trim the last space value
document.querySelector('.filtered-search').value = inputValue.trim();
2016-11-08 18:47:53 +00:00
if (inputValue.trim()) {
document.querySelector('.clear-search').classList.remove('hidden');
}
2016-11-07 22:19:15 +00:00
}
toggleClearSearchButton(event) {
const clearSearch = document.querySelector('.clear-search');
if (event.target.value) {
clearSearch.classList.remove('hidden');
} else {
clearSearch.classList.add('hidden');
}
}
2016-11-04 21:27:11 +00:00
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) {
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-08 20:18:46 +00:00
const match = validTokenKeys.find((v) => {
2016-11-07 22:18:50 +00:00
return 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-07 22:18:50 +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);
}
path += `&state=${currentState}`
2016-11-04 21:27:11 +00:00
2016-11-07 22:18:50 +00:00
this.tokens.forEach((token) => {
const param = validTokenKeys.find((t) => {
return 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) {
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 = {}));