• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

twbs / bootstrap / 9856469587

09 Jul 2024 11:49AM CUT coverage: 96.07%. Remained the same
9856469587

Pull #40619

github

web-flow
Merge ac79a1794 into 7c392498f
Pull Request #40619: Docs: Fix a minor accessibility issue (checkout example missing h1)

666 of 726 branches covered (91.74%)

Branch coverage included in aggregate %.

2023 of 2073 relevant lines covered (97.59%)

249.89 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

95.89
/js/src/util/swipe.js
1
/**
2
 * --------------------------------------------------------------------------
3
 * Bootstrap util/swipe.js
4
 * Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)
5
 * --------------------------------------------------------------------------
6
 */
7

8
import EventHandler from '../dom/event-handler.js'
9
import Config from './config.js'
10
import { execute } from './index.js'
11

12
/**
13
 * Constants
14
 */
15

16
const NAME = 'swipe'
2✔
17
const EVENT_KEY = '.bs.swipe'
2✔
18
const EVENT_TOUCHSTART = `touchstart${EVENT_KEY}`
2✔
19
const EVENT_TOUCHMOVE = `touchmove${EVENT_KEY}`
2✔
20
const EVENT_TOUCHEND = `touchend${EVENT_KEY}`
2✔
21
const EVENT_POINTERDOWN = `pointerdown${EVENT_KEY}`
2✔
22
const EVENT_POINTERUP = `pointerup${EVENT_KEY}`
2✔
23
const POINTER_TYPE_TOUCH = 'touch'
2✔
24
const POINTER_TYPE_PEN = 'pen'
2✔
25
const CLASS_NAME_POINTER_EVENT = 'pointer-event'
2✔
26
const SWIPE_THRESHOLD = 40
2✔
27

28
const Default = {
2✔
29
  endCallback: null,
30
  leftCallback: null,
31
  rightCallback: null
32
}
33

34
const DefaultType = {
2✔
35
  endCallback: '(function|null)',
36
  leftCallback: '(function|null)',
37
  rightCallback: '(function|null)'
38
}
39

40
/**
41
 * Class definition
42
 */
43

44
class Swipe extends Config {
45
  constructor(element, config) {
46
    super()
29✔
47
    this._element = element
29✔
48

49
    if (!element || !Swipe.isSupported()) {
29✔
50
      return
2✔
51
    }
52

53
    this._config = this._getConfig(config)
27✔
54
    this._deltaX = 0
27✔
55
    this._supportPointerEvents = Boolean(window.PointerEvent)
27✔
56
    this._initEvents()
27✔
57
  }
58

59
  // Getters
60
  static get Default() {
61
    return Default
27✔
62
  }
63

64
  static get DefaultType() {
65
    return DefaultType
27✔
66
  }
67

68
  static get NAME() {
69
    return NAME
×
70
  }
71

72
  // Public
73
  dispose() {
74
    EventHandler.off(this._element, EVENT_KEY)
3✔
75
  }
76

77
  // Private
78
  _start(event) {
79
    if (!this._supportPointerEvents) {
14✔
80
      this._deltaX = event.touches[0].clientX
10✔
81

82
      return
10✔
83
    }
84

85
    if (this._eventIsPointerPenTouch(event)) {
4✔
86
      this._deltaX = event.clientX
4✔
87
    }
88
  }
89

90
  _end(event) {
91
    if (this._eventIsPointerPenTouch(event)) {
14✔
92
      this._deltaX = event.clientX - this._deltaX
4✔
93
    }
94

95
    this._handleSwipe()
14✔
96
    execute(this._config.endCallback)
14✔
97
  }
98

99
  _move(event) {
100
    this._deltaX = event.touches && event.touches.length > 1 ?
230✔
101
      0 :
230✔
102
      event.touches[0].clientX - this._deltaX
103
  }
104

105
  _handleSwipe() {
106
    const absDeltaX = Math.abs(this._deltaX)
14✔
107

108
    if (absDeltaX <= SWIPE_THRESHOLD) {
14✔
109
      return
4✔
110
    }
111

112
    const direction = absDeltaX / this._deltaX
10✔
113

114
    this._deltaX = 0
10✔
115

116
    if (!direction) {
10!
117
      return
×
118
    }
119

120
    execute(direction > 0 ? this._config.rightCallback : this._config.leftCallback)
10✔
121
  }
122

123
  _initEvents() {
124
    if (this._supportPointerEvents) {
27✔
125
      EventHandler.on(this._element, EVENT_POINTERDOWN, event => this._start(event))
20✔
126
      EventHandler.on(this._element, EVENT_POINTERUP, event => this._end(event))
20✔
127

128
      this._element.classList.add(CLASS_NAME_POINTER_EVENT)
20✔
129
    } else {
130
      EventHandler.on(this._element, EVENT_TOUCHSTART, event => this._start(event))
10✔
131
      EventHandler.on(this._element, EVENT_TOUCHMOVE, event => this._move(event))
230✔
132
      EventHandler.on(this._element, EVENT_TOUCHEND, event => this._end(event))
10✔
133
    }
134
  }
135

136
  _eventIsPointerPenTouch(event) {
137
    return this._supportPointerEvents && (event.pointerType === POINTER_TYPE_PEN || event.pointerType === POINTER_TYPE_TOUCH)
18✔
138
  }
139

140
  // Static
141
  static isSupported() {
142
    return 'ontouchstart' in document.documentElement || navigator.maxTouchPoints > 0
89✔
143
  }
144
}
145

146
export default Swipe
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc