summaryrefslogtreecommitdiffstats
path: root/client/components/sidebar/sidebarArchives.js
blob: 75b694e9e7e967e19ed25bbaa37d90128ed33c2b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
archivedRequested = false;
const subManager = new SubsManager();

BlazeComponent.extendComponent({
  onCreated() {
    this.isArchiveReady = new ReactiveVar(false);

    // The pattern we use to manually handle data loading is described here:
    // https://kadira.io/academy/meteor-routing-guide/content/subscriptions-and-data-management/using-subs-manager
    // XXX The boardId should be readed from some sort the component "props",
    // unfortunatly, Blaze doesn't have this notion.
    this.autorun(() => {
      const currentBoardId = Session.get('currentBoard');
      if (!currentBoardId) return;
      const handle = subManager.subscribe('board', currentBoardId, true);
      archivedRequested = true;
      Tracker.nonreactive(() => {
        Tracker.autorun(() => {
          this.isArchiveReady.set(handle.ready());
        });
      });
    });
  },

  tabs() {
    return [
      { name: TAPi18n.__('cards'), slug: 'cards' },
      { name: TAPi18n.__('lists'), slug: 'lists' },
      { name: TAPi18n.__('swimlanes'), slug: 'swimlanes' },
    ];
  },

  archivedCards() {
    return Cards.find({
      archived: true,
      boardId: Session.get('currentBoard'),
    });
  },

  archivedLists() {
    return Lists.find({
      archived: true,
      boardId: Session.get('currentBoard'),
    });
  },

  archivedSwimlanes() {
    return Swimlanes.find({
      archived: true,
      boardId: Session.get('currentBoard'),
    });
  },

  cardIsInArchivedList() {
    return this.currentData().list().archived;
  },

  onRendered() {
    // XXX We should support dragging a card from the sidebar to the board
  },

  events() {
    return [
      {
        'click .js-restore-card'() {
          const card = this.currentData();
          if (card.canBeRestored()) {
            card.restore();
          }
        },
        'click .js-restore-all-cards'() {
          this.archivedCards().forEach(card => {
            if (card.canBeRestored()) {
              card.restore();
            }
          });
        },

        'click .js-delete-card': Popup.afterConfirm('cardDelete', function() {
          const cardId = this._id;
          Cards.remove(cardId);
          Popup.close();
        }),
        'click .js-delete-all-cards': Popup.afterConfirm('cardDelete', () => {
          this.archivedCards().forEach(card => {
            Cards.remove(card._id);
          });
          Popup.close();
        }),

        'click .js-restore-list'() {
          const list = this.currentData();
          list.restore();
        },
        'click .js-restore-all-lists'() {
          this.archivedLists().forEach(list => {
            list.restore();
          });
        },

        'click .js-delete-list': Popup.afterConfirm('listDelete', function() {
          this.remove();
          Popup.close();
        }),
        'click .js-delete-all-lists': Popup.afterConfirm('listDelete', () => {
          this.archivedLists().forEach(list => {
            list.remove();
          });
          Popup.close();
        }),

        'click .js-restore-swimlane'() {
          const swimlane = this.currentData();
          swimlane.restore();
        },
        'click .js-restore-all-swimlanes'() {
          this.archivedSwimlanes().forEach(swimlane => {
            swimlane.restore();
          });
        },

        'click .js-delete-swimlane': Popup.afterConfirm(
          'swimlaneDelete',
          function() {
            this.remove();
            Popup.close();
          },
        ),
        'click .js-delete-all-swimlanes': Popup.afterConfirm(
          'swimlaneDelete',
          () => {
            this.archivedSwimlanes().forEach(swimlane => {
              swimlane.remove();
            });
            Popup.close();
          },
        ),
      },
    ];
  },
}).register('archivesSidebar');

Template.archivesSidebar.helpers({
  isWorker() {
    const currentBoard = Boards.findOne(Session.get('currentBoard'));
    return (
      !currentBoard.hasAdmin(this.userId) && currentBoard.hasWorker(this.userId)
    );
  },
});