summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/notes/components/noteable_discussion.vue
blob: 9d0a9cd77466dabd5e3c51e562626d3f17172398 (plain)
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
<script>
import _ from 'underscore';
import { mapActions, mapGetters } from 'vuex';
import resolveDiscussionsSvg from 'icons/_icon_mr_issue.svg';
import nextDiscussionsSvg from 'icons/_next_discussion.svg';
import Flash from '../../flash';
import { SYSTEM_NOTE } from '../constants';
import userAvatarLink from '../../vue_shared/components/user_avatar/user_avatar_link.vue';
import noteableNote from './noteable_note.vue';
import noteHeader from './note_header.vue';
import noteSignedOutWidget from './note_signed_out_widget.vue';
import noteEditedText from './note_edited_text.vue';
import noteForm from './note_form.vue';
import diffWithNote from './diff_with_note.vue';
import placeholderNote from '../../vue_shared/components/notes/placeholder_note.vue';
import placeholderSystemNote from '../../vue_shared/components/notes/placeholder_system_note.vue';
import autosave from '../mixins/autosave';
import noteable from '../mixins/noteable';
import resolvable from '../mixins/resolvable';
import tooltip from '../../vue_shared/directives/tooltip';
import { scrollToElement } from '../../lib/utils/common_utils';

export default {
  components: {
    noteableNote,
    diffWithNote,
    userAvatarLink,
    noteHeader,
    noteSignedOutWidget,
    noteEditedText,
    noteForm,
    placeholderNote,
    placeholderSystemNote,
  },
  directives: {
    tooltip,
  },
  mixins: [autosave, noteable, resolvable],
  props: {
    note: {
      type: Object,
      required: true,
    },
    renderHeader: {
      type: Boolean,
      required: false,
      default: true,
    },
    renderDiffFile: {
      type: Boolean,
      required: false,
      default: true,
    },
    alwaysExpanded: {
      type: Boolean,
      required: false,
      default: false,
    },
  },
  data() {
    return {
      isReplying: false,
      isResolving: false,
      resolveAsThread: true,
    };
  },
  computed: {
    ...mapGetters([
      'getNoteableData',
      'discussionCount',
      'resolvedDiscussionCount',
      'allDiscussions',
      'unresolvedDiscussions',
    ]),
    actionTextHtml() {
      let text = 'started a discussion';

      if (!this.discussion.diffDiscussion) {
        return text;
      }

      text += ' on';
      let urlText;
      if (this.discussion.active) {
        urlText = 'the diff';
      } else {
        urlText = 'an old version of the diff';
      }

      text += ` <a href="${this.discussion.discussionPath}">${urlText}</a>`;

      return text;
    },
    discussion() {
      return {
        ...this.note.notes[0],
        truncatedDiffLines: this.note.truncated_diff_lines,
        diffFile: this.note.diff_file,
        diffDiscussion: this.note.diff_discussion,
        imageDiffHtml: this.note.image_diff_html,
        active: this.note.active,
        discussionPath: this.note.discussion_path,
      };
    },
    author() {
      return this.discussion.author;
    },
    canReply() {
      return this.getNoteableData.current_user.can_create_note;
    },
    newNotePath() {
      return this.getNoteableData.create_note_path;
    },
    lastUpdatedBy() {
      const { notes } = this.note;

      if (notes.length > 1) {
        return notes[notes.length - 1].author;
      }

      return null;
    },
    lastUpdatedAt() {
      const { notes } = this.note;

      if (notes.length > 1) {
        return notes[notes.length - 1].created_at;
      }

      return null;
    },
    hasUnresolvedDiscussion() {
      return this.unresolvedDiscussions.length > 0;
    },
    shouldRenderDiffs() {
      const { diffDiscussion, diffFile } = this.discussion;

      return diffDiscussion && diffFile && this.renderDiffFile;
    },
    wrapperComponent() {
      return this.shouldRenderDiffs ? diffWithNote : 'div';
    },
    wrapperClass() {
      return this.isDiffDiscussion ? '' : 'panel panel-default discussion-wrapper';
    },
  },
  mounted() {
    if (this.isReplying) {
      this.initAutoSave(this.discussion.noteable_type);
    }
  },
  updated() {
    if (this.isReplying) {
      if (!this.autosave) {
        this.initAutoSave(this.discussion.noteable_type);
      } else {
        this.setAutoSave();
      }
    }
  },
  created() {
    this.resolveDiscussionsSvg = resolveDiscussionsSvg;
    this.nextDiscussionsSvg = nextDiscussionsSvg;
  },
  methods: {
    ...mapActions(['saveNote', 'toggleDiscussion', 'removePlaceholderNotes', 'toggleResolveNote']),
    componentName(note) {
      if (note.isPlaceholderNote) {
        if (note.placeholderType === SYSTEM_NOTE) {
          return placeholderSystemNote;
        }
        return placeholderNote;
      }

      return noteableNote;
    },
    componentData(note) {
      return note.isPlaceholderNote ? this.note.notes[0] : note;
    },
    toggleDiscussionHandler() {
      this.toggleDiscussion({ discussionId: this.note.id });
    },
    showReplyForm() {
      this.isReplying = true;
    },
    cancelReplyForm(shouldConfirm) {
      if (shouldConfirm && this.$refs.noteForm.isDirty) {
        // eslint-disable-next-line no-alert
        if (!confirm('Are you sure you want to cancel creating this comment?')) {
          return;
        }
      }

      this.resetAutoSave();
      this.isReplying = false;
    },
    saveReply(noteText, form, callback) {
      const replyData = {
        endpoint: this.newNotePath,
        flashContainer: this.$el,
        data: {
          in_reply_to_discussion_id: this.note.reply_id,
          target_type: this.noteableType,
          target_id: this.discussion.noteable_id,
          note: { note: noteText },
        },
      };
      this.isReplying = false;

      this.saveNote(replyData)
        .then(() => {
          this.resetAutoSave();
          callback();
        })
        .catch(err => {
          this.removePlaceholderNotes();
          this.isReplying = true;
          this.$nextTick(() => {
            const msg = `Your comment could not be submitted!
Please check your network connection and try again.`;
            Flash(msg, 'alert', this.$el);
            this.$refs.noteForm.note = noteText;
            callback(err);
          });
        });
    },
    jumpToDiscussion() {
      const discussionIds = this.allDiscussions.map(d => d.id);
      const unresolvedIds = this.unresolvedDiscussions.map(d => d.id);
      const currentIndex = discussionIds.indexOf(this.note.id);
      const remainingAfterCurrent = discussionIds.slice(currentIndex + 1);
      const nextIndex = _.findIndex(remainingAfterCurrent, id => unresolvedIds.indexOf(id) > -1);

      if (nextIndex > -1) {
        const nextId = remainingAfterCurrent[nextIndex];
        const el = document.querySelector(`[data-discussion-id="${nextId}"]`);

        if (el) {
          scrollToElement(el);
        }
      }
    },
  },
};
</script>

