SkuRow.js 3.4 KB

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