filter.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. // Filtered view manager
  2. // We define local filter objects for each different type of field (SetFilter,
  3. // RangeFilter, dateFilter, etc.). We then define a global `Filter` object whose
  4. // goal is to filter complete documents by using the local filters for each
  5. // fields.
  6. function showFilterSidebar() {
  7. Sidebar.setView('filter');
  8. }
  9. // Use a "set" filter for a field that is a set of documents uniquely
  10. // identified. For instance `{ labels: ['labelA', 'labelC', 'labelD'] }`.
  11. // use "subField" for searching inside object Fields.
  12. // For instance '{ 'customFields._id': ['field1','field2']} (subField would be: _id)
  13. class SetFilter {
  14. constructor(subField = '') {
  15. this._dep = new Tracker.Dependency();
  16. this._selectedElements = [];
  17. this.subField = subField;
  18. }
  19. isSelected(val) {
  20. this._dep.depend();
  21. return this._selectedElements.indexOf(val) > -1;
  22. }
  23. add(val) {
  24. if (this._indexOfVal(val) === -1) {
  25. this._selectedElements.push(val);
  26. this._dep.changed();
  27. showFilterSidebar();
  28. }
  29. }
  30. remove(val) {
  31. const indexOfVal = this._indexOfVal(val);
  32. if (this._indexOfVal(val) !== -1) {
  33. this._selectedElements.splice(indexOfVal, 1);
  34. this._dep.changed();
  35. }
  36. }
  37. toggle(val) {
  38. if (this._indexOfVal(val) === -1) {
  39. this.add(val);
  40. } else {
  41. this.remove(val);
  42. }
  43. }
  44. reset() {
  45. this._selectedElements = [];
  46. this._dep.changed();
  47. }
  48. _indexOfVal(val) {
  49. return this._selectedElements.indexOf(val);
  50. }
  51. _isActive() {
  52. this._dep.depend();
  53. return this._selectedElements.length !== 0;
  54. }
  55. _getMongoSelector() {
  56. this._dep.depend();
  57. return { $in: this._selectedElements };
  58. }
  59. _getEmptySelector() {
  60. this._dep.depend();
  61. let includeEmpty = false;
  62. this._selectedElements.forEach((el) => {
  63. if (el === undefined) {
  64. includeEmpty = true;
  65. }
  66. });
  67. return includeEmpty ? { $eq: [] } : null;
  68. }
  69. }
  70. // The global Filter object.
  71. // XXX It would be possible to re-write this object more elegantly, and removing
  72. // the need to provide a list of `_fields`. We also should move methods into the
  73. // object prototype.
  74. Filter = {
  75. // XXX I would like to rename this field into `labels` to be consistent with
  76. // the rest of the schema, but we need to set some migrations architecture
  77. // before changing the schema.
  78. labelIds: new SetFilter(),
  79. members: new SetFilter(),
  80. customFields: new SetFilter('_id'),
  81. _fields: ['labelIds', 'members', 'customFields'],
  82. // We don't filter cards that have been added after the last filter change. To
  83. // implement this we keep the id of these cards in this `_exceptions` fields
  84. // and use a `$or` condition in the mongo selector we return.
  85. _exceptions: [],
  86. _exceptionsDep: new Tracker.Dependency(),
  87. isActive() {
  88. return _.any(this._fields, (fieldName) => {
  89. return this[fieldName]._isActive();
  90. });
  91. },
  92. _getMongoSelector() {
  93. if (!this.isActive())
  94. return {};
  95. const filterSelector = {};
  96. const emptySelector = {};
  97. let includeEmptySelectors = false;
  98. this._fields.forEach((fieldName) => {
  99. const filter = this[fieldName];
  100. if (filter._isActive()) {
  101. if (filter.subField !== '')
  102. {
  103. filterSelector[`${fieldName}.${filter.subField}`] = filter._getMongoSelector();
  104. }
  105. else
  106. {
  107. filterSelector[fieldName] = filter._getMongoSelector();
  108. }
  109. emptySelector[fieldName] = filter._getEmptySelector();
  110. if (emptySelector[fieldName] !== null) {
  111. includeEmptySelectors = true;
  112. }
  113. }
  114. });
  115. const exceptionsSelector = {_id: {$in: this._exceptions}};
  116. this._exceptionsDep.depend();
  117. if (includeEmptySelectors)
  118. return {$or: [filterSelector, exceptionsSelector, emptySelector]};
  119. else
  120. return {$or: [filterSelector, exceptionsSelector]};
  121. },
  122. mongoSelector(additionalSelector) {
  123. const filterSelector = this._getMongoSelector();
  124. if (_.isUndefined(additionalSelector))
  125. return filterSelector;
  126. else
  127. return {$and: [filterSelector, additionalSelector]};
  128. },
  129. reset() {
  130. this._fields.forEach((fieldName) => {
  131. const filter = this[fieldName];
  132. filter.reset();
  133. });
  134. this.resetExceptions();
  135. },
  136. addException(_id) {
  137. if (this.isActive()) {
  138. this._exceptions.push(_id);
  139. this._exceptionsDep.changed();
  140. Tracker.flush();
  141. }
  142. },
  143. resetExceptions() {
  144. this._exceptions = [];
  145. this._exceptionsDep.changed();
  146. },
  147. };
  148. Blaze.registerHelper('Filter', Filter);