-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathScroller.js
308 lines (271 loc) · 11.7 KB
/
Scroller.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
define(function(require, exports, module) {
var Entity = require('famous/core/Entity');
var Group = require('famous/core/Group');
var OptionsManager = require('famous/core/OptionsManager');
var Transform = require('famous/core/Transform');
var Utility = require('famous/utilities/Utility');
var ViewSequence = require('famous/core/ViewSequence');
var EventHandler = require('famous/core/EventHandler');
/**
* Scroller lays out a collection of renderables, and will browse through them based on
* accessed position. Scroller also broadcasts an 'edgeHit' event, with a position property of the location of the edge,
* when you've hit the 'edges' of it's renderable collection.
* @class Scroller
* @constructor
* @event error
* @param {Options} [options] An object of configurable options.
* @param {Number} [options.direction=Utility.Direction.Y] Using the direction helper found in the famous Utility
* module, this option will lay out the Scroller instance's renderables either horizontally
* (x) or vertically (y). Utility's direction is essentially either zero (X) or one (Y), so feel free
* to just use integers as well.
* @param {Number} [clipSize=undefined] The size of the area (in pixels) that Scroller will display content in.
* @param {Number} [margin=undefined] The size of the area (in pixels) that Scroller will process renderables' associated calculations in.
*/
function Scroller(options) {
this.options = Object.create(this.constructor.DEFAULT_OPTIONS);
this._optionsManager = new OptionsManager(this.options);
if (options) this._optionsManager.setOptions(options);
this._node = null;
this._position = 0;
// used for shifting nodes
this._positionOffset = 0;
this._positionGetter = null;
this._outputFunction = null;
this._masterOutputFunction = null;
this.outputFrom();
this._onEdge = 0; // -1 for top, 1 for bottom
this.group = new Group();
this.group.add({render: _innerRender.bind(this)});
this._entityId = Entity.register(this);
this._size = [undefined, undefined];
this._contextSize = [undefined, undefined];
this._eventInput = new EventHandler();
this._eventOutput = new EventHandler();
EventHandler.setInputHandler(this, this._eventInput);
EventHandler.setOutputHandler(this, this._eventOutput);
}
Scroller.DEFAULT_OPTIONS = {
direction: Utility.Direction.Y,
margin: 0,
clipSize: undefined,
groupScroll: false
};
function _sizeForDir(size) {
if (!size) size = this._contextSize;
var dimension = (this.options.direction === Utility.Direction.X) ? 0 : 1;
return (size[dimension] === undefined) ? this._contextSize[dimension] : size[dimension];
}
function _output(node, offset, target) {
var size = node.getSize ? node.getSize() : this._contextSize;
var transform = this._outputFunction(offset);
target.push({transform: transform, target: node.render()});
return _sizeForDir.call(this, size);
}
function _getClipSize() {
if (this.options.clipSize) return this.options.clipSize;
else return _sizeForDir.call(this, this._contextSize);
}
/**
* Patches the Scroller instance's options with the passed-in ones.
* @method setOptions
* @param {Options} options An object of configurable options for the Scroller instance.
*/
Scroller.prototype.setOptions = function setOptions(options) {
this._optionsManager.setOptions(options);
if (this.options.groupScroll) {
this.group.pipe(this._eventOutput);
}
else {
this.group.unpipe(this._eventOutput);
}
};
/**
* Tells you if the Scroller instance is on an edge.
* @method onEdge
* @return {Boolean} Whether the Scroller instance is on an edge or not.
*/
Scroller.prototype.onEdge = function onEdge() {
return this._onEdge;
};
/**
* Allows you to overwrite the way Scroller lays out it's renderables. Scroller will
* pass an offset into the function. By default the Scroller instance just translates each node
* in it's direction by the passed-in offset.
* Scroller will translate each renderable down
* @method outputFrom
* @param {Function} fn A function that takes an offset and returns a transform.
* @param {Function} [masterFn]
*/
Scroller.prototype.outputFrom = function outputFrom(fn, masterFn) {
if (!fn) {
fn = function(offset) {
return (this.options.direction === Utility.Direction.X) ? Transform.translate(offset, 0) : Transform.translate(0, offset);
}.bind(this);
if (!masterFn) masterFn = fn;
}
this._outputFunction = fn;
this._masterOutputFunction = masterFn ? masterFn : function(offset) {
return Transform.inverse(fn(-offset));
};
};
/**
* The Scroller instance's method for reading from an external position. Scroller uses
* the external position to actually scroll through it's renderables.
* @method positionFrom
* @param {Getter} position Can be either a function that returns a position,
* or an object with a get method that returns a position.
*/
Scroller.prototype.positionFrom = function positionFrom(position) {
if (position instanceof Function) this._positionGetter = position;
else if (position && position.get) this._positionGetter = position.get.bind(position);
else {
this._positionGetter = null;
this._position = position;
}
if (this._positionGetter) this._position = this._positionGetter.call(this);
};
/**
* Sets the collection of renderables under the Scroller instance's control.
*
* @method sequenceFrom
* @param {Array|ViewSequence} items Either an array of renderables or a Famous viewSequence.
* @chainable
*/
Scroller.prototype.sequenceFrom = function sequenceFrom(node) {
if (node instanceof Array) node = new ViewSequence({array: node});
this._node = node;
this._positionOffset = 0;
};
/**
* Returns the width and the height of the Scroller instance.
*
* @method getSize
* @return {Array} A two value array of the Scroller instance's current width and height (in that order).
*/
Scroller.prototype.getSize = function getSize(actual) {
return actual ? this._contextSize : this._size;
};
/**
* Generate a render spec from the contents of this component.
*
* @private
* @method render
* @return {number} Render spec for this component
*/
Scroller.prototype.render = function render() {
if (!this._node) return null;
if (this._positionGetter) this._position = this._positionGetter.call(this);
return this._entityId;
};
/**
* Apply changes from this component to the corresponding document element.
* This includes changes to classes, styles, size, content, opacity, origin,
* and matrix transforms.
*
* @private
* @method commit
* @param {Context} context commit context
*/
Scroller.prototype.commit = function commit(context) {
var transform = context.transform;
var opacity = context.opacity;
var origin = context.origin;
var size = context.size;
// reset edge detection on size change
if (!this.options.clipSize && (size[0] !== this._contextSize[0] || size[1] !== this._contextSize[1])) {
this._onEdge = 0;
this._contextSize[0] = size[0];
this._contextSize[1] = size[1];
if (this.options.direction === Utility.Direction.X) {
this._size[0] = _getClipSize.call(this);
this._size[1] = undefined;
}
else {
this._size[0] = undefined;
this._size[1] = _getClipSize.call(this);
}
}
var scrollTransform = this._masterOutputFunction(-this._position);
return {
transform: Transform.multiply(transform, scrollTransform),
size: size,
opacity: opacity,
origin: origin,
target: this.group.render()
};
};
function _normalizeState() {
var nodeSize = _sizeForDir.call(this, this._node.getSize());
var nextNode = this._node && this._node.getNext ? this._node.getNext() : null;
while (nextNode && this._position + this._positionOffset >= nodeSize) {
this._positionOffset -= nodeSize;
this._node = nextNode;
nodeSize = _sizeForDir.call(this, this._node.getSize());
nextNode = this._node && this._node.getNext ? this._node.getNext() : null;
}
var prevNode = this._node && this._node.getPrevious ? this._node.getPrevious() : null;
while (prevNode && this._position + this._positionOffset < 0) {
var prevNodeSize = _sizeForDir.call(this, prevNode.getSize());
this._positionOffset += prevNodeSize;
this._node = prevNode;
prevNode = this._node && this._node.getPrevious ? this._node.getPrevious() : null;
}
}
function _innerRender() {
var size = null;
var position = this._position;
var result = [];
this._onEdge = 0;
var offset = -this._positionOffset;
var clipSize = _getClipSize.call(this);
var currNode = this._node;
while (currNode && offset - position < clipSize + this.options.margin) {
offset += _output.call(this, currNode, offset, result);
currNode = currNode.getNext ? currNode.getNext() : null;
}
var sizeNode = this._node;
var nodesSize = _sizeForDir.call(this, sizeNode.getSize());
if (offset < clipSize) {
while (sizeNode && nodesSize < clipSize) {
sizeNode = sizeNode.getPrevious();
if (sizeNode) nodesSize += _sizeForDir.call(this, sizeNode.getSize());
}
sizeNode = this._node;
while (sizeNode && nodesSize < clipSize) {
sizeNode = sizeNode.getNext();
if (sizeNode) nodesSize += _sizeForDir.call(this, sizeNode.getSize());
}
}
var edgeSize = (nodesSize !== undefined && nodesSize < clipSize) ? nodesSize : clipSize;
if (!currNode && offset - position <= edgeSize) {
this._onEdge = 1;
this._eventOutput.emit('edgeHit', {
position: offset - edgeSize
});
}
else if (!this._node.getPrevious() && position <= 0) {
this._onEdge = -1;
this._eventOutput.emit('edgeHit', {
position: 0
});
}
// backwards
currNode = (this._node && this._node.getPrevious) ? this._node.getPrevious() : null;
offset = -this._positionOffset;
if (currNode) {
size = currNode.getSize ? currNode.getSize() : this._contextSize;
offset -= _sizeForDir.call(this, size);
}
while (currNode && ((offset - position) > -(_getClipSize.call(this) + this.options.margin))) {
_output.call(this, currNode, offset, result);
currNode = currNode.getPrevious ? currNode.getPrevious() : null;
if (currNode) {
size = currNode.getSize ? currNode.getSize() : this._contextSize;
offset -= _sizeForDir.call(this, size);
}
}
_normalizeState.call(this);
return result;
}
module.exports = Scroller;
});