summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/design_management/components/image.vue
blob: 8ab94cd2c4be2b922afaaa46b8803dee84f52f5c (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
<script>
import { GlIcon } from '@gitlab/ui';
import { throttle } from 'lodash';
import { DESIGN_MARK_APP_START, DESIGN_MAIN_IMAGE_OUTPUT } from '~/performance/constants';
import { performanceMarkAndMeasure } from '~/performance/utils';

export default {
  components: {
    GlIcon,
  },
  props: {
    image: {
      type: String,
      required: false,
      default: '',
    },
    name: {
      type: String,
      required: false,
      default: '',
    },
    scale: {
      type: Number,
      required: false,
      default: 1,
    },
  },
  data() {
    return {
      baseImageSize: null,
      imageStyle: null,
      imageError: false,
    };
  },
  watch: {
    scale(val) {
      this.zoom(val);
    },
  },
  beforeDestroy() {
    window.removeEventListener('resize', this.resizeThrottled, false);
  },
  mounted() {
    if (!this.image) {
      this.onImgLoad();
    }

    this.resizeThrottled = throttle(() => {
      // NOTE: if imageStyle is set, then baseImageSize
      // won't change due to resize. We must still emit a
      // `resize` event so that the parent can handle
      // resizes appropriately (e.g. for design_overlay)
      this.setBaseImageSize();
    }, 400);
    window.addEventListener('resize', this.resizeThrottled, false);
  },
  methods: {
    onImgLoad() {
      requestIdleCallback(this.setBaseImageSize, { timeout: 1000 });
      performanceMarkAndMeasure({
        measures: [
          {
            name: DESIGN_MAIN_IMAGE_OUTPUT,
            start: DESIGN_MARK_APP_START,
          },
        ],
      });
    },
    onImgError() {
      this.imageError = true;
    },
    setBaseImageSize() {
      const { contentImg } = this.$refs;
      if (!contentImg || contentImg.offsetHeight === 0 || contentImg.offsetWidth === 0) return;

      this.baseImageSize = {
        height: contentImg.offsetHeight,
        width: contentImg.offsetWidth,
      };
      this.onResize({ width: this.baseImageSize.width, height: this.baseImageSize.height });
    },
    onResize({ width, height }) {
      this.$emit('resize', { width, height });
    },
    zoom(amount) {
      if (amount === 1) {
        this.imageStyle = null;
        this.$nextTick(() => {
          this.setBaseImageSize();
        });
        return;
      }
      const width = this.baseImageSize.width * amount;
      const height = this.baseImageSize.height * amount;

      this.imageStyle = {
        width: `${width}px`,
        height: `${height}px`,
      };

      this.onResize({ width, height });
    },
  },
};
</script>

<template>
  <div class="gl-mx-auto gl-my-auto js-design-image">
    <gl-icon v-if="imageError" class="gl-text-gray-200" name="media-broken" :size="48" />
    <img
      v-show="!imageError"
      ref="contentImg"
      class="mh-100"
      :src="image"
      :alt="name"
      :style="imageStyle"
      :class="{ 'img-fluid': !imageStyle }"
      @error="onImgError"
      @load="onImgLoad"
    />
  </div>
</template>