Repository navigation
Expand file tree
/
Copy pathHeaderFooterLayout.js
More file actions
155 lines (137 loc) · 5.57 KB
/
Copy pathHeaderFooterLayout.js
File metadata and controls
155 lines (137 loc) · 5.57 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
148
149
150
151
152
153
154
155
/* 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 RenderNode = require('famous/core/RenderNode');
var Transform = require('famous/core/Transform');
var OptionsManager = require('famous/core/OptionsManager');
/**
* A layout which will arrange three renderables into a header and footer area of defined size,
and a content area of flexible size.
* @class HeaderFooterLayout
* @constructor
* @param {Options} [options] An object of configurable options.
* @param {Number} [options.direction=HeaderFooterLayout.DIRECTION_Y] A direction of HeaderFooterLayout.DIRECTION_X
* lays your HeaderFooterLayout instance horizontally, and a direction of HeaderFooterLayout.DIRECTION_Y
* lays it out vertically.
* @param {Number} [options.headerSize=undefined] The amount of pixels allocated to the header node
* in the HeaderFooterLayout instance's direction.
* @param {Number} [options.footerSize=undefined] The amount of pixels allocated to the footer node
* in the HeaderFooterLayout instance's direction.
*/
function HeaderFooterLayout(options) {
this.options = Object.create(HeaderFooterLayout.DEFAULT_OPTIONS);
this._optionsManager = new OptionsManager(this.options);
if (options) this.setOptions(options);
this._entityId = Entity.register(this);
this.header = new RenderNode();
this.footer = new RenderNode();
this.content = new RenderNode();
}
/**
* When used as a value for your HeaderFooterLayout's direction option, causes it to lay out horizontally.
*
* @attribute DIRECTION_X
* @type Number
* @static
* @default 0
* @protected
*/
HeaderFooterLayout.DIRECTION_X = 0;
/**
* When used as a value for your HeaderFooterLayout's direction option, causes it to lay out vertically.
*
* @attribute DIRECTION_Y
* @type Number
* @static
* @default 1
* @protected
*/
HeaderFooterLayout.DIRECTION_Y = 1;
HeaderFooterLayout.DEFAULT_OPTIONS = {
direction: HeaderFooterLayout.DIRECTION_Y,
headerSize: undefined,
footerSize: undefined,
defaultHeaderSize: 0,
defaultFooterSize: 0
};
/**
* Generate a render spec from the contents of this component.
*
* @private
* @method render
* @return {Object} Render spec for this component
*/
HeaderFooterLayout.prototype.render = function render() {
return this._entityId;
};
/**
* Patches the HeaderFooterLayout instance's options with the passed-in ones.
*
* @method setOptions
* @param {Options} options An object of configurable options for the HeaderFooterLayout instance.
*/
HeaderFooterLayout.prototype.setOptions = function setOptions(options) {
return this._optionsManager.setOptions(options);
};
function _resolveNodeSize(node, defaultSize) {
var nodeSize = node.getSize();
return nodeSize ? nodeSize[this.options.direction] : defaultSize;
}
function _outputTransform(offset) {
if (this.options.direction === HeaderFooterLayout.DIRECTION_X) return Transform.translate(offset, 0, 0);
else return Transform.translate(0, offset, 0);
}
function _finalSize(directionSize, size) {
if (this.options.direction === HeaderFooterLayout.DIRECTION_X) return [directionSize, size[1]];
else return [size[0], directionSize];
}
/**
* 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
*/
HeaderFooterLayout.prototype.commit = function commit(context) {
var transform = context.transform;
var origin = context.origin;
var size = context.size;
var opacity = context.opacity;
var headerSize = (this.options.headerSize !== undefined) ? this.options.headerSize : _resolveNodeSize.call(this, this.header, this.options.defaultHeaderSize);
var footerSize = (this.options.footerSize !== undefined) ? this.options.footerSize : _resolveNodeSize.call(this, this.footer, this.options.defaultFooterSize);
var contentSize = size[this.options.direction] - headerSize - footerSize;
if (size) transform = Transform.moveThen([-size[0]*origin[0], -size[1]*origin[1], 0], transform);
var result = [
{
size: _finalSize.call(this, headerSize, size),
target: this.header.render()
},
{
transform: _outputTransform.call(this, headerSize),
size: _finalSize.call(this, contentSize, size),
target: this.content.render()
},
{
transform: _outputTransform.call(this, headerSize + contentSize),
size: _finalSize.call(this, footerSize, size),
target: this.footer.render()
}
];
return {
transform: transform,
opacity: opacity,
size: size,
target: result
};
};
module.exports = HeaderFooterLayout;
});