[d565449] | 1 | /*!
|
---|
| 2 | * Bootstrap swipe.js v5.3.3 (https://getbootstrap.com/)
|
---|
| 3 | * Copyright 2011-2024 The Bootstrap Authors (https://github.com/twbs/bootstrap/graphs/contributors)
|
---|
| 4 | * Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)
|
---|
| 5 | */
|
---|
| 6 | (function (global, factory) {
|
---|
| 7 | typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory(require('../dom/event-handler.js'), require('./config.js'), require('./index.js')) :
|
---|
| 8 | typeof define === 'function' && define.amd ? define(['../dom/event-handler', './config', './index'], factory) :
|
---|
| 9 | (global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.Swipe = factory(global.EventHandler, global.Config, global.Index));
|
---|
| 10 | })(this, (function (EventHandler, Config, index_js) { 'use strict';
|
---|
| 11 |
|
---|
| 12 | /**
|
---|
| 13 | * --------------------------------------------------------------------------
|
---|
| 14 | * Bootstrap util/swipe.js
|
---|
| 15 | * Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)
|
---|
| 16 | * --------------------------------------------------------------------------
|
---|
| 17 | */
|
---|
| 18 |
|
---|
| 19 |
|
---|
| 20 | /**
|
---|
| 21 | * Constants
|
---|
| 22 | */
|
---|
| 23 |
|
---|
| 24 | const NAME = 'swipe';
|
---|
| 25 | const EVENT_KEY = '.bs.swipe';
|
---|
| 26 | const EVENT_TOUCHSTART = `touchstart${EVENT_KEY}`;
|
---|
| 27 | const EVENT_TOUCHMOVE = `touchmove${EVENT_KEY}`;
|
---|
| 28 | const EVENT_TOUCHEND = `touchend${EVENT_KEY}`;
|
---|
| 29 | const EVENT_POINTERDOWN = `pointerdown${EVENT_KEY}`;
|
---|
| 30 | const EVENT_POINTERUP = `pointerup${EVENT_KEY}`;
|
---|
| 31 | const POINTER_TYPE_TOUCH = 'touch';
|
---|
| 32 | const POINTER_TYPE_PEN = 'pen';
|
---|
| 33 | const CLASS_NAME_POINTER_EVENT = 'pointer-event';
|
---|
| 34 | const SWIPE_THRESHOLD = 40;
|
---|
| 35 | const Default = {
|
---|
| 36 | endCallback: null,
|
---|
| 37 | leftCallback: null,
|
---|
| 38 | rightCallback: null
|
---|
| 39 | };
|
---|
| 40 | const DefaultType = {
|
---|
| 41 | endCallback: '(function|null)',
|
---|
| 42 | leftCallback: '(function|null)',
|
---|
| 43 | rightCallback: '(function|null)'
|
---|
| 44 | };
|
---|
| 45 |
|
---|
| 46 | /**
|
---|
| 47 | * Class definition
|
---|
| 48 | */
|
---|
| 49 |
|
---|
| 50 | class Swipe extends Config {
|
---|
| 51 | constructor(element, config) {
|
---|
| 52 | super();
|
---|
| 53 | this._element = element;
|
---|
| 54 | if (!element || !Swipe.isSupported()) {
|
---|
| 55 | return;
|
---|
| 56 | }
|
---|
| 57 | this._config = this._getConfig(config);
|
---|
| 58 | this._deltaX = 0;
|
---|
| 59 | this._supportPointerEvents = Boolean(window.PointerEvent);
|
---|
| 60 | this._initEvents();
|
---|
| 61 | }
|
---|
| 62 |
|
---|
| 63 | // Getters
|
---|
| 64 | static get Default() {
|
---|
| 65 | return Default;
|
---|
| 66 | }
|
---|
| 67 | static get DefaultType() {
|
---|
| 68 | return DefaultType;
|
---|
| 69 | }
|
---|
| 70 | static get NAME() {
|
---|
| 71 | return NAME;
|
---|
| 72 | }
|
---|
| 73 |
|
---|
| 74 | // Public
|
---|
| 75 | dispose() {
|
---|
| 76 | EventHandler.off(this._element, EVENT_KEY);
|
---|
| 77 | }
|
---|
| 78 |
|
---|
| 79 | // Private
|
---|
| 80 | _start(event) {
|
---|
| 81 | if (!this._supportPointerEvents) {
|
---|
| 82 | this._deltaX = event.touches[0].clientX;
|
---|
| 83 | return;
|
---|
| 84 | }
|
---|
| 85 | if (this._eventIsPointerPenTouch(event)) {
|
---|
| 86 | this._deltaX = event.clientX;
|
---|
| 87 | }
|
---|
| 88 | }
|
---|
| 89 | _end(event) {
|
---|
| 90 | if (this._eventIsPointerPenTouch(event)) {
|
---|
| 91 | this._deltaX = event.clientX - this._deltaX;
|
---|
| 92 | }
|
---|
| 93 | this._handleSwipe();
|
---|
| 94 | index_js.execute(this._config.endCallback);
|
---|
| 95 | }
|
---|
| 96 | _move(event) {
|
---|
| 97 | this._deltaX = event.touches && event.touches.length > 1 ? 0 : event.touches[0].clientX - this._deltaX;
|
---|
| 98 | }
|
---|
| 99 | _handleSwipe() {
|
---|
| 100 | const absDeltaX = Math.abs(this._deltaX);
|
---|
| 101 | if (absDeltaX <= SWIPE_THRESHOLD) {
|
---|
| 102 | return;
|
---|
| 103 | }
|
---|
| 104 | const direction = absDeltaX / this._deltaX;
|
---|
| 105 | this._deltaX = 0;
|
---|
| 106 | if (!direction) {
|
---|
| 107 | return;
|
---|
| 108 | }
|
---|
| 109 | index_js.execute(direction > 0 ? this._config.rightCallback : this._config.leftCallback);
|
---|
| 110 | }
|
---|
| 111 | _initEvents() {
|
---|
| 112 | if (this._supportPointerEvents) {
|
---|
| 113 | EventHandler.on(this._element, EVENT_POINTERDOWN, event => this._start(event));
|
---|
| 114 | EventHandler.on(this._element, EVENT_POINTERUP, event => this._end(event));
|
---|
| 115 | this._element.classList.add(CLASS_NAME_POINTER_EVENT);
|
---|
| 116 | } else {
|
---|
| 117 | EventHandler.on(this._element, EVENT_TOUCHSTART, event => this._start(event));
|
---|
| 118 | EventHandler.on(this._element, EVENT_TOUCHMOVE, event => this._move(event));
|
---|
| 119 | EventHandler.on(this._element, EVENT_TOUCHEND, event => this._end(event));
|
---|
| 120 | }
|
---|
| 121 | }
|
---|
| 122 | _eventIsPointerPenTouch(event) {
|
---|
| 123 | return this._supportPointerEvents && (event.pointerType === POINTER_TYPE_PEN || event.pointerType === POINTER_TYPE_TOUCH);
|
---|
| 124 | }
|
---|
| 125 |
|
---|
| 126 | // Static
|
---|
| 127 | static isSupported() {
|
---|
| 128 | return 'ontouchstart' in document.documentElement || navigator.maxTouchPoints > 0;
|
---|
| 129 | }
|
---|
| 130 | }
|
---|
| 131 |
|
---|
| 132 | return Swipe;
|
---|
| 133 |
|
---|
| 134 | }));
|
---|
| 135 | //# sourceMappingURL=swipe.js.map
|
---|