grid-item.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. column: 0,
  55. labelID: '',
  56. };
  57. this.observers = {
  58. icon(icon) {
  59. const obj = setIcon('icon', icon, '');
  60. this.setData(Object.assign({}, obj));
  61. },
  62. };
  63. this.lifetimes = {
  64. ready() {
  65. this.setData({
  66. labelID: getUniqueID(),
  67. });
  68. },
  69. };
  70. }
  71. updateStyle() {
  72. const { hover, align } = this.parent.properties;
  73. const gridItemStyles = [];
  74. const gridItemWrapperStyles = [];
  75. const gridItemContentStyles = [];
  76. const widthStyle = this.getWidthStyle();
  77. const paddingStyle = this.getPaddingStyle();
  78. const borderStyle = this.getBorderStyle();
  79. widthStyle && gridItemStyles.push(widthStyle);
  80. paddingStyle && gridItemWrapperStyles.push(paddingStyle);
  81. borderStyle && gridItemContentStyles.push(borderStyle);
  82. this.setData({
  83. gridItemStyle: `${gridItemStyles.join(';')}`,
  84. gridItemWrapperStyle: gridItemWrapperStyles.join(';'),
  85. gridItemContentStyle: gridItemContentStyles.join(';'),
  86. hover,
  87. layout: this.properties.layout,
  88. align: align,
  89. });
  90. }
  91. getWidthStyle() {
  92. const { column } = this.parent.properties;
  93. return column > 0 ? `width:${(1 / column) * 100}%` : '';
  94. }
  95. getPaddingStyle() {
  96. const { gutter } = this.parent.properties;
  97. if (gutter)
  98. return `padding-left:${gutter}rpx;padding-top:${gutter}rpx`;
  99. return '';
  100. }
  101. getBorderStyle() {
  102. const { gutter } = this.parent.properties;
  103. let { border } = this.parent.properties;
  104. if (!border)
  105. return '';
  106. if (!isObject(border))
  107. border = {};
  108. const { color = '#266FE8', width = 2, style = 'solid' } = border;
  109. if (gutter)
  110. return `border:${width}rpx ${style} ${color}`;
  111. return `border-top:${width}rpx ${style} ${color};border-left:${width}rpx ${style} ${color}`;
  112. }
  113. onClick(e) {
  114. const { item } = e.currentTarget.dataset;
  115. this.triggerEvent('click', item);
  116. this.jumpLink();
  117. }
  118. jumpLink() {
  119. const { url, jumpType } = this.properties;
  120. if (url && jumpType) {
  121. if (LinkTypes[jumpType]) {
  122. wx[LinkTypes[jumpType]]({ url });
  123. }
  124. }
  125. }
  126. };
  127. GridItem = __decorate([
  128. wxComponent()
  129. ], GridItem);
  130. export default GridItem;