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',
|
2016-11-07 22:33:51 +00:00
|
|
|
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',
|
2016-11-07 22:33:51 +00:00
|
|
|
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
|
|
|
}];
|
|
|
|
|
2016-11-09 23:07:30 +00:00
|
|
|
function clearSearch(event) {
|
|
|
|
event.stopPropagation();
|
|
|
|
event.preventDefault();
|
|
|
|
|
|
|
|
document.querySelector('.filtered-search').value = '';
|
|
|
|
document.querySelector('.clear-search').classList.add('hidden');
|
|
|
|
}
|
|
|
|
|
2016-11-09 20:31:58 +00:00
|
|
|
function toggleClearSearchButton(event) {
|
2016-11-09 23:07:30 +00:00
|
|
|
const clearSearchButton = document.querySelector('.clear-search');
|
2016-11-09 20:31:58 +00:00
|
|
|
|
|
|
|
if (event.target.value) {
|
2016-11-09 23:07:30 +00:00
|
|
|
clearSearchButton.classList.remove('hidden');
|
2016-11-09 20:31:58 +00:00
|
|
|
} else {
|
2016-11-09 23:07:30 +00:00
|
|
|
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];
|
|
|
|
|
|
|
|
// 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() {
|
2016-11-09 23:07:30 +00:00
|
|
|
this.tokenizer = new gl.FilteredSearchTokenizer(validTokenKeys);
|
2016-11-04 21:27:11 +00:00
|
|
|
this.bindEvents();
|
2016-11-09 20:31:58 +00:00
|
|
|
loadSearchParamsFromURL();
|
2016-11-04 21:27:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bindEvents() {
|
2016-11-09 23:07:30 +00:00
|
|
|
const filteredSearchInput = document.querySelector('.filtered-search');
|
2016-11-08 18:47:53 +00:00
|
|
|
|
2016-11-09 23:07:30 +00:00
|
|
|
filteredSearchInput.addEventListener('input', this.processInput.bind(this));
|
|
|
|
filteredSearchInput.addEventListener('input', toggleClearSearchButton);
|
|
|
|
filteredSearchInput.addEventListener('keydown', this.checkForEnter.bind(this));
|
2016-11-04 21:27:11 +00:00
|
|
|
|
2016-11-09 23:07:30 +00:00
|
|
|
document.querySelector('.clear-search').addEventListener('click', clearSearch);
|
2016-11-04 21:27:11 +00:00
|
|
|
}
|
|
|
|
|
2016-11-09 23:07:30 +00:00
|
|
|
processInput(event) {
|
2016-11-04 21:27:11 +00:00
|
|
|
const input = event.target.value;
|
2016-11-09 23:07:30 +00:00
|
|
|
this.tokenizer.processTokens(input);
|
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;
|
|
|
|
|
2016-11-09 23:07:30 +00:00
|
|
|
const tokens = this.tokenizer.getTokens();
|
|
|
|
const searchToken = this.tokenizer.getSearchToken();
|
|
|
|
|
2016-11-08 19:20:37 +00:00
|
|
|
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-09 23:07:30 +00:00
|
|
|
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
|
|
|
});
|
|
|
|
|
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 = {}));
|