Commit f4db7572 authored by Clement Ho's avatar Clement Ho

Refactor filtered_search_dropdown

parent 46a1f369
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
constructor(droplab, dropdown, input, endpoint, symbol) { constructor(droplab, dropdown, input, endpoint, symbol) {
super(droplab, dropdown, input); super(droplab, dropdown, input);
this.listId = dropdown.id; this.listId = dropdown.id;
this.symbol = symbol;
this.config = { this.config = {
droplabAjax: { droplabAjax: {
endpoint: endpoint, endpoint: endpoint,
...@@ -13,7 +14,7 @@ ...@@ -13,7 +14,7 @@
loadingTemplate: this.loadingTemplate, loadingTemplate: this.loadingTemplate,
}, },
droplabFilter: { droplabFilter: {
filterFunction: this.filterWithSymbol.bind(this, symbol), filterFunction: this.filterWithSymbol.bind(this, this.symbol),
} }
}; };
} }
...@@ -23,13 +24,47 @@ ...@@ -23,13 +24,47 @@
if (!dataValueSet) { if (!dataValueSet) {
const title = e.detail.selected.querySelector('.js-data-value').innerText.trim(); const title = e.detail.selected.querySelector('.js-data-value').innerText.trim();
const name = `%${this.getEscapedText(title)}`; const name = `${this.symbol}${this.getEscapedText(title)}`;
gl.FilteredSearchManager.addWordToInput(this.getSelectedText(name)); gl.FilteredSearchManager.addWordToInput(this.getSelectedText(name));
} }
this.dismissDropdown(!dataValueSet); this.dismissDropdown(!dataValueSet);
} }
getEscapedText(text) {
let escapedText = text;
// Encapsulate value with quotes if it has spaces
if (text.indexOf(' ') !== -1) {
if (text.indexOf('"') !== -1) {
// Use single quotes if value contains double quotes
escapedText = `'${text}'`;
} else {
// Known side effect: values's with both single and double quotes
// won't escape properly
escapedText = `"${text}"`;
}
}
return escapedText;
}
filterWithSymbol(filterSymbol, item, query) {
const { value } = gl.FilteredSearchTokenizer.getLastTokenObject(query);
const valueWithoutColon = value.slice(1).toLowerCase();
const prefix = valueWithoutColon[0];
const valueWithoutPrefix = valueWithoutColon.slice(1);
const title = item.title.toLowerCase();
// Eg. filterSymbol = ~ for labels
const matchWithoutPrefix = prefix === filterSymbol && title.indexOf(valueWithoutPrefix) !== -1;
const match = title.indexOf(valueWithoutColon) !== -1;
item.droplab_hidden = !match && !matchWithoutPrefix;
return item;
}
renderContent(forceShowList = false) { renderContent(forceShowList = false) {
this.droplab.changeHookList(this.hookId, this.dropdown, [droplabAjax, droplabFilter], this.config); this.droplab.changeHookList(this.hookId, this.dropdown, [droplabAjax, droplabFilter], this.config);
super.renderContent(forceShowList); super.renderContent(forceShowList);
......
...@@ -38,6 +38,10 @@ ...@@ -38,6 +38,10 @@
super.renderContent(forceShowList); super.renderContent(forceShowList);
} }
getProjectId() {
return this.input.getAttribute('data-project-id');
}
getSearchInput() { getSearchInput() {
const query = document.querySelector('.filtered-search').value; const query = document.querySelector('.filtered-search').value;
const { value } = gl.FilteredSearchTokenizer.getLastTokenObject(query); const { value } = gl.FilteredSearchTokenizer.getLastTokenObject(query);
......
...@@ -4,7 +4,6 @@ ...@@ -4,7 +4,6 @@
class FilteredSearchDropdown { class FilteredSearchDropdown {
constructor(droplab, dropdown, input) { constructor(droplab, dropdown, input) {
console.log('constructor');
this.droplab = droplab; this.droplab = droplab;
this.hookId = 'filtered-search'; this.hookId = 'filtered-search';
this.input = input; this.input = input;
...@@ -24,32 +23,10 @@ ...@@ -24,32 +23,10 @@
this.dropdown.removeEventListener('click.dl', this.itemClickedWrapper); this.dropdown.removeEventListener('click.dl', this.itemClickedWrapper);
} }
getProjectId() {
return this.input.getAttribute('data-project-id');
}
getCurrentHook() { getCurrentHook() {
return this.droplab.hooks.filter(h => h.id === this.hookId)[0]; return this.droplab.hooks.filter(h => h.id === this.hookId)[0];
} }
getEscapedText(text) {
let escapedText = text;
// Encapsulate value with quotes if it has spaces
if (text.indexOf(' ') !== -1) {
if (text.indexOf('"') !== -1) {
// Use single quotes if value contains double quotes
escapedText = `'${text}'`;
} else {
// Known side effect: values's with both single and double quotes
// won't escape properly
escapedText = `"${text}"`;
}
}
return escapedText;
}
getSelectedText(selectedToken) { getSelectedText(selectedToken) {
// TODO: Get last word from FilteredSearchTokenizer // TODO: Get last word from FilteredSearchTokenizer
const lastWord = this.input.value.split(' ').last(); const lastWord = this.input.value.split(' ').last();
...@@ -109,22 +86,6 @@ ...@@ -109,22 +86,6 @@
} }
} }
filterWithSymbol(filterSymbol, item, query) {
const { value } = gl.FilteredSearchTokenizer.getLastTokenObject(query);
const valueWithoutColon = value.slice(1).toLowerCase();
const prefix = valueWithoutColon[0];
const valueWithoutPrefix = valueWithoutColon.slice(1);
const title = item.title.toLowerCase();
// Eg. filterSymbol = ~ for labels
const matchWithoutPrefix = prefix === filterSymbol && title.indexOf(valueWithoutPrefix) !== -1;
const match = title.indexOf(valueWithoutColon) !== -1;
item.droplab_hidden = !match && !matchWithoutPrefix;
return item;
}
hideDropdown() { hideDropdown() {
this.getCurrentHook().list.hide(); this.getCurrentHook().list.hide();
} }
......
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