2021-03-09 02:21:43 +02:00
|
|
|
import {
|
|
|
|
OPERATOR_ASSIGNEE,
|
|
|
|
OPERATOR_BOARD,
|
|
|
|
OPERATOR_COMMENT,
|
|
|
|
OPERATOR_LABEL,
|
|
|
|
OPERATOR_LIST,
|
|
|
|
OPERATOR_MEMBER,
|
|
|
|
OPERATOR_SWIMLANE,
|
|
|
|
OPERATOR_USER,
|
|
|
|
} from './search-const';
|
2021-03-09 19:30:04 +02:00
|
|
|
import Boards from '../models/boards';
|
2021-03-09 02:21:43 +02:00
|
|
|
|
|
|
|
export class QueryParams {
|
|
|
|
text = '';
|
|
|
|
|
|
|
|
constructor(params = {}) {
|
|
|
|
this.params = params;
|
|
|
|
}
|
|
|
|
|
|
|
|
hasOperator(operator) {
|
|
|
|
return this.params[operator];
|
|
|
|
}
|
|
|
|
|
|
|
|
addPredicate(operator, predicate) {
|
|
|
|
if (!this.hasOperator(operator)) {
|
|
|
|
this.params[operator] = [];
|
|
|
|
}
|
|
|
|
this.params[operator].push(predicate);
|
|
|
|
}
|
|
|
|
|
|
|
|
setPredicate(operator, predicate) {
|
|
|
|
this.params[operator] = predicate;
|
|
|
|
}
|
|
|
|
|
|
|
|
getPredicate(operator) {
|
|
|
|
return this.params[operator][0];
|
|
|
|
}
|
|
|
|
|
|
|
|
getPredicates(operator) {
|
|
|
|
return this.params[operator];
|
|
|
|
}
|
|
|
|
|
|
|
|
getParams() {
|
|
|
|
return this.params;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class QueryErrors {
|
2021-03-09 19:30:04 +02:00
|
|
|
operatorTagMap = [
|
|
|
|
[OPERATOR_BOARD, 'board-title-not-found'],
|
|
|
|
[OPERATOR_SWIMLANE, 'swimlane-title-not-found'],
|
|
|
|
[
|
|
|
|
OPERATOR_LABEL,
|
|
|
|
label => {
|
|
|
|
if (Boards.labelColors().includes(label)) {
|
|
|
|
return {
|
|
|
|
tag: 'label-color-not-found',
|
|
|
|
value: label,
|
|
|
|
color: true,
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
return {
|
|
|
|
tag: 'label-not-found',
|
|
|
|
value: label,
|
|
|
|
color: false,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
},
|
|
|
|
],
|
|
|
|
[OPERATOR_LIST, 'list-title-not-found'],
|
|
|
|
[OPERATOR_COMMENT, 'comment-not-found'],
|
|
|
|
[OPERATOR_USER, 'user-username-not-found'],
|
|
|
|
[OPERATOR_ASSIGNEE, 'user-username-not-found'],
|
|
|
|
[OPERATOR_MEMBER, 'user-username-not-found'],
|
|
|
|
];
|
|
|
|
|
2021-03-09 02:21:43 +02:00
|
|
|
constructor() {
|
2021-03-09 19:30:04 +02:00
|
|
|
this._errors = {};
|
|
|
|
|
|
|
|
this.operatorTags = {};
|
|
|
|
this.operatorTagMap.forEach(([operator, tag]) => {
|
|
|
|
this.operatorTags[operator] = tag;
|
|
|
|
});
|
2021-03-09 02:21:43 +02:00
|
|
|
|
|
|
|
this.colorMap = Boards.colorMap();
|
|
|
|
}
|
|
|
|
|
2021-03-09 19:30:04 +02:00
|
|
|
addError(operator, error) {
|
|
|
|
if (!this._errors[operator]) {
|
|
|
|
this._errors[operator] = [];
|
|
|
|
}
|
|
|
|
this._errors[operator].push(error);
|
|
|
|
}
|
|
|
|
|
|
|
|
addNotFound(operator, value) {
|
|
|
|
if (typeof this.operatorTags[operator] === 'function') {
|
|
|
|
this.addError(operator, this.operatorTags[operator](value));
|
|
|
|
} else {
|
|
|
|
this.addError(operator, { tag: this.operatorTags[operator], value });
|
2021-03-09 02:21:43 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
hasErrors() {
|
2021-03-09 19:30:04 +02:00
|
|
|
return Object.entries(this._errors).length > 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
errors() {
|
|
|
|
const errs = [];
|
|
|
|
Object.entries(this._errors).forEach(([operator, errors]) => {
|
|
|
|
errors.forEach(err => {
|
|
|
|
errs.push(err);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
return errs;
|
2021-03-09 02:21:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
errorMessages() {
|
|
|
|
const messages = [];
|
2021-03-09 19:30:04 +02:00
|
|
|
Object.entries(this._errors).forEach(([operator, errors]) => {
|
|
|
|
errors.forEach(err => {
|
|
|
|
messages.push(TAPi18n.__(err.tag, err.value));
|
|
|
|
});
|
2021-03-09 02:21:43 +02:00
|
|
|
});
|
|
|
|
return messages;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class Query {
|
|
|
|
params = {};
|
|
|
|
selector = {};
|
|
|
|
projection = {};
|
|
|
|
|
|
|
|
constructor(selector, projection) {
|
2021-03-09 19:30:04 +02:00
|
|
|
this._errors = new QueryErrors();
|
2021-03-09 02:21:43 +02:00
|
|
|
if (selector) {
|
|
|
|
this.selector = selector;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (projection) {
|
|
|
|
this.projection = projection;
|
|
|
|
}
|
|
|
|
}
|
2021-03-09 19:30:04 +02:00
|
|
|
|
|
|
|
hasErrors() {
|
|
|
|
return this._errors.hasErrors();
|
|
|
|
}
|
|
|
|
|
|
|
|
errors() {
|
|
|
|
return this._errors.errors();
|
|
|
|
}
|
|
|
|
|
|
|
|
errorMessages() {
|
|
|
|
return this._errors.errorMessages();
|
|
|
|
}
|
2021-03-09 02:21:43 +02:00
|
|
|
}
|