grid-item.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 = [
  23. `${prefix}-class`,
  24. `${prefix}-class-content`,
  25. `${prefix}-class-image`,
  26. `${prefix}-class-text`,
  27. `${prefix}-class-description`,
  28. ];
  29. this.options = {
  30. multipleSlots: true,
  31. };
  32. this.relations = {
  33. './grid': {
  34. type: 'ancestor',
  35. linked(target) {
  36. this.parent = target;
  37. this.updateStyle();
  38. },
  39. },
  40. };
  41. this.properties = props;
  42. this.data = {
  43. prefix,
  44. classPrefix: name,
  45. gridItemStyle: '',
  46. gridItemWrapperStyle: '',
  47. gridItemContentStyle: '',
  48. align: 'center',
  49. layout: 'vertical',
  50. };
  51. }
  52. updateStyle() {
  53. const { hover, align } = this.parent.properties;
  54. const gridItemStyles = [];
  55. const gridItemWrapperStyles = [];
  56. const gridItemContentStyles = [];
  57. const widthStyle = this.getWidthStyle();
  58. const paddingStyle = this.getPaddingStyle();
  59. const borderStyle = this.getBorderStyle();
  60. widthStyle && gridItemStyles.push(widthStyle);
  61. paddingStyle && gridItemWrapperStyles.push(paddingStyle);
  62. borderStyle && gridItemContentStyles.push(borderStyle);
  63. this.setData({
  64. gridItemStyle: gridItemStyles.join(';'),
  65. gridItemWrapperStyle: gridItemWrapperStyles.join(';'),
  66. gridItemContentStyle: gridItemContentStyles.join(';'),
  67. hover,
  68. layout: this.properties.layout,
  69. align: align,
  70. });
  71. }
  72. getWidthStyle() {
  73. const { column } = this.parent.properties;
  74. return `width:${(1 / column) * 100}%`;
  75. }
  76. getPaddingStyle() {
  77. const { gutter } = this.parent.properties;
  78. if (gutter)
  79. return `padding-left:${gutter}rpx;padding-top:${gutter}rpx`;
  80. return '';
  81. }
  82. getBorderStyle() {
  83. const { gutter } = this.parent.properties;
  84. let { border } = this.parent.properties;
  85. if (!border)
  86. return '';
  87. if (!isObject(border))
  88. border = {};
  89. const { color = '#266FE8', width = 2, style = 'solid' } = border;
  90. if (gutter)
  91. return `border:${width}rpx ${style} ${color}`;
  92. return `border-top:${width}rpx ${style} ${color};border-left:${width}rpx ${style} ${color}`;
  93. }
  94. onClick(e) {
  95. const { item } = e.currentTarget.dataset;
  96. this.triggerEvent('click', item);
  97. this.jumpLink();
  98. }
  99. jumpLink() {
  100. const { url, jumpType } = this.properties;
  101. if (url && jumpType) {
  102. if (LinkTypes[jumpType]) {
  103. wx[LinkTypes[jumpType]]({ url });
  104. }
  105. }
  106. }
  107. };
  108. GridItem = __decorate([
  109. wxComponent()
  110. ], GridItem);
  111. export default GridItem;