avatar-group.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
  2. var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
  3. if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
  4. else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
  5. return c > 3 && r && Object.defineProperty(target, key, r), r;
  6. };
  7. import { SuperComponent, wxComponent } from '../common/src/index';
  8. import config from '../common/config';
  9. import avatarGroupProps from './avatar-group-props';
  10. const { prefix } = config;
  11. const name = `${prefix}-avatar-group`;
  12. let AvatarGroup = class AvatarGroup extends SuperComponent {
  13. constructor() {
  14. super(...arguments);
  15. this.externalClasses = [`${prefix}-class`, `${prefix}-class-content`, `${prefix}-class-image`];
  16. this.properties = avatarGroupProps;
  17. this.data = {
  18. prefix,
  19. classPrefix: name,
  20. hasChild: true,
  21. length: 0,
  22. className: '',
  23. borderSize: '',
  24. };
  25. this.options = {
  26. multipleSlots: true,
  27. };
  28. this.relations = {
  29. './avatar': {
  30. type: 'descendant',
  31. linked() {
  32. this.children = this.getRelationNodes('./avatar');
  33. },
  34. },
  35. };
  36. this.lifetimes = {
  37. attached() {
  38. this.setClass();
  39. },
  40. };
  41. this.methods = {
  42. isINcludePX(size) {
  43. return size.indexOf('px') > -1;
  44. },
  45. setClass() {
  46. const { cascading, size } = this.properties;
  47. const classList = [name, `${prefix}-class`];
  48. const direction = cascading.split('-')[0];
  49. if (this.isINcludePX(size)) {
  50. classList.push(`${name}-offset-${direction}-medium`);
  51. }
  52. else {
  53. classList.push(`${name}-offset-${direction}-${size}`);
  54. }
  55. this.setData({
  56. className: classList.join(' '),
  57. });
  58. },
  59. handleHasChild(children, hasChild) {
  60. children.forEach((child) => {
  61. child.updateIsChild(hasChild);
  62. });
  63. },
  64. handleChildSlot(max, children, f) {
  65. const query = this.createSelectorQuery();
  66. const slotName = `.${this.data.classPrefix}__collapse--slot`;
  67. query.select(slotName).boundingClientRect();
  68. query.exec((res) => {
  69. const isSlot = !!res[0].width;
  70. f(max, children, isSlot);
  71. });
  72. },
  73. handleChildMax(max, children, isSlotElement) {
  74. const len = children.length;
  75. if (!max || max > len)
  76. return;
  77. const slotElement = isSlotElement ? children.pop() : '';
  78. const leftChildren = children.splice(max, len - max, isSlotElement && slotElement);
  79. leftChildren.forEach((child) => {
  80. child.updateShow();
  81. });
  82. },
  83. handleChildSize(size, children) {
  84. if (!size)
  85. return;
  86. children.forEach((child) => {
  87. child.updateSize(size);
  88. });
  89. },
  90. handleChildCascading(cascading, children) {
  91. if (cascading === 'right-up')
  92. return;
  93. const defaultZIndex = 100;
  94. children.forEach((child, index) => {
  95. child.updateCascading(defaultZIndex - index * 10);
  96. });
  97. },
  98. handleChildBorder(size, children) {
  99. const borderSize = this.isINcludePX(size) ? 'medium' : size;
  100. this.setData({
  101. borderSize,
  102. });
  103. children.forEach((child) => {
  104. child.updateBorder(borderSize);
  105. });
  106. },
  107. };
  108. }
  109. ready() {
  110. this.setData({
  111. length: this.children.length,
  112. });
  113. this.handleHasChild(this.children, this.data.hasChild);
  114. this.handleChildSlot(this.properties.max, this.children, this.handleChildMax);
  115. this.handleChildSize(this.properties.size, this.children);
  116. this.handleChildCascading(this.properties.cascading, this.children);
  117. this.handleChildBorder(this.properties.size, this.children);
  118. }
  119. };
  120. AvatarGroup = __decorate([
  121. wxComponent()
  122. ], AvatarGroup);
  123. export default AvatarGroup;