filter.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. // Advanced filter forms a MongoSelector from a users String.
  71. // Build by: Ignatz 19.05.2018 (github feuerball11)
  72. class AdvancedFilter {
  73. constructor() {
  74. this._dep = new Tracker.Dependency();
  75. this._filter = '';
  76. }
  77. set(str)
  78. {
  79. this._filter = str;
  80. this._dep.changed();
  81. }
  82. reset() {
  83. this._filter = '';
  84. this._dep.changed();
  85. }
  86. _isActive() {
  87. this._dep.depend();
  88. return this._filter !== '';
  89. }
  90. _filterToCommands(){
  91. const commands = [];
  92. let current = '';
  93. let string = false;
  94. let ignore = false;
  95. for (let i = 0; i < this._filter.length; i++)
  96. {
  97. const char = this._filter.charAt(i);
  98. if (ignore)
  99. {
  100. ignore = false;
  101. continue;
  102. }
  103. if (char === '\'')
  104. {
  105. string = true;
  106. continue;
  107. }
  108. if (char === '\\')
  109. {
  110. ignore = true;
  111. continue;
  112. }
  113. if (char === ' ' && !string)
  114. {
  115. commands.push({'cmd':current, string});
  116. string = false;
  117. current = '';
  118. continue;
  119. }
  120. current += char;
  121. }
  122. if (current !== '')
  123. {
  124. commands.push(current);
  125. }
  126. return commands;
  127. }
  128. _fieldNameToId(name)
  129. {
  130. CustomFields.find({name})._id;
  131. }
  132. _arrayToSelector(commands)
  133. {
  134. console.log(commands);
  135. try {
  136. //let changed = false;
  137. for (let i = 0; i < commands.length; i++)
  138. {
  139. if (!commands[i].string && commands[i].cmd)
  140. {
  141. switch (commands[i].cmd)
  142. {
  143. case '=':
  144. case '==':
  145. case '===':
  146. {
  147. const field = commands[i-1].cmd;
  148. const str = commands[i+1].cmd;
  149. commands[i] = {'customFields._id':this._fieldNameToId(field), 'customFields.value':str};
  150. commands.splice(i-1, 1);
  151. commands.splice(i, 1);
  152. //changed = true;
  153. i--;
  154. break;
  155. }
  156. }
  157. }
  158. }
  159. }
  160. catch (e){return { $in: [] };}
  161. return {$or: commands};
  162. }
  163. _getMongoSelector() {
  164. this._dep.depend();
  165. const commands = this._filterToCommands();
  166. return this._arrayToSelector(commands);
  167. }
  168. }
  169. // The global Filter object.
  170. // XXX It would be possible to re-write this object more elegantly, and removing
  171. // the need to provide a list of `_fields`. We also should move methods into the
  172. // object prototype.
  173. Filter = {
  174. // XXX I would like to rename this field into `labels` to be consistent with
  175. // the rest of the schema, but we need to set some migrations architecture
  176. // before changing the schema.
  177. labelIds: new SetFilter(),
  178. members: new SetFilter(),
  179. customFields: new SetFilter('_id'),
  180. advanced: new AdvancedFilter(),
  181. _fields: ['labelIds', 'members', 'customFields'],
  182. // We don't filter cards that have been added after the last filter change. To
  183. // implement this we keep the id of these cards in this `_exceptions` fields
  184. // and use a `$or` condition in the mongo selector we return.
  185. _exceptions: [],
  186. _exceptionsDep: new Tracker.Dependency(),
  187. isActive() {
  188. return _.any(this._fields, (fieldName) => {
  189. return this[fieldName]._isActive();
  190. }) || this.advanced._isActive();
  191. },
  192. _getMongoSelector() {
  193. if (!this.isActive())
  194. return {};
  195. const filterSelector = {};
  196. const emptySelector = {};
  197. let includeEmptySelectors = false;
  198. this._fields.forEach((fieldName) => {
  199. const filter = this[fieldName];
  200. if (filter._isActive()) {
  201. if (filter.subField !== '')
  202. {
  203. filterSelector[`${fieldName}.${filter.subField}`] = filter._getMongoSelector();
  204. }
  205. else
  206. {
  207. filterSelector[fieldName] = filter._getMongoSelector();
  208. }
  209. emptySelector[fieldName] = filter._getEmptySelector();
  210. if (emptySelector[fieldName] !== null) {
  211. includeEmptySelectors = true;
  212. }
  213. }
  214. });
  215. const exceptionsSelector = {_id: {$in: this._exceptions}};
  216. this._exceptionsDep.depend();
  217. console.log(this.advanced._getMongoSelector());
  218. if (includeEmptySelectors)
  219. return {
  220. $or: [filterSelector, exceptionsSelector, this.advanced._getMongoSelector(), emptySelector],
  221. };
  222. else
  223. return {
  224. $or: [filterSelector, exceptionsSelector, this.advanced._getMongoSelector()],
  225. };
  226. },
  227. mongoSelector(additionalSelector) {
  228. const filterSelector = this._getMongoSelector();
  229. if (_.isUndefined(additionalSelector))
  230. return filterSelector;
  231. else
  232. return {$and: [filterSelector, additionalSelector]};
  233. },
  234. reset() {
  235. this._fields.forEach((fieldName) => {
  236. const filter = this[fieldName];
  237. filter.reset();
  238. });
  239. this.resetExceptions();
  240. },
  241. addException(_id) {
  242. if (this.isActive()) {
  243. this._exceptions.push(_id);
  244. this._exceptionsDep.changed();
  245. Tracker.flush();
  246. }
  247. },
  248. resetExceptions() {
  249. this._exceptions = [];
  250. this._exceptionsDep.changed();
  251. },
  252. };
  253. Blaze.registerHelper('Filter', Filter);