grid-item.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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, isObject } from '../common/src/index';
  8. import config from '../common/config';
  9. import props from './props';
  10. import { uniqueFactory, setIcon } from '../common/utils';
  11. const { prefix } = config;
  12. const name = `${prefix}-grid-item`;
  13. const getUniqueID = uniqueFactory('grid_item');
  14. var LinkTypes;
  15. (function (LinkTypes) {
  16. LinkTypes["redirect-to"] = "redirectTo";
  17. LinkTypes["switch-tab"] = "switchTab";
  18. LinkTypes["relaunch"] = "reLaunch";
  19. LinkTypes["navigate-to"] = "navigateTo";
  20. })(LinkTypes || (LinkTypes = {}));
  21. let GridItem = class GridItem extends SuperComponent {
  22. constructor() {
  23. super(...arguments);
  24. this.externalClasses = [
  25. `${prefix}-class`,
  26. `${prefix}-class-content`,
  27. `${prefix}-class-image`,
  28. `${prefix}-class-text`,
  29. `${prefix}-class-description`,
  30. ];
  31. this.options = {
  32. multipleSlots: true,
  33. };
  34. this.relations = {
  35. '../grid/grid': {
  36. type: 'ancestor',
  37. linked(target) {
  38. this.parent = target;
  39. this.updateStyle();
  40. this.setData({
  41. column: target.data.column,
  42. });
  43. },
  44. },
  45. };
  46. this.properties = props;
  47. this.data = {
  48. prefix,
  49. classPrefix: name,
  50. gridItemStyle: '',
  51. gridItemWrapperStyle: '',
  52. gridItemContentStyle: '',
  53. align: 'center',
  54. layout: 'vertical',
  55. column: 0,
  56. labelID: '',
  57. };
  58. this.observers = {
  59. icon(icon) {
  60. const obj = setIcon('icon', icon, '');
  61. this.setData(Object.assign({}, obj));
  62. },
  63. };
  64. this.lifetimes = {
  65. ready() {
  66. this.setData({
  67. labelID: getUniqueID(),
  68. });
  69. },
  70. };
  71. }
  72. updateStyle() {
  73. const { hover, align } = this.parent.properties;
  74. const { customStyle } = this.properties;
  75. const gridItemStyles = [];
  76. const gridItemWrapperStyles = [];
  77. const gridItemContentStyles = [];
  78. const widthStyle = this.getWidthStyle();
  79. const paddingStyle = this.getPaddingStyle();
  80. const borderStyle = this.getBorderStyle();
  81. widthStyle && gridItemStyles.push(widthStyle);
  82. paddingStyle && gridItemWrapperStyles.push(paddingStyle);
  83. borderStyle && gridItemContentStyles.push(borderStyle);
  84. this.setData({
  85. gridItemStyle: `${gridItemStyles.join(';')}${customStyle ? `;${customStyle}` : ''}`,
  86. gridItemWrapperStyle: gridItemWrapperStyles.join(';'),
  87. gridItemContentStyle: gridItemContentStyles.join(';'),
  88. hover,
  89. layout: this.properties.layout,
  90. align: align,
  91. });
  92. }
  93. getWidthStyle() {
  94. const { column } = this.parent.properties;
  95. return column > 0 ? `width:${(1 / column) * 100}%` : '';
  96. }
  97. getPaddingStyle() {
  98. const { gutter } = this.parent.properties;
  99. if (gutter)
  100. return `padding-left:${gutter}rpx;padding-top:${gutter}rpx`;
  101. return '';
  102. }
  103. getBorderStyle() {
  104. const { gutter } = this.parent.properties;
  105. let { border } = this.parent.properties;
  106. if (!border)
  107. return '';
  108. if (!isObject(border))
  109. border = {};
  110. const { color = '#266FE8', width = 2, style = 'solid' } = border;
  111. if (gutter)
  112. return `border:${width}rpx ${style} ${color}`;
  113. return `border-top:${width}rpx ${style} ${color};border-left:${width}rpx ${style} ${color}`;
  114. }
  115. onClick(e) {
  116. const { item } = e.currentTarget.dataset;
  117. this.triggerEvent('click', item);
  118. this.jumpLink();
  119. }
  120. jumpLink() {
  121. const { url, jumpType } = this.properties;
  122. if (url && jumpType) {
  123. if (LinkTypes[jumpType]) {
  124. wx[LinkTypes[jumpType]]({ url });
  125. }
  126. }
  127. }
  128. };
  129. GridItem = __decorate([
  130. wxComponent()
  131. ], GridItem);
  132. export default GridItem;