filter.js 13 KB

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