{"version":3,"sources":["swipeable/swipeable.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,OAAO,CAAC;AAI1B,eAAO,MAAM,cAAc,mDAExB,OAAO,CAAC,UAAU,GAAG,YAAY,CAAC;;;CAIpC,CAAC;AAEF,oBAAY,cAAc,GAAG;IAEzB;;OAEG;IACH,KAAK,CAAC,EAAE,MAAM,CAAC;IAEf;;OAEG;IACH,OAAO,EAAE,CAAC,SAAS,EAAE,KAAK,GAAG,OAAO,GAAG,QAAQ,GAAE,MAAM,KAAK,IAAI,CAAC;IAEjE;;;OAGG;IACH,QAAQ,EAAE,GAAG,CAAC;CACjB,CAAC;AAEF,cAAM,SAAU,SAAQ,KAAK,CAAC,aAAa,CAAC,cAAc,CAAC;IACvD,MAAM,CAAC,YAAY,EAAE,OAAO,CAAC,cAAc,CAAC,CAE1C;IAEF,WAAW,SAAK;IAEhB,WAAW,SAAK;IAEhB,MAAM,SAAK;IAEX,MAAM,SAAK;IAEX,oBAAoB;IAIpB,MAAM;;;;IAON,OAAO,CAAC,eAAe,CASrB;IAEF,OAAO,CAAC,gBAAgB,CActB;IAEF,OAAO,CAAC,gBAAgB,CAKtB;IAEF,OAAO,CAAC,cAAc,CAkBpB;IAEF,OAAO,CAAC,eAAe,CAUrB;CACL;AAED,eAAe,SAAS,CAAC","file":"swipeable.d.ts","sourcesContent":["import React from 'react';\n\n// TODO: функция вызывается на TouchEvent и PointerEvent хотя больше от этого путанницы\n// Тут надо отдельно рефачить и типы и имплементацию чтоб ничего не сломать\nexport const getCoordinates = ({\n    touches, changedTouches, clientX, clientY,\n}: Partial<TouchEvent & PointerEvent>) => (\n    (touches && changedTouches)\n        ? { clientX: (touches[0] || changedTouches[0]).clientX, clientY: (touches[0] || changedTouches[0]).clientY }\n        : { clientX, clientY }\n);\n\nexport type SwipeableProps = {\n\n    /**\n     * Число пикселей, на которое нужно сместиться, чтобы запустить функцию по свайпу\n     */\n    delta?: number;\n\n    /**\n     * Функция запускаемая по свайпу.\n     */\n    onSwipe: (direction: 'top' | 'right' | 'bottom' |'left') => void;\n\n    /**\n     * Дочерний компонент представленный в виде единичного элемента\n     * TODO: // тут должны быть такие children что у ниъ есть onMouseDown onTouchStart\n     */\n    children: any;\n};\n\nclass Swipeable extends React.PureComponent<SwipeableProps> {\n    static defaultProps: Partial<SwipeableProps> = {\n        delta: 100,\n    };\n\n    swipeStartX = 0;\n\n    swipeStartY = 0;\n\n    deltaX = 0;\n\n    deltaY = 0;\n\n    componentWillUnmount() {\n        this.removeListeners();\n    }\n\n    render() {\n        return React.cloneElement(this.props.children, {\n            onMouseDown: this.handleMouseDown,\n            onTouchStart: this.handleTouchStart,\n        });\n    }\n\n    private handleMouseDown = (event) => {\n        if (this.props.children.props.onMouseDown) {\n            this.props.children.props.onMouseDown(event);\n        }\n\n        this.handleSwipeStart(event);\n\n        document.addEventListener('mouseup', this.handleSwipeEnd);\n        document.addEventListener('mouseup', this.removeListeners);\n    };\n\n    private handleTouchStart = (event) => {\n        if (event.touches && event.touches.length > 1) {\n            return;\n        }\n\n        if (this.props.children.props.onTouchStart) {\n            this.props.children.props.onTouchStart(event);\n        }\n\n        this.handleSwipeStart(event);\n\n        document.addEventListener('touchend', this.handleSwipeEnd);\n        document.addEventListener('touchend', this.removeListeners);\n        document.addEventListener('touchcancel', this.removeListeners);\n    };\n\n    private handleSwipeStart = (event) => {\n        const { clientX, clientY } = getCoordinates(event);\n\n        this.swipeStartX = clientX;\n        this.swipeStartY = clientY;\n    };\n\n    private handleSwipeEnd = (event) => {\n        const { props: { delta, onSwipe } } = this;\n        const { clientX, clientY } = getCoordinates(event);\n\n        this.deltaX = this.swipeStartX - clientX;\n        this.deltaY = this.swipeStartY - clientY;\n\n        if (typeof onSwipe === 'function') {\n            if (this.deltaX > delta) {\n                onSwipe('left');\n            } else if (this.deltaX < -delta) {\n                onSwipe('right');\n            } else if (this.deltaY > delta) {\n                onSwipe('top');\n            } else if (this.deltaY < -delta) {\n                onSwipe('bottom');\n            }\n        }\n    };\n\n    private removeListeners = () => {\n        document.removeEventListener('mouseup', this.handleSwipeEnd);\n        document.removeEventListener('mouseup', this.removeListeners);\n        document.removeEventListener('touchend', this.handleSwipeEnd);\n        document.removeEventListener('touchend', this.removeListeners);\n        document.removeEventListener('touchcancel', this.removeListeners);\n        this.swipeStartX = 0;\n        this.swipeStartY = 0;\n        this.deltaX = 0;\n        this.deltaY = 0;\n    };\n}\n\nexport default Swipeable;\n"]}