-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathFlexibleLayout.js
201 lines (171 loc) · 6.9 KB
/
FlexibleLayout.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
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* Owner: [email protected]
* @license MPL 2.0
* @copyright Famous Industries, Inc. 2014
*/
define(function(require, exports, module) {
var Entity = require('famous/core/Entity');
var Transform = require('famous/core/Transform');
var OptionsManager = require('famous/core/OptionsManager');
var EventHandler = require('famous/core/EventHandler');
var Transitionable = require('famous/transitions/Transitionable');
/**
* A layout which divides a context into sections based on a proportion
* of the total sum of ratios. FlexibleLayout can either lay renderables
* out vertically or horizontally.
* @class FlexibleLayout
* @constructor
* @param {Options} [options] An object of configurable options.
* @param {Number} [options.direction=0] Direction the FlexibleLayout instance should lay out renderables.
* @param {Transition} [options.transition=false] The transiton that controls the FlexibleLayout instance's reflow.
* @param {Ratios} [options.ratios=[]] The proportions for the renderables to maintain
*/
function FlexibleLayout(options) {
this.options = Object.create(FlexibleLayout.DEFAULT_OPTIONS);
this.optionsManager = new OptionsManager(this.options);
if (options) this.setOptions(options);
this.id = Entity.register(this);
this._ratios = new Transitionable(this.options.ratios);
this._nodes = [];
this._cachedDirection = null;
this._cachedTotalLength = false;
this._cachedLengths = [];
this._cachedTransforms = null;
this._ratiosDirty = false;
this._eventOutput = new EventHandler();
EventHandler.setOutputHandler(this, this._eventOutput);
}
FlexibleLayout.DIRECTION_X = 0;
FlexibleLayout.DIRECTION_Y = 1;
FlexibleLayout.DEFAULT_OPTIONS = {
direction: FlexibleLayout.DIRECTION_X,
transition: false,
ratios : []
};
function _reflow(ratios, length, direction) {
var currTransform;
var translation = 0;
var flexLength = length;
var ratioSum = 0;
var ratio;
var node;
var i;
this._cachedLengths = [];
this._cachedTransforms = [];
for (i = 0; i < ratios.length; i++){
ratio = ratios[i];
node = this._nodes[i];
if (typeof ratio !== 'number')
flexLength -= node.getSize()[direction] || 0;
else
ratioSum += ratio;
}
for (i = 0; i < ratios.length; i++) {
node = this._nodes[i];
ratio = ratios[i];
length = (typeof ratio === 'number')
? flexLength * ratio / ratioSum
: node.getSize()[direction];
currTransform = (direction === FlexibleLayout.DIRECTION_X)
? Transform.translate(translation, 0, 0)
: Transform.translate(0, translation, 0);
this._cachedTransforms.push(currTransform);
this._cachedLengths.push(length);
translation += length;
}
}
/**
* Generate a render spec from the contents of this component.
*
* @private
* @method render
* @return {Object} Render spec for this component
*/
FlexibleLayout.prototype.render = function render() {
return this.id;
};
/**
* Patches the FlexibleLayouts instance's options with the passed-in ones.
*
* @method setOptions
* @param {Options} options An object of configurable options for the FlexibleLayout instance.
*/
FlexibleLayout.prototype.setOptions = function setOptions(options) {
this.optionsManager.setOptions(options);
};
/**
* Sets the collection of renderables under the FlexibleLayout instance's control. Also sets
* the associated ratio values for sizing the renderables if given.
*
* @method sequenceFrom
* @param {Array} sequence An array of renderables.
*/
FlexibleLayout.prototype.sequenceFrom = function sequenceFrom(sequence) {
this._nodes = sequence;
if (this._ratios.get().length === 0) {
var ratios = [];
for (var i = 0; i < this._nodes.length; i++) ratios.push(1);
this.setRatios(ratios);
}
};
/**
* Sets the associated ratio values for sizing the renderables.
*
* @method setRatios
* @param {Array} ratios Array of ratios corresponding to the percentage sizes each renderable should be
*/
FlexibleLayout.prototype.setRatios = function setRatios(ratios, transition, callback) {
if (transition === undefined) transition = this.options.transition;
var currRatios = this._ratios;
if (currRatios.get().length === 0) transition = undefined;
if (currRatios.isActive()) currRatios.halt();
currRatios.set(ratios, transition, callback);
this._ratiosDirty = true;
};
/**
* 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
*/
FlexibleLayout.prototype.commit = function commit(context) {
var parentSize = context.size;
var parentTransform = context.transform;
var parentOrigin = context.origin;
var ratios = this._ratios.get();
var direction = this.options.direction;
var length = parentSize[direction];
var size;
if (length !== this._cachedTotalLength || this._ratiosDirty || this._ratios.isActive() || direction !== this._cachedDirection) {
_reflow.call(this, ratios, length, direction);
if (length !== this._cachedTotalLength) this._cachedTotalLength = length;
if (direction !== this._cachedDirection) this._cachedDirection = direction;
if (this._ratiosDirty) this._ratiosDirty = false;
}
var result = [];
for (var i = 0; i < ratios.length; i++) {
size = [undefined, undefined];
length = this._cachedLengths[i];
size[direction] = length;
result.push({
transform : this._cachedTransforms[i],
size: size,
target : this._nodes[i].render()
});
}
if (parentSize && (parentOrigin[0] !== 0 && parentOrigin[1] !== 0))
parentTransform = Transform.moveThen([-parentSize[0]*parentOrigin[0], -parentSize[1]*parentOrigin[1], 0], parentTransform);
return {
transform: parentTransform,
size: parentSize,
target: result
};
};
module.exports = FlexibleLayout;
});