filter.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  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. this._lastValide={};
  77. }
  78. set(str)
  79. {
  80. this._filter = str;
  81. this._dep.changed();
  82. }
  83. reset() {
  84. this._filter = '';
  85. this._lastValide={};
  86. this._dep.changed();
  87. }
  88. _isActive() {
  89. this._dep.depend();
  90. return this._filter !== '';
  91. }
  92. _filterToCommands(){
  93. const commands = [];
  94. let current = '';
  95. let string = false;
  96. let wasString = false;
  97. let ignore = false;
  98. for (let i = 0; i < this._filter.length; i++)
  99. {
  100. const char = this._filter.charAt(i);
  101. if (ignore)
  102. {
  103. ignore = false;
  104. continue;
  105. }
  106. if (char === '\'')
  107. {
  108. string = !string;
  109. if (string) wasString = true;
  110. continue;
  111. }
  112. if (char === '\\')
  113. {
  114. ignore = true;
  115. continue;
  116. }
  117. if (char === ' ' && !string)
  118. {
  119. commands.push({'cmd':current, 'string':wasString});
  120. wasString = false;
  121. current = '';
  122. continue;
  123. }
  124. current += char;
  125. }
  126. if (current !== '')
  127. {
  128. commands.push({'cmd':current, 'string':wasString});
  129. }
  130. return commands;
  131. }
  132. _fieldNameToId(field)
  133. {
  134. const found = CustomFields.findOne({'name':field});
  135. return found._id;
  136. }
  137. _arrayToSelector(commands)
  138. {
  139. try {
  140. //let changed = false;
  141. this._processSubCommands(commands);
  142. }
  143. catch (e){return this._lastValide;}
  144. this._lastValide = {$or: commands};
  145. return {$or: commands};
  146. }
  147. _processSubCommands(commands)
  148. {
  149. const subcommands = [];
  150. let level = 0;
  151. let start = -1;
  152. for (let i = 0; i < commands.length; i++)
  153. {
  154. if (commands[i].cmd)
  155. {
  156. switch (commands[i].cmd)
  157. {
  158. case '(':
  159. {
  160. level++;
  161. if (start === -1) start = i;
  162. continue;
  163. }
  164. case ')':
  165. {
  166. level--;
  167. commands.splice(i, 1);
  168. i--;
  169. continue;
  170. }
  171. default:
  172. {
  173. if (level > 0)
  174. {
  175. subcommands.push(commands[i]);
  176. commands.splice(i, 1);
  177. i--;
  178. continue;
  179. }
  180. }
  181. }
  182. }
  183. }
  184. if (start !== -1)
  185. {
  186. this._processSubCommands(subcommands);
  187. if (subcommands.length === 1)
  188. commands.splice(start, 0, subcommands[0]);
  189. else
  190. commands.splice(start, 0, subcommands);
  191. }
  192. this._processConditions(commands);
  193. this._processLogicalOperators(commands);
  194. }
  195. _processConditions(commands)
  196. {
  197. for (let i = 0; i < commands.length; i++)
  198. {
  199. if (!commands[i].string && commands[i].cmd)
  200. {
  201. switch (commands[i].cmd)
  202. {
  203. case '=':
  204. case '==':
  205. case '===':
  206. {
  207. const field = commands[i-1].cmd;
  208. const str = commands[i+1].cmd;
  209. commands[i] = {'customFields._id':this._fieldNameToId(field), 'customFields.value':str};
  210. commands.splice(i-1, 1);
  211. commands.splice(i, 1);
  212. //changed = true;
  213. i--;
  214. break;
  215. }
  216. case '!=':
  217. case '!==':
  218. {
  219. const field = commands[i-1].cmd;
  220. const str = commands[i+1].cmd;
  221. commands[i] = {'customFields._id':this._fieldNameToId(field), 'customFields.value': { $not: str }};
  222. commands.splice(i-1, 1);
  223. commands.splice(i, 1);
  224. //changed = true;
  225. i--;
  226. break;
  227. }
  228. case '>':
  229. case 'gt':
  230. case 'Gt':
  231. case 'GT':
  232. {
  233. const field = commands[i-1].cmd;
  234. const str = commands[i+1].cmd;
  235. commands[i] = {'customFields._id':this._fieldNameToId(field), 'customFields.value': { $gt: str } };
  236. commands.splice(i-1, 1);
  237. commands.splice(i, 1);
  238. //changed = true;
  239. i--;
  240. break;
  241. }
  242. case '>=':
  243. case '>==':
  244. case 'gte':
  245. case 'Gte':
  246. case 'GTE':
  247. {
  248. const field = commands[i-1].cmd;
  249. const str = commands[i+1].cmd;
  250. commands[i] = {'customFields._id':this._fieldNameToId(field), 'customFields.value': { $gte: str } };
  251. commands.splice(i-1, 1);
  252. commands.splice(i, 1);
  253. //changed = true;
  254. i--;
  255. break;
  256. }
  257. case '<':
  258. case 'lt':
  259. case 'Lt':
  260. case 'LT':
  261. {
  262. const field = commands[i-1].cmd;
  263. const str = commands[i+1].cmd;
  264. commands[i] = {'customFields._id':this._fieldNameToId(field), 'customFields.value': { $lt: str } };
  265. commands.splice(i-1, 1);
  266. commands.splice(i, 1);
  267. //changed = true;
  268. i--;
  269. break;
  270. }
  271. case '<=':
  272. case '<==':
  273. case 'lte':
  274. case 'Lte':
  275. case 'LTE':
  276. {
  277. const field = commands[i-1].cmd;
  278. const str = commands[i+1].cmd;
  279. commands[i] = {'customFields._id':this._fieldNameToId(field), 'customFields.value': { $lte: str } };
  280. commands.splice(i-1, 1);
  281. commands.splice(i, 1);
  282. //changed = true;
  283. i--;
  284. break;
  285. }
  286. }
  287. }
  288. }
  289. }
  290. _processLogicalOperators(commands)
  291. {
  292. for (let i = 0; i < commands.length; i++)
  293. {
  294. if (!commands[i].string && commands[i].cmd)
  295. {
  296. switch (commands[i].cmd)
  297. {
  298. case 'or':
  299. case 'Or':
  300. case 'OR':
  301. case '|':
  302. case '||':
  303. {
  304. const op1 = commands[i-1];
  305. const op2 = commands[i+1];
  306. commands[i] = {$or: [op1, op2]};
  307. commands.splice(i-1, 1);
  308. commands.splice(i, 1);
  309. //changed = true;
  310. i--;
  311. break;
  312. }
  313. case 'and':
  314. case 'And':
  315. case 'AND':
  316. case '&':
  317. case '&&':
  318. {
  319. const op1 = commands[i-1];
  320. const op2 = commands[i+1];
  321. commands[i] = {$and: [op1, op2]};
  322. commands.splice(i-1, 1);
  323. commands.splice(i, 1);
  324. //changed = true;
  325. i--;
  326. break;
  327. }
  328. case 'not':
  329. case 'Not':
  330. case 'NOT':
  331. case '!':
  332. {
  333. const op1 = commands[i+1];
  334. commands[i] = {$not: op1};
  335. commands.splice(i+1, 1);
  336. //changed = true;
  337. i--;
  338. break;
  339. }
  340. }
  341. }
  342. }
  343. }
  344. _getMongoSelector() {
  345. this._dep.depend();
  346. const commands = this._filterToCommands();
  347. return this._arrayToSelector(commands);
  348. }
  349. }
  350. // The global Filter object.
  351. // XXX It would be possible to re-write this object more elegantly, and removing
  352. // the need to provide a list of `_fields`. We also should move methods into the
  353. // object prototype.
  354. Filter = {
  355. // XXX I would like to rename this field into `labels` to be consistent with
  356. // the rest of the schema, but we need to set some migrations architecture
  357. // before changing the schema.
  358. labelIds: new SetFilter(),
  359. members: new SetFilter(),
  360. customFields: new SetFilter('_id'),
  361. advanced: new AdvancedFilter(),
  362. _fields: ['labelIds', 'members', 'customFields'],
  363. // We don't filter cards that have been added after the last filter change. To
  364. // implement this we keep the id of these cards in this `_exceptions` fields
  365. // and use a `$or` condition in the mongo selector we return.
  366. _exceptions: [],
  367. _exceptionsDep: new Tracker.Dependency(),
  368. isActive() {
  369. return _.any(this._fields, (fieldName) => {
  370. return this[fieldName]._isActive();
  371. }) || this.advanced._isActive();
  372. },
  373. _getMongoSelector() {
  374. if (!this.isActive())
  375. return {};
  376. const filterSelector = {};
  377. const emptySelector = {};
  378. let includeEmptySelectors = false;
  379. this._fields.forEach((fieldName) => {
  380. const filter = this[fieldName];
  381. if (filter._isActive()) {
  382. if (filter.subField !== '')
  383. {
  384. filterSelector[`${fieldName}.${filter.subField}`] = filter._getMongoSelector();
  385. }
  386. else
  387. {
  388. filterSelector[fieldName] = filter._getMongoSelector();
  389. }
  390. emptySelector[fieldName] = filter._getEmptySelector();
  391. if (emptySelector[fieldName] !== null) {
  392. includeEmptySelectors = true;
  393. }
  394. }
  395. });
  396. const exceptionsSelector = {_id: {$in: this._exceptions}};
  397. this._exceptionsDep.depend();
  398. const selectors = [exceptionsSelector];
  399. if (_.any(this._fields, (fieldName) => {
  400. return this[fieldName]._isActive();
  401. })) selectors.push(filterSelector);
  402. if (includeEmptySelectors) selectors.push(emptySelector);
  403. if (this.advanced._isActive()) selectors.push(this.advanced._getMongoSelector());
  404. return {$or: selectors};
  405. },
  406. mongoSelector(additionalSelector) {
  407. const filterSelector = this._getMongoSelector();
  408. if (_.isUndefined(additionalSelector))
  409. return filterSelector;
  410. else
  411. return {$and: [filterSelector, additionalSelector]};
  412. },
  413. reset() {
  414. this._fields.forEach((fieldName) => {
  415. const filter = this[fieldName];
  416. filter.reset();
  417. });
  418. this.advanced.reset();
  419. this.resetExceptions();
  420. },
  421. addException(_id) {
  422. if (this.isActive()) {
  423. this._exceptions.push(_id);
  424. this._exceptionsDep.changed();
  425. Tracker.flush();
  426. }
  427. },
  428. resetExceptions() {
  429. this._exceptions = [];
  430. this._exceptionsDep.changed();
  431. },
  432. };
  433. Blaze.registerHelper('Filter', Filter);