import {BtnRefs, TimeSliderInitData, TimeSliderProp} from "./type"; import {computed, nextTick, ref} from "vue"; import {CHANGE_EVENT} from "../../utils/common"; import {off, on} from "../../utils/dom"; export const useSlider = (props: TimeSliderProp, initData: TimeSliderInitData, emit) => { const slider = ref>(null) const firstBtn = ref(null) const secondBtn = ref(null) const buttonRefs: BtnRefs = { firstBtn, secondBtn, } const minValue = computed(() => { return Math.min(initData.firstValue, initData.secondValue) }) const maxValue = computed(() => { return Math.max(initData.firstValue, initData.secondValue) }) const limit = computed(() => { return initData.limit / (props.max - props.min) * 100 }) const barSize = computed(() => { return 100 * (maxValue.value - minValue.value) / (props.max - props.min) }) const barStart = computed(() => { return 100 * (minValue.value - props.min) / (props.max - props.min) }) const runwayStyle = computed(() => { return (props.vertical ? {height: props.height} : {}) as CSSStyleDeclaration }) const barStyle = computed(() => { return (props.vertical ? { height: `${barSize.value}%`, bottom: `${barStart.value}%`, width: `${props.sliderWeight - 2}px` } : { width: `${barSize.value}%`, left: `${barStart.value}%`, height: `${props.sliderWeight - 2}px` }) as CSSStyleDeclaration }) const barStyleLeft = computed(() => { return (props.vertical ? { height: `${barStart.value}%`, bottom: 0, width: `${props.sliderWeight - 2}px` } : { width: `${barStart.value}%`, left: 0, height: `${props.sliderWeight - 2}px` }) as unknown as CSSStyleDeclaration }) const barStyleRight = computed(() => { return (props.vertical ? { bottom: `${barStart.value + barSize.value}%`, top: 0, width: `${props.sliderWeight - 2}px` } : { left: `${barStart.value + barSize.value}%`, right: 0, height: `${props.sliderWeight - 2}px` }) as unknown as CSSStyleDeclaration }) const handleMouseEnter = () => { initData.hovering = true Enter() } const handleMouseLeave = () => { initData.hovering = false if (!initData.drag) { Leave() } } const Enter = () => { buttonRefs['firstBtn'].value.handleMouseEnter() buttonRefs['secondBtn'].value.handleMouseEnter() } const Leave = () => { buttonRefs['firstBtn'].value.handleMouseLeave() buttonRefs['secondBtn'].value.handleMouseLeave() } const onBarDown = (event: MouseEvent | TouchEvent) => { if (props.disabled) return event.preventDefault() onDragStart(event) on(window, 'mousemove', onDragging) on(window, 'touchmove', onDragging) on(window, 'mouseup', onDragEnd) on(window, 'touchend', onDragEnd) on(window, 'contextmenu', onDragEnd) } const onDragStart = (event: MouseEvent | TouchEvent) => { initData.drag = true const {clientX, clientY} = getClientXY(event) if (props.vertical) { initData.startY = clientY } else { initData.startX = clientX } initData.firstPosition = (initData.firstValue - props.min) / (props.max - props.min) * 100 initData.secondPosition = (initData.secondValue - props.min) / (props.max - props.min) * 100 initData.poorValue = initData.secondPosition - initData.firstPosition handleMouseEnter() } const onDragging = (event: MouseEvent | TouchEvent) => { const {clientX, clientY} = getClientXY(event) let diff if (props.vertical) { diff = (initData.startY - clientY) / initData.sliderSize * 100 } else { diff = (clientX - initData.startX) / initData.sliderSize * 100 } let first = initData.firstPosition + diff let second = initData.secondPosition + diff let limitMax if (isNaN(limit.value)) { limitMax = 100 } else { limitMax = limit.value } if (first + initData.poorValue > limitMax) { buttonRefs['firstBtn'].value.setPosition(limitMax - initData.poorValue) buttonRefs['secondBtn'].value.setPosition(second) } else if (second - initData.poorValue < 0) { buttonRefs['firstBtn'].value.setPosition(first) buttonRefs['secondBtn'].value.setPosition(initData.poorValue) } else { buttonRefs['firstBtn'].value.setPosition(first) buttonRefs['secondBtn'].value.setPosition(second) } } const onDragEnd = () => { setTimeout(() => { initData.drag = false if (!initData.hovering) { Leave() } emitChange() }, 0) off(window, 'mousemove', onDragging) off(window, 'touchmove', onDragging) off(window, 'mouseup', onDragEnd) off(window, 'touchend', onDragEnd) off(window, 'contextmenu', onDragEnd) } //获得当前鼠标位置、当前触摸的位置 const getClientXY = (event: MouseEvent | TouchEvent) => { let clientX: number let clientY: number if (event.type.startsWith('touch')) { clientX = (event as TouchEvent).touches[0].clientX clientY = (event as TouchEvent).touches[0].clientY } else { clientX = (event as MouseEvent).clientX clientY = (event as MouseEvent).clientY } return { clientX, clientY } } const resetSize = () => { if (slider.value) { initData.sliderSize = slider.value[`client${props.vertical ? 'Height' : 'Width'}`] } } const setPosition = (percent: number, event: MouseEvent) => { onDragStart(event) initData.drag = false let limitMax if (isNaN(limit.value)) { limitMax = 100 } else { limitMax = limit.value } if (percent > limitMax - initData.poorValue / 2) { buttonRefs['firstBtn'].value.setPosition(limitMax - initData.poorValue) buttonRefs['secondBtn'].value.setPosition(limitMax) } else if (percent > initData.poorValue / 2 && percent < limitMax - initData.poorValue / 2) { buttonRefs['firstBtn'].value.setPosition(percent - initData.poorValue / 2) buttonRefs['secondBtn'].value.setPosition(percent + initData.poorValue / 2) } else { buttonRefs['firstBtn'].value.setPosition(0) buttonRefs['secondBtn'].value.setPosition(initData.poorValue) } } const emitChange = async () => { await nextTick() emit(CHANGE_EVENT, [minValue.value, maxValue.value]) } const onSliderClick = (event: MouseEvent) => { if (props.disabled || initData.dragging) return resetSize() if (props.vertical) { const sliderOffsetBottom = slider.value.getBoundingClientRect().bottom setPosition((sliderOffsetBottom - event.clientY) / initData.sliderSize * 100, event) } else { const sliderOffsetLeft = slider.value.getBoundingClientRect().left setPosition((event.clientX - sliderOffsetLeft) / initData.sliderSize * 100, event) } emitChange() } return { slider, firstBtn, secondBtn, minValue, maxValue, barStyle, runwayStyle, barStyleLeft, barStyleRight, resetSize, setPosition, emitChange, onSliderClick, onBarDown, handleMouseEnter, handleMouseLeave, } }