This repository has been archived by the owner on Dec 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 44
/
LayoutDockHelper.js
276 lines (263 loc) · 9.6 KB
/
LayoutDockHelper.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
/**
* This Source Code is licensed under the MIT license. If a copy of the
* MIT-license was not distributed with this file, You can obtain one at:
* http://opensource.org/licenses/mit-license.html.
*
* @author: Hein Rutjes (IjzerenHein)
* @license MIT
* @copyright Gloey Apps, 2014 - 2015
*/
/**
* LayoutDockHelper helps positioning nodes using docking principles.
*
* **Example:**
*
* ```javascript
* var LayoutDockHelper = require('famous-flex/helpers/LayoutDockHelper');
*
* function HeaderFooterLayout(context, options) {
* var dock = new LayoutDockHelper(context);
* dock.top('header', options.headerSize);
* dock.bottom('footer', options.footerSize);
* dock.fill('content');
* };
* ```
*
* You can also use layout-literals to create layouts using docking semantics:
*
* ```javascript
* var layoutController = new LayoutController({
* layout: {dock: [
* ['top', 'header', 40],
* ['bottom', 'footer', 40, 1], // z-index +1
* ['fill', 'content']
* ]},
* dataSource: {
* header: new Surface({content: 'header'}),
* footer: new Surface({content: 'footer'}),
* content: new Surface({content: 'content'}),
* }
* });
* ```
*
* @module
*/
define(function(require, exports, module) {
// import dependencies
var LayoutUtility = require('../LayoutUtility');
/**
* @class
* @param {LayoutContext} context layout-context
* @param {Object} [options] additional options
* @param {Object} [options.margins] margins to start out with (default: 0px)
* @param {Number} [options.translateZ] z-index to use when translating objects (default: 0)
* @alias module:LayoutDockHelper
*/
function LayoutDockHelper(context, options) {
var size = context.size;
this._size = size;
this._context = context;
this._options = options;
this._data = {
z: (options && options.translateZ) ? options.translateZ : 0
};
if (options && options.margins) {
var margins = LayoutUtility.normalizeMargins(options.margins);
this._data.left = margins[3];
this._data.top = margins[0];
this._data.right = size[0] - margins[1];
this._data.bottom = size[1] - margins[2];
}
else {
this._data.left = 0;
this._data.top = 0;
this._data.right = size[0];
this._data.bottom = size[1];
}
}
/**
* Parses the layout-rules based on a JSON data object.
* The object should be an array with the following syntax:
* `[[rule, node, value, z], [rule, node, value, z], ...]`
*
* **Example:**
*
* ```JSON
* [
* ['top', 'header', 50],
* ['bottom', 'footer', 50, 10], // z-index: 10
* ['margins', [10, 5]], // marginate remaining space: 10px top/bottom, 5px left/right
* ['fill', 'content']
* ]
* ```
*
* @param {Object} data JSON object
*/
LayoutDockHelper.prototype.parse = function(data) {
for (var i = 0; i < data.length; i++) {
var rule = data[i];
var value = (rule.length >= 3) ? rule[2] : undefined;
if (rule[0] === 'top') {
this.top(rule[1], value, (rule.length >=4) ? rule[3] : undefined);
}
else if (rule[0] === 'left') {
this.left(rule[1], value, (rule.length >=4) ? rule[3] : undefined);
}
else if (rule[0] === 'right') {
this.right(rule[1], value, (rule.length >=4) ? rule[3] : undefined);
}
else if (rule[0] === 'bottom') {
this.bottom(rule[1], value, (rule.length >=4) ? rule[3] : undefined);
}
else if (rule[0] === 'fill') {
this.fill(rule[1], (rule.length >=3) ? rule[2] : undefined);
}
else if (rule[0] === 'margins') {
this.margins(rule[1]);
}
}
};
/**
* Dock the node to the top.
*
* @param {LayoutNode|String} [node] layout-node to dock, when omitted the `height` argument argument is used for padding
* @param {Number} [height] height of the layout-node, when omitted the height of the node is used
* @param {Number} [z] z-index to use for the node
* @return {LayoutDockHelper} this
*/
LayoutDockHelper.prototype.top = function(node, height, z) {
if (height instanceof Array) {
height = height[1];
}
if (height === undefined) {
var size = this._context.resolveSize(node, [this._data.right - this._data.left, this._data.bottom - this._data.top]);
height = size[1];
}
this._context.set(node, {
size: [this._data.right - this._data.left, height],
origin: [0, 0],
align: [0, 0],
translate: [this._data.left, this._data.top, (z === undefined) ? this._data.z : z]
});
this._data.top += height;
return this;
};
/**
* Dock the node to the left
*
* @param {LayoutNode|String} [node] layout-node to dock, when omitted the `width` argument argument is used for padding
* @param {Number} [width] width of the layout-node, when omitted the width of the node is used
* @param {Number} [z] z-index to use for the node
* @return {LayoutDockHelper} this
*/
LayoutDockHelper.prototype.left = function(node, width, z) {
if (width instanceof Array) {
width = width[0];
}
if (width === undefined) {
var size = this._context.resolveSize(node, [this._data.right - this._data.left, this._data.bottom - this._data.top]);
width = size[0];
}
this._context.set(node, {
size: [width, this._data.bottom - this._data.top],
origin: [0, 0],
align: [0, 0],
translate: [this._data.left, this._data.top, (z === undefined) ? this._data.z : z]
});
this._data.left += width;
return this;
};
/**
* Dock the node to the bottom
*
* @param {LayoutNode|String} [node] layout-node to dock, when omitted the `height` argument argument is used for padding
* @param {Number} [height] height of the layout-node, when omitted the height of the node is used
* @param {Number} [z] z-index to use for the node
* @return {LayoutDockHelper} this
*/
LayoutDockHelper.prototype.bottom = function(node, height, z) {
if (height instanceof Array) {
height = height[1];
}
if (height === undefined) {
var size = this._context.resolveSize(node, [this._data.right - this._data.left, this._data.bottom - this._data.top]);
height = size[1];
}
this._context.set(node, {
size: [this._data.right - this._data.left, height],
origin: [0, 1],
align: [0, 1],
translate: [this._data.left, -(this._size[1] - this._data.bottom), (z === undefined) ? this._data.z : z]
});
this._data.bottom -= height;
return this;
};
/**
* Dock the node to the right.
*
* @param {LayoutNode|String} [node] layout-node to dock, when omitted the `width` argument argument is used for padding
* @param {Number} [width] width of the layout-node, when omitted the width of the node is used
* @param {Number} [z] z-index to use for the node
* @return {LayoutDockHelper} this
*/
LayoutDockHelper.prototype.right = function(node, width, z) {
if (width instanceof Array) {
width = width[0];
}
if (node) {
if (width === undefined) {
var size = this._context.resolveSize(node, [this._data.right - this._data.left, this._data.bottom - this._data.top]);
width = size[0];
}
this._context.set(node, {
size: [width, this._data.bottom - this._data.top],
origin: [1, 0],
align: [1, 0],
translate: [-(this._size[0] - this._data.right), this._data.top, (z === undefined) ? this._data.z : z]
});
}
if (width) {
this._data.right -= width;
}
return this;
};
/**
* Fills the node to the remaining content.
*
* @param {LayoutNode|String} node layout-node to dock
* @param {Number} [z] z-index to use for the node
* @return {LayoutDockHelper} this
*/
LayoutDockHelper.prototype.fill = function(node, z) {
this._context.set(node, {
size: [this._data.right - this._data.left, this._data.bottom - this._data.top],
translate: [this._data.left, this._data.top, (z === undefined) ? this._data.z : z]
});
return this;
};
/**
* Applies indent margins to the remaining content.
*
* @param {Number|Array} margins margins shorthand (e.g. '5', [10, 10], [5, 10, 5, 10])
* @return {LayoutDockHelper} this
*/
LayoutDockHelper.prototype.margins = function(margins) {
margins = LayoutUtility.normalizeMargins(margins);
this._data.left += margins[3];
this._data.top += margins[0];
this._data.right -= margins[1];
this._data.bottom -= margins[2];
return this;
};
/**
* Gets the current left/right/top/bottom/z bounds used by the dock-helper.
*
* @return {Object} `{left: x, right: x, top: x, bottom: x, z: x}`
*/
LayoutDockHelper.prototype.get = function() {
return this._data;
};
// Register the helper
LayoutUtility.registerHelper('dock', LayoutDockHelper);
module.exports = LayoutDockHelper;
});