issue.js 3.07 KB
Newer Older
1
/* eslint-disable no-unused-vars */
2 3
/* global ListLabel */
/* global ListMilestone */
4
/* global ListAssignee */
5

6
import Vue from 'vue';
7
import '~/vue_shared/models/label';
Felipe Artur's avatar
Felipe Artur committed
8
import IssueProject from './project';
9
import boardsStore from '../stores/boards_store';
10

Phil Hughes's avatar
Phil Hughes committed
11
class ListIssue {
12
  constructor (obj, defaultAvatar) {
13 14
    this.id = obj.id;
    this.iid = obj.iid;
15
    this.title = obj.title;
Phil Hughes's avatar
Phil Hughes committed
16
    this.confidential = obj.confidential;
17
    this.dueDate = obj.due_date;
18
    this.subscribed = obj.subscribed;
Phil Hughes's avatar
Phil Hughes committed
19
    this.labels = [];
20
    this.assignees = [];
Phil Hughes's avatar
Phil Hughes committed
21
    this.selected = false;
22
    this.position = obj.relative_position || Infinity;
23 24 25
    this.isFetching = {
      subscriptions: true,
    };
26
    this.isLoading = {};
27
    this.sidebarInfoEndpoint = obj.issue_sidebar_endpoint;
28 29
    this.referencePath = obj.reference_path;
    this.path = obj.real_path;
30
    this.toggleSubscriptionEndpoint = obj.toggle_subscription_endpoint;
Felipe Artur's avatar
Felipe Artur committed
31 32 33 34 35 36
    this.milestone_id = obj.milestone_id;
    this.project_id = obj.project_id;

    if (obj.project) {
      this.project = new IssueProject(obj.project);
    }
Phil Hughes's avatar
Phil Hughes committed
37

38 39 40 41
    if (obj.milestone) {
      this.milestone = new ListMilestone(obj.milestone);
    }

Phil Hughes's avatar
Phil Hughes committed
42
    obj.labels.forEach((label) => {
Phil Hughes's avatar
Phil Hughes committed
43
      this.labels.push(new ListLabel(label));
Phil Hughes's avatar
Phil Hughes committed
44
    });
45

46
    this.assignees = obj.assignees.map(a => new ListAssignee(a, defaultAvatar));
47 48 49
  }

  addLabel (label) {
Phil Hughes's avatar
Phil Hughes committed
50 51
    if (!this.findLabel(label)) {
      this.labels.push(new ListLabel(label));
52 53 54 55
    }
  }

  findLabel (findLabel) {
56
    return this.labels.filter(label => label.title === findLabel.title)[0];
57 58 59
  }

  removeLabel (removeLabel) {
Phil Hughes's avatar
Phil Hughes committed
60
    if (removeLabel) {
61
      this.labels = this.labels.filter(label => removeLabel.title !== label.title);
Phil Hughes's avatar
Phil Hughes committed
62
    }
63
  }
64

Phil Hughes's avatar
Phil Hughes committed
65
  removeLabels (labels) {
Phil Hughes's avatar
Phil Hughes committed
66
    labels.forEach(this.removeLabel.bind(this));
Phil Hughes's avatar
Phil Hughes committed
67 68
  }

69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
  addAssignee (assignee) {
    if (!this.findAssignee(assignee)) {
      this.assignees.push(new ListAssignee(assignee));
    }
  }

  findAssignee (findAssignee) {
    return this.assignees.filter(assignee => assignee.id === findAssignee.id)[0];
  }

  removeAssignee (removeAssignee) {
    if (removeAssignee) {
      this.assignees = this.assignees.filter(assignee => assignee.id !== removeAssignee.id);
    }
  }

  removeAllAssignees () {
    this.assignees = [];
  }

89
  getLists () {
90
    return boardsStore.state.lists.filter(list => list.findIssue(this.id));
91
  }
92

93 94 95 96 97 98 99 100
  updateData(newData) {
    Object.assign(this, newData);
  }

  setFetchingState(key, value) {
    this.isFetching[key] = value;
  }

101 102 103 104
  setLoadingState(key, value) {
    this.isLoading[key] = value;
  }

105
  update () {
106 107 108 109
    const data = {
      issue: {
        milestone_id: this.milestone ? this.milestone.id : null,
        due_date: this.dueDate,
110
        assignee_ids: this.assignees.length > 0 ? this.assignees.map((u) => u.id) : [0],
111
        label_ids: this.labels.map((label) => label.id)
112 113 114
      }
    };

115 116 117 118
    if (!data.issue.label_ids.length) {
      data.issue.label_ids = [''];
    }

Felipe Artur's avatar
Felipe Artur committed
119
    const projectPath = this.project ? this.project.path : '';
Felipe Artur's avatar
Felipe Artur committed
120
    return Vue.http.patch(`${this.path}.json`, data);
121
  }
122
}
123 124

window.ListIssue = ListIssue;
125 126

export default ListIssue;