boards_store.js.es6 3.3 KB
Newer Older
P
Phil Hughes 已提交
1 2 3
((w) => {
  w.BoardsStore = {
    state: {
P
Phil Hughes 已提交
4
      lists: [],
P
Phil Hughes 已提交
5 6 7 8 9 10 11 12
      done: {},
      filters: {
        author: {},
        assignee: {},
        milestone: {},
      }
    },
    removeBoard: (id) => {
P
Phil Hughes 已提交
13
      BoardsStore.state.lists = _.reject(BoardsStore.state.lists, (board) => {
P
Phil Hughes 已提交
14 15 16 17
        return board.id === id;
      });
    },
    moveBoard: (oldIndex, newIndex) => {
P
Phil Hughes 已提交
18
      const boardFrom = _.find(BoardsStore.state.lists, (board) => {
P
Phil Hughes 已提交
19 20 21 22 23
        return board.index === oldIndex;
      });

      service.updateBoard(boardFrom.id, newIndex);

P
Phil Hughes 已提交
24
      const boardTo = _.find(BoardsStore.state.lists, (board) => {
P
Phil Hughes 已提交
25 26 27 28 29 30 31 32 33 34 35
        return board.index === newIndex;
      });

      boardFrom.index = newIndex;
      if (newIndex > boardTo.index) {
        boardTo.index--;
      } else {
        boardTo.index++;
      }
    },
    moveCardToBoard: (boardFromId, boardToId, issueId, toIndex) => {
P
Phil Hughes 已提交
36
      const boardFrom = _.find(BoardsStore.state.lists, (board) => {
P
Phil Hughes 已提交
37 38
        return board.id === boardFromId;
      });
P
Phil Hughes 已提交
39
      const boardTo = _.find(BoardsStore.state.lists, (board) => {
P
Phil Hughes 已提交
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
        return board.id === boardToId;
      });
      let issue = _.find(boardFrom.issues, (issue) => {
        return issue.id === issueId;
      });
      const issueTo = _.find(boardTo.issues, (issue) => {
        return issue.id === issueId;
      });
      const issueBoards = BoardsStore.getBoardsForIssue(issue);

      // Remove the issue from old board
      boardFrom.issues = _.reject(boardFrom.issues, (issue) => {
        return issue.id === issueId;
      });

      // Add to new boards issues if it doesn't already exist
P
Phil Hughes 已提交
56
      if (issueTo) {
P
Phil Hughes 已提交
57 58 59 60 61 62 63 64 65 66 67
        issue = issueTo;
      } else {
        boardTo.issues.splice(toIndex, 0, issue);
      }

      if (boardTo.id === 'done' && boardFrom.id !== 'backlog') {
        BoardsStore.removeIssueFromBoards(issue, issueBoards);
        issue.labels = _.reject(issue.labels, (label) => {
          return label.title === boardFrom.title;
        });
      } else {
P
Phil Hughes 已提交
68 69 70 71 72
        if (boardTo.label) {
          if (boardFrom.id !== 'backlog') {
            BoardsStore.removeIssueFromBoard(issue, boardFrom);
          }
          
P
Phil Hughes 已提交
73 74 75 76
          foundLabel = _.find(issue.labels, (label) => {
            return label.title === boardTo.title;
          });

P
Phil Hughes 已提交
77
          if (!foundLabel) {
P
Phil Hughes 已提交
78 79 80 81 82 83 84
            issue.labels.push(boardTo.label);
          }
        }
      }
    },
    removeIssueFromBoards: (issue, boards) => {
      const boardLabels = _.map(boards, (board) => {
P
Phil Hughes 已提交
85
        return board.title;
P
Phil Hughes 已提交
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
      });

      boards.issues = _.each(boards, (board) => {
        board.issues = _.reject(board.issues, (boardIssue) => {
          return issue.id === boardIssue.id;
        });
      });

      issue.labels = _.reject(issue.labels, (label) => {
        return boardLabels.indexOf(label.title) !== -1;
      });
    },
    removeIssueFromBoard: (issue, board) => {
      issue.labels = _.reject(issue.labels, (label) => {
        return label.title === board.title;
      });
    },
    getBoardsForIssue: (issue) => {
P
Phil Hughes 已提交
104
      return _.filter(BoardsStore.state.lists, (board) => {
P
Phil Hughes 已提交
105 106 107
        const foundIssue = _.find(board.issues, (boardIssue) => {
          return issue.id === boardIssue.id;
        });
P
Phil Hughes 已提交
108
        return foundIssue;
P
Phil Hughes 已提交
109 110 111 112 113 114 115
      });
    },
    clearDone: () => {
      Vue.set(BoardsStore.state, 'done', {});
    }
  };
}(window));