# All files / src Thumbnail.vue

0% Statements 0/21
0% Branches 0/12
0% Functions 0/8
0% Lines 0/21

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

     <template>
      <div :class="['thumb-wrapper', {'thumb-circle': circle}, addClass]" :style="[getBorder, getFontSize, getFontColor, getBgColor]">
        <img ref="pic" @load.once="computeImgSize" :src="src" class="thumb-image" :width="computedWidth" :height="computedHeight" :alt="alt" v-if="hasSrc">
        <slot></slot>
      </div>
    </template>

    <script>
      const DEFAULT_SIZE = 100;

      export default {
        props: {
          circle: {
            type: Boolean,
            default: false
          },
          addClass: {
            type: String,
            default: ''
          },
          alt: {
            type: String,
            default: ''
          },
          background: {
            type: String,
            default: ''
          },
          border: {
            type: String,
            default: ''
          },
          fontColor: {
            type: String,
            default: ''
          },
          fontSize: {
            type: String,
            default: ''
          },
          size: {
            type: String,
            default: ''
          },
          src: {
            type: String
          },
          text: {
            type: String,
            default: ''
          },
        },
        computed: {
          getBorder() {
            // Default is no border
            return this.border === '' ? {} : {border: this.border};
          },
          getFontSize() {
            const defaultSize = this.size/2;
            const definedSize = this.fontSize;

            return {'font-size': ((definedSize === undefined || definedSize === '') ? defaultSize : definedSize) + 'px'};
          },
          getFontColor() {
            // Default is no setting (inherit)
            return this.fontColor === '' ? {} : {'color': this.fontColor};
          },
          getBgColor() {
            // Default is no setting (inherit)
            return this.background === '' ? {} : {'background': this.background};
          },
          hasSrc() {
            // Returns true if src is defined and is not an empty string
            return this.src !== undefined && this.src !== '';
          }
        },
        data () {
          return {
            computedWidth:'',
            computedHeight:''
          }
        },
        mounted() {
          this.size = this.size || DEFAULT_SIZE;
          let wrapperElement = this.$el;
          wrapperElement.style.width = this.size + 'px';
          wrapperElement.style.height = this.size + 'px';
          wrapperElement.style.lineHeight = this.size + 'px';
        },
        methods: {
          computeImgSize() {
            // Scale the img within such that the cropped image is the required size
            const renderedImg = this.$refs.pic;
            const imgHeight = renderedImg.naturalHeight;
            const imgWidth = renderedImg.naturalWidth;
            const imgSize = Math.min(imgHeight, imgWidth);
            const expansionFactor = this.size/imgSize;

            this.computedWidth = imgWidth * expansionFactor;
            this.computedHeight = imgHeight * expansionFactor;
          }
        }
      }
    </script>

    <style>
      .thumb-wrapper {
        display: inline-block;
        overflow: hidden;
        position: relative;
        text-align: center;
        vertical-align: middle;
      }

      .thumb-circle {
        border-radius: 50%;
        -moz-border-radius: 50%;
        -webkit-border-radius: 50%;
      }

      .thumb-image {
        left: 50%;
        position: absolute;
        top: 50%;
        transform: translate(-50%,-50%);
        -ms-transform: translate(-50%,-50%);
        -webkit-transform: translate(-50%,-50%);
      }
    </style>