query-classes.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. import {
  2. OPERATOR_ASSIGNEE,
  3. OPERATOR_BOARD,
  4. OPERATOR_COMMENT,
  5. OPERATOR_LABEL,
  6. OPERATOR_LIST,
  7. OPERATOR_MEMBER,
  8. OPERATOR_SWIMLANE,
  9. OPERATOR_USER,
  10. } from './search-const';
  11. import Boards from '../models/boards';
  12. export class QueryParams {
  13. text = '';
  14. constructor(params = {}) {
  15. this.params = params;
  16. }
  17. hasOperator(operator) {
  18. return this.params[operator];
  19. }
  20. addPredicate(operator, predicate) {
  21. if (!this.hasOperator(operator)) {
  22. this.params[operator] = [];
  23. }
  24. this.params[operator].push(predicate);
  25. }
  26. setPredicate(operator, predicate) {
  27. this.params[operator] = predicate;
  28. }
  29. getPredicate(operator) {
  30. return this.params[operator][0];
  31. }
  32. getPredicates(operator) {
  33. return this.params[operator];
  34. }
  35. getParams() {
  36. return this.params;
  37. }
  38. }
  39. export class QueryErrors {
  40. operatorTagMap = [
  41. [OPERATOR_BOARD, 'board-title-not-found'],
  42. [OPERATOR_SWIMLANE, 'swimlane-title-not-found'],
  43. [
  44. OPERATOR_LABEL,
  45. label => {
  46. if (Boards.labelColors().includes(label)) {
  47. return {
  48. tag: 'label-color-not-found',
  49. value: label,
  50. color: true,
  51. };
  52. } else {
  53. return {
  54. tag: 'label-not-found',
  55. value: label,
  56. color: false,
  57. };
  58. }
  59. },
  60. ],
  61. [OPERATOR_LIST, 'list-title-not-found'],
  62. [OPERATOR_COMMENT, 'comment-not-found'],
  63. [OPERATOR_USER, 'user-username-not-found'],
  64. [OPERATOR_ASSIGNEE, 'user-username-not-found'],
  65. [OPERATOR_MEMBER, 'user-username-not-found'],
  66. ];
  67. constructor() {
  68. this._errors = {};
  69. this.operatorTags = {};
  70. this.operatorTagMap.forEach(([operator, tag]) => {
  71. this.operatorTags[operator] = tag;
  72. });
  73. this.colorMap = Boards.colorMap();
  74. }
  75. addError(operator, error) {
  76. if (!this._errors[operator]) {
  77. this._errors[operator] = [];
  78. }
  79. this._errors[operator].push(error);
  80. }
  81. addNotFound(operator, value) {
  82. if (typeof this.operatorTags[operator] === 'function') {
  83. this.addError(operator, this.operatorTags[operator](value));
  84. } else {
  85. this.addError(operator, { tag: this.operatorTags[operator], value });
  86. }
  87. }
  88. hasErrors() {
  89. return Object.entries(this._errors).length > 0;
  90. }
  91. errors() {
  92. const errs = [];
  93. Object.entries(this._errors).forEach(([operator, errors]) => {
  94. errors.forEach(err => {
  95. errs.push(err);
  96. });
  97. });
  98. return errs;
  99. }
  100. errorMessages() {
  101. const messages = [];
  102. Object.entries(this._errors).forEach(([operator, errors]) => {
  103. errors.forEach(err => {
  104. messages.push(TAPi18n.__(err.tag, err.value));
  105. });
  106. });
  107. return messages;
  108. }
  109. }
  110. export class Query {
  111. params = {};
  112. selector = {};
  113. projection = {};
  114. constructor(selector, projection) {
  115. this._errors = new QueryErrors();
  116. if (selector) {
  117. this.selector = selector;
  118. }
  119. if (projection) {
  120. this.projection = projection;
  121. }
  122. }
  123. hasErrors() {
  124. return this._errors.hasErrors();
  125. }
  126. errors() {
  127. return this._errors.errors();
  128. }
  129. errorMessages() {
  130. return this._errors.errorMessages();
  131. }
  132. }