Files
yoga/src/Layout.js

38 lines
759 B
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-30 19:33:24 -07:00
function layoutNode(node, parent) {
var top = 0;
var children = [];
(node.children || []).forEach(function(child) {
children.push(layoutNode(child, {
top: top,
left: 0
}));
2014-03-30 20:33:40 -07:00
top += child.style.height + 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-30 19:51:14 -07:00
top: getMargin(node) + parent.top + 0,
left: getMargin(node) + parent.left + 0
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
}