boardMultiSelection.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import { ReactiveCache } from '/imports/reactiveCache';
  2. BoardMultiSelection = {
  3. _selectedBoards: new ReactiveVar([]),
  4. _isActive: new ReactiveVar(false),
  5. reset() {
  6. this._selectedBoards.set([]);
  7. },
  8. isActive() {
  9. return this._isActive.get();
  10. },
  11. count() {
  12. return this._selectedBoards.get().length;
  13. },
  14. isEmpty() {
  15. return this.count() === 0;
  16. },
  17. getSelectedBoardIds() {
  18. return this._selectedBoards.get();
  19. },
  20. activate() {
  21. if (!this.isActive()) {
  22. this._isActive.set(true);
  23. Tracker.flush();
  24. }
  25. },
  26. disable() {
  27. if (this.isActive()) {
  28. this._isActive.set(false);
  29. this.reset();
  30. }
  31. },
  32. add(boardIds) {
  33. return this.toggle(boardIds, { add: true, remove: false });
  34. },
  35. remove(boardIds) {
  36. return this.toggle(boardIds, { add: false, remove: true });
  37. },
  38. toogle(boardIds) {
  39. return this.toggle(boardIds, { add: true, remove: true });
  40. },
  41. toggle(boardIds, { add, remove } = {}) {
  42. boardIds = _.isString(boardIds) ? [boardIds] : boardIds;
  43. let selectedBoards = this._selectedBoards.get();
  44. boardIds.forEach(boardId => {
  45. const index = selectedBoards.indexOf(boardId);
  46. if (index > -1 && remove) {
  47. selectedBoards = selectedBoards.filter(id => id !== boardId);
  48. } else if (index === -1 && add) {
  49. selectedBoards.push(boardId);
  50. }
  51. });
  52. this._selectedBoards.set(selectedBoards);
  53. },
  54. isSelected(boardId) {
  55. return this._selectedBoards.get().includes(boardId);
  56. },
  57. };