Commit f1574e45 authored by Clement Ho's avatar Clement Ho

Fix ESLint errors

parent fe4d33cf
/* eslint-disable no-param-reassign */
((global) => { ((global) => {
const TOKEN_TYPE_STRING = 'string';
const TOKEN_TYPE_ARRAY = 'array';
const validTokenKeys = [{ const validTokenKeys = [{
key: 'author', key: 'author',
type: 'string', type: 'string',
param: 'username', param: 'username',
},{ }, {
key: 'assignee', key: 'assignee',
type: 'string', type: 'string',
param: 'username', param: 'username',
},{ }, {
key: 'milestone', key: 'milestone',
type: 'string', type: 'string',
param: 'title', param: 'title',
},{ }, {
key: 'label', key: 'label',
type: 'array', type: 'array',
param: 'name[]', param: 'name[]',
},]; }];
class FilteredSearchManager { function toggleClearSearchButton(event) {
constructor() {
this.bindEvents();
this.loadSearchParamsFromURL();
this.clearTokens();
}
bindEvents() {
const input = document.querySelector('.filtered-search');
const clearSearch = document.querySelector('.clear-search'); const clearSearch = document.querySelector('.clear-search');
input.addEventListener('input', this.tokenize.bind(this)); if (event.target.value) {
input.addEventListener('input', this.toggleClearSearchButton); clearSearch.classList.remove('hidden');
input.addEventListener('keydown', this.checkForEnter.bind(this)); } else {
clearSearch.classList.add('hidden');
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');
} }
clearTokens() {
this.tokens = [];
this.searchToken = '';
} }
loadSearchParamsFromURL() { function loadSearchParamsFromURL() {
// We can trust that each param has one & since values containing & will be encoded // We can trust that each param has one & since values containing & will be encoded
// Remove the first character of search as it is always ? // Remove the first character of search as it is always ?
const params = window.location.search.slice(1).split('&'); const params = window.location.search.slice(1).split('&');
...@@ -66,10 +42,7 @@ ...@@ -66,10 +42,7 @@
// Sanitize value since URL converts spaces into + // Sanitize value since URL converts spaces into +
// Replace before decode so that we know what was originally + versus the encoded + // Replace before decode so that we know what was originally + versus the encoded +
const sanitizedValue = value ? decodeURIComponent(value.replace(/[+]/g, ' ')) : value; const sanitizedValue = value ? decodeURIComponent(value.replace(/[+]/g, ' ')) : value;
const match = validTokenKeys.find(t => key === `${t.key}_${t.param}`);
const match = validTokenKeys.find((t) => {
return key === `${t.key}_${t.param}`;
});
if (match) { if (match) {
const sanitizedKey = key.slice(0, key.indexOf('_')); const sanitizedKey = key.slice(0, key.indexOf('_'));
...@@ -85,7 +58,6 @@ ...@@ -85,7 +58,6 @@
inputValue += valueHasSpace ? `${sanitizedKey}:${quotationsToUse}${sanitizedValue}${quotationsToUse}` : `${sanitizedKey}:${sanitizedValue}`; inputValue += valueHasSpace ? `${sanitizedKey}:${quotationsToUse}${sanitizedValue}${quotationsToUse}` : `${sanitizedKey}:${sanitizedValue}`;
inputValue += ' '; inputValue += ' ';
} else if (!match && key === 'search') { } else if (!match && key === 'search') {
inputValue += sanitizedValue; inputValue += sanitizedValue;
inputValue += ' '; inputValue += ' ';
...@@ -100,14 +72,36 @@ ...@@ -100,14 +72,36 @@
} }
} }
toggleClearSearchButton(event) { class FilteredSearchManager {
constructor() {
this.bindEvents();
loadSearchParamsFromURL();
this.clearTokens();
}
bindEvents() {
const input = document.querySelector('.filtered-search');
const clearSearch = document.querySelector('.clear-search'); const clearSearch = document.querySelector('.clear-search');
if (event.target.value) { input.addEventListener('input', this.tokenize.bind(this));
clearSearch.classList.remove('hidden'); input.addEventListener('input', toggleClearSearchButton);
} else { input.addEventListener('keydown', this.checkForEnter.bind(this));
clearSearch.classList.add('hidden');
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');
} }
clearTokens() {
this.tokens = [];
this.searchToken = '';
} }
tokenize(event) { tokenize(event) {
...@@ -121,8 +115,9 @@ ...@@ -121,8 +115,9 @@
let incompleteToken = false; let incompleteToken = false;
const addSearchTerm = function addSearchTerm(term) { const addSearchTerm = function addSearchTerm(term) {
searchTerms += term + ' '; // Add space for next term
} searchTerms += `${term} `;
};
inputs.forEach((i) => { inputs.forEach((i) => {
if (incompleteToken) { if (incompleteToken) {
...@@ -147,10 +142,7 @@ ...@@ -147,10 +142,7 @@
if (colonIndex !== -1) { if (colonIndex !== -1) {
const tokenKey = i.slice(0, colonIndex).toLowerCase(); const tokenKey = i.slice(0, colonIndex).toLowerCase();
const tokenValue = i.slice(colonIndex + 1); const tokenValue = i.slice(colonIndex + 1);
const match = validTokenKeys.find(v => v.key === tokenKey);
const match = validTokenKeys.find((v) => {
return v.key === tokenKey;
});
if (tokenValue.indexOf('"') !== -1) { if (tokenValue.indexOf('"') !== -1) {
lastQuotation = '"'; lastQuotation = '"';
...@@ -178,11 +170,9 @@ ...@@ -178,11 +170,9 @@
} }
printTokens() { printTokens() {
console.log('tokens:') console.log('tokens:');
this.tokens.forEach((token) => { this.tokens.forEach(token => console.log(token));
console.log(token); console.log(`search: ${this.searchToken}`);
})
console.log('search: ' + this.searchToken);
} }
checkForEnter(event) { checkForEnter(event) {
...@@ -210,18 +200,14 @@ ...@@ -210,18 +200,14 @@
currentState = separatorIndex === -1 ? remaining : remaining.slice(0, separatorIndex); currentState = separatorIndex === -1 ? remaining : remaining.slice(0, separatorIndex);
} }
path += `&state=${currentState}` path += `&state=${currentState}`;
this.tokens.forEach((token) => { this.tokens.forEach((token) => {
const param = validTokenKeys.find((t) => { const param = validTokenKeys.find(t => t.key === token.key).param;
return t.key === token.key;
}).param;
path += `&${token.key}_${param}=${encodeURIComponent(token.value)}`; path += `&${token.key}_${param}=${encodeURIComponent(token.value)}`;
}); });
if (this.searchToken) { if (this.searchToken) {
path += '&search=' + encodeURIComponent(this.searchToken); path += `&search=${encodeURIComponent(this.searchToken)}`;
} }
window.location = path; window.location = path;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment