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-14 19:22:32 +00:00
|
|
|
symbol: '@',
|
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-14 19:22:32 +00:00
|
|
|
symbol: '@',
|
|
|
|
conditions: [{
|
|
|
|
keyword: 'none',
|
|
|
|
url: 'assignee_id=0',
|
|
|
|
}]
|
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-14 19:22:32 +00:00
|
|
|
symbol: '%',
|
|
|
|
conditions: [{
|
|
|
|
keyword: 'none',
|
|
|
|
url: 'milestone_title=No+Milestone',
|
|
|
|
}, {
|
|
|
|
keyword: 'upcoming',
|
|
|
|
url: 'milestone_title=%23upcoming',
|
|
|
|
}]
|
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-14 19:22:32 +00:00
|
|
|
symbol: '~',
|
|
|
|
conditions: [{
|
|
|
|
keyword: 'none',
|
|
|
|
url: 'label_name[]=No+Label',
|
|
|
|
}]
|
2016-11-09 20:31:58 +00:00
|
|
|
}];
|
|
|
|
|
2016-11-14 16:37:55 +00:00
|
|
|
function clearSearch(e) {
|
|
|
|
e.stopPropagation();
|
|
|
|
e.preventDefault();
|
2016-11-09 23:07:30 +00:00
|
|
|
|
|
|
|
document.querySelector('.filtered-search').value = '';
|
|
|
|
document.querySelector('.clear-search').classList.add('hidden');
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
if (event.target.value) {
|
2016-11-14 16:37:55 +00:00
|
|
|
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-14 19:22:32 +00:00
|
|
|
// Check if it matches edge conditions listed in validTokenKeys
|
|
|
|
let conditionIndex = 0;
|
|
|
|
const validCondition = validTokenKeys.filter(v => v.conditions && v.conditions.filter((c, index) => {
|
|
|
|
if (c.url === p) {
|
|
|
|
conditionIndex = index;
|
|
|
|
}
|
|
|
|
return c.url === p;
|
|
|
|
})[0])[0];
|
|
|
|
|
|
|
|
if (validCondition) {
|
|
|
|
inputValue += `${validCondition.key}:${validCondition.conditions[conditionIndex].keyword}`;
|
|
|
|
} else {
|
|
|
|
// 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.filter(t => key === `${t.key}_${t.param}`)[0];
|
|
|
|
|
|
|
|
if (match) {
|
|
|
|
const sanitizedKey = key.slice(0, key.indexOf('_'));
|
|
|
|
const valueHasSpace = sanitizedValue.indexOf(' ') !== -1;
|
|
|
|
const symbol = match.symbol;
|
|
|
|
|
|
|
|
const preferredQuotations = '"';
|
|
|
|
let quotationsToUse = preferredQuotations;
|
|
|
|
|
|
|
|
if (valueHasSpace) {
|
|
|
|
// Prefer ", but use ' if required
|
|
|
|
quotationsToUse = sanitizedValue.indexOf(preferredQuotations) === -1 ? preferredQuotations : '\'';
|
|
|
|
}
|
|
|
|
|
|
|
|
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-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-14 16:37:55 +00:00
|
|
|
processInput(e) {
|
|
|
|
const input = e.target.value;
|
2016-11-09 23:07:30 +00:00
|
|
|
this.tokenizer.processTokens(input);
|
2016-11-04 21:27:11 +00:00
|
|
|
}
|
|
|
|
|
2016-11-14 16:37:55 +00:00
|
|
|
checkForEnter(e) {
|
2016-11-10 01:10:15 +00:00
|
|
|
// Enter KeyCode
|
2016-11-14 16:37:55 +00:00
|
|
|
if (e.keyCode === 13) {
|
|
|
|
e.stopPropagation();
|
|
|
|
e.preventDefault();
|
2016-11-04 21:27:11 +00:00
|
|
|
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-14 19:22:32 +00:00
|
|
|
const match = validTokenKeys.filter(t => t.key === token.key)[0];
|
|
|
|
let tokenPath = '';
|
|
|
|
|
|
|
|
if (token.wildcard && match.conditions) {
|
|
|
|
const condition = match.conditions.filter(c => c.keyword === token.value.toLowerCase())[0];
|
|
|
|
|
|
|
|
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 = {}));
|