summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/vue_shared/components/diff_viewer/viewers/image_diff/swipe_viewer.vue
blob: eddafc759a2cbe64d8a5d412e46d60d7b7d6252b (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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
<script>
import _ from 'underscore';
import { pixeliseValue } from '../../../lib/utils/dom_utils';
import ImageViewer from '../../../content_viewer/viewers/image_viewer.vue';

export default {
  components: {
    ImageViewer,
  },
  props: {
    newPath: {
      type: String,
      required: true,
    },
    oldPath: {
      type: String,
      required: true,
    },
  },
  data() {
    return {
      dragging: false,
      swipeOldImgInfo: null,
      swipeNewImgInfo: null,
      swipeMaxWidth: undefined,
      swipeMaxHeight: undefined,
      swipeBarPos: 1,
      swipeWrapWidth: 0,
    };
  },
  computed: {
    swipeMaxPixelWidth() {
      return pixeliseValue(this.swipeMaxWidth);
    },
    swipeMaxPixelHeight() {
      return pixeliseValue(this.swipeMaxHeight);
    },
    swipeWrapPixelWidth() {
      return pixeliseValue(this.swipeWrapWidth);
    },
    swipeBarPixelPos() {
      return pixeliseValue(this.swipeBarPos);
    },
  },
  beforeDestroy() {
    window.removeEventListener('resize', this.resizeThrottled, false);
    document.body.removeEventListener('mouseup', this.stopDrag);
    document.body.removeEventListener('mousemove', this.dragMove);
  },
  mounted() {
    window.addEventListener('resize', this.resize, false);
  },
  methods: {
    dragMove(e) {
      if (!this.dragging) return;

      let leftValue = e.pageX - this.$refs.swipeFrame.getBoundingClientRect().left;
      const spaceLeft = 20;
      const { clientWidth } = this.$refs.swipeFrame;
      if (leftValue <= 0) {
        leftValue = 0;
      } else if (leftValue > clientWidth - spaceLeft) {
        leftValue = clientWidth - spaceLeft;
      }

      this.swipeWrapWidth = (leftValue / clientWidth) * 100;
      this.swipeBarPos = leftValue;
    },
    startDrag() {
      this.dragging = true;
      document.body.addEventListener('mousemove', this.dragMove);
    },
    stopDrag() {
      this.dragging = false;
      document.body.removeEventListener('mousemove', this.dragMove);
    },
    prepareSwipe() {
      if (this.swipeOldImgInfo && this.swipeNewImgInfo) {
        // Add 2 for border width
        this.swipeMaxWidth =
          Math.max(this.swipeOldImgInfo.renderedWidth, this.swipeNewImgInfo.renderedWidth) + 2;
        this.swipeMaxHeight =
          Math.max(this.swipeOldImgInfo.renderedHeight, this.swipeNewImgInfo.renderedHeight) + 2;

        document.body.addEventListener('mouseup', this.stopDrag);
      }
    },
    swipeNewImgLoaded(imgInfo) {
      this.swipeNewImgInfo = imgInfo;
      this.prepareSwipe();
    },
    swipeOldImgLoaded(imgInfo) {
      this.swipeOldImgInfo = imgInfo;
      this.prepareSwipe();
    },
    resize: _.throttle(function throttledResize() {
      this.swipeBarPos = 0;
    }, 400),
  },
};
</script>

<template>
  <div class="swipe view">
    <div
      ref="swipeFrame"
      :style="{
        'user-select': dragging ? 'none' : null,
      }"
      class="swipe-frame"
    >
      <image-viewer
        key="swipeOldImg"
        ref="swipeOldImg"
        :render-info="false"
        :path="oldPath"
        class="frame deleted"
        @imgLoaded="swipeOldImgLoaded"
      />
      <div
        ref="swipeWrap"
        :style="{
          width: `${swipeWrapWidth}%`,
        }"
        class="swipe-wrap"
      >
        <image-viewer
          key="swipeNewImg"
          :render-info="false"
          :path="newPath"
          :style="{
            width: swipeMaxPixelWidth,
          }"
          class="frame added"
          @imgLoaded="swipeNewImgLoaded"
        >
          <slot slot="image-overlay" name="image-overlay"> </slot>
        </image-viewer>
      </div>
      <span
        ref="swipeBar"
        :style="{ left: swipeBarPixelPos }"
        class="swipe-bar"
        @mousedown="startDrag"
        @mouseup="stopDrag"
      >
        <span class="top-handle"></span> <span class="bottom-handle"></span>
      </span>
    </div>
  </div>
</template>