diff_content.vue 4.32 KB
Newer Older
Felipe Artur's avatar
Felipe Artur committed
1
<script>
2
import { mapActions, mapGetters, mapState } from 'vuex';
3
import DiffViewer from '~/vue_shared/components/diff_viewer/diff_viewer.vue';
4
import EmptyFileViewer from '~/vue_shared/components/diff_viewer/viewers/empty_file.vue';
Felipe Artur's avatar
Felipe Artur committed
5 6
import InlineDiffView from './inline_diff_view.vue';
import ParallelDiffView from './parallel_diff_view.vue';
7 8 9 10 11
import NoteForm from '../../notes/components/note_form.vue';
import ImageDiffOverlay from './image_diff_overlay.vue';
import DiffDiscussions from './diff_discussions.vue';
import { IMAGE_DIFF_POSITION_TYPE } from '../constants';
import { getDiffMode } from '../store/utils';
Felipe Artur's avatar
Felipe Artur committed
12 13 14 15 16

export default {
  components: {
    InlineDiffView,
    ParallelDiffView,
17
    DiffViewer,
18 19 20
    NoteForm,
    DiffDiscussions,
    ImageDiffOverlay,
21
    EmptyFileViewer,
Felipe Artur's avatar
Felipe Artur committed
22 23 24 25 26 27
  },
  props: {
    diffFile: {
      type: Object,
      required: true,
    },
28 29 30 31 32
    helpPagePath: {
      type: String,
      required: false,
      default: '',
    },
Felipe Artur's avatar
Felipe Artur committed
33 34
  },
  computed: {
35 36 37 38
    ...mapState({
      projectPath: state => state.diffs.projectPath,
      endpoint: state => state.diffs.endpoint,
    }),
39
    ...mapGetters('diffs', ['isInlineView', 'isParallelView']),
40 41
    ...mapGetters('diffs', ['getCommentFormForDiffFile']),
    ...mapGetters(['getNoteableData', 'noteableType']),
42
    diffMode() {
43
      return getDiffMode(this.diffFile);
44 45
    },
    isTextFile() {
46
      return this.diffFile.viewer.name === 'text';
47
    },
48 49 50
    errorMessage() {
      return this.diffFile.viewer.error;
    },
51
    diffFileCommentForm() {
52
      return this.getCommentFormForDiffFile(this.diffFile.file_hash);
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
    },
    showNotesContainer() {
      return this.diffFile.discussions.length || this.diffFileCommentForm;
    },
  },
  methods: {
    ...mapActions('diffs', ['saveDiffDiscussion', 'closeDiffFileCommentForm']),
    handleSaveNote(note) {
      this.saveDiffDiscussion({
        note,
        formData: {
          noteableData: this.getNoteableData,
          noteableType: this.noteableType,
          diffFile: this.diffFile,
          positionType: IMAGE_DIFF_POSITION_TYPE,
          x: this.diffFileCommentForm.x,
          y: this.diffFileCommentForm.y,
70 71
          width: this.diffFileCommentForm.width,
          height: this.diffFileCommentForm.height,
72 73 74
        },
      });
    },
Felipe Artur's avatar
Felipe Artur committed
75 76 77 78 79 80
  },
};
</script>

<template>
  <div class="diff-content">
81
    <div v-if="!errorMessage" class="diff-viewer">
82
      <template v-if="isTextFile">
83
        <empty-file-viewer v-if="diffFile.empty" />
84
        <inline-diff-view
85
          v-else-if="isInlineView"
86
          :diff-file="diffFile"
87
          :diff-lines="diffFile.highlighted_diff_lines || []"
88
          :help-page-path="helpPagePath"
89 90
        />
        <parallel-diff-view
91
          v-else-if="isParallelView"
92
          :diff-file="diffFile"
93
          :diff-lines="diffFile.parallel_diff_lines || []"
94
          :help-page-path="helpPagePath"
95 96 97 98 99
        />
      </template>
      <diff-viewer
        v-else
        :diff-mode="diffMode"
100 101 102 103 104
        :new-path="diffFile.new_path"
        :new-sha="diffFile.diff_refs.head_sha"
        :old-path="diffFile.old_path"
        :old-sha="diffFile.diff_refs.base_sha"
        :file-hash="diffFile.file_hash"
105
        :project-path="projectPath"
106 107
        :a-mode="diffFile.a_mode"
        :b-mode="diffFile.b_mode"
108 109 110 111
      >
        <image-diff-overlay
          slot="image-overlay"
          :discussions="diffFile.discussions"
112
          :file-hash="diffFile.file_hash"
113 114
          :can-comment="getNoteableData.current_user.can_create_note"
        />
Mike Greiling's avatar
Mike Greiling committed
115
        <div v-if="showNotesContainer" class="note-container">
116 117 118 119 120 121 122 123 124 125 126 127 128 129
          <diff-discussions
            v-if="diffFile.discussions.length"
            class="diff-file-discussions"
            :discussions="diffFile.discussions"
            :should-collapse-discussions="true"
            :render-avatar-badge="true"
          />
          <note-form
            v-if="diffFileCommentForm"
            ref="noteForm"
            :is-editing="false"
            :save-button-title="__('Comment')"
            class="diff-comment-form new-note discussion-form discussion-form-container"
            @handleFormUpdate="handleSaveNote"
130
            @cancelForm="closeDiffFileCommentForm(diffFile.file_hash)"
Mike Greiling's avatar
Mike Greiling committed
131
          />
132 133
        </div>
      </diff-viewer>
Felipe Artur's avatar
Felipe Artur committed
134
    </div>
135 136 137
    <div v-else class="diff-viewer">
      <div class="nothing-here-block" v-html="errorMessage"></div>
    </div>
Felipe Artur's avatar
Felipe Artur committed
138 139
  </div>
</template>