use-carousel-item.js 4.3 KB

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