<template>
  <li class="note note-discussion timeline-entry">
    <div class="timeline-entry-inner">
      <div class="timeline-icon">
        <user-avatar-link
          :link-href="author.path"
          :img-src="author.avatar_url"
          :img-alt="author.name"
          :img-size="40"
        />
      </div>
      <div class="timeline-content">
        <div
          class="discussion js-discussion-container"
          :data-discussion-id="discussion.discussion_id"
        >
          <div
            v-if="renderHeader"
            class="discussion-header"
          >
            <note-header
              :author="author"
              :created-at="discussion.created_at"
              :note-id="discussion.id"
              :include-toggle="true"
              :expanded="note.expanded"
              @toggleHandler="toggleDiscussionHandler"
              :action-text-html="actionTextHtml"
              class="discussion"
            />
            <note-edited-text
              v-if="lastUpdatedAt"
              :edited-at="lastUpdatedAt"
              :edited-by="lastUpdatedBy"
              action-text="Last updated"
              class-name="discussion-headline-light js-discussion-headline"
            />
          </div>
          <div
            v-if="note.expanded || alwaysExpanded"
            class="discussion-body">
            <component
              :is="wrapperComponent"
              :discussion="shouldRenderDiffs ? discussion : undefined"
              :class="wrapperClass"
            >
              <div class="discussion-notes js-discussion-note-form">
                <ul class="notes">
                  <component
                    v-for="note in note.notes"
                    :is="componentName(note)"
                    :note="componentData(note)"
                    :key="note.id"
                  />
                </ul>
                <div
                  :class="{ 'is-replying': isReplying }"
                  class="discussion-reply-holder">
                  <template v-if="!isReplying && canReply">
                    <div
                      class="btn-group-justified discussion-with-resolve-btn"
                      role="group">
                      <div
                        class="btn-group"
                        role="group">
                        <button
                          @click="showReplyForm"
                          type="button"
                          class="js-vue-discussion-reply btn btn-text-field"
                          title="Add a reply">Reply...</button>
                      </div>
                      <div
                        v-if="note.resolvable"
                        class="btn-group"
                        role="group">
                        <button
                          @click="resolveHandler()"
                          type="button"
                          class="btn btn-default"
                        >
                          <i
                            v-if="isResolving"
                            aria-hidden="true"
                            class="fa fa-spinner fa-spin"
                          ></i>
                          {{ resolveButtonTitle }}
                        </button>
                      </div>
                      <div
                        v-if="note.resolvable"
                        class="btn-group discussion-actions"
                        role="group"
                      >
                        <div
                          v-if="!discussionResolved"
                          class="btn-group"
                          role="group">
                          <a
                            :href="note.resolve_with_issue_path"
                            v-tooltip
                            class="new-issue-for-discussion btn
                              btn-default discussion-create-issue-btn"
                            title="Resolve this discussion in a new issue"
                            data-container="body"
                          >
                            <span v-html="resolveDiscussionsSvg"></span>
                          </a>
                        </div>
                        <div
                          v-if="hasUnresolvedDiscussion"
                          class="btn-group"
                          role="group">
                          <button
                            @click="jumpToDiscussion"
                            v-tooltip
                            class="btn btn-default discussion-next-btn"
                            title="Jump to next unresolved discussion"
                            data-container="body"
                          >
                            <span v-html="nextDiscussionsSvg"></span>
                          </button>
                        </div>
                      </div>
                    </div>
                  </template>
                  <note-form
                    v-if="isReplying"
                    save-button-title="Comment"
                    :note="note"
                    :is-editing="false"
                    @handleFormUpdate="saveReply"
                    @cancelForm="cancelReplyForm"
                    ref="noteForm" />
                  <note-signed-out-widget v-if="!canReply" />
                </div>
              </div>
            </component>
          </div>
        </div>
      </div>
    </div>
  </li>
</template>