board_service.js.es6 2.1 KB
Newer Older
1
/* eslint-disable space-before-function-paren, comma-dangle, no-param-reassign, camelcase, max-len, no-unused-vars */
2 3
/* global Vue */

P
Phil Hughes 已提交
4
class BoardService {
5
  constructor (root, bulkUpdatePath, boardId) {
P
Phil Hughes 已提交
6
    this.boards = Vue.resource(`${root}{/id}.json`, {}, {
7
      issues: {
P
Phil Hughes 已提交
8
        method: 'GET',
9
        url: `${root}/${boardId}/issues.json`
P
Phil Hughes 已提交
10 11
      }
    });
12
    this.lists = Vue.resource(`${root}/${boardId}/lists{/id}`, {}, {
13 14
      generate: {
        method: 'POST',
15
        url: `${root}/${boardId}/lists/generate.json`
16
      }
17
    });
18
    this.issue = Vue.resource(`${root}/${boardId}/issues{/id}`, {});
19 20 21 22 23 24
    this.issues = Vue.resource(`${root}/${boardId}/lists{/id}/issues`, {}, {
      bulkUpdate: {
        method: 'POST',
        url: bulkUpdatePath,
      },
    });
P
Phil Hughes 已提交
25

P
Phil Hughes 已提交
26 27 28 29
    Vue.http.interceptors.push((request, next) => {
      request.headers['X-CSRF-Token'] = $.rails.csrfToken();
      next();
    });
P
Phil Hughes 已提交
30 31 32
  }

  all () {
33 34 35
    return this.lists.get();
  }

36
  generateDefaultLists () {
P
Phil Hughes 已提交
37
    return this.lists.generate({});
38 39
  }

P
Phil Hughes 已提交
40
  createList (label_id) {
P
Phil Hughes 已提交
41
    return this.lists.save({}, {
42
      list: {
P
Phil Hughes 已提交
43
        label_id
44 45
      }
    });
P
Phil Hughes 已提交
46 47
  }

P
Phil Hughes 已提交
48 49
  updateList (id, position) {
    return this.lists.update({ id }, {
50
      list: {
P
Phil Hughes 已提交
51
        position
52 53 54 55 56
      }
    });
  }

  destroyList (id) {
P
Phil Hughes 已提交
57
    return this.lists.delete({ id });
P
Phil Hughes 已提交
58
  }
P
Phil Hughes 已提交
59

P
Phil Hughes 已提交
60
  getIssuesForList (id, filter = {}) {
61
    const data = { id };
P
Phil Hughes 已提交
62
    Object.keys(filter).forEach((key) => { data[key] = filter[key]; });
P
Phil Hughes 已提交
63

P
Phil Hughes 已提交
64
    return this.issues.get(data);
P
Phil Hughes 已提交
65
  }
P
Phil Hughes 已提交
66

67
  moveIssue (id, from_list_id = null, to_list_id = null, move_after_iid = null, move_before_iid = null) {
P
Phil Hughes 已提交
68
    return this.issue.update({ id }, {
P
Phil Hughes 已提交
69
      from_list_id,
70 71 72
      to_list_id,
      move_before_iid,
      move_after_iid
P
Phil Hughes 已提交
73 74
    });
  }
P
Phil Hughes 已提交
75 76 77

  newIssue (id, issue) {
    return this.issues.save({ id }, {
P
Phil Hughes 已提交
78
      issue
P
Phil Hughes 已提交
79 80
    });
  }
P
Phil Hughes 已提交
81

P
Phil Hughes 已提交
82
  getBacklog(data) {
83
    return this.boards.issues(data);
P
Phil Hughes 已提交
84
  }
85 86 87 88 89 90 91 92 93 94

  bulkUpdate(issueIds, extraData = {}) {
    const data = {
      update: Object.assign(extraData, {
        issuable_ids: issueIds.join(','),
      }),
    };

    return this.issues.bulkUpdate(data);
  }
95 96 97
}

window.BoardService = BoardService;