filter.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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 wasString = false;
  95. let ignore = false;
  96. for (let i = 0; i < this._filter.length; i++)
  97. {
  98. const char = this._filter.charAt(i);
  99. if (ignore)
  100. {
  101. ignore = false;
  102. continue;
  103. }
  104. if (char === '\'')
  105. {
  106. string = !string;
  107. if (string) wasString = true;
  108. continue;
  109. }
  110. if (char === '\\')
  111. {
  112. ignore = true;
  113. continue;
  114. }
  115. if (char === ' ' && !string)
  116. {
  117. commands.push({'cmd':current, 'string':wasString});
  118. wasString = false;
  119. current = '';
  120. continue;
  121. }
  122. current += char;
  123. }
  124. if (current !== '')
  125. {
  126. commands.push({'cmd':current, 'string':wasString});
  127. }
  128. return commands;
  129. }
  130. _fieldNameToId(field)
  131. {
  132. console.log(`searching: ${field}`);
  133. const found = CustomFields.findOne({'name':field});
  134. console.log(found);
  135. return found._id;
  136. }
  137. _arrayToSelector(commands)
  138. {
  139. console.log(commands);
  140. try {
  141. //let changed = false;
  142. this._processConditions(commands);
  143. this._processLogicalOperators(commands);
  144. }
  145. catch (e){return { $in: [] };}
  146. return {$or: commands};
  147. }
  148. _processConditions(commands)
  149. {
  150. for (let i = 0; i < commands.length; i++)
  151. {
  152. if (!commands[i].string && commands[i].cmd)
  153. {
  154. switch (commands[i].cmd)
  155. {
  156. case '=':
  157. case '==':
  158. case '===':
  159. {
  160. const field = commands[i-1].cmd;
  161. const str = commands[i+1].cmd;
  162. commands[i] = {'customFields._id':this._fieldNameToId(field), 'customFields.value':str};
  163. commands.splice(i-1, 1);
  164. commands.splice(i, 1);
  165. //changed = true;
  166. i--;
  167. break;
  168. }
  169. }
  170. }
  171. }
  172. }
  173. _processLogicalOperators(commands)
  174. {
  175. for (let i = 0; i < commands.length; i++)
  176. {
  177. if (!commands[i].string && commands[i].cmd)
  178. {
  179. switch (commands[i].cmd)
  180. {
  181. case 'or':
  182. case 'Or':
  183. case 'OR':
  184. case '|':
  185. case '||':
  186. {
  187. const op1 = commands[i-1].cmd;
  188. const op2 = commands[i+1].cmd;
  189. commands[i] = {$or: [op1, op2]};
  190. commands.splice(i-1, 1);
  191. commands.splice(i, 1);
  192. //changed = true;
  193. i--;
  194. break;
  195. }
  196. }
  197. }
  198. }
  199. }
  200. _getMongoSelector() {
  201. this._dep.depend();
  202. const commands = this._filterToCommands();
  203. return this._arrayToSelector(commands);
  204. }
  205. }
  206. // The global Filter object.
  207. // XXX It would be possible to re-write this object more elegantly, and removing
  208. // the need to provide a list of `_fields`. We also should move methods into the
  209. // object prototype.
  210. Filter = {
  211. // XXX I would like to rename this field into `labels` to be consistent with
  212. // the rest of the schema, but we need to set some migrations architecture
  213. // before changing the schema.
  214. labelIds: new SetFilter(),
  215. members: new SetFilter(),
  216. customFields: new SetFilter('_id'),
  217. advanced: new AdvancedFilter(),
  218. _fields: ['labelIds', 'members', 'customFields'],
  219. // We don't filter cards that have been added after the last filter change. To
  220. // implement this we keep the id of these cards in this `_exceptions` fields
  221. // and use a `$or` condition in the mongo selector we return.
  222. _exceptions: [],
  223. _exceptionsDep: new Tracker.Dependency(),
  224. isActive() {
  225. return _.any(this._fields, (fieldName) => {
  226. return this[fieldName]._isActive();
  227. }) || this.advanced._isActive();
  228. },
  229. _getMongoSelector() {
  230. if (!this.isActive())
  231. return {};
  232. const filterSelector = {};
  233. const emptySelector = {};
  234. let includeEmptySelectors = false;
  235. this._fields.forEach((fieldName) => {
  236. const filter = this[fieldName];
  237. if (filter._isActive()) {
  238. if (filter.subField !== '')
  239. {
  240. filterSelector[`${fieldName}.${filter.subField}`] = filter._getMongoSelector();
  241. }
  242. else
  243. {
  244. filterSelector[fieldName] = filter._getMongoSelector();
  245. }
  246. emptySelector[fieldName] = filter._getEmptySelector();
  247. if (emptySelector[fieldName] !== null) {
  248. includeEmptySelectors = true;
  249. }
  250. }
  251. });
  252. const exceptionsSelector = {_id: {$in: this._exceptions}};
  253. this._exceptionsDep.depend();
  254. const selectors = [exceptionsSelector];
  255. if (_.any(this._fields, (fieldName) => {
  256. return this[fieldName]._isActive();
  257. })) selectors.push(filterSelector);
  258. if (includeEmptySelectors) selectors.push(emptySelector);
  259. if (this.advanced._isActive()) selectors.push(this.advanced._getMongoSelector());
  260. return {$or: selectors};
  261. },
  262. mongoSelector(additionalSelector) {
  263. const filterSelector = this._getMongoSelector();
  264. if (_.isUndefined(additionalSelector))
  265. return filterSelector;
  266. else
  267. return {$and: [filterSelector, additionalSelector]};
  268. },
  269. reset() {
  270. this._fields.forEach((fieldName) => {
  271. const filter = this[fieldName];
  272. filter.reset();
  273. });
  274. this.resetExceptions();
  275. },
  276. addException(_id) {
  277. if (this.isActive()) {
  278. this._exceptions.push(_id);
  279. this._exceptionsDep.changed();
  280. Tracker.flush();
  281. }
  282. },
  283. resetExceptions() {
  284. this._exceptions = [];
  285. this._exceptionsDep.changed();
  286. },
  287. };
  288. Blaze.registerHelper('Filter', Filter);