summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/vue_shared/components/content_viewer/viewers/image_viewer.vue
blob: cc4f123cf99a287857a0d3656d49f3c908f0e312 (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
<script>
import { numberToHumanSize } from '../../../../lib/utils/number_utils';

export default {
  props: {
    path: {
      type: String,
      required: true,
    },
    fileSize: {
      type: Number,
      required: false,
      default: 0,
    },
  },
  data() {
    return {
      width: 0,
      height: 0,
      zoomable: false,
      zoomed: false,
    };
  },
  computed: {
    fileSizeReadable() {
      return numberToHumanSize(this.fileSize);
    },
  },
  methods: {
    onImgLoad() {
      const contentImg = this.$refs.contentImg;
      this.zoomable =
        contentImg.naturalWidth > contentImg.width || contentImg.naturalHeight > contentImg.height;

      this.width = contentImg.naturalWidth;
      this.height = contentImg.naturalHeight;
    },
    onImgClick() {
      if (this.zoomable) this.zoomed = !this.zoomed;
    },
  },
};
</script>

<template>
  <div class="file-holder">
    <div class="file-content image_file">
      <img
        ref="contentImg"
        :class="{ 'zoomable': zoomable === true, 'zoomed': zoomed === true }"
        :src="path"
        :alt="path"
        @load="onImgLoad"
        @click="onImgClick"/>
    </div>
    <p
      class="image-info"
      v-if="width && height">
      <template v-if="fileSize>0">
        {{ fileSizeReadable }} -
      </template>
      {{ width }} x {{ height }}
    </p>
  </div>
</template>