This repository was archived by the owner on May 4, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathforceify.umd.js
More file actions
634 lines (598 loc) · 16.9 KB
/
Copy pathforceify.umd.js
File metadata and controls
634 lines (598 loc) · 16.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
/* globals define, requestAnimationFrame, cancelAnimationFrame, performance */
(function (factory) {
if (typeof define === 'function' && define.amd) {
define([], factory)
} else if (typeof module !== 'undefined' && module.exports) {
module.exports = factory()
} else if (typeof exports !== 'undefined') {
exports.default = factory()
exports.__esModule = true
} else if (typeof window !== 'undefined' && window.document) {
window.Forceify = factory()
window.Forceify.__esModule = true
} else {
this.Forceify = factory()
this.Forceify.__esModule = true
}
})(function () {
const tasks = []
const id = {}
/* Small shim for lighter size */
const last = Date.now()
const reqAnimFrame =
typeof requestAnimationFrame !== 'undefined'
? requestAnimationFrame
: fn => setTimeout(() => fn(Date.now() - last), 50)
const cancelAnimFrame =
typeof cancelAnimationFrame !== 'undefined'
? cancelAnimationFrame
: fn => clearTimeout(fn)
if (typeof performance === 'object' && !performance.now) {
performance.now = () => Date.now() - last
}
const now = () => {
return typeof performance !== 'undefined' && !!performance.now
? performance.now()
: Date.now() - last
}
const root =
typeof global !== 'undefined'
? global
: typeof window !== 'undefined'
? window
: typeof exports !== 'undefined'
? exports
: this
reqAnimFrame(function update (time) {
reqAnimFrame(update)
let i = 0
while (i < tasks.length) {
const task = tasks[i]
if (task.update(time)) {
i++
} else {
tasks.splice(i, 1)
}
}
})
function _default (value, def) {
if (value === null || value === undefined) {
return def
}
return value
}
let _id = 0
class TimingUpdate {
constructor (queueID, elem, self) {
this._queueID = queueID
this._onUpdate = null
this._duration = 200
this._delayTime = 0
this._startTime = null
this._id = _id++
this.startValue = 0
this.currentValue = {
force: 0,
target: elem
}
this.__self = self
this.endValue = 1
this.callElem = elem
}
duration (amount) {
this._duration = amount
return this
}
start () {
const { _queueID, currentValue } = this
let queue = id[_queueID]
if (queue) {
const i = 0
let queueItem
while ((queueItem = queue.shift())) {
const idx = tasks.indexOf(queueItem)
if (idx > -1) {
tasks.splice(i, 1)
}
}
} else {
queue = id[_queueID] = []
}
this.startValue = currentValue.force
this.endValue = 1
queue.push(this)
tasks.push(this)
this._startTime = now() + this._delayTime
return this
}
delay (amount) {
this._delayTime = amount
return this
}
onUpdate (callback) {
this._onUpdate = callback
return this
}
restart (asReverse) {
const { _delayTime, currentValue } = this
const i = tasks.indexOf(this)
if (i === -1) {
this.start()
} else {
this._startTime = now() + _delayTime
}
if (asReverse) {
this.startValue = currentValue.force
this.endValue = 0
}
return this
}
update (time) {
const {
_startTime,
_duration,
_onUpdate,
currentValue,
startValue,
endValue,
callElem,
__self
} = this
if (time < _startTime) {
return true
}
let elapsed = (time - _startTime) / _duration
elapsed = elapsed > 1 ? 1 : elapsed
currentValue.force =
__self.__force !== undefined &&
__self.__force !== 0 &&
__self.__force !== 1
? __self.__force
: startValue + (endValue - startValue) * elapsed
if (_onUpdate) {
_onUpdate.call(callElem, currentValue)
}
if (elapsed === 1) {
return false
}
return true
}
}
const navig =
typeof navigator !== 'undefined'
? navigator
: {
userAgent: '',
maxTouchPoints: 0,
msMaxTouchPoints: 0
}
const ua = navig.userAgent
const _isIOSDevices =
ua.indexOf('; CPU') !== -1 && ua.indexOf(' like Mac') !== -1
const _document = !root.document ? {} : root.document
const _isTouchSimulate =
(_document.body && 'ontouchend' in _document.body) ||
root.DocumentTouch ||
navig.maxTouchPoints > 0 ||
navig.msMaxTouchPoints > 0
let _isReal3DTouch =
_document.body && 'ontouchforcechange' in _document.body && _isIOSDevices
function getTouch (e, targ, changed) {
const touches = changed ? e.touches : e.changedTouches
if (touches) {
let i = 0
const maxLen = touches.length
if (maxLen > 1) {
if (e.scale && e.scale !== 1) {
return null
}
}
if (e && e.preventDefault) {
e.preventDefault()
}
while (i < maxLen) {
if (!!touches[i] && touches[i].target === targ) {
return touches[i]
}
i++
}
}
return null
}
return class Forceify {
constructor (el, params = {}) {
let forceifyID = 0
if (!el.forceifyQueueId) {
forceifyID = el.forceifyQueueId = Math.floor(
Date.now() + Math.random() * 1000
)
} else {
forceifyID = el.forceifyQueueId
}
this.id = forceifyID
this._callback = null
this.el = el
this._pressDuration = _default(params.pressDuration, 200)
this._leaveDurationTolerance = _default(
params.leaveDurationTolerance,
0.35
)
this._leaveDuration = this._pressDuration * this._leaveDurationTolerance
this._delay = _default(params.delay, 0)
this._eventPress = null
this._eventLeave = null
this._eventUp = null
this._checkResult = null
this._useSameDurInLeave = _default(params.useSameDurInLeave, false)
this._resetOnLeave = _default(params.resetOnLeave, true)
this.el = el
this.polyfill = _default(params.polyfill, true)
this._touchTicks = 0
this.tick = null
return this
}
getEnv () {
return this._checkResult
}
resetOnLeave (state = true) {
this._resetOnLeave = state
return this
}
useLongPressEqualDuration (state = true) {
this._useSameDurInLeave = state
return this
}
setLongPressLeaveTolerance (amount) {
this._leaveDurationTolerance = amount
this._leaveDuration = this._pressDuration * amount
return this
}
setLongPressDelay (amount) {
this._delay = amount
return this
}
setLongPressDuration (amount) {
this._pressDuration = amount
this._leaveDuration = amount * this._leaveDurationTolerance
return this
}
onForce (fn) {
this._callback = fn
return this.init()
}
on (eventName, callbackListener, capture) {
if (root.addEventListener) {
this.el.addEventListener(eventName, callbackListener, capture)
}
return this
}
off (eventName, callbackListener, capture) {
if (root.removeEventListener) {
this.el.removeEventListener(eventName, callbackListener, capture)
}
return this
}
preventTouchCallout () {
const el = this.el
const touchCallout = [
'webkitTouchCallout',
'MozTouchCallout',
'msTouchCallout',
'touchCallout',
'webkitUserSelect',
'MozUserSelect',
'msUserSelect',
'userSelect',
'webkitUserDrag',
'MozUserDrag',
'msUserDrag',
'userDrag'
]
for (const property of touchCallout) {
if (property in el.style) {
el.style[property] = 'none'
}
}
const touchActions = [
'webkitTouchAction',
'mozTouchAction',
'msTouchAction',
'touchAction'
]
for (const property of touchActions) {
if (property in el.style) {
el.style[property] = 'manipulation'
}
}
return this
}
handleForceChange (e) {
if (e.preventDefault) {
e.preventDefault()
}
if (e.stopPropagation) {
e.stopPropagation()
}
this._iterateOfHandleForceChange++
let force =
e.webkitForce !== undefined
? e.webkitForce > 1
? (e.webkitForce - 1) / 2
: 0
: e.force !== undefined
? e.force
: undefined
if (force > 0.999) {
force = 1
} else if (force < 0.003) {
force = 0
}
if (force === undefined) {
const touches = getTouch(e, this.el, true)
if (!touches) {
force = 0
} else {
if (touches.force !== undefined) {
force = touches.force
} else if (touches.webkitForce) {
force = touches.force
}
}
}
const { type } = e
if (
force > 0 &&
force < 1 &&
!this.nativeAPIExists &&
(type === 'touchforcechange' || type === 'webkitmouseforcechanged')
) {
this.nativeAPIExists = true
}
e.force = force
this.__force = force
this._calledTimeout = true
this._callback.call(this, e); // eslint-disable-line
return false
}
init () {
const { el, polyfill } = this
const isPointerSupported = 'onpointerdown' in el
const __self$1 = this
this._iterateOfHandleForceChange = 0
let tickForce
let perfNow
let currentEvent
// Rebind to self
this.handleForceChange = this.handleForceChange.bind(this)
const renderUntilBecomeZero = time => {
const { __force } = this
const force =
(1 - Math.min((time - perfNow) / this._leaveDuration, 1)) * __force
if (force > 0) {
tickForce = reqAnimFrame(renderUntilBecomeZero)
} else if (force === 0) {
cancelAnimFrame(tickForce)
}
currentEvent.force = force
this.__force = force
this.handleForceChange(currentEvent)
}
this.preventTouchCallout()
if (!_isReal3DTouch && 'onwebkitmouseforcechanged' in el) {
this.on('webkitmouseforcechanged', this.handleForceChange)
this.on('mousedown', function checkForceTouchVerify () {
if (__self$1.nativeAPIExists) {
__self$1.off('mousedown', checkForceTouchVerify)
} else {
if (__self$1._touchTicks === 0 && polyfill) {
_isReal3DTouch = false
__self$1._eventPress = 'mousedown'
__self$1._eventLeave = 'mouseleave'
__self$1._eventUp = 'mouseup'
__self$1._checkResult = root.chrome
? 'macOS Chrome'
: 'macOS Safari'
__self$1.isPressed = true
__self$1.handleSimulate()
__self$1.handlePress()
}
__self$1.off('mousedown', checkForceTouchVerify)
}
})
this.on('mouseup', function onMousUp (e) {
if (__self$1.nativeAPIExists) {
__self$1.off('mouseup', onMousUp)
} else {
const { __force: force, _iterateOfHandleForceChange } = __self$1
currentEvent = e
if (force > 0 && _iterateOfHandleForceChange > 1) {
perfNow = performance.now()
tickForce = reqAnimFrame(renderUntilBecomeZero)
} else {
__self$1.off('mouseup', onMousUp)
}
}
})
this._checkResult = 'macOSForce'
return this
} else if (_isReal3DTouch) {
const verify3DTouch = () => {
if (this.nativeAPIExists) {
this.off('touchstart', verify3DTouch)
} else {
if (this._touchTicks === 0 && polyfill) {
_isReal3DTouch = false
this._eventPress = 'touchstart'
this._eventLeave = 'touchleave'
this._eventUp = 'touchend'
this._checkResult = root.chrome ? 'ChromeMobile' : 'Touch'
this.isPressed = true
this.handleSimulate()
this.handlePress()
}
this.off('touchstart', verify3DTouch)
}
}
const onTouchEnd = e => {
if (this.nativeAPIExists) {
this.off('touchend', onTouchEnd)
} else {
const { __force: force, _iterateOfHandleForceChange } = this
currentEvent = e
if (force > 0 && _iterateOfHandleForceChange > 1) {
perfNow = performance.now()
tickForce = reqAnimFrame(renderUntilBecomeZero)
} else {
this.off('touchend', onTouchEnd)
}
}
}
this.on('touchforcechange', this.handleForceChange)
this.on('touchstart', verify3DTouch)
this.on('touchend', onTouchEnd)
this._checkResult = 'iOSForce'
return this
} else if (polyfill && isPointerSupported) {
this._eventPress = 'pointerdown'
this._eventLeave = 'pointerleave'
this._eventUp = 'pointerup'
this._checkResult = root.chrome ? 'Chrome' : 'Modern'
} else if (polyfill && 'onmspointerdown' in el) {
this._eventPress = 'mspointerdown'
this._eventLeave = 'mspointerleave'
this._eventUp = 'mspointerup'
this._checkResult = root.chrome ? 'Chrome' : 'Modern'
} else if (polyfill && _isTouchSimulate) {
this._eventPress = 'touchstart'
this._eventLeave = 'touchleave'
this._eventUp = 'touchend'
this._checkResult = root.chrome ? 'ChromeMobile' : 'Touch'
} else if (polyfill && 'onmousedown' in el) {
this._eventPress = 'mousedown'
this._eventLeave = 'mouseup'
this._eventUp = 'mouseleave'
this._checkResult = root.chrome ? 'Chrome' : 'Desktop'
}
this.isPressed = false
return this.handleSimulate()
}
handleLeave () {
const {
_simulatedCallback,
_useSameDurInLeave,
_pressDuration,
_leaveDuration
} = this
if (_simulatedCallback) {
_simulatedCallback
.duration(_useSameDurInLeave ? _pressDuration : _leaveDuration)
.delay(0)
.restart(true)
}
return this
}
handlePress () {
const {
_simulatedCallback,
_pressDuration,
_delay,
nativeAPIExists
} = this
if (_simulatedCallback && !nativeAPIExists) {
_simulatedCallback
.duration(_pressDuration)
.delay(_delay)
.start()
}
return this
}
handleSimulate () {
let {
_simulatedCallback,
_eventPress,
_eventUp,
_eventLeave,
isPressed,
_callback,
id: forceifyID,
el
} = this
if (!_simulatedCallback) {
_simulatedCallback = this._simulatedCallback = new TimingUpdate(
forceifyID,
el,
this
)
}
if (_simulatedCallback) {
_simulatedCallback.onUpdate(_callback)
}
const pressListener = e => {
if (this.nativeAPIExists) {
this.off(_eventPress, pressListener)
return
}
if (!isPressed && !_isReal3DTouch) {
if (e.type === _eventPress) {
if (e.preventDefault) {
e.preventDefault()
}
if (e.stopPropagation) {
e.stopPropagation()
}
this.handlePress()
isPressed = this.isPressed = true
}
}
return false
}
// LONG PRESS
this.on(_eventPress, pressListener)
// LEAVE
const leaveListener = e => {
if (this.nativeAPIExists) {
this.off(_eventUp, leaveListener)
this.off(_eventLeave, leaveListener)
return
}
if (isPressed) {
if (
e.type === _eventUp ||
(e.type === _eventLeave && this._resetOnLeave)
) {
if (e.preventDefault) {
e.preventDefault()
}
if (e.stopPropagation) {
e.stopPropagation()
}
this.handleLeave()
isPressed = this.isPressed = false
}
}
return false
}
this.on('contextmenu', e => e.preventDefault())
this.on(_eventUp, leaveListener)
this.on(_eventLeave, leaveListener)
return this
}
isChrome () {
return (
this._checkResult === 'Chrome' || this._checkResult === 'ChromeMobile'
)
}
isMacOSForceTouch () {
return this._checkResult === 'macOSForce'
}
isIOS3DTouch () {
return this._checkResult === 'iOSForce'
}
isTouch () {
return this._checkResult === 'Touch'
}
isMouse () {
return this._checkResult === 'Desktop'
}
}
})