Commit d91d586a authored by Clement Ho's avatar Clement Ho

Add filter by last token

parent 00ed5aaf
......@@ -10,13 +10,22 @@ droplab.plugin(function init(DropLab) {
var matches = [];
// will only work on dynamically set data
// and if a config text property is set
if(!data || !config.hasOwnProperty('text')){
if(!data || (!config.hasOwnProperty('text') && !config.hasOwnProperty('filter'))){
return;
}
matches = data.map(function(o){
var filterFunction = function(o){
// cheap string search
o.droplab_hidden = o[config.text].toLowerCase().indexOf(value) === -1;
return o;
};
if (config.hasOwnProperty('filter') && config.filter !== undefined) {
filterFunction = config.filter;
}
matches = data.map(function(o) {
return filterFunction(o, value);
});
list.render(matches);
}
......
......@@ -19,6 +19,14 @@
super.renderContent();
droplab.setData(this.hookId, '/autocomplete/users.json?search=&per_page=20&active=true&project_id=2&group_id=&skip_ldap=&todo_filter=&todo_state_filter=&current_user=true&push_code_to_protected_branches=&author_id=&skip_users=');
}
filterMethod(item, query) {
const { value } = gl.FilteredSearchTokenizer.getLastTokenObject(query);
const valueWithoutPrefix = value.slice(1);
item.droplab_hidden = item['username'].indexOf(valueWithoutPrefix) === -1;
return item;
}
}
global.DropdownAuthor = DropdownAuthor;
......
......@@ -50,12 +50,17 @@
getFilterConfig(filterKeyword) {
const config = {};
const filterConfig = {
text: filterKeyword,
};
const filterConfig = {};
config[this.hookId] = filterKeyword ? filterConfig : {};
if (filterKeyword) {
filterConfig.text = filterKeyword;
}
if (this.filterMethod) {
filterConfig.filter = this.filterMethod;
}
config[this.hookId] = filterConfig;
return config;
}
......
......@@ -29,6 +29,55 @@
}
}
static getLastTokenObject(input) {
const token = FilteredSearchTokenizer.getLastToken(input);
const colonIndex = token.indexOf(':');
const key = colonIndex !== -1 ? token.slice(0, colonIndex) : '';
const value = colonIndex !== -1 ? token.slice(colonIndex) : token;
return {
key,
value,
}
}
static getLastToken(input) {
let completeToken = false;
let completeQuotation = true;
let lastQuotation = '';
let i = input.length;
const doubleQuote = '"';
const singleQuote = '\'';
while(!completeToken && i >= 0) {
const isDoubleQuote = input[i] === doubleQuote;
const isSingleQuote = input[i] === singleQuote;
// If the second quotation is found
if ((lastQuotation === doubleQuote && input[i] === doubleQuote) ||
(lastQuotation === singleQuote && input[i] === singleQuote)) {
completeQuotation = true;
}
// Save the first quotation
if ((input[i] === doubleQuote && lastQuotation === '') ||
(input[i] === singleQuote && lastQuotation === '')) {
lastQuotation = input[i];
completeQuotation = false;
}
if (completeQuotation && input[i] === ' ') {
completeToken = true;
} else {
i--;
}
}
// Adjust by 1 because of empty space
return input.slice(i + 1);
}
static processTokens(input) {
let tokens = [];
let searchToken = '';
......
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