Avatar.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. import _mergeJSXProps from 'babel-helper-vue-jsx-merge-props';
  2. import _extends from 'babel-runtime/helpers/extends';
  3. import _defineProperty from 'babel-runtime/helpers/defineProperty';
  4. import { ConfigConsumerProps } from '../config-provider/configConsumerProps';
  5. import Icon from '../icon';
  6. import { getListeners, getComponentFromProp } from '../_util/props-util';
  7. import PropTypes from '../_util/vue-types';
  8. export default {
  9. name: 'AAvatar',
  10. props: {
  11. prefixCls: {
  12. type: String,
  13. 'default': undefined
  14. },
  15. shape: {
  16. validator: function validator(val) {
  17. return ['circle', 'square'].includes(val);
  18. },
  19. 'default': 'circle'
  20. },
  21. size: {
  22. validator: function validator(val) {
  23. return typeof val === 'number' || ['small', 'large', 'default'].includes(val);
  24. },
  25. 'default': 'default'
  26. },
  27. src: String,
  28. /** Srcset of image avatar */
  29. srcSet: String,
  30. icon: PropTypes.any,
  31. alt: String,
  32. loadError: Function
  33. },
  34. inject: {
  35. configProvider: { 'default': function _default() {
  36. return ConfigConsumerProps;
  37. } }
  38. },
  39. data: function data() {
  40. return {
  41. isImgExist: true,
  42. isMounted: false,
  43. scale: 1
  44. };
  45. },
  46. watch: {
  47. src: function src() {
  48. var _this = this;
  49. this.$nextTick(function () {
  50. _this.isImgExist = true;
  51. _this.scale = 1;
  52. // force uodate for position
  53. _this.$forceUpdate();
  54. });
  55. }
  56. },
  57. mounted: function mounted() {
  58. var _this2 = this;
  59. this.$nextTick(function () {
  60. _this2.setScale();
  61. _this2.isMounted = true;
  62. });
  63. },
  64. updated: function updated() {
  65. var _this3 = this;
  66. this.$nextTick(function () {
  67. _this3.setScale();
  68. });
  69. },
  70. methods: {
  71. setScale: function setScale() {
  72. if (!this.$refs.avatarChildren || !this.$refs.avatarNode) {
  73. return;
  74. }
  75. var childrenWidth = this.$refs.avatarChildren.offsetWidth; // offsetWidth avoid affecting be transform scale
  76. var nodeWidth = this.$refs.avatarNode.offsetWidth;
  77. // denominator is 0 is no meaning
  78. if (childrenWidth === 0 || nodeWidth === 0 || this.lastChildrenWidth === childrenWidth && this.lastNodeWidth === nodeWidth) {
  79. return;
  80. }
  81. this.lastChildrenWidth = childrenWidth;
  82. this.lastNodeWidth = nodeWidth;
  83. // add 4px gap for each side to get better performance
  84. this.scale = nodeWidth - 8 < childrenWidth ? (nodeWidth - 8) / childrenWidth : 1;
  85. },
  86. handleImgLoadError: function handleImgLoadError() {
  87. var loadError = this.$props.loadError;
  88. var errorFlag = loadError ? loadError() : undefined;
  89. if (errorFlag !== false) {
  90. this.isImgExist = false;
  91. }
  92. }
  93. },
  94. render: function render() {
  95. var _sizeCls, _extends3;
  96. var h = arguments[0];
  97. var _$props = this.$props,
  98. customizePrefixCls = _$props.prefixCls,
  99. shape = _$props.shape,
  100. size = _$props.size,
  101. src = _$props.src,
  102. alt = _$props.alt,
  103. srcSet = _$props.srcSet;
  104. var icon = getComponentFromProp(this, 'icon');
  105. var getPrefixCls = this.configProvider.getPrefixCls;
  106. var prefixCls = getPrefixCls('avatar', customizePrefixCls);
  107. var _$data = this.$data,
  108. isImgExist = _$data.isImgExist,
  109. scale = _$data.scale,
  110. isMounted = _$data.isMounted;
  111. var sizeCls = (_sizeCls = {}, _defineProperty(_sizeCls, prefixCls + '-lg', size === 'large'), _defineProperty(_sizeCls, prefixCls + '-sm', size === 'small'), _sizeCls);
  112. var classString = _extends(_defineProperty({}, prefixCls, true), sizeCls, (_extends3 = {}, _defineProperty(_extends3, prefixCls + '-' + shape, shape), _defineProperty(_extends3, prefixCls + '-image', src && isImgExist), _defineProperty(_extends3, prefixCls + '-icon', icon), _extends3));
  113. var sizeStyle = typeof size === 'number' ? {
  114. width: size + 'px',
  115. height: size + 'px',
  116. lineHeight: size + 'px',
  117. fontSize: icon ? size / 2 + 'px' : '18px'
  118. } : {};
  119. var children = this.$slots['default'];
  120. if (src && isImgExist) {
  121. children = h('img', {
  122. attrs: { src: src, srcSet: srcSet, alt: alt },
  123. on: {
  124. 'error': this.handleImgLoadError
  125. }
  126. });
  127. } else if (icon) {
  128. if (typeof icon === 'string') {
  129. children = h(Icon, {
  130. attrs: { type: icon }
  131. });
  132. } else {
  133. children = icon;
  134. }
  135. } else {
  136. var childrenNode = this.$refs.avatarChildren;
  137. if (childrenNode || scale !== 1) {
  138. var transformString = 'scale(' + scale + ') translateX(-50%)';
  139. var childrenStyle = {
  140. msTransform: transformString,
  141. WebkitTransform: transformString,
  142. transform: transformString
  143. };
  144. var sizeChildrenStyle = typeof size === 'number' ? {
  145. lineHeight: size + 'px'
  146. } : {};
  147. children = h(
  148. 'span',
  149. {
  150. 'class': prefixCls + '-string',
  151. ref: 'avatarChildren',
  152. style: _extends({}, sizeChildrenStyle, childrenStyle)
  153. },
  154. [children]
  155. );
  156. } else {
  157. var _childrenStyle = {};
  158. if (!isMounted) {
  159. _childrenStyle.opacity = 0;
  160. }
  161. children = h(
  162. 'span',
  163. { 'class': prefixCls + '-string', ref: 'avatarChildren', style: { opacity: 0 } },
  164. [children]
  165. );
  166. }
  167. }
  168. return h(
  169. 'span',
  170. _mergeJSXProps([{ ref: 'avatarNode' }, { on: getListeners(this), 'class': classString, style: sizeStyle }]),
  171. [children]
  172. );
  173. }
  174. };