forked from IjzerenHein/famous-refresh-loader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRefreshLoaderStandalone.js
352 lines (326 loc) · 12.1 KB
/
RefreshLoaderStandalone.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
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
/**
* This Source Code is licensed under the MIT license. If a copy of the
* MIT-license was not distributed with this file, You can obtain one at:
* http://opensource.org/licenses/mit-license.html.
*
* @author: Hein Rutjes (IjzerenHein)
* @license MIT
* @copyright Gloey Apps, 2014
*/
/**
* @module
*/
define(function(require, exports, module) {
'use strict';
// import dependencies
var Entity = require('famous/core/Entity');
var Surface = require('famous/core/Surface');
var Transform = require('famous/core/Transform');
var Modifier = require('famous/core/Modifier');
var View = require('famous/core/View');
//
// Pull to refresh states
//
var PullToRefreshState = {
HIDDEN: 0,
PULLING: 1,
ACTIVE: 2,
COMPLETED: 3,
HIDDING: 4
};
/**
* @class
* @extends View
* @param {Object} [options] Configuration options
*/
function RefreshLoader(options) {
View.apply(this, arguments);
this._rotateOffset = 0;
this._scale = 1;
this.id = Entity.register(this); // register entity-id to capture size prior to rendering
if (this.options.pullToRefresh && this.options.pullToRefreshBackgroundColor) {
_createForeground.call(this, _translateBehind.call(this));
}
_createParticles.call(this, _translateBehind.call(this), this.options.particleCount);
if (!this._pullToRefresh) {
this._pullToRefresh = {
state: PullToRefreshState.HIDDEN,
prevState: PullToRefreshState.HIDDEN,
};
}
this._eventOutput.on('click', function (event) {
console.log(event);
});
}
RefreshLoader.prototype = Object.create(View.prototype);
RefreshLoader.prototype.constructor = RefreshLoader;
RefreshLoader.PullToRefreshState = PullToRefreshState;
// default options
RefreshLoader.DEFAULT_OPTIONS = {
direction: 1, // 0 = horizontal, 1 = vertical
color: '#AAAAAA',
particleCount: 10,
particleSize: 6,
rotateVelocity: 0.09,
hideVelocity: 0.05,
quickHideVelocity: 0.2,
pullToRefresh: false,
pullToRefreshBackgroundColor: 'white',
pullToRefreshDirection: 1,
pullToRefreshFooter: false,
pullToRefreshFactor: 1.5 // pull 1.5x the size to activate refresh
};
/**
* Helper function for giving all surfaces the correct z-index.
*/
function _translateBehind() {
if (this._zNode) {
this._zNode = this._zNode.add(new Modifier({
transform: Transform.behind
}));
}
else {
this._zNode = this.add(new Modifier({
transform: Transform.behind
}));
}
return this._zNode;
}
/**
* Creates the particles
*/
function _createParticles(node, count) {
this._particles = [];
var options = {
size: [this.options.particleSize, this.options.particleSize],
properties: {
backgroundColor: this.options.color,
borderRadius: '50%'
}
};
for (var i = 0; i < count; i++) {
var particle = {
surface: new Surface(options),
mod: new Modifier({})
};
this._particles.push(particle);
node.add(particle.mod).add(particle.surface);
}
}
/**
* Creates the foreground behind which the particles can hide in case of pull to refresh.
*/
function _createForeground(node) {
this._foreground = {
surface: new Surface({
size: this.options.size,
properties: {
backgroundColor: this.options.pullToRefreshBackgroundColor
}
}),
mod: new Modifier({})
};
node.add(this._foreground.mod).add(this._foreground.surface);
}
/**
* Positions/rotates partciles.
*/
var devicePixelRatio = window.devicePixelRatio || 1;
function _positionParticles(renderSize) {
var shapeSize = this.options.size[this.options.pullToRefreshDirection] / 2;
var visiblePerc = Math.min(Math.max(renderSize[this.options.pullToRefreshDirection] / (this.options.size[this.options.pullToRefreshDirection] * 2), 0), 1);
switch (this._pullToRefreshStatus) {
case 0:
case 1:
this._rotateOffset = 0;
this._scale = 1;
break;
case 2:
visiblePerc = 1;
this._rotateOffset += this.options.rotateVelocity;
break;
case 3:
visiblePerc = 1;
this._rotateOffset += this.options.rotateVelocity;
this._scale -= this.options.hideVelocity;
this._scale = Math.max(0, this._scale);
break;
case 4:
visiblePerc = 1;
this._rotateOffset += this.options.rotateVelocity;
this._scale -= this.options.quickHideVelocity;
this._scale = Math.max(0, this._scale);
break;
}
//console.log('visiblePerc: ' + visiblePerc + ', renderSize: ' + JSON.stringify(renderSize));
var rTotal = visiblePerc * Math.PI * 2;
for (var i = 0, cnt = this._particles.length; i < cnt; i++) {
var mod = this._particles[i].mod;
var r = (((i / cnt) * rTotal) - (Math.PI / 2)) + this._rotateOffset + (this.options.pullToRefreshFooter ? Math.PI : 0);
var x = Math.cos(r) * (shapeSize / 2) * this._scale;
var y = Math.sin(r) * (shapeSize / 2) * this._scale;
if (this.options.pullToRefreshDirection) {
x += (renderSize[0] / 2);
y += shapeSize;
y = Math.round(y * devicePixelRatio) / devicePixelRatio;
}
else {
x += shapeSize;
y += (renderSize[1] / 2);
x = Math.round(x * devicePixelRatio) / devicePixelRatio;
}
mod.transformFrom(Transform.translate(x, y, 0));
mod.opacityFrom(this._scale);
}
}
/**
* Positions the foreground in front of the particles.
*/
function _positionForeground(renderSize) {
if (this._pullToRefreshDirection) {
this._foreground.mod.transformFrom(Transform.translate(0, renderSize[1], 0));
}
else {
this._foreground.mod.transformFrom(Transform.translate(renderSize[0], 0, 0));
}
}
/**
* Helper function for setting the pull-to-refresh status.
*/
function _setPullToRefreshState(state) {
if (this._pullToRefresh.state !== state) {
this._pullToRefresh.state = state;
this.setPullToRefreshStatus(state);
}
}
/**
* Ensure that our commit is called passing along the size.
* @private
*/
RefreshLoader.prototype.render = function render() {
return [this.id, this._node.render()];
};
/**
* Position renderables based on size
* @private
*/
RefreshLoader.prototype.commit = function commit(context) {
_positionParticles.call(this, context.size);
if (this._foreground) {
_positionForeground.call(this, context.size);
}
if ((this._pullToRefresh.state === PullToRefreshState.ACTIVE) &&
(this._pullToRefresh.prevState !== PullToRefreshState.ACTIVE)) {
this._eventOutput.emit('refresh', {
target: this
});
}
this._pullToRefresh.prevState = this._pullToRefresh.state;
return {};
};
RefreshLoader.prototype.pull = function pull(pullOffset) {
// Calculate offset
var length = this.getSize()[this.options.direction];
var pullLength = this.getPullToRefreshSize ? this.getPullToRefreshSize()[this.options.direction] : length;
var prevHeight = (length === undefined) ? -1 : length;
var offset = (prevHeight >= 0) ? (pullOffset - prevHeight) : prevHeight;
// Determine current state
var visiblePerc = Math.max(Math.min(offset / pullLength, 1), 0);
switch (this._pullToRefresh.state) {
case PullToRefreshState.HIDDEN:
if (visiblePerc >= 1) {
_setPullToRefreshState.call(this, PullToRefreshState.ACTIVE);
}
else if (offset >= 0.2) {
_setPullToRefreshState.call(this, PullToRefreshState.PULLING);
}
break;
case PullToRefreshState.PULLING:
if (visiblePerc >= 1) {
_setPullToRefreshState.call(this, PullToRefreshState.ACTIVE);
}
else if (offset < 0.2) {
_setPullToRefreshState.call(this, PullToRefreshState.HIDDEN);
}
break;
case PullToRefreshState.ACTIVE:
// nothing to do, wait for completed
break;
case PullToRefreshState.COMPLETED:
if (offset >= 0.2) {
_setPullToRefreshState.call(this, PullToRefreshState.HIDDING);
}
else {
_setPullToRefreshState.call(this, PullToRefreshState.HIDDEN);
}
break;
case PullToRefreshState.HIDDING:
if (offset < 0.2) {
_setPullToRefreshState.call(this, PullToRefreshState.HIDDEN);
}
break;
}
// Show pull to refresh node
if (this._pullToRefresh.state !== PullToRefreshState.HIDDEN) {
var size = this.getSize();
size[this.options.direction] = Math.max(Math.min(offset, pullLength), 0);
this.setSize(size);
}
}
/**
* Called by the flex ScrollView whenever the pull-to-refresh renderable is shown
* or the state has changed.
*
* @param {Number} status Status, 0: hidden, 1: pulling, 2: active, 3: completed, 4: hidding
*/
RefreshLoader.prototype.setPullToRefreshStatus = function(status) {
this._pullToRefreshStatus = status;
};
/**
* Called by the flex ScrollView to get the size on how far to pull before the
* refresh is activated.
*
* @return {Size} Pull to refresh size
*/
RefreshLoader.prototype.getPullToRefreshSize = function() {
if (this.options.pullToRefreshDirection) {
return [this.options.size[0], this.options.size[1] * this.options.pullToRefreshFactor];
}
else {
return [this.options.size[1] * this.options.pullToRefreshFactor, this.options.size[1]];
}
};
/**
* Shows the pulls-to-refresh renderable indicating that a refresh is in progress.
*
* @param {Bool} [footer] set to true to show pull-to-refresh at the footer (default: false).
* @return {RefreshLoader} this
*/
RefreshLoader.prototype.showPullToRefresh = function(footer) {
if (this._pullToRefresh) {
_setPullToRefreshState.call(this, PullToRefreshState.ACTIVE);
}
return this;
};
/**
* Hides the pull-to-refresh renderable in case it was visible.
*
* @param {Bool} [footer] set to true to hide the pull-to-refresh at the footer (default: false).
* @return {RefreshLoader} this
*/
RefreshLoader.prototype.hidePullToRefresh = function(footer) {
if (this._pullToRefresh && (this._pullToRefresh.state === PullToRefreshState.ACTIVE)) {
_setPullToRefreshState.call(this, PullToRefreshState.COMPLETED);
}
return this;
};
/**
* Get the visible state of the pull-to-refresh renderable.
*
* @param {Bool} [footer] set to true to get the state of the pull-to-refresh footer (default: false).
*/
RefreshLoader.prototype.isPullToRefreshVisible = function(footer) {
return this._pullToRefresh ? (this._pullToRefresh.state === PullToRefreshState.ACTIVE) : false;
};
module.exports = RefreshLoader;
});