Files
yoga/src/Layout.js

51 lines
1.0 KiB
JavaScript
Raw Normal View History

2014-03-30 17:12:38 -07:00
function computeLayout(node) {
2014-03-30 19:33:24 -07:00
2014-03-30 19:51:14 -07:00
function getMargin(node) {
if ('margin' in node.style) {
return node.style.margin;
}
return 0;
}
2014-03-31 11:09:33 -07:00
var pos = {
row: 'left',
column: 'top'
};
var dim = {
row: 'width',
column: 'height'
};
2014-03-30 19:33:24 -07:00
function layoutNode(node, parent) {
2014-03-31 11:09:33 -07:00
var mainAxis = node.style.flexDirection === 'row' ? 'row' : 'column';
var crossAxis = mainAxis === 'row' ? 'column' : 'row';
var mainPos = 0;
2014-03-30 19:33:24 -07:00
var children = [];
(node.children || []).forEach(function(child) {
2014-03-31 11:09:33 -07:00
var offset = {};
offset[pos[mainAxis]] = mainPos;
offset[pos[crossAxis]] = 0;
children.push(layoutNode(child, offset));
mainPos += child.style[dim[mainAxis]] + 2 * getMargin(child);
2014-03-30 19:18:06 -07:00
});
2014-03-30 19:33:24 -07:00
var result = {
width: node.style.width,
height: node.style.height,
2014-03-31 11:09:33 -07:00
top: getMargin(node) + parent.top,
left: getMargin(node) + parent.left
2014-03-30 19:33:24 -07:00
};
2014-03-30 19:18:06 -07:00
2014-03-30 19:33:24 -07:00
if (children.length > 0) {
result.children = children;
}
return result;
2014-03-30 19:18:06 -07:00
}
2014-03-30 19:33:24 -07:00
return layoutNode(node, {top: 0, left: 0});
2014-03-30 17:12:38 -07:00
}