2016-11-09 20:31:58 +00:00
|
|
|
/* eslint-disable no-param-reassign */
|
2016-11-04 21:27:11 +00:00
|
|
|
((global) => {
|
|
|
|
class FilteredSearchManager {
|
|
|
|
constructor() {
|
2016-11-30 18:30:52 +00:00
|
|
|
this.tokenizer = gl.FilteredSearchTokenizer;
|
2016-12-09 17:44:09 +00:00
|
|
|
this.filteredSearchInput = document.querySelector('.filtered-search');
|
|
|
|
this.clearSearchButton = document.querySelector('.clear-search');
|
2016-12-12 15:21:38 +00:00
|
|
|
this.dropdownManager = new gl.FilteredSearchDropdownManager();
|
2016-12-09 17:44:09 +00:00
|
|
|
|
2016-11-04 21:27:11 +00:00
|
|
|
this.bindEvents();
|
2016-12-12 22:37:49 +00:00
|
|
|
this.loadSearchParamsFromURL();
|
2016-12-12 15:21:38 +00:00
|
|
|
this.dropdownManager.setDropdown();
|
2016-12-08 21:42:58 +00:00
|
|
|
|
2016-12-09 17:44:09 +00:00
|
|
|
this.cleanupWrapper = this.cleanup.bind(this);
|
|
|
|
document.addEventListener('page:fetch', this.cleanupWrapper);
|
2016-12-08 21:42:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cleanup() {
|
2016-12-09 21:36:44 +00:00
|
|
|
this.unbindEvents();
|
2016-12-09 17:44:09 +00:00
|
|
|
document.removeEventListener('page:fetch', this.cleanupWrapper);
|
|
|
|
}
|
2016-12-08 21:42:58 +00:00
|
|
|
|
2016-11-04 21:27:11 +00:00
|
|
|
bindEvents() {
|
2016-12-12 15:21:38 +00:00
|
|
|
this.setDropdownWrapper = this.dropdownManager.setDropdown.bind(this.dropdownManager);
|
2016-12-12 22:37:49 +00:00
|
|
|
this.toggleClearSearchButtonWrapper = this.toggleClearSearchButton.bind(this);
|
2016-12-09 21:36:44 +00:00
|
|
|
this.checkForEnterWrapper = this.checkForEnter.bind(this);
|
|
|
|
this.clearSearchWrapper = this.clearSearch.bind(this);
|
|
|
|
this.checkForBackspaceWrapper = this.checkForBackspace.bind(this);
|
|
|
|
|
|
|
|
this.filteredSearchInput.addEventListener('input', this.setDropdownWrapper);
|
2016-12-12 22:37:49 +00:00
|
|
|
this.filteredSearchInput.addEventListener('input', this.toggleClearSearchButtonWrapper);
|
2016-12-09 21:36:44 +00:00
|
|
|
this.filteredSearchInput.addEventListener('keydown', this.checkForEnterWrapper);
|
|
|
|
this.filteredSearchInput.addEventListener('keyup', this.checkForBackspaceWrapper);
|
|
|
|
this.clearSearchButton.addEventListener('click', this.clearSearchWrapper);
|
|
|
|
}
|
|
|
|
|
|
|
|
unbindEvents() {
|
|
|
|
this.filteredSearchInput.removeEventListener('input', this.setDropdownWrapper);
|
2016-12-12 22:37:49 +00:00
|
|
|
this.filteredSearchInput.removeEventListener('input', this.toggleClearSearchButtonWrapper);
|
2016-12-09 21:36:44 +00:00
|
|
|
this.filteredSearchInput.removeEventListener('keydown', this.checkForEnterWrapper);
|
|
|
|
this.filteredSearchInput.removeEventListener('keyup', this.checkForBackspaceWrapper);
|
|
|
|
this.clearSearchButton.removeEventListener('click', this.clearSearchWrapper);
|
2016-12-07 18:55:03 +00:00
|
|
|
}
|
|
|
|
|
2016-12-09 21:36:44 +00:00
|
|
|
checkForBackspace(e) {
|
2016-12-12 22:27:10 +00:00
|
|
|
// 8 = Backspace Key
|
|
|
|
// 46 = Delete Key
|
|
|
|
if (e.keyCode === 8 || e.keyCode === 46) {
|
2016-12-09 21:36:44 +00:00
|
|
|
// Reposition dropdown so that it is aligned with cursor
|
2016-12-12 15:21:38 +00:00
|
|
|
this.dropdownManager.updateCurrentDropdownOffset();
|
2016-12-09 21:36:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-14 16:37:55 +00:00
|
|
|
checkForEnter(e) {
|
|
|
|
if (e.keyCode === 13) {
|
|
|
|
e.preventDefault();
|
2016-12-09 21:20:41 +00:00
|
|
|
|
|
|
|
// Prevent droplab from opening dropdown
|
2016-12-12 15:21:38 +00:00
|
|
|
this.dropdownManager.destroyDroplab();
|
2016-12-09 21:20:41 +00:00
|
|
|
|
2016-11-04 21:27:11 +00:00
|
|
|
this.search();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-12 22:37:49 +00:00
|
|
|
toggleClearSearchButton(e) {
|
|
|
|
if (e.target.value) {
|
|
|
|
this.clearSearchButton.classList.remove('hidden');
|
|
|
|
} else {
|
|
|
|
this.clearSearchButton.classList.add('hidden');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
clearSearch(e) {
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
|
|
this.filteredSearchInput.value = '';
|
|
|
|
this.clearSearchButton.classList.add('hidden');
|
|
|
|
|
|
|
|
this.dropdownManager.resetDropdowns();
|
|
|
|
}
|
|
|
|
|
|
|
|
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('&');
|
2016-12-12 22:42:00 +00:00
|
|
|
let inputValues = [];
|
2016-12-12 22:37:49 +00:00
|
|
|
|
|
|
|
params.forEach((p) => {
|
|
|
|
const split = p.split('=');
|
|
|
|
const key = decodeURIComponent(split[0]);
|
|
|
|
const value = split[1];
|
|
|
|
|
|
|
|
// Check if it matches edge conditions listed in gl.FilteredSearchTokenKeys.get()
|
|
|
|
let conditionIndex = 0;
|
|
|
|
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) {
|
|
|
|
// Parse params based on rules provided in the conditions key of gl.FilteredSearchTokenKeys.get()
|
2016-12-12 22:42:00 +00:00
|
|
|
inputValues.push(`${validCondition.key}:${validCondition.conditions[conditionIndex].keyword}`);
|
2016-12-12 22:37:49 +00:00
|
|
|
} 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 = gl.FilteredSearchTokenKeys.get().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;
|
|
|
|
let quotationsToUse;
|
|
|
|
|
|
|
|
if (valueHasSpace) {
|
|
|
|
// Prefer ", but use ' if required
|
|
|
|
quotationsToUse = sanitizedValue.indexOf('"') === -1 ? '"' : '\'';
|
|
|
|
}
|
|
|
|
|
2016-12-12 22:42:00 +00:00
|
|
|
inputValues.push(valueHasSpace ? `${sanitizedKey}:${symbol}${quotationsToUse}${sanitizedValue}${quotationsToUse}` : `${sanitizedKey}:${symbol}${sanitizedValue}`);
|
2016-12-12 22:37:49 +00:00
|
|
|
} else if (!match && key === 'search') {
|
2016-12-12 22:42:00 +00:00
|
|
|
inputValues.push(sanitizedValue);
|
2016-12-12 22:37:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Trim the last space value
|
2016-12-12 22:42:00 +00:00
|
|
|
this.filteredSearchInput.value = inputValues.join(' ');
|
2016-12-12 22:37:49 +00:00
|
|
|
|
2016-12-12 22:42:00 +00:00
|
|
|
if (inputValues.length > 0) {
|
2016-12-12 22:37:49 +00:00
|
|
|
this.clearSearchButton.classList.remove('hidden');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-04 21:27:11 +00:00
|
|
|
search() {
|
2016-12-12 22:42:00 +00:00
|
|
|
let paths = [];
|
2016-12-12 16:28:22 +00:00
|
|
|
const { tokens, searchToken } = this.tokenizer.processTokens(this.filteredSearchInput.value);
|
2016-12-13 03:15:50 +00:00
|
|
|
const currentState = gl.utils.getParameterByName('state') || 'opened';
|
2016-12-12 22:42:00 +00:00
|
|
|
paths.push(`state=${currentState}`);
|
2016-12-13 03:15:50 +00:00
|
|
|
|
2016-11-09 23:07:30 +00:00
|
|
|
tokens.forEach((token) => {
|
2016-11-30 18:30:52 +00:00
|
|
|
const match = gl.FilteredSearchTokenKeys.get().filter(t => t.key === token.key)[0];
|
2016-11-14 19:22:32 +00:00
|
|
|
let tokenPath = '';
|
|
|
|
|
|
|
|
if (token.wildcard && match.conditions) {
|
2016-11-14 23:45:26 +00:00
|
|
|
const condition = match.conditions
|
|
|
|
.filter(c => c.keyword === token.value.toLowerCase())[0];
|
2016-11-14 19:22:32 +00:00
|
|
|
|
|
|
|
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)}`;
|
|
|
|
}
|
|
|
|
|
2016-12-12 22:42:00 +00:00
|
|
|
paths.push(tokenPath);
|
2016-11-04 21:27:11 +00:00
|
|
|
});
|
|
|
|
|
2016-11-09 23:07:30 +00:00
|
|
|
if (searchToken) {
|
2016-12-12 22:42:00 +00:00
|
|
|
paths.push(`search=${encodeURIComponent(searchToken)}`);
|
2016-11-04 21:27:11 +00:00
|
|
|
}
|
|
|
|
|
2016-12-12 22:42:00 +00:00
|
|
|
window.location = `?scope=all&utf8=✓&${paths.join('&')}`;
|
2016-11-04 21:27:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
global.FilteredSearchManager = FilteredSearchManager;
|
2016-11-08 17:35:28 +00:00
|
|
|
})(window.gl || (window.gl = {}));
|