-
-
Notifications
You must be signed in to change notification settings - Fork 78.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract Carousel's swipe functionality to a separate Class (#32999)
- Loading branch information
Showing
5 changed files
with
417 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -54,7 +54,7 @@ | |
}, | ||
{ | ||
"path": "./dist/js/bootstrap.min.js", | ||
"maxSize": "16 kB" | ||
"maxSize": "16.25 kB" | ||
} | ||
], | ||
"ci": { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
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 NAME = 'swipe' | ||
|
||
const Default = { | ||
leftCallback: null, | ||
rightCallback: null, | ||
endCallback: null | ||
} | ||
|
||
const DefaultType = { | ||
leftCallback: '(function|null)', | ||
rightCallback: '(function|null)', | ||
endCallback: '(function|null)' | ||
} | ||
|
||
class Swipe { | ||
constructor(element, config) { | ||
this._element = element | ||
|
||
if (!element || !Swipe.isSupported()) { | ||
return | ||
} | ||
|
||
this._config = this._getConfig(config) | ||
this._deltaX = 0 | ||
this._supportPointerEvents = Boolean(window.PointerEvent) | ||
this._initEvents() | ||
} | ||
|
||
dispose() { | ||
EventHandler.off(this._element, EVENT_KEY) | ||
} | ||
|
||
_start(event) { | ||
if (!this._supportPointerEvents) { | ||
this._deltaX = event.touches[0].clientX | ||
|
||
return | ||
} | ||
|
||
if (this._eventIsPointerPenTouch(event)) { | ||
this._deltaX = event.clientX | ||
} | ||
} | ||
|
||
_end(event) { | ||
if (this._eventIsPointerPenTouch(event)) { | ||
this._deltaX = event.clientX - this._deltaX | ||
} | ||
|
||
this._handleSwipe() | ||
execute(this._config.endCallback) | ||
} | ||
|
||
_move(event) { | ||
this._deltaX = event.touches && event.touches.length > 1 ? | ||
0 : | ||
event.touches[0].clientX - this._deltaX | ||
} | ||
|
||
_handleSwipe() { | ||
const absDeltaX = Math.abs(this._deltaX) | ||
|
||
if (absDeltaX <= SWIPE_THRESHOLD) { | ||
return | ||
} | ||
|
||
const direction = absDeltaX / this._deltaX | ||
|
||
this._deltaX = 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 | ||
} | ||
|
||
_eventIsPointerPenTouch(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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.