SkuRow.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. "use strict";
  2. exports.__esModule = true;
  3. exports.default = exports.bem = void 0;
  4. var _utils = require("../../utils");
  5. var _constant = require("../../utils/constant");
  6. var _relation = require("../../mixins/relation");
  7. var _bindEvent = require("../../mixins/bind-event");
  8. // Utils
  9. // Mixins
  10. var _createNamespace = (0, _utils.createNamespace)('sku-row'),
  11. createComponent = _createNamespace[0],
  12. bem = _createNamespace[1],
  13. t = _createNamespace[2];
  14. exports.bem = bem;
  15. var _default = createComponent({
  16. mixins: [(0, _relation.ParentMixin)('vanSkuRows'), (0, _bindEvent.BindEventMixin)(function (bind) {
  17. if (this.scrollable && this.$refs.scroller) {
  18. bind(this.$refs.scroller, 'scroll', this.onScroll);
  19. }
  20. })],
  21. props: {
  22. skuRow: Object
  23. },
  24. data: function data() {
  25. return {
  26. progress: 0
  27. };
  28. },
  29. computed: {
  30. scrollable: function scrollable() {
  31. return this.skuRow.largeImageMode && this.skuRow.v.length > 6;
  32. }
  33. },
  34. methods: {
  35. onScroll: function onScroll() {
  36. var _this$$refs = this.$refs,
  37. scroller = _this$$refs.scroller,
  38. row = _this$$refs.row;
  39. var distance = row.offsetWidth - scroller.offsetWidth;
  40. this.progress = scroller.scrollLeft / distance;
  41. },
  42. genTitle: function genTitle() {
  43. var h = this.$createElement;
  44. return h("div", {
  45. "class": bem('title')
  46. }, [this.skuRow.k, this.skuRow.is_multiple && h("span", {
  47. "class": bem('title-multiple')
  48. }, ["\uFF08", t('multiple'), "\uFF09"])]);
  49. },
  50. genIndicator: function genIndicator() {
  51. var h = this.$createElement;
  52. if (this.scrollable) {
  53. var style = {
  54. transform: "translate3d(" + this.progress * 20 + "px, 0, 0)"
  55. };
  56. return h("div", {
  57. "class": bem('indicator-wrapper')
  58. }, [h("div", {
  59. "class": bem('indicator')
  60. }, [h("div", {
  61. "class": bem('indicator-slider'),
  62. "style": style
  63. })])]);
  64. }
  65. },
  66. genContent: function genContent() {
  67. var h = this.$createElement;
  68. var nodes = this.slots();
  69. if (this.skuRow.largeImageMode) {
  70. var top = [];
  71. var bottom = [];
  72. nodes.forEach(function (node, index) {
  73. var group = Math.floor(index / 3) % 2 === 0 ? top : bottom;
  74. group.push(node);
  75. });
  76. return h("div", {
  77. "class": bem('scroller'),
  78. "ref": "scroller"
  79. }, [h("div", {
  80. "class": bem('row'),
  81. "ref": "row"
  82. }, [top]), bottom.length ? h("div", {
  83. "class": bem('row')
  84. }, [bottom]) : null]);
  85. }
  86. return nodes;
  87. },
  88. centerItem: function centerItem(selectSkuId) {
  89. if (!this.skuRow.largeImageMode || !selectSkuId) {
  90. return;
  91. }
  92. var _this$children = this.children,
  93. children = _this$children === void 0 ? [] : _this$children;
  94. var _this$$refs2 = this.$refs,
  95. scroller = _this$$refs2.scroller,
  96. row = _this$$refs2.row;
  97. var child = children.find(function (it) {
  98. return +it.skuValue.id === +selectSkuId;
  99. });
  100. if (scroller && row && child && child.$el) {
  101. var target = child.$el;
  102. var to = target.offsetLeft - (scroller.offsetWidth - target.offsetWidth) / 2;
  103. scroller.scrollLeft = to;
  104. }
  105. }
  106. },
  107. render: function render() {
  108. var h = arguments[0];
  109. return h("div", {
  110. "class": [bem(), _constant.BORDER_BOTTOM]
  111. }, [this.genTitle(), this.genContent(), this.genIndicator()]);
  112. }
  113. });
  114. exports.default = _default;