{"version":3,"file":"item.vue.mjs","sources":["../../../../../packages/components/carousel/src/item.vue"],"sourcesContent":["<template>\n  <div\n    v-show=\"data.ready\"\n    :class=\"[\n      ns.e('item'),\n      ns.is('active', data.active),\n      ns.is('in-stage', data.inStage),\n      ns.is('hover', data.hover),\n      ns.is('animating', data.animating),\n      { [ns.em('item', 'card')]: type === 'card' },\n    ]\"\n    :style=\"itemStyle\"\n    @click=\"handleItemClick\"\n  >\n    <div\n      v-if=\"type === 'card'\"\n      v-show=\"!data.active\"\n      :class=\"ns.e('mask')\"\n    ></div>\n    <slot></slot>\n  </div>\n</template>\n<script lang=\"ts\">\nimport {\n  defineComponent,\n  reactive,\n  onMounted,\n  inject,\n  computed,\n  toRefs,\n  getCurrentInstance,\n  onUnmounted,\n} from 'vue'\nimport { debugWarn } from '@element-ultra/utils'\nimport { useNamespace } from '@element-ultra/hooks'\nimport type { CSSProperties } from 'vue'\nimport type { InjectCarouselScope, ICarouselItemProps } from './carousel'\n\nconst CARD_SCALE = 0.83\nexport default defineComponent({\n  name: 'ElCarouselItem',\n  props: {\n    name: { type: String, default: '' },\n    label: {\n      type: [String, Number],\n      default: '',\n    },\n  },\n  setup(props: ICarouselItemProps) {\n    const ns = useNamespace('carousel')\n    // instance\n    const instance = getCurrentInstance()\n\n    // data\n    const data = reactive({\n      hover: false,\n      translate: 0,\n      scale: 1,\n      active: false,\n      ready: false,\n      inStage: false,\n      animating: false,\n    })\n\n    // inject\n    const injectCarouselScope: InjectCarouselScope = inject(\n      'injectCarouselScope'\n    )\n\n    // computed\n    const parentDirection = computed(() => {\n      return injectCarouselScope.direction\n    })\n\n    const itemStyle = computed(() => {\n      const translateType =\n        parentDirection.value === 'vertical' ? 'translateY' : 'translateX'\n      const value = `${translateType}(${data.translate}px) scale(${data.scale})`\n      const style: CSSProperties = {\n        transform: value,\n      }\n      return style\n    })\n\n    // methods\n\n    function processIndex(index, activeIndex, length) {\n      if (activeIndex === 0 && index === length - 1) {\n        return -1\n      } else if (activeIndex === length - 1 && index === 0) {\n        return length\n      } else if (index < activeIndex - 1 && activeIndex - index >= length / 2) {\n        return length + 1\n      } else if (index > activeIndex + 1 && index - activeIndex >= length / 2) {\n        return -2\n      }\n      return index\n    }\n\n    function calcCardTranslate(index, activeIndex) {\n      const parentWidth = injectCarouselScope.root.value?.offsetWidth || 0\n      if (data.inStage) {\n        return (\n          (parentWidth * ((2 - CARD_SCALE) * (index - activeIndex) + 1)) / 4\n        )\n      } else if (index < activeIndex) {\n        return (-(1 + CARD_SCALE) * parentWidth) / 4\n      } else {\n        return ((3 + CARD_SCALE) * parentWidth) / 4\n      }\n    }\n\n    function calcTranslate(index, activeIndex, isVertical) {\n      const distance =\n        (isVertical\n          ? injectCarouselScope.root.value?.offsetHeight\n          : injectCarouselScope.root.value?.offsetWidth) || 0\n      return distance * (index - activeIndex)\n    }\n\n    const translateItem = (\n      index: number,\n      activeIndex: number,\n      oldIndex: number\n    ) => {\n      const parentType = injectCarouselScope.type\n      const length = injectCarouselScope.items.value.length\n      if (parentType !== 'card' && oldIndex !== undefined) {\n        data.animating = index === activeIndex || index === oldIndex\n      }\n      if (index !== activeIndex && length > 2 && injectCarouselScope.loop) {\n        index = processIndex(index, activeIndex, length)\n      }\n      if (parentType === 'card') {\n        if (parentDirection.value === 'vertical') {\n          debugWarn(\n            'Carousel',\n            'vertical direction is not supported in card mode'\n          )\n        }\n        data.inStage = Math.round(Math.abs(index - activeIndex)) <= 1\n        data.active = index === activeIndex\n        data.translate = calcCardTranslate(index, activeIndex)\n        data.scale = data.active ? 1 : CARD_SCALE\n      } else {\n        data.active = index === activeIndex\n        const isVertical = parentDirection.value === 'vertical'\n        data.translate = calcTranslate(index, activeIndex, isVertical)\n      }\n      data.ready = true\n    }\n\n    function handleItemClick() {\n      if (injectCarouselScope && injectCarouselScope.type === 'card') {\n        const index = injectCarouselScope.items.value\n          .map((d) => d.uid)\n          .indexOf(instance.uid)\n        injectCarouselScope.setActiveItem(index)\n      }\n    }\n\n    // lifecycle\n    onMounted(() => {\n      if (injectCarouselScope.addItem) {\n        injectCarouselScope.addItem({\n          uid: instance.uid,\n          ...props,\n          ...toRefs(data),\n          translateItem,\n        })\n      }\n    })\n\n    onUnmounted(() => {\n      if (injectCarouselScope.removeItem) {\n        injectCarouselScope.removeItem(instance.uid)\n      }\n    })\n\n    return {\n      data,\n      itemStyle,\n      translateItem,\n      type: injectCarouselScope.type,\n      handleItemClick,\n      ns,\n    }\n  },\n})\n</script>\n"],"names":["_withDirectives","_openBlock","_createElementBlock","_normalizeClass","_normalizeStyle","_createCommentVNode"],"mappings":";;;;AAGU,SAAA,YAAA,IAAA,EAAA,MAAA,EAAA,MAAA,EAAA,MAAA,EAAA,OAAA,QAAA,EAAA;EAAU,OAAAA,cAAA,EAAIC,WAAA,EAAAC,kBAAA;AAAA,IAAA,KAAA;AAAA,IAAA;AAAA,MAAA,OAAgBC,cAAgB,CAAA;AAAA,QAAoB,IAAA,GAAG,CAAA,CAAA,CAAE,MAAA,CAAA;AAAA,QAAkC,KAAG,EAAA,CAAA,EAAA,CAAE,QAAU,EAAA,IAAA,CAAA,KAAK,MAAK,CAAA;AAAA,QAAS,KAAG,EAAA,CAAA,EAAA,CAAE,UAAc,EAAA,IAAA,CAAA,KAAI,OAAC,CAAA;AAAA,QAAqB,KAAA,EAAA,CAAA,EAAA,CAAG,OAAE,EAAA,IAAA,CAAmB,KAAA,KAAA,CAAA;AAAA,QAAA,KAAA,EAAA,CAAA,EAAA,CAAA,WAAA,EAAA,IAAA,CAAA,KAAA,SAAA,CAAA;AAAA,QAQrN,EAAA,CAAK,IAAA,aAAE,EAAA,MAAS,CAAA,GAAA,IAAA,CAAA,IAAA,KAAA,MAAA,EAAA;AAAA,OAChB,CAAA;AAAA,MAAA,KAAA,EAAAC,cAAA,CAAA,IAAA,CAAA,SAAA,CAAA;AAAA,aAGO,EAAA,MAAA,CAAI,CAAA,CAAA,KAAA,OAAA,CAAA,CAAA,GAAA,CAAA,GAAA,IAAA,K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