This repository was archived by the owner on Mar 5, 2026. It is now read-only.
forked from FamousArchives/views
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSequentialLayout.js
More file actions
147 lines (127 loc) · 5.42 KB
/
SequentialLayout.js
File metadata and controls
147 lines (127 loc) · 5.42 KB
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
/* 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: felix@famo.us
* @license MPL 2.0
* @copyright Famous Industries, Inc. 2014
*/
define(function(require, exports, module) {
var OptionsManager = require('famous/core/OptionsManager');
var Transform = require('famous/core/Transform');
var ViewSequence = require('famous/core/ViewSequence');
var Utility = require('famous/utilities/Utility');
/**
* SequentialLayout will lay out a collection of renderables sequentially in the specified direction.
* @class SequentialLayout
* @constructor
* @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 SequentialLayout 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 {Array.Number} [options.defaultItemSize=[50, 50]] In the case where a renderable layed out
* under SequentialLayout's control doesen't have a getSize method, SequentialLayout will assign it
* this default size. (Commonly a case with Views).
*/
function SequentialLayout(options) {
this._items = null;
this._size = null;
this._outputFunction = SequentialLayout.DEFAULT_OUTPUT_FUNCTION;
this.options = Object.create(this.constructor.DEFAULT_OPTIONS);
this.optionsManager = new OptionsManager(this.options);
if (options) this.setOptions(options);
}
SequentialLayout.DEFAULT_OPTIONS = {
direction: Utility.Direction.Y,
defaultItemSize: [50, 50]
};
SequentialLayout.DEFAULT_OUTPUT_FUNCTION = function DEFAULT_OUTPUT_FUNCTION(input, offset, index) {
var transform = (this.options.direction === Utility.Direction.X) ? Transform.translate(offset, 0) : Transform.translate(0, offset);
return {
transform: transform,
target: input.render()
};
};
/**
* Returns the width and the height of the SequentialLayout instance.
*
* @method getSize
* @return {Array} A two value array of the SequentialLayout instance's current width and height (in that order).
*/
SequentialLayout.prototype.getSize = function getSize() {
if (!this._size) this.render(); // hack size in
return this._size;
};
/**
* Sets the collection of renderables under the SequentialLayout instance's control.
*
* @method sequenceFrom
* @param {Array|ViewSequence} items Either an array of renderables or a Famous viewSequence.
* @chainable
*/
SequentialLayout.prototype.sequenceFrom = function sequenceFrom(items) {
if (items instanceof Array) items = new ViewSequence(items);
this._items = items;
return this;
};
/**
* Patches the SequentialLayout instance's options with the passed-in ones.
*
* @method setOptions
* @param {Options} options An object of configurable options for the SequentialLayout instance.
* @chainable
*/
SequentialLayout.prototype.setOptions = function setOptions(options) {
this.optionsManager.setOptions.apply(this.optionsManager, arguments);
return this;
};
/**
* setOutputFunction is used to apply a user-defined output transform on each processed renderable.
* For a good example, check out SequentialLayout's own DEFAULT_OUTPUT_FUNCTION in the code.
*
* @method setOptions
* @param {Function} outputFunction An output processer for each renderable in the SequentialLayout
* instance.
* @chainable
*/
SequentialLayout.prototype.setOutputFunction = function setOutputFunction(outputFunction) {
this._outputFunction = outputFunction;
return this;
};
/**
* Generate a render spec from the contents of this component.
*
* @private
* @method render
* @return {number} Render spec for this component
*/
SequentialLayout.prototype.render = function render() {
var length = 0;
var girth = 0;
var lengthDim = (this.options.direction === Utility.Direction.X) ? 0 : 1;
var girthDim = (this.options.direction === Utility.Direction.X) ? 1 : 0;
var currentNode = this._items;
var result = [];
while (currentNode) {
var item = currentNode.get();
var itemSize;
if (item && item.getSize) itemSize = item.getSize();
if (!itemSize) itemSize = this.options.defaultItemSize;
if (itemSize[girthDim] !== true) girth = Math.max(girth, itemSize[girthDim]);
var output = this._outputFunction.call(this, item, length, result.length);
result.push(output);
if (itemSize[lengthDim] && (itemSize[lengthDim] !== true)) length += itemSize[lengthDim];
currentNode = currentNode.getNext();
}
if (!girth) girth = undefined;
if (!this._size) this._size = [0, 0];
this._size[lengthDim] = length;
this._size[girthDim] = girth;
return {
size: this.getSize(),
target: result
};
};
module.exports = SequentialLayout;
});