diff_line_gutter_content.vue 4.8 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 68 69 70 71 72
    isHover: {
      type: Boolean,
      required: false,
      default: false,
    },
    discussions: {
      type: Array,
      required: false,
      default: () => [],
    },
F
Felipe Artur 已提交
73 74 75 76 77 78
  },
  computed: {
    ...mapState({
      diffViewType: state => state.diffs.diffViewType,
      diffFiles: state => state.diffs.diffFiles,
    }),
79
    ...mapGetters(['isLoggedIn']),
F
Felipe Artur 已提交
80
    lineHref() {
T
Tim Zallmann 已提交
81
      return this.line.code ? `#${this.line.code}` : '#';
F
Felipe Artur 已提交
82 83 84 85 86
    },
    shouldShowCommentButton() {
      return (
        this.isLoggedIn &&
        this.showCommentButton &&
87
        this.isHover &&
F
Felipe Artur 已提交
88 89
        !this.isMatchLine &&
        !this.isContextLine &&
90 91
        !this.isMetaLine &&
        !this.hasDiscussions
F
Felipe Artur 已提交
92 93 94
      );
    },
    hasDiscussions() {
T
Tim Zallmann 已提交
95
      return this.line.discussions && this.line.discussions.length > 0;
F
Felipe Artur 已提交
96 97
    },
    shouldShowAvatarsOnGutter() {
T
Tim Zallmann 已提交
98
      if (!this.line.type && this.linePosition === LINE_POSITION_RIGHT) {
99
        return false;
F
Felipe Artur 已提交
100 101
      }

102
      return this.showCommentButton && this.hasDiscussions;
F
Felipe Artur 已提交
103 104 105
    },
  },
  methods: {
106
    ...mapActions('diffs', ['loadMoreLines', 'showCommentForm']),
F
Felipe Artur 已提交
107
    handleCommentButton() {
T
Tim Zallmann 已提交
108
      this.showCommentForm({ lineCode: this.line.code });
F
Felipe Artur 已提交
109 110 111 112 113 114 115 116
    },
    handleLoadMoreLines() {
      if (this.isRequesting) {
        return;
      }

      this.isRequesting = true;
      const endpoint = this.contextLinesPath;
T
Tim Zallmann 已提交
117 118
      const oldLineNumber = this.line.metaData.oldPos || 0;
      const newLineNumber = this.line.metaData.newPos || 0;
F
Felipe Artur 已提交
119 120
      const offset = newLineNumber - oldLineNumber;
      const bottom = this.isBottom;
121
      const { fileHash } = this;
F
Felipe Artur 已提交
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
      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
174
        v-if="shouldShowCommentButton"
F
Felipe Artur 已提交
175 176 177 178 179
        type="button"
        class="add-diff-note js-add-diff-note-button"
        title="Add a comment to this line"
        @click="handleCommentButton"
      >
180
        <!--<icon
F
Felipe Artur 已提交
181 182
          :size="12"
          name="comment"
183
        />-->
F
Felipe Artur 已提交
184 185 186 187 188 189 190 191 192
      </button>
      <a
        v-if="lineNumber"
        :data-linenumber="lineNumber"
        :href="lineHref"
      >
      </a>
      <diff-gutter-avatars
        v-if="shouldShowAvatarsOnGutter"
193
        :discussions="line.discussions"
F
Felipe Artur 已提交
194 195 196 197
      />
    </template>
  </div>
</template>