Blame view

src/components/avatar/avatar.vue 3.24 KB
ad225578   梁灏   add Avatar component
1
2
3
  <template>
      <span :class="classes">
          <img :src="src" v-if="src">
afafafce   梁灏   Avatar add custom...
4
          <Icon :type="icon" :custom="customIcon" v-else-if="icon || customIcon"></Icon>
ad225578   梁灏   add Avatar component
5
6
7
          <span ref="children" :class="[prefixCls + '-string']" :style="childrenStyle" v-else><slot></slot></span>
      </span>
  </template>
2c5faf30   梁灏   init Avatar compo...
8
  <script>
a1530fac   梁灏   update Avatar
9
10
11
      import Icon from '../icon';
      import { oneOf } from '../../utils/assist';
  
2c5faf30   梁灏   init Avatar compo...
12
13
14
15
      const prefixCls = 'ivu-avatar';
  
      export default {
          name: 'Avatar',
ad225578   梁灏   add Avatar component
16
          components: { Icon },
2c5faf30   梁灏   init Avatar compo...
17
          props: {
a1530fac   梁灏   update Avatar
18
19
20
21
22
23
24
25
26
27
              shape: {
                  validator (value) {
                      return oneOf(value, ['circle', 'square']);
                  },
                  default: 'circle'
              },
              size: {
                  validator (value) {
                      return oneOf(value, ['small', 'large', 'default']);
                  },
55238beb   梁灏   Avatar support gl...
28
                  default () {
fe5ffd7f   梁灏   fixed #4196 #4165
29
                      return !this.$IVIEW || this.$IVIEW.size === '' ? 'default' : this.$IVIEW.size;
55238beb   梁灏   Avatar support gl...
30
                  }
a1530fac   梁灏   update Avatar
31
32
33
34
35
36
              },
              src: {
                  type: String
              },
              icon: {
                  type: String
afafafce   梁灏   Avatar add custom...
37
38
39
40
41
              },
              customIcon: {
                  type: String,
                  default: ''
              },
2c5faf30   梁灏   init Avatar compo...
42
          },
ad225578   梁灏   add Avatar component
43
44
45
46
          data () {
              return {
                  prefixCls: prefixCls,
                  scale: 1,
a4a202e3   daiyanze   update avatar slo...
47
                  childrenWidth: 0,
ad225578   梁灏   add Avatar component
48
49
50
                  isSlotShow: false
              };
          },
2c5faf30   梁灏   init Avatar compo...
51
          computed: {
a1530fac   梁灏   update Avatar
52
53
54
55
56
57
58
              classes () {
                  return [
                      `${prefixCls}`,
                      `${prefixCls}-${this.shape}`,
                      `${prefixCls}-${this.size}`,
                      {
                          [`${prefixCls}-image`]: !!this.src,
afafafce   梁灏   Avatar add custom...
59
                          [`${prefixCls}-icon`]: !!this.icon || !!this.customIcon
a1530fac   梁灏   update Avatar
60
61
                      }
                  ];
ad225578   梁灏   add Avatar component
62
63
64
65
66
67
68
69
70
71
              },
              childrenStyle () {
                  let style = {};
                  if (this.isSlotShow) {
                      style = {
                          msTransform: `scale(${this.scale})`,
                          WebkitTransform: `scale(${this.scale})`,
                          transform: `scale(${this.scale})`,
                          position: 'absolute',
                          display: 'inline-block',
a4a202e3   daiyanze   update avatar slo...
72
                          left: `calc(50% - ${Math.round(this.childrenWidth / 2)}px)`
ad225578   梁灏   add Avatar component
73
74
75
                      };
                  }
                  return style;
a1530fac   梁灏   update Avatar
76
              }
2c5faf30   梁灏   init Avatar compo...
77
          },
ad225578   梁灏   add Avatar component
78
79
80
          methods: {
              setScale () {
                  this.isSlotShow = !this.src && !this.icon;
81b328c6   Hsiaoming Yang   Patch fix on Avat...
81
                  if (this.$refs.children) {
a4a202e3   daiyanze   update avatar slo...
82
83
                      // set children width again to make slot centered
                      this.childrenWidth = this.$refs.children.offsetWidth;
ad225578   梁灏   add Avatar component
84
85
                      const avatarWidth = this.$el.getBoundingClientRect().width;
                      // add 4px gap for each side to get better performance
a4a202e3   daiyanze   update avatar slo...
86
87
                      if (avatarWidth - 8 < this.childrenWidth) {
                          this.scale = (avatarWidth - 8) / this.childrenWidth;
ad225578   梁灏   add Avatar component
88
89
                      } else {
                          this.scale = 1;
ececc3bb   梁灏   update Avatar
90
                      }
ad225578   梁灏   add Avatar component
91
                  }
ececc3bb   梁灏   update Avatar
92
              }
ad225578   梁灏   add Avatar component
93
94
95
96
97
98
          },
          mounted () {
              this.setScale();
          },
          updated () {
              this.setScale();
2c5faf30   梁灏   init Avatar compo...
99
100
          }
      };
81b328c6   Hsiaoming Yang   Patch fix on Avat...
101
  </script>