All files / app/assets/javascripts/design_management/components image.vue

79.48% Statements 31/39
72.72% Branches 8/11
83.33% Functions 10/12
81.57% Lines 31/38

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 152 153 154 155 156 157 1585x                                                         8x                       8x     8x 1x     8x             8x       1x 1x                     1x     2x 2x 2x 1x         1x           2x     1x   1x       1x     1x 1x                               4x     3x 1x 1x 1x   1x   2x 2x   2x         2x                                            
<!-- eslint-disable vue/multi-word-component-names -->
<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 });
      requestIdleCallback(this.setImageNaturalScale, { timeout: 1000 });
      performanceMarkAndMeasure({
        measures: [
          {
            name: DESIGN_MAIN_IMAGE_OUTPUT,
            start: DESIGN_MARK_APP_START,
          },
        ],
      });
    },
    onImgError() {
      this.imageError = true;
    },
    setBaseImageSize() {
      const { contentImg } = this.$refs;
      Iif (!contentImg) return;
      if (contentImg.offsetHeight === 0 || contentImg.offsetWidth === 0) {
        this.baseImageSize = {
          height: contentImg.naturalHeight,
          width: contentImg.naturalWidth,
        };
      } else {
        this.baseImageSize = {
          height: contentImg.offsetHeight,
          width: contentImg.offsetWidth,
        };
      }
 
      this.onResize({ width: this.baseImageSize.width, height: this.baseImageSize.height });
    },
    setImageNaturalScale() {
      const { contentImg } = this.$refs;
 
      Iif (!contentImg) {
        return;
      }
 
      const { naturalHeight, naturalWidth } = contentImg;
 
      // In case image 404s
      if (naturalHeight === 0 || naturalWidth === 0) {
        return;
      }
 
      const { height, width } = this.baseImageSize;
 
      this.imageStyle = {
        width: `${width}px`,
        height: `${height}px`,
      };
 
      this.$parent.$emit(
        'setMaxScale',
        Math.round(((height + width) / (naturalHeight + naturalWidth)) * 100) / 100,
      );
    },
    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="gl-max-h-full gl-border"
      :src="image"
      :alt="name"
      :style="imageStyle"
      :class="{ 'img-fluid': !imageStyle }"
      @error="onImgError"
      @load="onImgLoad"
    />
  </div>
</template>