fabric8-planner
Version:
A planner front-end for Fabric8.
167 lines • 7.4 kB
JavaScript
import { Injectable } from '@angular/core';
import { UserService } from 'ngx-login-client';
import { BehaviorSubject, of } from 'rxjs';
import { debounceTime, distinctUntilChanged, map, switchMap } from 'rxjs/operators';
import { AreaQuery } from '../models/area.model';
import { IterationQuery } from '../models/iteration.model';
import { LabelQuery } from '../models/label.model';
import { AND, ENCLOSURE, EQUAL_QUERY, NOT_EQUAL_QUERY, OR, P_START } from './query-keys';
var keys_before_field = [
AND, OR, P_START
];
var keys_before_value = [
EQUAL_QUERY, NOT_EQUAL_QUERY
];
var QuerySuggestionService = /** @class */ (function () {
function QuerySuggestionService(areaQuery, iterationQuery, userService, labelQuery) {
this.areaQuery = areaQuery;
this.iterationQuery = iterationQuery;
this.userService = userService;
this.labelQuery = labelQuery;
this.queryObservable = new BehaviorSubject('-');
this.valueToSuggest = '';
this.valueToSuggestObservable = new BehaviorSubject('-');
this.fields = of({
'area.name': this.areaQuery.getAreaNames(),
'iteration.name': this.iterationQuery.getIterationNames,
'parent.number': of([]),
'title': of([]),
'assignee': this.fetchUsersBySearchValue(),
'label.name': this.labelQuery.getlabelNames
});
}
QuerySuggestionService.prototype.fetchUsersBySearchValue = function () {
var _this = this;
return this.valueToSuggestObservable.pipe(debounceTime(500), switchMap(function (value) { return _this.userService.getUsersBySearchString(value); }), map(function (users) { return users.map(function (user) { return user.attributes.username; }); }));
};
/**
* Takes the entire query written so far
* split the query by keywords for the
* ease of processing
* @param query
*/
QuerySuggestionService.prototype.shouldSuggestField = function (query) {
var output = { suggest: true, value: query.trim(), lastKey: '' };
for (var i = 0; i < keys_before_field.length; i++) {
var temp = output.value.split(keys_before_field[i]);
if (temp.length > 1) {
// split with the key and take the last value from the array
output = {
suggest: true,
value: temp[temp.length - 1].trim(),
lastKey: keys_before_field[i]
};
}
}
if (output.value.indexOf(EQUAL_QUERY) > -1 ||
output.value.indexOf(NOT_EQUAL_QUERY) > -1) {
output = {
suggest: false,
value: output.value,
lastKey: ''
};
}
return output;
};
/**
* Takes a chunk of the query
* extract the field and typed value
* @param query
*/
QuerySuggestionService.prototype.suggestValue = function (query) {
// split with equal
var splitField = query.split(EQUAL_QUERY);
// if no luck split with not equal
if (splitField.length === 1) {
splitField = query.split(NOT_EQUAL_QUERY);
}
var field = splitField[0].trim();
// could be a IN query so we split it by ,
var splittedvalues = splitField[1].split(',');
// take the last one from the array
var value = splittedvalues[splittedvalues.length - 1].trim();
// trim the value if it has enclouser
if (value[0] == ENCLOSURE) {
value = value.substr(1);
}
if (value[value.length - 1] == ENCLOSURE) {
value = value.substr(0, value.length - 1);
}
return { field: field, value: value };
};
QuerySuggestionService.prototype.replaceSuggestion = function (query_before_cursor, query_after_cursor, suggestion) {
return this.replaceSuggestedValue(query_before_cursor, query_after_cursor, this.valueToSuggest, suggestion);
};
/**
* This function takes the query and the suggested value to be replaced
* the value is what is typed so far or before the cursor after the last key
* for example, if the query before cursor is "area.name: area - 1 $AND iteration"
* and after cursor is ".name: iteration - 1"
* the value should be "iteration" and we'll have to remove that part from
* query before cursor
* For query after cursor section, we have to detect the first occurance of any key
* and remove till that from initial of that part.
* For our example the first key is ":" and we have to remove ".name" from that section
* @param query_before_cursor
* @param query_after_cursor
* @param value
* @param suggestion
*/
QuerySuggestionService.prototype.replaceSuggestedValue = function (query_before_cursor, query_after_cursor, value, suggestion) {
var all_keys = keys_before_field.concat(keys_before_value);
var first_part = value == '' ? query_before_cursor :
query_before_cursor.substr(0, query_before_cursor.length - value.length).trim();
var after_cursor = query_after_cursor;
all_keys.forEach(function (key) {
if (after_cursor !== '') {
after_cursor = after_cursor.split(key)[0];
}
});
var last_part = query_after_cursor.substr(after_cursor.length);
return (first_part + " " + suggestion + " " + last_part).trim();
};
QuerySuggestionService.prototype.getSuggestions = function () {
var _this = this;
return this.queryObservable
.pipe(distinctUntilChanged(), switchMap(function (query) {
if (query === '-') {
return of([]);
}
var fieldSuggest = _this.shouldSuggestField(query);
if (fieldSuggest.suggest) {
_this.valueToSuggest = fieldSuggest.value;
_this.valueToSuggestObservable.next(fieldSuggest.value);
return _this.fields.pipe(map(function (fields) { return Object.keys(fields)
.filter(function (f) { return f.indexOf(fieldSuggest.value) > -1; }); }));
}
else {
var fieldValue_1 = _this.suggestValue(fieldSuggest.value);
_this.valueToSuggest = fieldValue_1.value;
_this.valueToSuggestObservable.next(fieldValue_1.value);
return _this.fields.pipe(switchMap(function (fields) {
if (fields[fieldValue_1.field]) {
return fields[fieldValue_1.field].pipe(map(function (values) {
return values.filter(function (v) { return v.indexOf(fieldValue_1.value) > -1; });
}));
}
else {
return of([]);
}
}));
}
}));
};
QuerySuggestionService.decorators = [
{ type: Injectable },
];
/** @nocollapse */
QuerySuggestionService.ctorParameters = function () { return [
{ type: AreaQuery, },
{ type: IterationQuery, },
{ type: UserService, },
{ type: LabelQuery, },
]; };
return QuerySuggestionService;
}());
export { QuerySuggestionService };
//# sourceMappingURL=query-suggestion.service.js.map