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();
|
2016-12-17 19:22:00 +00:00
|
|
|
const { lastToken, searchToken } = gl.FilteredSearchTokenizer.processTokens(query);
|
2016-12-14 05:30:28 +00:00
|
|
|
|
2016-12-17 19:22:00 +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
|
|
|
|
2016-12-17 19:22:00 +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;
|
|
|
|
}
|
|
|
|
|
2016-12-17 19:22:00 +00:00
|
|
|
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;
|
2017-01-19 10:10:53 +00:00
|
|
|
const { right } = gl.DropdownUtils.getInputSelectionPosition(filteredSearchInput);
|
2017-01-18 21:39:43 +00:00
|
|
|
|
2017-01-19 10:10:53 +00:00
|
|
|
if (right < 0) {
|
2017-01-18 21:39:43 +00:00
|
|
|
return inputValue;
|
|
|
|
}
|
|
|
|
|
2017-01-19 10:10:53 +00:00
|
|
|
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;
|
|
|
|
})();
|