diff_line_gutter_content.vue 4.7 KB
Newer Older
F
Felipe Artur 已提交
1 2 3 4 5 6
<script>
import createFlash from '~/flash';
import { s__ } from '~/locale';
import { mapState, mapGetters, mapActions } from 'vuex';
import Icon from '~/vue_shared/components/icon.vue';
import DiffGutterAvatars from './diff_gutter_avatars.vue';
7
import { LINE_POSITION_RIGHT, UNFOLD_COUNT } from '../constants';
F
Felipe Artur 已提交
8 9 10 11 12 13 14 15
import * as utils from '../store/utils';

export default {
  components: {
    DiffGutterAvatars,
    Icon,
  },
  props: {
T
Tim Zallmann 已提交
16 17 18 19
    line: {
      type: Object,
      required: true,
    },
F
Felipe Artur 已提交
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
    fileHash: {
      type: String,
      required: true,
    },
    contextLinesPath: {
      type: String,
      required: true,
    },
    lineNumber: {
      type: Number,
      required: false,
      default: 0,
    },
    linePosition: {
      type: String,
      required: false,
      default: '',
    },
    showCommentButton: {
      type: Boolean,
      required: false,
      default: false,
    },
    isBottom: {
      type: Boolean,
      required: false,
      default: false,
    },
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
    isMatchLine: {
      type: Boolean,
      required: false,
      default: false,
    },
    isMetaLine: {
      type: Boolean,
      required: false,
      default: false,
    },
    isContextLine: {
      type: Boolean,
      required: false,
      default: false,
    },
63 64 65 66 67
    isHover: {
      type: Boolean,
      required: false,
      default: false,
    },
F
Felipe Artur 已提交
68 69 70 71 72 73
  },
  computed: {
    ...mapState({
      diffViewType: state => state.diffs.diffViewType,
      diffFiles: state => state.diffs.diffFiles,
    }),
74
    ...mapGetters(['isLoggedIn']),
F
Felipe Artur 已提交
75
    lineHref() {
76
      return `#${this.line.lineCode || ''}`;
F
Felipe Artur 已提交
77 78 79 80 81
    },
    shouldShowCommentButton() {
      return (
        this.isLoggedIn &&
        this.showCommentButton &&
82
        this.isHover &&
F
Felipe Artur 已提交
83 84
        !this.isMatchLine &&
        !this.isContextLine &&
85 86
        !this.isMetaLine &&
        !this.hasDiscussions
F
Felipe Artur 已提交
87 88 89
      );
    },
    hasDiscussions() {
90
      return this.line.discussions && this.line.discussions.length > 0;
F
Felipe Artur 已提交
91 92
    },
    shouldShowAvatarsOnGutter() {
93
      if (!this.line.type && this.linePosition === LINE_POSITION_RIGHT) {
94
        return false;
F
Felipe Artur 已提交
95
      }
96
      return this.showCommentButton && this.hasDiscussions;
F
Felipe Artur 已提交
97 98 99
    },
  },
  methods: {
100
    ...mapActions('diffs', ['loadMoreLines', 'showCommentForm']),
F
Felipe Artur 已提交
101
    handleCommentButton() {
T
Tim Zallmann 已提交
102
      this.showCommentForm({ lineCode: this.line.lineCode });
F
Felipe Artur 已提交
103 104 105 106 107 108 109 110
    },
    handleLoadMoreLines() {
      if (this.isRequesting) {
        return;
      }

      this.isRequesting = true;
      const endpoint = this.contextLinesPath;
T
Tim Zallmann 已提交
111 112
      const oldLineNumber = this.line.metaData.oldPos || 0;
      const newLineNumber = this.line.metaData.newPos || 0;
F
Felipe Artur 已提交
113 114
      const offset = newLineNumber - oldLineNumber;
      const bottom = this.isBottom;
115
      const { fileHash } = this;
F
Felipe Artur 已提交
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
      const view = this.diffViewType;
      let unfold = true;
      let lineNumber = newLineNumber - 1;
      let since = lineNumber - UNFOLD_COUNT;
      let to = lineNumber;

      if (bottom) {
        lineNumber = newLineNumber + 1;
        since = lineNumber;
        to = lineNumber + UNFOLD_COUNT;
      } else {
        const diffFile = utils.findDiffFile(this.diffFiles, this.fileHash);
        const indexForInline = utils.findIndexInInlineLines(diffFile.highlightedDiffLines, {
          oldLineNumber,
          newLineNumber,
        });
        const prevLine = diffFile.highlightedDiffLines[indexForInline - 2];
        const prevLineNumber = (prevLine && prevLine.newLine) || 0;

        if (since <= prevLineNumber + 1) {
          since = prevLineNumber + 1;
          unfold = false;
        }
      }

      const params = { since, to, bottom, offset, unfold, view };
      const lineNumbers = { oldLineNumber, newLineNumber };
      this.loadMoreLines({ endpoint, params, lineNumbers, fileHash })
        .then(() => {
          this.isRequesting = false;
        })
        .catch(() => {
          createFlash(s__('Diffs|Something went wrong while fetching diff lines.'));
          this.isRequesting = false;
        });
    },
  },
};
</script>

<template>
  <div>
    <span
      v-if="isMatchLine"
      class="context-cell"
      role="button"
      @click="handleLoadMoreLines"
    >...</span>
    <template
      v-else
    >
      <button
168
        v-if="shouldShowCommentButton"
F
Felipe Artur 已提交
169 170 171 172 173
        type="button"
        class="add-diff-note js-add-diff-note-button"
        title="Add a comment to this line"
        @click="handleCommentButton"
      >
T
Tim Zallmann 已提交
174
        <icon
F
Felipe Artur 已提交
175 176
          :size="12"
          name="comment"
T
Tim Zallmann 已提交
177
        />
F
Felipe Artur 已提交
178 179 180 181 182 183 184 185 186
      </button>
      <a
        v-if="lineNumber"
        :data-linenumber="lineNumber"
        :href="lineHref"
      >
      </a>
      <diff-gutter-avatars
        v-if="shouldShowAvatarsOnGutter"
187
        :discussions="line.discussions"
F
Felipe Artur 已提交
188 189 190 191
      />
    </template>
  </div>
</template>