-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathpull-to-reload.js
217 lines (169 loc) · 5.13 KB
/
pull-to-reload.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
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
/**
*
* pull-to-reload
*
* A pull-to-reload system that is compatible with both both web and mobile.
* Configurable with loads of options.
*
* @author Erlend Ellingsen <[email protected]>
* @copyright MIT, Erlend Ellingsen
* @version 1.1.5 08.03.2017
*/
var PullToReload = function (optsUser) {
var self = this;
// --- OPTIONS ---
this.opts = {
'refresh-element': 'ptr', // Required
'content-element': 'content', // Required
'border-height': 1,
height: 80,
'font-size': '30px',
threshold: 20,
'pre-content': '...',
'loading-content': 'Loading...',
'callback-loading': function () {
setTimeout(function () {
self.loadingEnd();
}, 1000);
} // Required
};
// Overwrite options with user-options if present
for (var prop in self.opts) {
if (optsUser[prop] !== undefined) {
self.opts[prop] = optsUser[prop];
}
}
// --- INIT CODE ---
this.ptr = document.querySelector('#' + self.opts['refresh-element']);
this.content = document.querySelector('#' + self.opts['content-element']);
// --- STYLING ---
// Set style
this.ptr.style.padding = '0px';
this.ptr.style.margin = '0px';
this.ptr.style.display = 'block';
this.ptr.style.height = self.opts.height + 'px';
this.ptr.style.border = self.opts['border-height'] + 'px solid #000';
this.ptr.style.borderTop = '0px';
this.ptr.style.borderLeft = '0px';
this.ptr.style.borderRight = '0px';
this.ptr.style.textAlign = 'center';
this.ptr.style.lineHeight = self.opts.height + 'px';
this.ptr.style.fontSize = self.opts['font-size'];
// Hide the margin
this.ptr.style.marginTop = '-' + (self.opts['border-height'] + self.opts.height) + 'px';
// --- CODE ---
// --- CODE: HANDLING ---
this.loadingStart = function () {
this.ptr.innerHTML = self.opts['loading-content'];
self.opts['callback-loading'](); // Call callback
// end loadingStart
};
this.loadingEnd = function () {
this.ptr.innerHTML = self.opts['pre-content'];
this.ptr.style.marginTop = '-' + (self.opts['border-height'] + self.opts.height + 'px');
// end loadingEnd
};
// --- CODE: COMMON FUNCS ---
this.getPageY = function (event) {
if (event.pageY === undefined && event.touches !== undefined) {
if (event.touches.length <= 0) {
return false;
}
event.pageY = event.touches[event.touches.length - 1].pageY;
}
return event.pageY;
// end getPageY
};
// --- CODE: EVENTS ---
// EVENT: MOUSEUP
this.isDragging = false;
this.isThresholdReached = false;
this.posStart = 0;
self.content.addEventListener('touchstart', function (event) {
self.mouseStart(event);
});
self.content.addEventListener('mousedown', function (event) {
self.mouseStart(event);
});
this.mouseStart = function (event) {
//Prevent PTR if position is beyond content start.
if (document.body.scrollTop >= self.content.getBoundingClientRect().top) {
return;
}
self.isDragging = true;
self.isThresholdReached = false;
self.posStart = self.getPageY(event);
// end mousedown touchstart
};
// EVENT: MOUSEUP
document.addEventListener('touchmove', function (event) {
self.mouseMove(event);
});
document.addEventListener('mousemove', function (event) {
self.mouseMove(event);
});
this.mouseMove = function (event) {
//Prevent PTR if position is beyond content start.
if (document.body.scrollTop >= self.content.getBoundingClientRect().top) {
return;
}
if (!self.isDragging) {
return;
}
// Calculate the drag distance
// Android / Chrome compability. Sometimes consists of a list of touches.
event.pageY = self.getPageY(event);
if (event.pageY === false) {
return;
}
var dragDistance = (event.pageY - self.posStart);
if (dragDistance <= 0) {
return;
} // Do not inverse the drag..
//Prevent defaults after dragDistance-check. (We still want to keep site scrolling functionality.)
event.preventDefault();
event.stopImmediatePropagation();
var newMargin = (self.opts['border-height'] + (self.opts.height - dragDistance));
if (newMargin <= 0) {
return;
}
// Update
if (newMargin <= self.opts.threshold) {
self.isThresholdReached = true;
}
self.ptr.style.marginTop = '-' + (newMargin + 'px');
// end mousemove touchmove
};
// EVENT: MOUSEUP
document.addEventListener('touchend', function (event) {
self.mouseEnd(event);
});
document.addEventListener('mouseup', function (event) {
self.mouseEnd(event);
});
this.mouseEnd = function (event) {
//Prevent PTR if position is beyond content start.
if (document.body.scrollTop >= self.content.getBoundingClientRect().top) {
return;
}
if (!self.isDragging) {
return;
}
event.preventDefault();
event.stopImmediatePropagation();
if (self.isThresholdReached) {
// Set margin to show entire
self.ptr.style.marginTop = '0px';
self.isDragging = false;
self.isThresholdReached = false;
self.loadingStart();
return;
}
// Reset margin
self.ptr.style.marginTop = '-' + (self.opts['border-height'] + self.opts.height + 'px');
self.isDragging = false;
self.isThresholdReached = false;
// end mouseup touchend
};
// end PullToReload
};