skeleton.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 props from './props';
  10. import { isNumber, classNames } from '../common/utils';
  11. const { prefix } = config;
  12. const name = `${prefix}-skeleton`;
  13. const ThemeMap = {
  14. avatar: [{ type: 'circle', size: '96rpx' }],
  15. image: [{ type: 'rect', size: '144rpx' }],
  16. text: [
  17. [
  18. { width: '24%', height: '32rpx', marginRight: '32rpx' },
  19. { width: '76%', height: '32rpx' },
  20. ],
  21. 1,
  22. ],
  23. paragraph: [1, 1, 1, { width: '55%' }],
  24. };
  25. let Skeleton = class Skeleton extends SuperComponent {
  26. constructor() {
  27. super(...arguments);
  28. this.externalClasses = [`${prefix}-class`, `${prefix}-class-col`, `${prefix}-class-row`];
  29. this.properties = props;
  30. this.data = {
  31. prefix,
  32. classPrefix: name,
  33. parsedRowcols: [],
  34. };
  35. this.observers = {
  36. rowCol() {
  37. this.init();
  38. },
  39. 'loading, delay'() {
  40. this.isShowSkeleton();
  41. },
  42. };
  43. this.lifetimes = {
  44. attached() {
  45. this.init();
  46. this.isShowSkeleton();
  47. },
  48. };
  49. this.methods = {
  50. init() {
  51. const { theme, rowCol } = this.properties;
  52. const rowCols = [];
  53. if (rowCol.length) {
  54. rowCols.push(...rowCol);
  55. }
  56. else {
  57. rowCols.push(...ThemeMap[theme || 'text']);
  58. }
  59. const parsedRowcols = rowCols.map((item) => {
  60. if (isNumber(item)) {
  61. return [
  62. {
  63. class: this.getColItemClass({ type: 'text' }),
  64. style: {},
  65. },
  66. ];
  67. }
  68. if (Array.isArray(item)) {
  69. return item.map((col) => {
  70. return Object.assign(Object.assign({}, col), { class: this.getColItemClass(col), style: this.getColItemStyle(col) });
  71. });
  72. }
  73. const nItem = item;
  74. return [
  75. Object.assign(Object.assign({}, nItem), { class: this.getColItemClass(nItem), style: this.getColItemStyle(nItem) }),
  76. ];
  77. });
  78. this.setData({
  79. parsedRowcols,
  80. });
  81. },
  82. getColItemClass(obj) {
  83. return classNames([
  84. `${name}__col`,
  85. `${name}--type-${obj.type || 'text'}`,
  86. `${name}--animation-${this.properties.animation}`,
  87. ]);
  88. },
  89. getColItemStyle(obj) {
  90. const styleName = [
  91. 'width',
  92. 'height',
  93. 'marginRight',
  94. 'marginLeft',
  95. 'margin',
  96. 'size',
  97. 'background',
  98. 'backgroundColor',
  99. 'borderRadius',
  100. ];
  101. const style = {};
  102. styleName.forEach((name) => {
  103. if (name in obj) {
  104. const px = isNumber(obj[name]) ? `${obj[name]}px` : obj[name];
  105. if (name === 'size') {
  106. [style.width, style.height] = [px, px];
  107. }
  108. else {
  109. style[name] = px;
  110. }
  111. }
  112. });
  113. return style;
  114. },
  115. isShowSkeleton() {
  116. const { loading, delay } = this.properties;
  117. if (!loading || delay === 0) {
  118. this.setData({
  119. isShow: loading,
  120. });
  121. return;
  122. }
  123. setTimeout(() => {
  124. this.setData({
  125. isShow: loading,
  126. });
  127. }, delay);
  128. },
  129. };
  130. }
  131. };
  132. Skeleton = __decorate([
  133. wxComponent()
  134. ], Skeleton);
  135. export default Skeleton;