forked from ampproject/amphtml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpull-to-refresh.js
140 lines (122 loc) · 3.44 KB
/
pull-to-refresh.js
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
import {Services} from '#service';
/**
* Installs "pull-to-refresh" (P2R) blocker if viewer has requested. P2R can
* be very disruptive for different viewer scenarios. This is currently only
* done on Chrome (both Android and iOS).
* @param {!Window} win
*/
export function installPullToRefreshBlocker(win) {
// Only do when requested and don't even try it on Safari!
// This mode is only executed in the single-doc mode.
const {documentElement} = win.document;
if (
Services.viewerForDoc(documentElement).getParam('p2r') == '0' &&
Services.platformFor(win).isChrome()
) {
new PullToRefreshBlocker(
win.document,
Services.viewportForDoc(documentElement)
);
}
}
/**
* Visible for testing only.
* @private
*/
export class PullToRefreshBlocker {
/**
* @param {!Document} doc
* @param {!./service/viewport/viewport-interface.ViewportInterface} viewport
*/
constructor(doc, viewport) {
/** @private {!Document} */
this.doc_ = doc;
/** @private @const */
this.viewport_ = viewport;
/** @private {boolean} */
this.tracking_ = false;
/** @private {number} */
this.startPos_ = 0;
/** @private {!Function} */
this.boundTouchStart_ = this.onTouchStart_.bind(this);
/** @private {!Function} */
this.boundTouchMove_ = this.onTouchMove_.bind(this);
/** @private {!Function} */
this.boundTouchEnd_ = this.onTouchEnd_.bind(this);
/** @private {!Function} */
this.boundTouchCancel_ = this.onTouchCancel_.bind(this);
this.doc_.addEventListener('touchstart', this.boundTouchStart_, true);
}
/** */
cleanup() {
this.stopTracking_();
this.doc_.removeEventListener('touchstart', this.boundTouchStart_, true);
}
/**
* @param {!Event} event
* @private
*/
onTouchStart_(event) {
// P2R won't trigger when document is scrolled. Also can ignore when we are
// already tracking this touch and for non-single-touch events.
if (
this.tracking_ ||
!(event.touches && event.touches.length == 1) ||
this.viewport_.getScrollTop() > 0
) {
return;
}
this.startTracking_(event.touches[0].clientY);
}
/**
* @param {number} startPos
* @private
*/
startTracking_(startPos) {
this.tracking_ = true;
this.startPos_ = startPos;
this.doc_.addEventListener('touchmove', this.boundTouchMove_, true);
this.doc_.addEventListener('touchend', this.boundTouchEnd_, true);
this.doc_.addEventListener('touchcancel', this.boundTouchCancel_, true);
}
/** @private */
stopTracking_() {
this.tracking_ = false;
this.startPos_ = 0;
this.doc_.removeEventListener('touchmove', this.boundTouchMove_, true);
this.doc_.removeEventListener('touchend', this.boundTouchEnd_, true);
this.doc_.removeEventListener('touchcancel', this.boundTouchCancel_, true);
}
/**
* @param {!Event} event
* @private
*/
onTouchMove_(event) {
if (!this.tracking_) {
return;
}
const dy = event.touches[0].clientY - this.startPos_;
// Immediately cancel the P2R if dragging down.
if (dy > 0) {
event.preventDefault();
}
// Stop tracking if there was any motion at all.
if (dy != 0) {
this.stopTracking_();
}
}
/**
* @param {!Event} unusedEvent
* @private
*/
onTouchEnd_(unusedEvent) {
this.stopTracking_();
}
/**
* @param {!Event} unusedEvent
* @private
*/
onTouchCancel_(unusedEvent) {
this.stopTracking_();
}
}