diff options
| author | GeoSot <[email protected]> | 2021-02-06 01:04:19 +0200 |
|---|---|---|
| committer | XhmikosR <[email protected]> | 2021-09-07 19:19:12 +0300 |
| commit | a0e5cf46e066eea485704b3d6c932f57e2fa95ac (patch) | |
| tree | 7214962d80b31a9611a23a1cfe28bce85fffa33a /js/src/util | |
| parent | 0d81d3cbc14dfcdca8a868e3f25189a4f1ab273c (diff) | |
| download | bootstrap-swipe-events-as-separate-helper.tar.xz bootstrap-swipe-events-as-separate-helper.zip | |
Separate Swipe helper from carouselswipe-events-as-separate-helper
Diffstat (limited to 'js/src/util')
| -rw-r--r-- | js/src/util/swipe.js | 129 |
1 files changed, 129 insertions, 0 deletions
diff --git a/js/src/util/swipe.js b/js/src/util/swipe.js new file mode 100644 index 000000000..4c3f9e807 --- /dev/null +++ b/js/src/util/swipe.js @@ -0,0 +1,129 @@ +/** + * -------------------------------------------------------------------------- + * Bootstrap (v5.1.1): util/swipe.js + * Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE) + * -------------------------------------------------------------------------- + */ + +import EventHandler from '../dom/event-handler' +import { execute, typeCheckConfig } from './index' + +const EVENT_KEY = '.bs.swipe' +const EVENT_TOUCHSTART = `touchstart${EVENT_KEY}` +const EVENT_TOUCHMOVE = `touchmove${EVENT_KEY}` +const EVENT_TOUCHEND = `touchend${EVENT_KEY}` +const EVENT_POINTERDOWN = `pointerdown${EVENT_KEY}` +const EVENT_POINTERUP = `pointerup${EVENT_KEY}` +const POINTER_TYPE_TOUCH = 'touch' +const POINTER_TYPE_PEN = 'pen' +const CLASS_NAME_POINTER_EVENT = 'pointer-event' +const SWIPE_THRESHOLD = 40 + +const Default = { + leftCallback: null, + rightCallback: null, + endCallback: null +} +const DefaultType = { + leftCallback: '(function|null)', + rightCallback: '(function|null)', + endCallback: '(function|null)' +} + +const NAME = 'swipe' + +class Swipe { + constructor(element, config) { + this._element = element + + if (!element || !Swipe.isSupported()) { + return + } + + this._config = this._getConfig(config) + this._xDown = 0 + this._supportPointerEvents = Boolean(window.PointerEvent) + this._initEvents() + } + + dispose() { + EventHandler.off(this._element, EVENT_KEY) + } + + _start(event) { + if (!this._supportPointerEvents) { + this._xDown = event.touches[0].clientX + + return + } + + if (this._eventIsNotSwipe(event)) { + this._xDown = event.clientX + } + } + + _end(event) { + if (this._eventIsNotSwipe(event)) { + this._xDown = event.clientX - this._xDown + } + + this._handleSwipe() + execute(this._config.endCallback) + } + + _move(event) { + this._xDown = event.touches && event.touches.length > 1 ? + 0 : + event.touches[0].clientX - this._xDown + } + + _handleSwipe() { + const absDeltaX = Math.abs(this._xDown) + + if (absDeltaX <= SWIPE_THRESHOLD) { + return + } + + const direction = absDeltaX / this._xDown + + this._xDown = 0 + + if (!direction) { + return + } + + execute(direction > 0 ? this._config.rightCallback : this._config.leftCallback) + } + + _initEvents() { + if (this._supportPointerEvents) { + EventHandler.on(this._element, EVENT_POINTERDOWN, event => this._start(event)) + EventHandler.on(this._element, EVENT_POINTERUP, event => this._end(event)) + + this._element.classList.add(CLASS_NAME_POINTER_EVENT) + } else { + EventHandler.on(this._element, EVENT_TOUCHSTART, event => this._start(event)) + EventHandler.on(this._element, EVENT_TOUCHMOVE, event => this._move(event)) + EventHandler.on(this._element, EVENT_TOUCHEND, event => this._end(event)) + } + } + + _getConfig(config) { + config = { + ...Default, + ...(typeof config === 'object' ? config : {}) + } + typeCheckConfig(NAME, config, DefaultType) + return config + } + + _eventIsNotSwipe(event) { + return this._supportPointerEvents && (event.pointerType === POINTER_TYPE_PEN || event.pointerType === POINTER_TYPE_TOUCH) + } + + static isSupported() { + return 'ontouchstart' in document.documentElement || navigator.maxTouchPoints > 0 + } +} + +export default Swipe |
