2020-06-08 07:38:07 +00:00
|
|
|
import { inject } from 'vue';
|
2019-03-11 11:58:32 +00:00
|
|
|
import { ConfigConsumerProps } from '../config-provider';
|
2020-06-08 07:38:07 +00:00
|
|
|
import { getComponent } from '../_util/props-util';
|
2020-03-07 11:45:13 +00:00
|
|
|
import PropTypes from '../_util/vue-types';
|
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,
|
2019-09-06 11:01:49 +00:00
|
|
|
default: undefined,
|
2017-11-09 10:57:34 +00:00
|
|
|
},
|
|
|
|
shape: {
|
2019-01-12 03:33:27 +00:00
|
|
|
validator: val => ['circle', 'square'].includes(val),
|
2017-11-09 10:57:34 +00:00
|
|
|
default: 'circle',
|
|
|
|
},
|
|
|
|
size: {
|
2019-01-12 03:33:27 +00:00
|
|
|
validator: val => {
|
|
|
|
return typeof val === 'number' || ['small', 'large', 'default'].includes(val);
|
2018-09-05 13:28:54 +00:00
|
|
|
},
|
2017-11-09 10:57:34 +00:00
|
|
|
default: 'default',
|
|
|
|
},
|
2017-11-10 02:28:49 +00:00
|
|
|
src: String,
|
2019-01-02 12:53:52 +00:00
|
|
|
/** Srcset of image avatar */
|
|
|
|
srcSet: String,
|
2020-03-07 11:45:13 +00:00
|
|
|
icon: PropTypes.any,
|
2018-09-05 13:28:54 +00:00
|
|
|
alt: String,
|
|
|
|
loadError: Function,
|
2017-11-09 10:57:34 +00:00
|
|
|
},
|
2020-06-08 07:38:07 +00:00
|
|
|
setup() {
|
|
|
|
return {
|
|
|
|
configProvider: inject('configProvider', ConfigConsumerProps),
|
|
|
|
};
|
2019-03-11 11:58:32 +00:00
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
data() {
|
2017-11-09 10:57:34 +00:00
|
|
|
return {
|
2018-09-05 13:28:54 +00:00
|
|
|
isImgExist: true,
|
2020-03-07 11:45:13 +00:00
|
|
|
isMounted: false,
|
2017-11-09 10:57:34 +00:00
|
|
|
scale: 1,
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|
2017-11-09 10:57:34 +00:00
|
|
|
},
|
2019-04-09 12:39:49 +00:00
|
|
|
watch: {
|
|
|
|
src() {
|
2019-09-06 11:01:49 +00:00
|
|
|
this.$nextTick(() => {
|
|
|
|
this.isImgExist = true;
|
|
|
|
this.scale = 1;
|
|
|
|
// force uodate for position
|
|
|
|
this.$forceUpdate();
|
|
|
|
});
|
2019-04-09 12:39:49 +00:00
|
|
|
},
|
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
mounted() {
|
2018-09-05 13:28:54 +00:00
|
|
|
this.$nextTick(() => {
|
2019-01-12 03:33:27 +00:00
|
|
|
this.setScale();
|
2020-03-07 11:45:13 +00:00
|
|
|
this.isMounted = true;
|
2019-01-12 03:33:27 +00:00
|
|
|
});
|
2018-09-05 13:28:54 +00:00
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
updated() {
|
2020-03-07 11:45:13 +00:00
|
|
|
this.$nextTick(() => {
|
|
|
|
this.setScale();
|
|
|
|
});
|
2017-11-09 10:57:34 +00:00
|
|
|
},
|
|
|
|
methods: {
|
2019-01-12 03:33:27 +00:00
|
|
|
setScale() {
|
2020-03-07 11:45:13 +00:00
|
|
|
if (!this.$refs.avatarChildren || !this.$refs.avatarNode) {
|
|
|
|
return;
|
2018-09-05 13:28:54 +00:00
|
|
|
}
|
2020-03-07 11:45:13 +00:00
|
|
|
const childrenWidth = this.$refs.avatarChildren.offsetWidth; // offsetWidth avoid affecting be transform scale
|
|
|
|
const nodeWidth = this.$refs.avatarNode.offsetWidth;
|
|
|
|
// denominator is 0 is no meaning
|
|
|
|
if (
|
|
|
|
childrenWidth === 0 ||
|
|
|
|
nodeWidth === 0 ||
|
|
|
|
(this.lastChildrenWidth === childrenWidth && this.lastNodeWidth === nodeWidth)
|
|
|
|
) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.lastChildrenWidth = childrenWidth;
|
|
|
|
this.lastNodeWidth = nodeWidth;
|
|
|
|
// add 4px gap for each side to get better performance
|
|
|
|
this.scale = nodeWidth - 8 < childrenWidth ? (nodeWidth - 8) / childrenWidth : 1;
|
2018-09-05 13:28:54 +00:00
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
handleImgLoadError() {
|
|
|
|
const { loadError } = this.$props;
|
|
|
|
const errorFlag = loadError ? loadError() : undefined;
|
2018-09-05 13:28:54 +00:00
|
|
|
if (errorFlag !== false) {
|
2019-01-12 03:33:27 +00:00
|
|
|
this.isImgExist = false;
|
2017-11-09 10:57:34 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
render() {
|
2020-03-07 11:45:13 +00:00
|
|
|
const { prefixCls: customizePrefixCls, shape, size, src, alt, srcSet } = this.$props;
|
2020-06-08 07:38:07 +00:00
|
|
|
const icon = getComponent(this, 'icon');
|
2019-09-11 14:35:25 +00:00
|
|
|
const getPrefixCls = this.configProvider.getPrefixCls;
|
2019-03-11 11:58:32 +00:00
|
|
|
const prefixCls = getPrefixCls('avatar', customizePrefixCls);
|
2018-09-05 13:28:54 +00:00
|
|
|
|
2020-03-07 11:45:13 +00:00
|
|
|
const { isImgExist, scale, isMounted } = this.$data;
|
2018-09-05 13:28:54 +00:00
|
|
|
|
|
|
|
const sizeCls = {
|
|
|
|
[`${prefixCls}-lg`]: size === 'large',
|
|
|
|
[`${prefixCls}-sm`]: size === 'small',
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|
2018-09-05 13:28:54 +00:00
|
|
|
|
|
|
|
const classString = {
|
|
|
|
[prefixCls]: true,
|
|
|
|
...sizeCls,
|
|
|
|
[`${prefixCls}-${shape}`]: shape,
|
|
|
|
[`${prefixCls}-image`]: src && isImgExist,
|
|
|
|
[`${prefixCls}-icon`]: icon,
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|
2018-09-05 13:28:54 +00:00
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
const sizeStyle =
|
|
|
|
typeof size === 'number'
|
|
|
|
? {
|
|
|
|
width: `${size}px`,
|
|
|
|
height: `${size}px`,
|
|
|
|
lineHeight: `${size}px`,
|
|
|
|
fontSize: icon ? `${size / 2}px` : '18px',
|
|
|
|
}
|
|
|
|
: {};
|
2018-09-05 13:28:54 +00:00
|
|
|
|
2020-06-08 07:38:07 +00:00
|
|
|
let children = this.$slots.default && this.$slots.default();
|
2018-09-05 13:28:54 +00:00
|
|
|
if (src && isImgExist) {
|
2019-01-12 03:33:27 +00:00
|
|
|
children = <img src={src} srcSet={srcSet} onError={this.handleImgLoadError} alt={alt} />;
|
2018-09-05 13:28:54 +00:00
|
|
|
} else if (icon) {
|
2020-03-28 07:28:06 +00:00
|
|
|
children = icon;
|
2018-09-05 13:28:54 +00:00
|
|
|
} else {
|
2019-01-12 03:33:27 +00:00
|
|
|
const childrenNode = this.$refs.avatarChildren;
|
2020-03-07 11:45:13 +00:00
|
|
|
if (childrenNode || scale !== 1) {
|
2019-01-12 03:33:27 +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,
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|
|
|
|
const sizeChildrenStyle =
|
|
|
|
typeof size === 'number'
|
|
|
|
? {
|
|
|
|
lineHeight: `${size}px`,
|
|
|
|
}
|
|
|
|
: {};
|
2018-09-05 13:28:54 +00:00
|
|
|
children = (
|
|
|
|
<span
|
|
|
|
class={`${prefixCls}-string`}
|
2019-01-12 03:33:27 +00:00
|
|
|
ref="avatarChildren"
|
2018-09-05 13:28:54 +00:00
|
|
|
style={{ ...sizeChildrenStyle, ...childrenStyle }}
|
|
|
|
>
|
|
|
|
{children}
|
|
|
|
</span>
|
2019-01-12 03:33:27 +00:00
|
|
|
);
|
2018-09-05 13:28:54 +00:00
|
|
|
} else {
|
2020-03-07 11:45:13 +00:00
|
|
|
const childrenStyle = {};
|
|
|
|
if (!isMounted) {
|
|
|
|
childrenStyle.opacity = 0;
|
|
|
|
}
|
2018-09-05 13:28:54 +00:00
|
|
|
children = (
|
2020-03-07 11:45:13 +00:00
|
|
|
<span class={`${prefixCls}-string`} ref="avatarChildren" style={{ opacity: 0 }}>
|
2018-09-05 13:28:54 +00:00
|
|
|
{children}
|
|
|
|
</span>
|
2019-01-12 03:33:27 +00:00
|
|
|
);
|
2018-09-05 13:28:54 +00:00
|
|
|
}
|
|
|
|
}
|
2018-03-19 01:43:31 +00:00
|
|
|
return (
|
2020-06-08 07:38:07 +00:00
|
|
|
<span ref="avatarNode" {...{ class: classString, style: sizeStyle }}>
|
2020-03-07 11:45:13 +00:00
|
|
|
{children}
|
|
|
|
</span>
|
2019-01-12 03:33:27 +00:00
|
|
|
);
|
2017-11-09 10:57:34 +00:00
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|