boards_store.js.es6 3.7 KB
Newer Older
1
/* eslint-disable comma-dangle, space-before-function-paren, one-var, no-shadow, dot-notation, max-len */
2 3 4
/* global Cookies */
/* global List */

5 6 7 8 9
(() => {
  window.gl = window.gl || {};
  window.gl.issueBoards = window.gl.issueBoards || {};

  gl.issueBoards.BoardsStore = {
10
    disabled: false,
11 12 13
    state: {},
    detail: {
      issue: {}
P
Phil Hughes 已提交
14
    },
P
Phil Hughes 已提交
15 16
    modal: {
      issues: [],
P
Phil Hughes 已提交
17
      showAddIssuesModal: true,
P
Phil Hughes 已提交
18 19
      activeTab: 'all',
    },
P
Phil Hughes 已提交
20 21 22 23
    moving: {
      issue: {},
      list: {}
    },
P
Phil Hughes 已提交
24
    create () {
25 26
      this.state.lists = [];
      this.state.filters = {
P
Phil Hughes 已提交
27 28
        author_id: gl.utils.getParameterValues('author_id')[0],
        assignee_id: gl.utils.getParameterValues('assignee_id')[0],
29
        milestone_title: gl.utils.getParameterValues('milestone_title')[0],
30 31
        label_name: gl.utils.getParameterValues('label_name[]'),
        search: ''
32 33
      };
    },
P
Phil Hughes 已提交
34
    addList (listObj) {
P
Phil Hughes 已提交
35
      const list = new List(listObj);
36 37
      this.state.lists.push(list);

38 39
      return list;
    },
P
Phil Hughes 已提交
40
    new (listObj) {
41 42
      const list = this.addList(listObj);
      const backlogList = this.findList('type', 'backlog', 'backlog');
P
Phil Hughes 已提交
43 44 45

      list
        .save()
P
Phil Hughes 已提交
46
        .then(() => {
P
Phil Hughes 已提交
47 48
          // Remove any new issues from the backlog
          // as they will be visible in the new list
P
Phil Hughes 已提交
49
          list.issues.forEach(backlogList.removeIssue.bind(backlogList));
50 51

          this.state.lists = _.sortBy(this.state.lists, 'position');
P
Phil Hughes 已提交
52 53 54
        });
      this.removeBlankState();
    },
55 56
    updateNewListDropdown (listId) {
      $(`.js-board-list-${listId}`).removeClass('is-active');
57
    },
P
Phil Hughes 已提交
58
    shouldAddBlankState () {
59
      // Decide whether to add the blank state
60
      return !(this.state.lists.filter(list => list.type !== 'backlog' && list.type !== 'done')[0]);
61
    },
P
Phil Hughes 已提交
62
    addBlankState () {
P
Phil Hughes 已提交
63
      if (!this.shouldAddBlankState() || this.welcomeIsHidden() || this.disabled) return;
64

P
Phil Hughes 已提交
65 66 67 68 69 70
      this.addList({
        id: 'blank',
        list_type: 'blank',
        title: 'Welcome to your Issue Board!',
        position: 0
      });
P
Phil Hughes 已提交
71 72

      this.state.lists = _.sortBy(this.state.lists, 'position');
73
    },
P
Phil Hughes 已提交
74
    removeBlankState () {
75
      this.removeList('blank');
76

77
      Cookies.set('issue_board_welcome_hidden', 'true', {
78 79
        expires: 365 * 10,
        path: ''
80
      });
P
Phil Hughes 已提交
81
    },
P
Phil Hughes 已提交
82
    welcomeIsHidden () {
83
      return Cookies.get('issue_board_welcome_hidden') === 'true';
84
    },
85 86
    removeList (id, type = 'blank') {
      const list = this.findList('id', id, type);
87 88 89

      if (!list) return;

90
      this.state.lists = this.state.lists.filter(list => list.id !== id);
P
Phil Hughes 已提交
91
    },
92
    moveList (listFrom, orderLists) {
P
Phil Hughes 已提交
93
      orderLists.forEach((id, i) => {
94
        const list = this.findList('id', parseInt(id, 10));
P
Phil Hughes 已提交
95

P
Phil Hughes 已提交
96
        list.position = i;
P
Phil Hughes 已提交
97
      });
98
      listFrom.update();
P
Phil Hughes 已提交
99
    },
100
    moveIssueToList (listFrom, listTo, issue, newIndex) {
101 102
      const issueTo = listTo.findIssue(issue.id);
      const issueLists = issue.getLists();
103
      const listLabels = issueLists.map(listIssue => listIssue.label);
104

P
Phil Hughes 已提交
105
      // Add to new lists issues if it doesn't already exist
106
      if (!issueTo) {
107
        listTo.addIssue(issue, listFrom, newIndex);
P
Phil Hughes 已提交
108 109
      }

110
      if (listTo.type === 'done' && listFrom.type !== 'backlog') {
P
Phil Hughes 已提交
111
        issueLists.forEach((list) => {
112
          list.removeIssue(issue);
113
        });
114
        issue.removeLabels(listLabels);
115 116
      } else {
        listFrom.removeIssue(issue);
P
Phil Hughes 已提交
117 118
      }
    },
119
    findList (key, val, type = 'label') {
P
Phil Hughes 已提交
120
      return this.state.lists.filter((list) => {
121
        const byType = type ? list['type'] === type : true;
122

123
        return list[key] === val && byType;
P
Phil Hughes 已提交
124
      })[0];
P
Phil Hughes 已提交
125
    },
P
Phil Hughes 已提交
126
    updateFiltersUrl () {
P
Phil Hughes 已提交
127
      history.pushState(null, null, `?${$.param(this.state.filters)}`);
P
Phil Hughes 已提交
128 129
    }
  };
130
})();