ad225578
梁灏
add Avatar component
|
1
2
3
4
5
6
7
|
<template>
<span :class="classes">
<img :src="src" v-if="src">
<Icon :type="icon" v-else-if="icon"></Icon>
<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
28
29
30
31
32
33
34
35
|
shape: {
validator (value) {
return oneOf(value, ['circle', 'square']);
},
default: 'circle'
},
size: {
validator (value) {
return oneOf(value, ['small', 'large', 'default']);
},
default: 'default'
},
src: {
type: String
},
icon: {
type: String
}
|
2c5faf30
梁灏
init Avatar compo...
|
36
|
},
|
ad225578
梁灏
add Avatar component
|
37
38
39
40
41
42
43
|
data () {
return {
prefixCls: prefixCls,
scale: 1,
isSlotShow: false
};
},
|
2c5faf30
梁灏
init Avatar compo...
|
44
|
computed: {
|
a1530fac
梁灏
update Avatar
|
45
46
47
48
49
50
51
52
53
54
|
classes () {
return [
`${prefixCls}`,
`${prefixCls}-${this.shape}`,
`${prefixCls}-${this.size}`,
{
[`${prefixCls}-image`]: !!this.src,
[`${prefixCls}-icon`]: !!this.icon
}
];
|
ad225578
梁灏
add Avatar component
|
55
56
57
58
59
60
61
62
63
64
65
66
67
68
|
},
childrenStyle () {
let style = {};
if (this.isSlotShow) {
style = {
msTransform: `scale(${this.scale})`,
WebkitTransform: `scale(${this.scale})`,
transform: `scale(${this.scale})`,
position: 'absolute',
display: 'inline-block',
left: `calc(50% - ${Math.round(this.$refs.children.offsetWidth / 2)}px)`
};
}
return style;
|
a1530fac
梁灏
update Avatar
|
69
|
}
|
2c5faf30
梁灏
init Avatar compo...
|
70
|
},
|
ad225578
梁灏
add Avatar component
|
71
72
73
74
75
76
77
78
79
80
81
|
methods: {
setScale () {
this.isSlotShow = !this.src && !this.icon;
if (this.$slots.default) {
const childrenWidth = this.$refs.children.offsetWidth;
const avatarWidth = this.$el.getBoundingClientRect().width;
// add 4px gap for each side to get better performance
if (avatarWidth - 8 < childrenWidth) {
this.scale = (avatarWidth - 8) / childrenWidth;
} else {
this.scale = 1;
|
ececc3bb
梁灏
update Avatar
|
82
|
}
|
ad225578
梁灏
add Avatar component
|
83
|
}
|
ececc3bb
梁灏
update Avatar
|
84
|
}
|
ad225578
梁灏
add Avatar component
|
85
86
87
88
89
90
|
},
mounted () {
this.setScale();
},
updated () {
this.setScale();
|
2c5faf30
梁灏
init Avatar compo...
|
91
92
93
|
}
};
</script>
|