filter.js 12 KB

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