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

105 lines
3.2 KiB
JavaScript
Raw Normal View History

2016-12-14 05:30:28 +00:00
(() => {
class DropdownUtils {
static getEscapedText(text) {
let escapedText = text;
const hasSpace = text.indexOf(' ') !== -1;
const hasDoubleQuote = text.indexOf('"') !== -1;
// Encapsulate value with quotes if it has spaces
// Known side effect: values's with both single and double quotes
// won't escape properly
if (hasSpace) {
if (hasDoubleQuote) {
escapedText = `'${text}'`;
} else {
// Encapsulate singleQuotes or if it hasSpace
escapedText = `"${text}"`;
}
}
return escapedText;
}
2017-01-19 10:30:29 +00:00
static filterWithSymbol(filterSymbol, input, item) {
2016-12-14 05:30:28 +00:00
const updatedItem = item;
2017-01-19 10:30:29 +00:00
const query = gl.DropdownUtils.getSearchInput(input).trim();
const { lastToken, searchToken } = gl.FilteredSearchTokenizer.processTokens(query);
2016-12-14 05:30:28 +00:00
if (lastToken !== searchToken) {
const title = updatedItem.title.toLowerCase();
2017-01-06 18:47:00 +00:00
let value = lastToken.value.toLowerCase();
if ((value[0] === '"' || value[0] === '\'') && title.indexOf(' ') !== -1) {
value = value.slice(1);
}
2016-12-14 05:30:28 +00:00
// Eg. filterSymbol = ~ for labels
const matchWithoutSymbol = lastToken.symbol === filterSymbol && title.indexOf(value) !== -1;
const match = title.indexOf(`${lastToken.symbol}${value}`) !== -1;
updatedItem.droplab_hidden = !match && !matchWithoutSymbol;
} else {
updatedItem.droplab_hidden = false;
}
2016-12-14 05:30:28 +00:00
return updatedItem;
}
static filterHint(item, query) {
2016-12-14 05:30:28 +00:00
const updatedItem = item;
2016-12-18 01:55:44 +00:00
let { lastToken } = gl.FilteredSearchTokenizer.processTokens(query);
2016-12-18 23:04:29 +00:00
lastToken = lastToken.key || lastToken || '';
2016-12-14 05:30:28 +00:00
2016-12-18 01:55:44 +00:00
if (!lastToken || query.split('').last() === ' ') {
2016-12-14 05:30:28 +00:00
updatedItem.droplab_hidden = false;
2016-12-18 01:55:44 +00:00
} else if (lastToken) {
const split = lastToken.split(':');
const tokenName = split[0].split(' ').last();
const match = updatedItem.hint.indexOf(tokenName.toLowerCase()) === -1;
updatedItem.droplab_hidden = tokenName ? match : false;
2016-12-14 05:30:28 +00:00
}
return updatedItem;
}
2017-01-05 19:56:23 +00:00
static setDataValueIfSelected(filter, selected) {
2016-12-14 05:30:28 +00:00
const dataValue = selected.getAttribute('data-value');
if (dataValue) {
2017-01-05 19:56:23 +00:00
gl.FilteredSearchDropdownManager.addWordToInput(filter, dataValue);
2016-12-14 05:30:28 +00:00
}
// Return boolean based on whether it was set
return dataValue !== null;
}
2017-01-18 21:39:43 +00:00
static getSearchInput(filteredSearchInput) {
const selectionStart = filteredSearchInput.selectionStart;
const inputValue = filteredSearchInput.value;
const { right } = gl.DropdownUtils.getInputSelectionPosition(filteredSearchInput);
2017-01-18 21:39:43 +00:00
if (right < 0) {
2017-01-18 21:39:43 +00:00
return inputValue;
}
return inputValue.slice(0, right + selectionStart + 1).trim();
}
static getInputSelectionPosition(input) {
const inputValue = input.value;
const selectionStart = input.selectionStart;
const left = inputValue.slice(0, selectionStart + 1).search(/\S+$/);
const right = inputValue.slice(selectionStart).search(/\s/);
return {
left,
right,
};
2017-01-18 21:39:43 +00:00
}
2016-12-14 05:30:28 +00:00
}
window.gl = window.gl || {};
gl.DropdownUtils = DropdownUtils;
})();