{"version":3,"file":"use-slide.mjs","sources":["../../../../../../../packages/components/slider/src/composables/use-slide.ts"],"sourcesContent":["import { computed, nextTick, ref, shallowRef } from 'vue'\nimport {\n CHANGE_EVENT,\n INPUT_EVENT,\n UPDATE_MODEL_EVENT,\n} from '@element-plus/constants'\nimport { useFormItem } from '@element-plus/components/form'\nimport type { CSSProperties, Ref, SetupContext } from 'vue'\nimport type { Arrayable } from '@element-plus/utils'\nimport type { SliderEmits, SliderInitData, SliderProps } from '../slider'\nimport type { ButtonRefs, SliderButtonInstance } from '../button'\n\nexport const useSlide = (\n props: SliderProps,\n initData: SliderInitData,\n emit: SetupContext['emit']\n) => {\n const { form: elForm, formItem: elFormItem } = useFormItem()\n\n const slider = shallowRef()\n\n const firstButton = ref()\n\n const secondButton = ref()\n\n const buttonRefs: ButtonRefs = {\n firstButton,\n secondButton,\n }\n\n const sliderDisabled = computed(() => {\n return props.disabled || elForm?.disabled || false\n })\n\n const minValue = computed(() => {\n return Math.min(initData.firstValue, initData.secondValue)\n })\n\n const maxValue = computed(() => {\n return Math.max(initData.firstValue, initData.secondValue)\n })\n\n const barSize = computed(() => {\n return props.range\n ? `${\n (100 * (maxValue.value - minValue.value)) / (props.max - props.min)\n }%`\n : `${\n (100 * (initData.firstValue - props.min)) / (props.max - props.min)\n }%`\n })\n\n const barStart = computed(() => {\n return props.range\n ? `${(100 * (minValue.value - props.min)) / (props.max - props.min)}%`\n : '0%'\n })\n\n const runwayStyle = computed(() => {\n return props.vertical ? { height: props.height } : {}\n })\n\n const barStyle = computed(() => {\n return props.vertical\n ? {\n height: barSize.value,\n bottom: barStart.value,\n }\n : {\n width: barSize.value,\n left: barStart.value,\n }\n })\n\n const resetSize = () => {\n if (slider.value) {\n initData.sliderSize =\n slider.value[`client${props.vertical ? 'Height' : 'Width'}`]\n }\n }\n\n const getButtonRefByPercent = (\n percent: number\n ): Ref => {\n const targetValue = props.min + (percent * (props.max - props.min)) / 100\n if (!props.range) {\n return firstButton\n }\n let buttonRefName: 'firstButton' | 'secondButton'\n if (\n Math.abs(minValue.value - targetValue) <\n Math.abs(maxValue.value - targetValue)\n ) {\n buttonRefName =\n initData.firstValue < initData.secondValue\n ? 'firstButton'\n : 'secondButton'\n } else {\n buttonRefName =\n initData.firstValue > initData.secondValue\n ? 'firstButton'\n : 'secondButton'\n }\n return buttonRefs[buttonRefName]\n }\n\n const setPosition = (\n percent: number\n ): Ref => {\n const buttonRef = getButtonRefByPercent(percent)\n buttonRef.value!.setPosition(percent)\n return buttonRef\n }\n\n const setFirstValue = (firstValue: number | undefined) => {\n initData.firstValue = firstValue ?? props.min\n _emit(\n props.range ? [minValue.value, maxValue.value] : firstValue ?? props.min\n )\n }\n\n const setSecondValue = (secondValue: number) => {\n initData.secondValue = secondValue\n\n if (props.range) {\n _emit([minValue.value, maxValue.value])\n }\n }\n\n const _emit = (val: Arrayable) => {\n emit(UPDATE_MODEL_EVENT, val)\n emit(INPUT_EVENT, val)\n }\n\n const emitChange = async () => {\n await nextTick()\n emit(\n CHANGE_EVENT,\n props.range ? [minValue.value, maxValue.value] : props.modelValue\n )\n }\n\n const handleSliderPointerEvent = (\n event: MouseEvent | TouchEvent\n ): Ref | undefined => {\n if (sliderDisabled.value || initData.dragging) return\n resetSize()\n let newPercent = 0\n if (props.vertical) {\n const clientY =\n (event as TouchEvent).touches?.item(0)?.clientY ??\n (event as MouseEvent).clientY\n const sliderOffsetBottom = slider.value!.getBoundingClientRect().bottom\n newPercent = ((sliderOffsetBottom - clientY) / initData.sliderSize) * 100\n } else {\n const clientX =\n (event as TouchEvent).touches?.item(0)?.clientX ??\n (event as MouseEvent).clientX\n const sliderOffsetLeft = slider.value!.getBoundingClientRect().left\n newPercent = ((clientX - sliderOffsetLeft) / initData.sliderSize) * 100\n }\n if (newPercent < 0 || newPercent > 100) return\n return setPosition(newPercent)\n }\n\n const onSliderWrapperPrevent = (event: TouchEvent) => {\n if (\n buttonRefs['firstButton'].value?.dragging ||\n buttonRefs['secondButton'].value?.dragging\n ) {\n event.preventDefault()\n }\n }\n\n const onSliderDown = async (event: MouseEvent | TouchEvent) => {\n const buttonRef = handleSliderPointerEvent(event)\n if (buttonRef) {\n await nextTick()\n buttonRef.value!.onButtonDown(event)\n }\n }\n\n const onSliderClick = (event: MouseEvent | TouchEvent) => {\n const buttonRef = handleSliderPointerEvent(event)\n if (buttonRef) {\n emitChange()\n }\n }\n\n const onSliderMarkerDown = (position: number) => {\n if (sliderDisabled.value || initData.dragging) return\n setPosition(position)\n }\n\n return {\n elFormItem,\n slider,\n firstButton,\n secondButton,\n sliderDisabled,\n minValue,\n maxValue,\n runwayStyle,\n barStyle,\n resetSize,\n setPosition,\n emitChange,\n onSliderWrapperPrevent,\n onSliderClick,\n onSliderDown,\n onSliderMarkerDown,\n setFirstValue,\n setSecondValue,\n }\n}\n"],"names":[],"mappings":";;;;;;AAOY,MAAC,QAAQ,GAAG,CAAC,KAAK,EAAE,QAAQ,EAAE,IAAI,KAAK;AACnD,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,QAAQ,EAAE,UAAU,EAAE,GAAG,WAAW,EAAE,CAAC;AAC/D,EAAE,MAAM,MAAM,GAAG,UAAU,EAAE,CAAC;AAC9B,EAAE,MAAM,WAAW,GAAG,GAAG,EAAE,CAAC;AAC5B,EAAE,MAAM,YAAY,GAAG,GAAG,EAAE,CAAC;AAC7B,EAAE,MAAM,UAAU,GAAG;AACrB,IAAI,WAAW;AACf,IAAI,YAAY;AAChB,GAAG,CAAC;AACJ,EAAE,MAAM,cAAc,GAAG,QAAQ,CAAC,MAAM;AACxC,IAAI,OAAO,KAAK,CAAC,QAAQ,KAAK,MAAM,IAAI,IAAI,GAAG,KAAK,CAAC,GAAG,MAAM,CAAC,QAAQ,CAAC,IAAI,KAAK,CAAC;AAClF,GAAG,CAAC,CAAC;AACL,EAAE,MAAM,QAAQ,GAAG,QAAQ,CAAC,MAAM;AAClC,IAAI,OAAO,IAAI,CAAC,GAAG,CAAC,QAAQ,CA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