list.js.es6 1.01 KB
Newer Older
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
class List {
  constructor (obj) {
    this.id = obj.id;
    this.index = obj.index;
    this.search = obj.search || false;
    this.title = obj.title;

    if (obj.label) {
      this.label = new Label(obj.label);
    }

    if (obj.issues) {
      this.issues = [];
      obj.issues.forEach((issue) => {
        this.issues.push(new Issue(issue));
      });
    }
  }

  addIssue (issue, index) {
    this.issues.splice(index, 0, issue);

    issue.addLabel(this.label);
  }

  findIssue (id) {
    return _.find(this.issues, (issue) => {
      return issue.id === id;
    });
  }

  removeIssue (removeIssue, listLabels) {
    this.issues = _.reject(this.issues, (issue) => {
      const matchesRemove = removeIssue.id === issue.id;

      if (matchesRemove) {
        if (typeof listLabels !== 'undefined') {
          listLabels.forEach((listLabel) => {
            issue.removeLabel(listLabel);
          });
        } else {
          issue.removeLabel(this.label);
        }
      }

      return matchesRemove;
    });
  }
}