use-carousel-item.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', { value: true });
  3. var vue = require('vue');
  4. var constants = require('./constants.js');
  5. var error = require('../../../utils/error.js');
  6. var types = require('../../../utils/types.js');
  7. const useCarouselItem = (props) => {
  8. const carouselContext = vue.inject(constants.carouselContextKey);
  9. const instance = vue.getCurrentInstance();
  10. if (!carouselContext) {
  11. error.debugWarn(constants.CAROUSEL_ITEM_NAME, "usage: <el-carousel></el-carousel-item></el-carousel>");
  12. }
  13. if (!instance) {
  14. error.debugWarn(constants.CAROUSEL_ITEM_NAME, "compositional hook can only be invoked inside setups");
  15. }
  16. const carouselItemRef = vue.ref();
  17. const hover = vue.ref(false);
  18. const translate = vue.ref(0);
  19. const scale = vue.ref(1);
  20. const active = vue.ref(false);
  21. const ready = vue.ref(false);
  22. const inStage = vue.ref(false);
  23. const animating = vue.ref(false);
  24. const { isCardType, isVertical, cardScale } = carouselContext;
  25. function processIndex(index, activeIndex, length) {
  26. const lastItemIndex = length - 1;
  27. const prevItemIndex = activeIndex - 1;
  28. const nextItemIndex = activeIndex + 1;
  29. const halfItemIndex = length / 2;
  30. if (activeIndex === 0 && index === lastItemIndex) {
  31. return -1;
  32. } else if (activeIndex === lastItemIndex && index === 0) {
  33. return length;
  34. } else if (index < prevItemIndex && activeIndex - index >= halfItemIndex) {
  35. return length + 1;
  36. } else if (index > nextItemIndex && index - activeIndex >= halfItemIndex) {
  37. return -2;
  38. }
  39. return index;
  40. }
  41. function calcCardTranslate(index, activeIndex) {
  42. var _a, _b;
  43. const parentWidth = vue.unref(isVertical) ? ((_a = carouselContext.root.value) == null ? void 0 : _a.offsetHeight) || 0 : ((_b = carouselContext.root.value) == null ? void 0 : _b.offsetWidth) || 0;
  44. if (inStage.value) {
  45. return parentWidth * ((2 - cardScale) * (index - activeIndex) + 1) / 4;
  46. } else if (index < activeIndex) {
  47. return -(1 + cardScale) * parentWidth / 4;
  48. } else {
  49. return (3 + cardScale) * parentWidth / 4;
  50. }
  51. }
  52. function calcTranslate(index, activeIndex, isVertical2) {
  53. const rootEl = carouselContext.root.value;
  54. if (!rootEl)
  55. return 0;
  56. const distance = (isVertical2 ? rootEl.offsetHeight : rootEl.offsetWidth) || 0;
  57. return distance * (index - activeIndex);
  58. }
  59. const translateItem = (index, activeIndex, oldIndex) => {
  60. var _a;
  61. const _isCardType = vue.unref(isCardType);
  62. const carouselItemLength = (_a = carouselContext.items.value.length) != null ? _a : Number.NaN;
  63. const isActive = index === activeIndex;
  64. if (!_isCardType && !types.isUndefined(oldIndex)) {
  65. animating.value = isActive || index === oldIndex;
  66. }
  67. if (!isActive && carouselItemLength > 2 && carouselContext.loop) {
  68. index = processIndex(index, activeIndex, carouselItemLength);
  69. }
  70. const _isVertical = vue.unref(isVertical);
  71. active.value = isActive;
  72. if (_isCardType) {
  73. inStage.value = Math.round(Math.abs(index - activeIndex)) <= 1;
  74. translate.value = calcCardTranslate(index, activeIndex);
  75. scale.value = vue.unref(active) ? 1 : cardScale;
  76. } else {
  77. translate.value = calcTranslate(index, activeIndex, _isVertical);
  78. }
  79. ready.value = true;
  80. if (isActive && carouselItemRef.value) {
  81. carouselContext.setContainerHeight(carouselItemRef.value.offsetHeight);
  82. }
  83. };
  84. function handleItemClick() {
  85. if (carouselContext && vue.unref(isCardType)) {
  86. const index = carouselContext.items.value.findIndex(({ uid }) => uid === instance.uid);
  87. carouselContext.setActiveItem(index);
  88. }
  89. }
  90. vue.onMounted(() => {
  91. carouselContext.addItem({
  92. props,
  93. states: vue.reactive({
  94. hover,
  95. translate,
  96. scale,
  97. active,
  98. ready,
  99. inStage,
  100. animating
  101. }),
  102. uid: instance.uid,
  103. translateItem
  104. });
  105. });
  106. vue.onUnmounted(() => {
  107. carouselContext.removeItem(instance.uid);
  108. });
  109. return {
  110. carouselItemRef,
  111. active,
  112. animating,
  113. hover,
  114. inStage,
  115. isVertical,
  116. translate,
  117. isCardType,
  118. scale,
  119. ready,
  120. handleItemClick
  121. };
  122. };
  123. exports.useCarouselItem = useCarouselItem;
  124. //# sourceMappingURL=use-carousel-item.js.map