internet-programming/lab2/node_modules/bootstrap/js/dist/util/swipe.js

136 lines
4.3 KiB
JavaScript
Raw Normal View History

2023-10-23 13:48:44 +04:00
/*!
2023-11-02 21:33:55 +04:00
* Bootstrap swipe.js v5.3.2 (https://getbootstrap.com/)
* Copyright 2011-2023 The Bootstrap Authors (https://github.com/twbs/bootstrap/graphs/contributors)
2023-10-23 13:48:44 +04:00
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)
*/
(function (global, factory) {
2023-11-02 21:33:55 +04:00
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory(require('../dom/event-handler.js'), require('./config.js'), require('./index.js')) :
typeof define === 'function' && define.amd ? define(['../dom/event-handler', './config', './index'], factory) :
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.Swipe = factory(global.EventHandler, global.Config, global.Index));
})(this, (function (EventHandler, Config, index_js) { 'use strict';
2023-10-23 13:48:44 +04:00
/**
* --------------------------------------------------------------------------
2023-11-02 21:33:55 +04:00
* Bootstrap util/swipe.js
2023-10-23 13:48:44 +04:00
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)
* --------------------------------------------------------------------------
*/
2023-11-02 21:33:55 +04:00
2023-10-23 13:48:44 +04:00
/**
* Constants
*/
const NAME = 'swipe';
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 = {
endCallback: null,
leftCallback: null,
rightCallback: null
};
const DefaultType = {
endCallback: '(function|null)',
leftCallback: '(function|null)',
rightCallback: '(function|null)'
};
2023-11-02 21:33:55 +04:00
2023-10-23 13:48:44 +04:00
/**
* Class definition
*/
2023-11-02 21:33:55 +04:00
class Swipe extends Config {
2023-10-23 13:48:44 +04:00
constructor(element, config) {
super();
this._element = element;
if (!element || !Swipe.isSupported()) {
return;
}
this._config = this._getConfig(config);
this._deltaX = 0;
this._supportPointerEvents = Boolean(window.PointerEvent);
this._initEvents();
2023-11-02 21:33:55 +04:00
}
2023-10-23 13:48:44 +04:00
2023-11-02 21:33:55 +04:00
// Getters
2023-10-23 13:48:44 +04:00
static get Default() {
return Default;
}
static get DefaultType() {
return DefaultType;
}
static get NAME() {
return NAME;
2023-11-02 21:33:55 +04:00
}
2023-10-23 13:48:44 +04:00
2023-11-02 21:33:55 +04:00
// Public
2023-10-23 13:48:44 +04:00
dispose() {
2023-11-02 21:33:55 +04:00
EventHandler.off(this._element, EVENT_KEY);
}
2023-10-23 13:48:44 +04:00
2023-11-02 21:33:55 +04:00
// Private
2023-10-23 13:48:44 +04:00
_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();
2023-11-02 21:33:55 +04:00
index_js.execute(this._config.endCallback);
2023-10-23 13:48:44 +04:00
}
_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;
}
2023-11-02 21:33:55 +04:00
index_js.execute(direction > 0 ? this._config.rightCallback : this._config.leftCallback);
2023-10-23 13:48:44 +04:00
}
_initEvents() {
if (this._supportPointerEvents) {
2023-11-02 21:33:55 +04:00
EventHandler.on(this._element, EVENT_POINTERDOWN, event => this._start(event));
EventHandler.on(this._element, EVENT_POINTERUP, event => this._end(event));
2023-10-23 13:48:44 +04:00
this._element.classList.add(CLASS_NAME_POINTER_EVENT);
} else {
2023-11-02 21:33:55 +04:00
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));
2023-10-23 13:48:44 +04:00
}
}
_eventIsPointerPenTouch(event) {
return this._supportPointerEvents && (event.pointerType === POINTER_TYPE_PEN || event.pointerType === POINTER_TYPE_TOUCH);
2023-11-02 21:33:55 +04:00
}
2023-10-23 13:48:44 +04:00
2023-11-02 21:33:55 +04:00
// Static
2023-10-23 13:48:44 +04:00
static isSupported() {
return 'ontouchstart' in document.documentElement || navigator.maxTouchPoints > 0;
}
}
return Swipe;
}));
//# sourceMappingURL=swipe.js.map