summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/diffs/components/image_diff_overlay.vue
blob: 703a281308e9d918d6b5931ea5a6d4f3a461427a (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
<script>
import { mapActions, mapGetters } from 'vuex';
import _ from 'underscore';
import imageDiffMixin from 'ee_else_ce/diffs/mixins/image_diff';
import Icon from '~/vue_shared/components/icon.vue';

export default {
  name: 'ImageDiffOverlay',
  components: {
    Icon,
  },
  mixins: [imageDiffMixin],
  props: {
    discussions: {
      type: [Array, Object],
      required: true,
    },
    fileHash: {
      type: String,
      required: true,
    },
    canComment: {
      type: Boolean,
      required: false,
      default: false,
    },
    showCommentIcon: {
      type: Boolean,
      required: false,
      default: false,
    },
    badgeClass: {
      type: String,
      required: false,
      default: 'badge badge-pill',
    },
    shouldToggleDiscussion: {
      type: Boolean,
      required: false,
      default: true,
    },
  },
  computed: {
    ...mapGetters('diffs', ['getDiffFileByHash', 'getCommentFormForDiffFile']),
    currentCommentForm() {
      return this.getCommentFormForDiffFile(this.fileHash);
    },
    allDiscussions() {
      return _.isArray(this.discussions) ? this.discussions : [this.discussions];
    },
  },
  methods: {
    ...mapActions('diffs', ['openDiffFileCommentForm']),
    getImageDimensions() {
      return {
        width: this.$parent.width,
        height: this.$parent.height,
      };
    },
    getPositionForObject(meta) {
      const { x, y, width, height } = meta;
      const imageWidth = this.getImageDimensions().width;
      const imageHeight = this.getImageDimensions().height;
      const widthRatio = imageWidth / width;
      const heightRatio = imageHeight / height;

      return {
        x: Math.round(x * widthRatio),
        y: Math.round(y * heightRatio),
      };
    },
    getPosition(discussion) {
      const { x, y } = this.getPositionForObject(discussion.position);

      return {
        left: `${x}px`,
        top: `${y}px`,
      };
    },
    clickedImage(x, y) {
      const { width, height } = this.getImageDimensions();

      this.openDiffFileCommentForm({
        fileHash: this.fileHash,
        width,
        height,
        x,
        y,
      });
    },
  },
};
</script>

<template>
  <div class="position-absolute w-100 h-100 image-diff-overlay">
    <button
      v-if="canComment"
      type="button"
      class="btn-transparent position-absolute image-diff-overlay-add-comment w-100 h-100 js-add-image-diff-note-button"
      @click="clickedImage($event.offsetX, $event.offsetY)"
    >
      <span class="sr-only"> {{ __('Add image comment') }} </span>
    </button>
    <button
      v-for="(discussion, index) in allDiscussions"
      :key="discussion.id"
      :style="getPosition(discussion)"
      :class="[badgeClass, { 'is-draft': discussion.isDraft }]"
      :disabled="!shouldToggleDiscussion"
      class="js-image-badge"
      type="button"
      @click="clickedToggle(discussion)"
    >
      <icon v-if="showCommentIcon" name="image-comment-dark" />
      <template v-else>
        {{ toggleText(discussion, index) }}
      </template>
    </button>
    <button
      v-if="currentCommentForm"
      :style="{
        left: `${currentCommentForm.x}px`,
        top: `${currentCommentForm.y}px`,
      }"
      :aria-label="__('Comment form position')"
      class="btn-transparent comment-indicator"
      type="button"
    >
      <icon name="image-comment-dark" />
    </button>
  </div>
</template>