parallel_diff_table_row.vue 3.6 KB
Newer Older
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
<script>
import $ from 'jquery';
import { mapGetters } from 'vuex';
import DiffTableCell from './diff_table_cell.vue';
import {
  NEW_LINE_TYPE,
  OLD_LINE_TYPE,
  CONTEXT_LINE_TYPE,
  CONTEXT_LINE_CLASS_NAME,
  OLD_NO_NEW_LINE_TYPE,
  PARALLEL_DIFF_VIEW_TYPE,
  NEW_NO_NEW_LINE_TYPE,
  LINE_POSITION_LEFT,
  LINE_POSITION_RIGHT,
} from '../constants';

export default {
  components: {
    DiffTableCell,
  },
  props: {
    diffFile: {
      type: Object,
      required: true,
    },
    line: {
      type: Object,
      required: true,
    },
    isBottom: {
      type: Boolean,
      required: false,
      default: false,
    },
  },
  data() {
    return {
      isLeftHover: false,
      isRightHover: false,
    };
  },
  computed: {
43
    ...mapGetters(['isParallelView']),
44
    isContextLine() {
45
      return this.line.left.type === CONTEXT_LINE_TYPE;
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
    },
    classNameMap() {
      return {
        [CONTEXT_LINE_CLASS_NAME]: this.isContextLine,
        [PARALLEL_DIFF_VIEW_TYPE]: this.isParallelView,
      };
    },
    parallelViewLeftLineType() {
      if (this.line.right.type === NEW_NO_NEW_LINE_TYPE) {
        return OLD_NO_NEW_LINE_TYPE;
      }

      return this.line.left.type;
    },
  },
  created() {
    this.newLineType = NEW_LINE_TYPE;
    this.oldLineType = OLD_LINE_TYPE;
    this.linePositionLeft = LINE_POSITION_LEFT;
    this.linePositionRight = LINE_POSITION_RIGHT;
66
    this.parallelDiffViewType = PARALLEL_DIFF_VIEW_TYPE;
67 68 69 70
  },
  methods: {
    handleMouseMove(e) {
      const isHover = e.type === 'mouseover';
71 72 73
      const hoveringCell = e.target.closest('td');
      const allCellsInHoveringRow = Array.from(e.currentTarget.children);
      const hoverIndex = allCellsInHoveringRow.indexOf(hoveringCell);
74

75 76
      if (hoverIndex >= 2) {
        this.isRightHover = isHover;
77
      } else {
78
        this.isLeftHover = isHover;
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
      }
    },
    // Prevent text selecting on both sides of parallel diff view
    // Backport of the same code from legacy diff notes.
    handleParallelLineMouseDown(e) {
      const line = $(e.currentTarget);
      const table = line.closest('table');

      table.removeClass('left-side-selected right-side-selected');
      const [lineClass] = ['left-side', 'right-side'].filter(name => line.hasClass(name));

      if (lineClass) {
        table.addClass(`${lineClass}-selected`);
      }
    },
  },
};
</script>

<template>
  <tr
    :class="classNameMap"
    class="line_holder"
    @mouseover="handleMouseMove"
    @mouseout="handleMouseMove"
  >
    <diff-table-cell
      :diff-file="diffFile"
      :line="line"
      :line-type="oldLineType"
      :line-position="linePositionLeft"
      :is-bottom="isBottom"
      :is-hover="isLeftHover"
      :show-comment-button="true"
113
      :diff-view-type="parallelDiffViewType"
114 115
      class="diff-line-num old_line"
    />
116 117
    <td
      v-once
118
      :id="line.left.lineCode"
119
      :class="parallelViewLeftLineType"
120 121
      class="line_content parallel left-side"
      @mousedown.native="handleParallelLineMouseDown"
122 123 124
      v-html="line.left.richText"
    >
    </td>
125 126 127 128 129 130 131 132
    <diff-table-cell
      :diff-file="diffFile"
      :line="line"
      :line-type="newLineType"
      :line-position="linePositionRight"
      :is-bottom="isBottom"
      :is-hover="isRightHover"
      :show-comment-button="true"
133
      :diff-view-type="parallelDiffViewType"
134 135
      class="diff-line-num new_line"
    />
136 137
    <td
      v-once
138
      :id="line.right.lineCode"
139
      :class="line.right.type"
140 141
      class="line_content parallel right-side"
      @mousedown.native="handleParallelLineMouseDown"
142 143 144
      v-html="line.right.richText"
    >
    </td>
145 146
  </tr>
</template>