BigW Consortium Gitlab

discussion.js.es6 2.13 KB
Newer Older
1 2 3 4
/* eslint-disable space-before-function-paren, camelcase, guard-for-in, no-restricted-syntax, no-unused-vars, max-len */
/* global Vue */
/* global NoteModel */

5 6
class DiscussionModel {
  constructor (discussionId) {
7
    this.id = discussionId;
8
    this.notes = {};
Phil Hughes committed
9
    this.loading = false;
10
    this.canResolve = false;
11 12
  }

13 14
  createNote (noteId, canResolve, resolved, resolved_by) {
    Vue.set(this.notes, noteId, new NoteModel(this.id, noteId, canResolve, resolved, resolved_by));
15 16 17 18 19 20 21 22 23 24
  }

  deleteNote (noteId) {
    Vue.delete(this.notes, noteId);
  }

  getNote (noteId) {
    return this.notes[noteId];
  }

25 26 27 28
  notesCount() {
    return Object.keys(this.notes).length;
  }

29 30 31 32 33 34 35 36 37 38 39
  isResolved () {
    for (const noteId in this.notes) {
      const note = this.notes[noteId];

      if (!note.resolved) {
        return false;
      }
    }
    return true;
  }

40
  resolveAllNotes (resolved_by) {
41 42 43 44 45
    for (const noteId in this.notes) {
      const note = this.notes[noteId];

      if (!note.resolved) {
        note.resolved = true;
46
        note.resolved_by = resolved_by;
47 48 49 50
      }
    }
  }

51
  unResolveAllNotes () {
52 53 54 55 56
    for (const noteId in this.notes) {
      const note = this.notes[noteId];

      if (note.resolved) {
        note.resolved = false;
57
        note.resolved_by = null;
58 59 60
      }
    }
  }
61 62

  updateHeadline (data) {
63 64
    const discussionSelector = `.discussion[data-discussion-id="${this.id}"]`;
    const $discussionHeadline = $(`${discussionSelector} .js-discussion-headline`);
65 66 67 68 69

    if (data.discussion_headline_html) {
      if ($discussionHeadline.length) {
        $discussionHeadline.replaceWith(data.discussion_headline_html);
      } else {
70
        $(`${discussionSelector} .discussion-header`).append(data.discussion_headline_html);
71
      }
72 73

      gl.utils.localTimeAgo($('.js-timeago', `${discussionSelector}`));
74
    } else {
75
      $discussionHeadline.remove();
76 77
    }
  }
78

79 80 81 82
  isResolvable () {
    if (!this.canResolve) {
      return false;
    }
83

84 85 86 87 88 89 90 91 92 93
    for (const noteId in this.notes) {
      const note = this.notes[noteId];

      if (note.canResolve) {
        return true;
      }
    }

    return false;
  }
94
}
95 96

window.DiscussionModel = DiscussionModel;