gitlab-org--gitlab-foss/app/assets/javascripts/filtered_search/filtered_search_manager.js.es6

275 lines
9.6 KiB
JavaScript
Raw Normal View History

2016-11-09 20:31:58 +00:00
/* eslint-disable no-param-reassign */
2016-11-04 21:27:11 +00:00
((global) => {
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
2016-11-14 23:45:26 +00:00
if (e.target.value) {
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-30 18:30:52 +00:00
// Check if it matches edge conditions listed in gl.FilteredSearchTokenKeys.get()
2016-11-14 19:22:32 +00:00
let conditionIndex = 0;
2016-11-30 18:30:52 +00:00
const validCondition = gl.FilteredSearchTokenKeys.get()
2016-11-14 23:45:26 +00:00
.filter(v => v.conditions && v.conditions.filter((c, index) => {
if (c.url === p) {
conditionIndex = index;
}
return c.url === p;
})[0])[0];
2016-11-14 19:22:32 +00:00
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;
2016-11-30 18:30:52 +00:00
const match = gl.FilteredSearchTokenKeys.get().filter(t => key === `${t.key}_${t.param}`)[0];
2016-11-14 19:22:32 +00:00
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
let dropdownHint;
2016-12-02 22:30:19 +00:00
let dropdownAuthor;
2016-12-02 21:04:10 +00:00
let dropdownAssignee;
2016-12-02 22:43:15 +00:00
let dropdownMilestone;
let dropdownLabel;
2016-11-04 21:27:11 +00:00
class FilteredSearchManager {
constructor() {
2016-11-30 18:30:52 +00:00
this.tokenizer = gl.FilteredSearchTokenizer;
2016-11-04 21:27:11 +00:00
this.bindEvents();
2016-11-09 20:31:58 +00:00
loadSearchParamsFromURL();
this.setDropdown();
2016-11-04 21:27:11 +00:00
}
static addWordToInput(word, addSpace) {
const filteredSearchValue = document.querySelector('.filtered-search').value;
const hasExistingValue = filteredSearchValue.length !== 0;
const { lastToken } = gl.FilteredSearchTokenizer.processTokens(filteredSearchValue);
if (lastToken.hasOwnProperty('key')) {
console.log(lastToken);
// Spaces inside the token means that the token value will be escaped by quotes
const hasQuotes = lastToken.value.indexOf(' ') !== -1;
const lengthToRemove = hasQuotes ? lastToken.value.length + 2 : lastToken.value.length;
document.querySelector('.filtered-search').value = filteredSearchValue.slice(0, -1 * (lengthToRemove));
}
document.querySelector('.filtered-search').value += hasExistingValue && addSpace ? ` ${word}` : word;
}
loadDropdown(dropdownName = '', hideDropdown) {
dropdownName = dropdownName.toLowerCase();
const filterIconPadding = 27;
const match = gl.FilteredSearchTokenKeys.get().filter(value => value.key === dropdownName)[0];
const filteredSearch = document.querySelector('.filtered-search');
if (!this.font) {
this.font = window.getComputedStyle(filteredSearch).font;
}
if (match && this.currentDropdown !== match.key) {
console.log(`🦄 load ${match.key} dropdown`);
const dynamicDropdownPadding = 12;
const dropdownOffset = gl.text.getTextWidth(filteredSearch.value, this.font) + filterIconPadding + dynamicDropdownPadding;
2016-12-02 21:04:10 +00:00
this.dismissCurrentDropdown();
this.currentDropdown = match.key;
2016-12-02 21:04:10 +00:00
2016-12-02 22:30:19 +00:00
if (match.key === 'author') {
if (!dropdownAuthor) {
dropdownAuthor = new gl.DropdownAuthor(document.querySelector('#js-dropdown-author'), filteredSearch);
2016-12-02 22:30:19 +00:00
}
2016-12-02 21:04:10 +00:00
dropdownAuthor.setOffset(dropdownOffset);
2016-12-02 22:30:19 +00:00
dropdownAuthor.render();
} else if (match.key === 'assignee') {
if (!dropdownAssignee) {
dropdownAssignee = new gl.DropdownAssignee(document.querySelector('#js-dropdown-assignee'), filteredSearch);
2016-12-02 21:04:10 +00:00
}
dropdownAssignee.setOffset(dropdownOffset);
2016-12-02 21:04:10 +00:00
dropdownAssignee.render();
2016-12-02 22:43:15 +00:00
} else if (match.key === 'milestone') {
if (!dropdownMilestone) {
dropdownMilestone = new gl.DropdownMilestone(document.querySelector('#js-dropdown-milestone'), filteredSearch);
2016-12-02 22:43:15 +00:00
}
dropdownMilestone.setOffset(dropdownOffset);
2016-12-02 22:43:15 +00:00
dropdownMilestone.render();
} else if (match.key === 'label') {
if (!dropdownLabel) {
dropdownLabel = new gl.DropdownLabel(document.querySelector('#js-dropdown-label'), filteredSearch);
2016-12-02 22:43:15 +00:00
}
dropdownLabel.setOffset(dropdownOffset);
2016-12-02 22:43:15 +00:00
dropdownLabel.render();
2016-12-02 21:04:10 +00:00
}
} else if (!match && this.currentDropdown !== 'hint') {
console.log('🦄 load hint dropdown');
const dropdownOffset = gl.text.getTextWidth(filteredSearch.value, this.font) + filterIconPadding;
console.log(dropdownOffset)
this.dismissCurrentDropdown();
this.currentDropdown = 'hint';
if (!dropdownHint) {
dropdownHint = new gl.DropdownHint(document.querySelector('#js-dropdown-hint'), filteredSearch, this.currentDropdown);
}
dropdownHint.setOffset(dropdownOffset);
dropdownHint.render();
}
}
setDropdown() {
const { lastToken } = this.tokenizer.processTokens(document.querySelector('.filtered-search').value);
if (typeof lastToken === 'string') {
// Token is not fully initialized yet
// because it has no value
// Eg. token = 'label:'
const { tokenKey } = this.tokenizer.parseToken(lastToken);
this.loadDropdown(tokenKey);
} else if (lastToken.hasOwnProperty('key')) {
// Token has been initialized into an object
// because it has a value
this.loadDropdown(lastToken.key);
} else {
this.loadDropdown('hint');
}
}
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
filteredSearchInput.addEventListener('input', this.setDropdown.bind(this));
2016-11-09 23:07:30 +00:00
filteredSearchInput.addEventListener('input', toggleClearSearchButton);
filteredSearchInput.addEventListener('keydown', this.checkForEnter.bind(this));
document.querySelector('.clear-search').addEventListener('click', clearSearch);
2016-11-04 21:27:11 +00:00
}
checkDropdownToken(e) {
2016-11-14 16:37:55 +00:00
const input = e.target.value;
const { lastToken } = this.tokenizer.processTokens(input);
// Check for dropdown token
if (lastToken[lastToken.length - 1] === ':') {
const token = lastToken.slice(0, -1);
}
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-30 18:30:52 +00:00
const { tokens, searchToken } = this.tokenizer.processTokens(document.querySelector('.filtered-search').value);
2016-11-09 23:07:30 +00:00
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-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)}`;
}
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 = {}));