index.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import _extends from "@babel/runtime/helpers/esm/extends";
  2. import { createNamespace } from '../utils';
  3. import { ChildrenMixin } from '../mixins/relation';
  4. var _createNamespace = createNamespace('swipe-item'),
  5. createComponent = _createNamespace[0],
  6. bem = _createNamespace[1];
  7. export default createComponent({
  8. mixins: [ChildrenMixin('vanSwipe')],
  9. data: function data() {
  10. return {
  11. offset: 0,
  12. inited: false,
  13. mounted: false
  14. };
  15. },
  16. mounted: function mounted() {
  17. var _this = this;
  18. this.$nextTick(function () {
  19. _this.mounted = true;
  20. });
  21. },
  22. computed: {
  23. style: function style() {
  24. var style = {};
  25. var _this$parent = this.parent,
  26. size = _this$parent.size,
  27. vertical = _this$parent.vertical;
  28. if (size) {
  29. style[vertical ? 'height' : 'width'] = size + "px";
  30. }
  31. if (this.offset) {
  32. style.transform = "translate" + (vertical ? 'Y' : 'X') + "(" + this.offset + "px)";
  33. }
  34. return style;
  35. },
  36. shouldRender: function shouldRender() {
  37. var index = this.index,
  38. inited = this.inited,
  39. parent = this.parent,
  40. mounted = this.mounted;
  41. if (!parent.lazyRender || inited) {
  42. return true;
  43. } // wait for all item to mount, so we can get the exact count
  44. if (!mounted) {
  45. return false;
  46. }
  47. var active = parent.activeIndicator;
  48. var maxActive = parent.count - 1;
  49. var prevActive = active === 0 && parent.loop ? maxActive : active - 1;
  50. var nextActive = active === maxActive && parent.loop ? 0 : active + 1;
  51. var shouldRender = index === active || index === prevActive || index === nextActive;
  52. if (shouldRender) {
  53. this.inited = true;
  54. }
  55. return shouldRender;
  56. }
  57. },
  58. render: function render() {
  59. var h = arguments[0];
  60. return h("div", {
  61. "class": bem(),
  62. "style": this.style,
  63. "on": _extends({}, this.$listeners)
  64. }, [this.shouldRender && this.slots()]);
  65. }
  66. });