2018-03-19 02:16:27 +00:00
|
|
|
|
2017-11-10 02:28:49 +00:00
|
|
|
import Icon from '../icon'
|
2017-11-09 10:57:34 +00:00
|
|
|
|
|
|
|
export default {
|
2018-04-08 13:17:20 +00:00
|
|
|
name: 'AAvatar',
|
2017-11-09 10:57:34 +00:00
|
|
|
props: {
|
|
|
|
prefixCls: {
|
|
|
|
type: String,
|
|
|
|
default: 'ant-avatar',
|
|
|
|
},
|
|
|
|
shape: {
|
2017-11-10 02:28:49 +00:00
|
|
|
validator: (val) => (['circle', 'square'].includes(val)),
|
2017-11-09 10:57:34 +00:00
|
|
|
default: 'circle',
|
|
|
|
},
|
|
|
|
size: {
|
2018-09-05 13:28:54 +00:00
|
|
|
validator: (val) => {
|
|
|
|
return typeof val === 'number' || ['small', 'large', 'default'].includes(val)
|
|
|
|
},
|
2017-11-09 10:57:34 +00:00
|
|
|
default: 'default',
|
|
|
|
},
|
2017-11-10 02:28:49 +00:00
|
|
|
src: String,
|
|
|
|
icon: String,
|
2018-09-05 13:28:54 +00:00
|
|
|
alt: String,
|
|
|
|
loadError: Function,
|
2017-11-09 10:57:34 +00:00
|
|
|
},
|
|
|
|
data () {
|
|
|
|
return {
|
2018-09-05 13:28:54 +00:00
|
|
|
isImgExist: true,
|
2017-11-09 10:57:34 +00:00
|
|
|
scale: 1,
|
|
|
|
}
|
|
|
|
},
|
2018-09-05 13:28:54 +00:00
|
|
|
mounted () {
|
|
|
|
this.prevChildren = this.$slots.default
|
|
|
|
this.prevState = { ...this.$data }
|
|
|
|
this.$nextTick(() => {
|
|
|
|
this.setScale()
|
|
|
|
})
|
|
|
|
},
|
|
|
|
updated () {
|
|
|
|
if (this.preChildren !== this.$slots.default ||
|
|
|
|
(this.prevState.scale !== this.$data.scale && this.$data.scale === 1) ||
|
|
|
|
(this.prevState.isImgExist !== this.$data.isImgExist)) {
|
|
|
|
this.$nextTick(() => {
|
|
|
|
this.setScale()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
this.preChildren = this.$slots.default
|
|
|
|
this.prevState = { ...this.$data }
|
2017-11-09 10:57:34 +00:00
|
|
|
},
|
|
|
|
methods: {
|
|
|
|
setScale () {
|
2018-09-05 13:28:54 +00:00
|
|
|
const childrenNode = this.$refs.avatarChildren
|
|
|
|
if (childrenNode) {
|
|
|
|
const childrenWidth = childrenNode.offsetWidth
|
|
|
|
const avatarWidth = this.$el.getBoundingClientRect().width
|
|
|
|
if (avatarWidth - 8 < childrenWidth) {
|
|
|
|
this.scale = (avatarWidth - 8) / childrenWidth
|
2017-11-09 10:57:34 +00:00
|
|
|
} else {
|
|
|
|
this.scale = 1
|
|
|
|
}
|
2018-09-05 13:28:54 +00:00
|
|
|
this.$forceUpdate()
|
|
|
|
}
|
|
|
|
},
|
|
|
|
handleImgLoadError () {
|
|
|
|
const { loadError } = this.$props
|
|
|
|
const errorFlag = loadError ? loadError() : undefined
|
|
|
|
if (errorFlag !== false) {
|
|
|
|
this.isImgExist = false
|
2017-11-09 10:57:34 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
2018-03-19 01:43:31 +00:00
|
|
|
render () {
|
2018-09-05 13:28:54 +00:00
|
|
|
const {
|
|
|
|
prefixCls, shape, size, src, icon, alt,
|
|
|
|
} = this.$props
|
|
|
|
|
|
|
|
const { isImgExist, scale } = this.$data
|
|
|
|
|
|
|
|
const sizeCls = {
|
|
|
|
[`${prefixCls}-lg`]: size === 'large',
|
|
|
|
[`${prefixCls}-sm`]: size === 'small',
|
|
|
|
}
|
|
|
|
|
|
|
|
const classString = {
|
|
|
|
[prefixCls]: true,
|
|
|
|
...sizeCls,
|
|
|
|
[`${prefixCls}-${shape}`]: shape,
|
|
|
|
[`${prefixCls}-image`]: src && isImgExist,
|
|
|
|
[`${prefixCls}-icon`]: icon,
|
|
|
|
}
|
|
|
|
|
|
|
|
const sizeStyle = typeof size === 'number' ? {
|
|
|
|
width: `${size}px`,
|
|
|
|
height: `${size}px`,
|
|
|
|
lineHeight: `${size}px`,
|
|
|
|
fontSize: icon ? `${size / 2}px` : '18px',
|
|
|
|
} : {}
|
|
|
|
|
|
|
|
let children = this.$slots.default
|
|
|
|
if (src && isImgExist) {
|
|
|
|
children = (
|
|
|
|
<img
|
|
|
|
src={src}
|
|
|
|
onError={this.handleImgLoadError}
|
|
|
|
alt={alt}
|
|
|
|
/>
|
|
|
|
)
|
|
|
|
} else if (icon) {
|
|
|
|
children = <Icon type={icon} />
|
|
|
|
} else {
|
|
|
|
const childrenNode = this.$refs.avatarChildren
|
|
|
|
if (childrenNode || (scale !== 1 && childrenNode)) {
|
2018-11-28 02:35:37 +00:00
|
|
|
const transformString = `scale(${scale}) translateX(-50%)`
|
2018-09-05 13:28:54 +00:00
|
|
|
const childrenStyle = {
|
2018-11-28 02:35:37 +00:00
|
|
|
msTransform: transformString,
|
|
|
|
WebkitTransform: transformString,
|
|
|
|
transform: transformString,
|
2018-09-05 13:28:54 +00:00
|
|
|
}
|
|
|
|
const sizeChildrenStyle = typeof size === 'number' ? {
|
|
|
|
lineHeight: `${size}px`,
|
|
|
|
} : {}
|
|
|
|
children = (
|
|
|
|
<span
|
|
|
|
class={`${prefixCls}-string`}
|
|
|
|
ref='avatarChildren'
|
|
|
|
style={{ ...sizeChildrenStyle, ...childrenStyle }}
|
|
|
|
>
|
|
|
|
{children}
|
|
|
|
</span>
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
children = (
|
|
|
|
<span
|
|
|
|
class={`${prefixCls}-string`}
|
|
|
|
ref='avatarChildren'
|
|
|
|
>
|
|
|
|
{children}
|
|
|
|
</span>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
2018-03-19 01:43:31 +00:00
|
|
|
return (
|
2018-09-05 13:28:54 +00:00
|
|
|
<span {...{ on: this.$listeners, class: classString, style: sizeStyle }}>
|
|
|
|
{children}
|
2018-03-19 01:43:31 +00:00
|
|
|
</span>
|
|
|
|
)
|
2017-11-09 10:57:34 +00:00
|
|
|
},
|
|
|
|
}
|
2018-03-19 02:16:27 +00:00
|
|
|
|