-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathFlexGrid.js
146 lines (120 loc) · 4.43 KB
/
FlexGrid.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
define(function(require, exports, module) {
var View = require('famous/core/View');
var Entity = require('famous/core/Entity');
var Modifier = require('famous/core/Modifier');
var Transform = require('famous/core/Transform');
var Transitionable = require('famous/transitions/Transitionable');
var TransitionableTransform = require('famous/transitions/TransitionableTransform');
var Easing = require('famous/transitions/Easing');
function FlexGrid() {
View.apply(this, arguments);
this._modifiers = [];
this._states = [];
this.id = Entity.register(this);
}
FlexGrid.prototype = Object.create(View.prototype);
FlexGrid.prototype.constructor = FlexGrid;
FlexGrid.DEFAULT_OPTIONS = {
marginTop: 0,
marginSide: 0,
gutterCol: 0,
gutterRow: 0,
itemSize: undefined,
numCols: undefined,
transition: { curve: Easing.outBack, duration: 500 }
};
function _calcSpacing(width) {
var itemWidth = this.options.itemSize[0];
var gutterCol = this.options.gutterCol;
var ySpacing = itemWidth + gutterCol;
var margin = this.options.marginSide;
var numCols = Math.floor((width - 2 * margin + gutterCol) / ySpacing);
if (this._items.length < numCols) numCols = this._items.length;
numCols = this.options.numCols || numCols;
margin = (width - numCols * ySpacing + gutterCol)/2;
return {
numCols: numCols,
marginSide: margin,
ySpacing: ySpacing
}
}
function _calcPositions(spacing) {
var positions = [];
var col = 0;
var row = 0;
var xPos;
var yPos;
for (var i = 0; i < this._items.length; i++) {
xPos = spacing.marginSide + col * spacing.ySpacing;
yPos = this.options.marginTop + row * (this.options.itemSize[1] + this.options.gutterRow);
positions.push([xPos, yPos, 0]);
col++
if (col === spacing.numCols) {
row++;
col = 0;
}
}
return positions;
}
function _createModifier(index, position, size) {
var transitionItem = {
transform: new TransitionableTransform(Transform.translate.apply(null, position)),
size: new Transitionable((size || this.options.itemSize))
}
var modifier = new Modifier(transitionItem);
this._states[index] = transitionItem;
this._modifiers[index] = modifier;
}
function _animateModifier(index, position, size) {
var transformTransitionable = this._states[index].transform;
var sizeTransitionable = this._states[index].size;
transformTransitionable.halt();
sizeTransitionable.halt();
transformTransitionable.setTranslate(position, this.options.transition);
sizeTransitionable.set(size, this.options.transition);
}
FlexGrid.prototype.sequenceFrom = function(items) {
this._items = items;
};
FlexGrid.prototype.render = function() {
return this.id;
};
FlexGrid.prototype.commit = function(context) {
var width = context.size[0];
var specs = [];
if (this._cachedWidth !== width) {
var spacing = _calcSpacing.call(this, width);
var size = this.options.itemSize;
if (spacing.numCols < 2) {
spacing.numCols = 1;
spacing.marginSide = 0;
size = [width, size[1]];
}
var positions = _calcPositions.call(this, spacing);
for (var i = 0; i < this._items.length; i++) {
if (this._modifiers[i] === undefined) {
_createModifier.call(this, i, positions[i], size);
}
else {
_animateModifier.call(this, i, positions[i], size);
}
}
this._cachedWidth = width;
}
for (var i = 0; i < this._modifiers.length; i++) {
var spec = this._modifiers[i].modify({
target: this._items[i].render()
});
specs.push(spec);
}
return {
target: specs,
size: context.size,
transform: context.transform,
opacity: context.opacity,
origin: context.origin,
align: context.align
}
};
module.exports = FlexGrid;
});