filter.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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'}]} (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. if (this.subField !== '')
  58. {
  59. const selector = [];
  60. this._selectedElements.forEach((element) => {
  61. const item = [];
  62. item[this.subField] = element;
  63. selector.push(item);
  64. });
  65. return {$in: selector};
  66. }
  67. else
  68. {
  69. return { $in: this._selectedElements };
  70. }
  71. }
  72. _getEmptySelector() {
  73. this._dep.depend();
  74. let includeEmpty = false;
  75. this._selectedElements.forEach((el) => {
  76. if (el === undefined) {
  77. includeEmpty = true;
  78. }
  79. });
  80. return includeEmpty ? { $eq: [] } : null;
  81. }
  82. }
  83. // The global Filter object.
  84. // XXX It would be possible to re-write this object more elegantly, and removing
  85. // the need to provide a list of `_fields`. We also should move methods into the
  86. // object prototype.
  87. Filter = {
  88. // XXX I would like to rename this field into `labels` to be consistent with
  89. // the rest of the schema, but we need to set some migrations architecture
  90. // before changing the schema.
  91. labelIds: new SetFilter(),
  92. members: new SetFilter(),
  93. customFields: new SetFilter('_id'),
  94. _fields: ['labelIds', 'members', 'customFields'],
  95. // We don't filter cards that have been added after the last filter change. To
  96. // implement this we keep the id of these cards in this `_exceptions` fields
  97. // and use a `$or` condition in the mongo selector we return.
  98. _exceptions: [],
  99. _exceptionsDep: new Tracker.Dependency(),
  100. isActive() {
  101. return _.any(this._fields, (fieldName) => {
  102. return this[fieldName]._isActive();
  103. });
  104. },
  105. _getMongoSelector() {
  106. if (!this.isActive())
  107. return {};
  108. const filterSelector = {};
  109. const emptySelector = {};
  110. let includeEmptySelectors = false;
  111. this._fields.forEach((fieldName) => {
  112. const filter = this[fieldName];
  113. if (filter._isActive()) {
  114. filterSelector[fieldName] = filter._getMongoSelector();
  115. emptySelector[fieldName] = filter._getEmptySelector();
  116. if (emptySelector[fieldName] !== null) {
  117. includeEmptySelectors = true;
  118. }
  119. }
  120. });
  121. const exceptionsSelector = {_id: {$in: this._exceptions}};
  122. this._exceptionsDep.depend();
  123. if (includeEmptySelectors)
  124. return {$or: [filterSelector, exceptionsSelector, emptySelector]};
  125. else
  126. return {$or: [filterSelector, exceptionsSelector]};
  127. },
  128. mongoSelector(additionalSelector) {
  129. const filterSelector = this._getMongoSelector();
  130. if (_.isUndefined(additionalSelector))
  131. return filterSelector;
  132. else
  133. return {$and: [filterSelector, additionalSelector]};
  134. },
  135. reset() {
  136. this._fields.forEach((fieldName) => {
  137. const filter = this[fieldName];
  138. filter.reset();
  139. });
  140. this.resetExceptions();
  141. },
  142. addException(_id) {
  143. if (this.isActive()) {
  144. this._exceptions.push(_id);
  145. this._exceptionsDep.changed();
  146. Tracker.flush();
  147. }
  148. },
  149. resetExceptions() {
  150. this._exceptions = [];
  151. this._exceptionsDep.changed();
  152. },
  153. };
  154. Blaze.registerHelper('Filter', Filter);