Commit bf16e91f authored by Clement Ho's avatar Clement Ho

Refactor FilteredSearchTokenKeys model

parent 49231cce
...@@ -125,7 +125,7 @@ ...@@ -125,7 +125,7 @@
this.droplab = new DropLab(); this.droplab = new DropLab();
} }
const match = gl.FilteredSearchTokenKeys.get().filter(value => value.key === dropdownName.toLowerCase())[0]; const match = gl.FilteredSearchTokenKeys.searchByKey(dropdownName.toLowerCase());
const shouldOpenFilterDropdown = match && this.currentDropdown !== match.key && this.mapping.hasOwnProperty(match.key); const shouldOpenFilterDropdown = match && this.currentDropdown !== match.key && this.mapping.hasOwnProperty(match.key);
const shouldOpenHintDropdown = !match && this.currentDropdown !== 'hint'; const shouldOpenHintDropdown = !match && this.currentDropdown !== 'hint';
......
...@@ -85,42 +85,32 @@ ...@@ -85,42 +85,32 @@
params.forEach((p) => { params.forEach((p) => {
const split = p.split('='); const split = p.split('=');
const key = decodeURIComponent(split[0]); const keyParam = decodeURIComponent(split[0]);
const value = split[1]; const value = split[1];
// Check if it matches edge conditions listed in gl.FilteredSearchTokenKeys.get() // Check if it matches edge conditions listed in gl.FilteredSearchTokenKeys
let conditionIndex = 0; const condition = gl.FilteredSearchTokenKeys.searchByConditionUrl(p);
const validCondition = gl.FilteredSearchTokenKeys.get()
.filter(v => v.conditions && v.conditions.filter((c, index) => {
// Return TokenKeys that have conditions that much the URL
if (c.url === p) {
conditionIndex = index;
}
return c.url === p;
})[0])[0];
if (validCondition) { if (condition) {
// Parse params based on rules provided in the conditions key of gl.FilteredSearchTokenKeys.get() inputValues.push(`${condition.tokenKey}:${condition.value}`);
inputValues.push(`${validCondition.key}:${validCondition.conditions[conditionIndex].keyword}`);
} else { } else {
// 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 = gl.FilteredSearchTokenKeys.get().filter(t => key === `${t.key}_${t.param}`)[0]; const match = gl.FilteredSearchTokenKeys.searchByKeyParam(keyParam);
if (match) { if (match) {
const sanitizedKey = key.slice(0, key.indexOf('_')); const sanitizedKey = keyParam.slice(0, keyParam.indexOf('_'));
const valueHasSpace = sanitizedValue.indexOf(' ') !== -1;
const symbol = match.symbol; const symbol = match.symbol;
let quotationsToUse; let quotationsToUse = '';
if (valueHasSpace) { if (sanitizedValue.indexOf(' ') !== -1) {
// Prefer ", but use ' if required // Prefer ", but use ' if required
quotationsToUse = sanitizedValue.indexOf('"') === -1 ? '"' : '\''; quotationsToUse = sanitizedValue.indexOf('"') === -1 ? '"' : '\'';
} }
inputValues.push(valueHasSpace ? `${sanitizedKey}:${symbol}${quotationsToUse}${sanitizedValue}${quotationsToUse}` : `${sanitizedKey}:${symbol}${sanitizedValue}`); inputValues.push(`${sanitizedKey}:${symbol}${quotationsToUse}${sanitizedValue}${quotationsToUse}`);
} else if (!match && key === 'search') { } else if (!match && keyParam === 'search') {
inputValues.push(sanitizedValue); inputValues.push(sanitizedValue);
} }
} }
...@@ -141,21 +131,17 @@ ...@@ -141,21 +131,17 @@
paths.push(`state=${currentState}`); paths.push(`state=${currentState}`);
tokens.forEach((token) => { tokens.forEach((token) => {
const match = gl.FilteredSearchTokenKeys.get().filter(t => t.key === token.key)[0]; const condition = gl.FilteredSearchTokenKeys.searchByConditionKeyValue(token.key, token.value.toLowerCase());
const { param } = gl.FilteredSearchTokenKeys.searchByKey(token.key);
let tokenPath = ''; let tokenPath = '';
if (token.wildcard && match.conditions) { if (token.wildcard && condition) {
const condition = match.conditions tokenPath = condition.url;
.filter(c => c.keyword === token.value.toLowerCase())[0];
if (condition) {
tokenPath = `${condition.url}`;
}
} else if (!token.wildcard) { } else if (!token.wildcard) {
// Remove the wildcard token // Remove the wildcard token
tokenPath = `${token.key}_${match.param}=${encodeURIComponent(token.value.slice(1))}`; tokenPath = `${token.key}_${param}=${encodeURIComponent(token.value.slice(1))}`;
} else { } else {
tokenPath = `${token.key}_${match.param}=${encodeURIComponent(token.value)}`; tokenPath = `${token.key}_${param}=${encodeURIComponent(token.value)}`;
} }
paths.push(tokenPath); paths.push(tokenPath);
......
/* eslint-disable no-param-reassign */ /* eslint-disable no-param-reassign */
((global) => { ((global) => {
const tokenKeys = [{
key: 'author',
type: 'string',
param: 'username',
symbol: '@',
}, {
key: 'assignee',
type: 'string',
param: 'username',
symbol: '@',
}, {
key: 'milestone',
type: 'string',
param: 'title',
symbol: '%',
}, {
key: 'label',
type: 'array',
param: 'name[]',
symbol: '~',
}];
const conditions = [{
url: 'assignee_id=0',
tokenKey: 'assignee',
value: 'none',
}, {
url: 'milestone_title=No+Milestone',
tokenKey: 'milestone',
value: 'none',
}, {
url: 'milestone_title=%23upcoming',
tokenKey: 'milestone',
value: 'upcoming',
}, {
url: 'label_name[]=No+Label',
tokenKey: 'label',
value: 'none',
}];
class FilteredSearchTokenKeys { class FilteredSearchTokenKeys {
static get() { static get() {
return [{ return tokenKeys;
key: 'author', }
type: 'string',
param: 'username', static searchByKey(key) {
symbol: '@', return tokenKeys.find(tokenKey => tokenKey.key === key) || null;
}, { }
key: 'assignee',
type: 'string', static searchBySymbol(symbol) {
param: 'username', return tokenKeys.find(tokenKey => tokenKey.symbol === symbol) || null;
symbol: '@', }
conditions: [{
keyword: 'none', static searchByKeyParam(keyParam) {
url: 'assignee_id=0', return tokenKeys.find(tokenKey => keyParam === `${tokenKey.key}_${tokenKey.param}`) || null;
}], }
}, {
key: 'milestone', static searchByConditionUrl(url) {
type: 'string', return conditions.find(condition => condition.url === url) || null;
param: 'title', }
symbol: '%',
conditions: [{ static searchByConditionKeyValue(key, value) {
keyword: 'none', return conditions.find(condition => condition.tokenKey === key && condition.value === value) || null;
url: 'milestone_title=No+Milestone',
}, {
keyword: 'upcoming',
url: 'milestone_title=%23upcoming',
}],
}, {
key: 'label',
type: 'array',
param: 'name[]',
symbol: '~',
conditions: [{
keyword: 'none',
url: 'label_name[]=No+Label',
}],
}];
} }
} }
......
...@@ -73,7 +73,6 @@ ...@@ -73,7 +73,6 @@
let tokens = []; let tokens = [];
let searchToken = ''; let searchToken = '';
let lastToken = ''; let lastToken = '';
const validTokenKeys = gl.FilteredSearchTokenKeys.get();
const inputs = input.split(' '); const inputs = input.split(' ');
let searchTerms = ''; let searchTerms = '';
...@@ -107,8 +106,8 @@ ...@@ -107,8 +106,8 @@
if (colonIndex !== -1) { if (colonIndex !== -1) {
const { tokenKey, tokenValue, tokenSymbol } = gl.FilteredSearchTokenizer.parseToken(i); const { tokenKey, tokenValue, tokenSymbol } = gl.FilteredSearchTokenizer.parseToken(i);
const keyMatch = validTokenKeys.filter(v => v.key === tokenKey)[0]; const keyMatch = gl.FilteredSearchTokenKeys.searchByKey(tokenKey);
const symbolMatch = validTokenKeys.filter(v => v.symbol === tokenSymbol)[0]; const symbolMatch = gl.FilteredSearchTokenKeys.searchBySymbol(tokenSymbol);
const doubleQuoteOccurrences = tokenValue.split('"').length - 1; const doubleQuoteOccurrences = tokenValue.split('"').length - 1;
const singleQuoteOccurrences = tokenValue.split('\'').length - 1; const singleQuoteOccurrences = tokenValue.split('\'').length - 1;
......
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