skeleton.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. };
  40. this.lifetimes = {
  41. attached() {
  42. this.init();
  43. },
  44. };
  45. this.methods = {
  46. init() {
  47. const { theme, rowCol } = this.properties;
  48. const rowCols = [];
  49. if (rowCol.length) {
  50. rowCols.push(...rowCol);
  51. }
  52. else {
  53. rowCols.push(...ThemeMap[theme || 'text']);
  54. }
  55. const parsedRowcols = rowCols.map((item) => {
  56. if (isNumber(item)) {
  57. return [
  58. {
  59. class: this.getColItemClass({ type: 'text' }),
  60. style: {},
  61. },
  62. ];
  63. }
  64. if (Array.isArray(item)) {
  65. return item.map((col) => {
  66. return Object.assign(Object.assign({}, col), { class: this.getColItemClass(col), style: this.getColItemStyle(col) });
  67. });
  68. }
  69. const nItem = item;
  70. return [
  71. Object.assign(Object.assign({}, nItem), { class: this.getColItemClass(nItem), style: this.getColItemStyle(nItem) }),
  72. ];
  73. });
  74. this.setData({
  75. parsedRowcols,
  76. });
  77. },
  78. getColItemClass(obj) {
  79. return classNames([
  80. `${name}__col`,
  81. `${name}--type-${obj.type || 'text'}`,
  82. `${name}--animation-${this.properties.animation}`,
  83. ]);
  84. },
  85. getColItemStyle(obj) {
  86. const styleName = [
  87. 'width',
  88. 'height',
  89. 'marginRight',
  90. 'marginLeft',
  91. 'margin',
  92. 'size',
  93. 'background',
  94. 'backgroundColor',
  95. 'borderRadius',
  96. ];
  97. const style = {};
  98. styleName.forEach((name) => {
  99. if (name in obj) {
  100. const px = isNumber(obj[name]) ? `${obj[name]}px` : obj[name];
  101. if (name === 'size') {
  102. [style.width, style.height] = [px, px];
  103. }
  104. else {
  105. style[name] = px;
  106. }
  107. }
  108. });
  109. return style;
  110. },
  111. };
  112. }
  113. };
  114. Skeleton = __decorate([
  115. wxComponent()
  116. ], Skeleton);
  117. export default Skeleton;