grid-item.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 './grid-item-props';
  10. const { prefix } = config;
  11. const name = `${prefix}-grid-item`;
  12. var LinkTypes;
  13. (function (LinkTypes) {
  14. LinkTypes["redirect-to"] = "redirectTo";
  15. LinkTypes["switch-tab"] = "switchTab";
  16. LinkTypes["relaunch"] = "reLaunch";
  17. LinkTypes["navigate-to"] = "navigateTo";
  18. })(LinkTypes || (LinkTypes = {}));
  19. let GridItem = class GridItem extends SuperComponent {
  20. constructor() {
  21. super(...arguments);
  22. this.externalClasses = ['t-class', 't-class-image', 't-class-text', 't-class-description'];
  23. this.options = {
  24. multipleSlots: true,
  25. };
  26. this.relations = {
  27. './grid': {
  28. type: 'ancestor',
  29. linked(target) {
  30. this.parent = target;
  31. this.updateStyle();
  32. },
  33. },
  34. };
  35. this.properties = props;
  36. this.data = {
  37. classPrefix: name,
  38. gridItemStyle: '',
  39. gridItemWrapperStyle: '',
  40. gridItemContentStyle: '',
  41. align: 'center',
  42. layout: 'vertical',
  43. };
  44. }
  45. updateStyle() {
  46. const { hover, align } = this.parent.properties;
  47. const gridItemStyles = [];
  48. const gridItemWrapperStyles = [];
  49. const gridItemContentStyles = [];
  50. const widthStyle = this.getWidthStyle();
  51. const paddingStyle = this.getPaddingStyle();
  52. const borderStyle = this.getBorderStyle();
  53. widthStyle && gridItemStyles.push(widthStyle);
  54. paddingStyle && gridItemWrapperStyles.push(paddingStyle);
  55. borderStyle && gridItemContentStyles.push(borderStyle);
  56. this.setData({
  57. gridItemStyle: gridItemStyles.join(';'),
  58. gridItemWrapperStyle: gridItemWrapperStyles.join(';'),
  59. gridItemContentStyle: gridItemContentStyles.join(';'),
  60. hover,
  61. layout: this.properties.layout,
  62. align: align,
  63. });
  64. }
  65. // 判断应该加在gridItem上的宽度
  66. getWidthStyle() {
  67. const { column = 4 } = this.parent.properties;
  68. return `width:${(1 / column) * 100}%`;
  69. }
  70. // 获取应该加在gridWrap上的padding
  71. getPaddingStyle() {
  72. const { gutter } = this.parent.properties;
  73. if (gutter)
  74. return `padding-left:${gutter}rpx;padding-top:${gutter}rpx`;
  75. return '';
  76. }
  77. // 判断border在grid-item-content上的css属性
  78. getBorderStyle() {
  79. const { gutter } = this.parent.properties;
  80. let { border } = this.parent.properties;
  81. if (!border)
  82. return ''; // 如果border的值没传或者是border的值为false
  83. if (!isObject(border))
  84. border = {};
  85. const { color = '#266FE8', width = 2, style = 'solid' } = border;
  86. if (gutter)
  87. return `border:${width}rpx ${style} ${color}`;
  88. return `border-top:${width}rpx ${style} ${color};border-left:${width}rpx ${style} ${color}`;
  89. }
  90. onClick(e) {
  91. const { item } = e.currentTarget.dataset;
  92. this.triggerEvent('click', item);
  93. this.jumpLink();
  94. }
  95. jumpLink() {
  96. const { url, jumpType } = this.properties;
  97. if (url && jumpType) {
  98. if (LinkTypes[jumpType]) {
  99. wx[LinkTypes[jumpType]]({ url });
  100. }
  101. }
  102. }
  103. };
  104. GridItem = __decorate([
  105. wxComponent()
  106. ], GridItem);
  107. export default GridItem;
  108. //# sourceMappingURL=grid-item.js.map