diff_note_avatars.js 4.33 KB
Newer Older
1 2 3
/* global CommentsStore */
/* global notes */

4 5 6
import Vue from 'vue';
import collapseIcon from '../icons/collapse_icon.svg';

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
const DiffNoteAvatars = Vue.extend({
  props: ['discussionId'],
  data() {
    return {
      isVisible: false,
      lineType: '',
      storeState: CommentsStore.state,
      shownAvatars: 3,
      collapseIcon,
    };
  },
  template: `
    <div class="diff-comment-avatar-holders"
      v-show="notesCount !== 0">
      <div v-if="!isVisible">
        <img v-for="note in notesSubset"
          class="avatar diff-comment-avatar has-tooltip js-diff-comment-avatar"
          width="19"
          height="19"
          role="button"
          data-container="body"
          data-placement="top"
          data-html="true"
          :data-line-type="lineType"
          :title="note.authorName + ': ' + note.noteTruncated"
          :src="note.authorAvatar"
          @click="clickedAvatar($event)" />
        <span v-if="notesCount > shownAvatars"
          class="diff-comments-more-count has-tooltip js-diff-comment-avatar"
          data-container="body"
          data-placement="top"
          ref="extraComments"
          role="button"
40
          :data-line-type="lineType"
41 42
          :title="extraNotesTitle"
          @click="clickedAvatar($event)">{{ moreText }}</span>
43
      </div>
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
      <button class="diff-notes-collapse js-diff-comment-avatar"
        type="button"
        aria-label="Show comments"
        :data-line-type="lineType"
        @click="clickedAvatar($event)"
        v-if="isVisible"
        v-html="collapseIcon">
      </button>
    </div>
  `,
  mounted() {
    this.$nextTick(() => {
      this.addNoCommentClass();
      this.setDiscussionVisible();

      this.lineType = $(this.$el).closest('.diff-line-num').hasClass('old_line') ? 'old' : 'new';
    });

    $(document).on('toggle.comments', () => {
63 64 65
      this.$nextTick(() => {
        this.setDiscussionVisible();
      });
66 67 68 69 70 71 72 73
    });
  },
  destroyed() {
    $(document).off('toggle.comments');
  },
  watch: {
    storeState: {
      handler() {
74
        this.$nextTick(() => {
75 76 77 78
          $('.has-tooltip', this.$el).tooltip('fixTitle');

          // We need to add/remove a class to an element that is outside the Vue instance
          this.addNoCommentClass();
79 80
        });
      },
81
      deep: true,
82
    },
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
  },
  computed: {
    notesSubset() {
      let notes = [];

      if (this.discussion) {
        notes = Object.keys(this.discussion.notes)
          .slice(0, this.shownAvatars)
          .map(noteId => this.discussion.notes[noteId]);
      }

      return notes;
    },
    extraNotesTitle() {
      if (this.discussion) {
        const extra = this.discussion.notesCount() - this.shownAvatars;
99

100 101
        return `${extra} more comment${extra > 1 ? 's' : ''}`;
      }
102

103 104 105 106 107 108 109 110 111
      return '';
    },
    discussion() {
      return this.storeState[this.discussionId];
    },
    notesCount() {
      if (this.discussion) {
        return this.discussion.notesCount();
      }
112

113 114 115 116
      return 0;
    },
    moreText() {
      const plusSign = this.notesCount < 100 ? '+' : '';
117

118
      return `${plusSign}${this.notesCount - this.shownAvatars}`;
119
    },
120 121 122
  },
  methods: {
    clickedAvatar(e) {
123
      notes.onAddDiffNote(e);
124

125 126
      // Toggle the active state of the toggle all button
      this.toggleDiscussionsToggleState();
127

128 129
      this.$nextTick(() => {
        this.setDiscussionVisible();
130

131 132 133 134 135 136
        $('.has-tooltip', this.$el).tooltip('fixTitle');
        $('.has-tooltip', this.$el).tooltip('hide');
      });
    },
    addNoCommentClass() {
      const notesCount = this.notesCount;
137

138 139 140 141 142 143 144 145 146
      $(this.$el).closest('.js-avatar-container')
        .toggleClass('js-no-comment-btn', notesCount > 0)
        .nextUntil('.js-avatar-container')
        .toggleClass('js-no-comment-btn', notesCount > 0);
    },
    toggleDiscussionsToggleState() {
      const $notesHolders = $(this.$el).closest('.code').find('.notes_holder');
      const $visibleNotesHolders = $notesHolders.filter(':visible');
      const $toggleDiffCommentsBtn = $(this.$el).closest('.diff-file').find('.js-toggle-diff-comments');
147

148 149 150 151
      $toggleDiffCommentsBtn.toggleClass('active', $notesHolders.length === $visibleNotesHolders.length);
    },
    setDiscussionVisible() {
      this.isVisible = $(`.diffs .notes[data-discussion-id="${this.discussion.id}"]`).is(':visible');
152
    },
153 154
  },
});
155

156
Vue.component('diff-note-avatars', DiffNoteAvatars);