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

142 lines
3.4 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',
param: 'name%5B%5D',
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');
input.addEventListener('input', this.tokenize.bind(this));
input.addEventListener('keydown', this.checkForEnter.bind(this));
}
clearTokens() {
this.tokens = [];
this.searchToken = '';
}
2016-11-07 22:19:15 +00:00
loadSearchParamsFromURL() {
const params = window.location.search.split('&');
let inputValue = '';
params.forEach((p) => {
const split = p.split('=');
const key = split[0];
const value = split[1];
const match = validTokenKeys.find((t) => {
return key === `${t.key}_${t.param}`;
});
if (match) {
const sanitizedKey = key.slice(0, key.indexOf('_'));
inputValue += `${sanitizedKey}:${value} `;
} else if (!match && key === 'search') {
inputValue += `${value} `;
}
});
// Trim the last space value
document.querySelector('.filtered-search').value = inputValue.trim();
}
2016-11-04 21:27:11 +00:00
tokenize(event) {
// Re-calculate tokens
this.clearTokens();
// TODO: Current implementation does not support token values that have valid spaces in them
// Example/ label:community contribution
const input = event.target.value;
const inputs = input.split(' ');
let searchTerms = '';
inputs.forEach((i) => {
const colonIndex = i.indexOf(':');
// Check if text is a token
if (colonIndex !== -1) {
const tokenKey = i.slice(0, colonIndex).toLowerCase();
const tokenValue = i.slice(colonIndex + 1);
const match = validTokenKeys.filter((v) => {
2016-11-07 22:18:50 +00:00
return v.key === tokenKey;
2016-11-04 21:27:11 +00:00
})[0];
2016-11-07 22:18:50 +00:00
if (match && tokenValue.length > 0) {
this.tokens.push({
key: match.key,
value: tokenValue,
});
2016-11-04 21:27:11 +00:00
}
} else {
searchTerms += i + ' ';
}
}, 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');
let path = '?scope=all&state=opened&utf8=✓';
2016-11-07 22:18:50 +00:00
this.tokens.forEach((token) => {
const param = validTokenKeys.find((t) => {
return t.key === token.key;
}).param;
path += `&${token.key}_${param}=${token.value}`;
2016-11-04 21:27:11 +00:00
});
if (this.searchToken) {
path += '&search=' + this.searchToken;
}
window.location = path;
}
}
global.FilteredSearchManager = FilteredSearchManager;
})(window.gl || (window.gl = {}));