Mutate instead of trying to be functional
This commit is contained in:
@@ -1,6 +1,25 @@
|
|||||||
|
|
||||||
function computeLayout(node) {
|
function computeLayout(node) {
|
||||||
|
|
||||||
|
function fillNodes(node) {
|
||||||
|
node.layout = {
|
||||||
|
top: undefined,
|
||||||
|
left: undefined,
|
||||||
|
width: undefined,
|
||||||
|
height: undefined
|
||||||
|
};
|
||||||
|
(node.children || []).forEach(fillNodes);
|
||||||
|
}
|
||||||
|
|
||||||
|
function extractNodes(node) {
|
||||||
|
var layout = node.layout;
|
||||||
|
delete node.layout;
|
||||||
|
if (node.children) {
|
||||||
|
layout.children = node.children.map(extractNodes);
|
||||||
|
}
|
||||||
|
return layout;
|
||||||
|
}
|
||||||
|
|
||||||
function getMargin(node) {
|
function getMargin(node) {
|
||||||
if ('margin' in node.style) {
|
if ('margin' in node.style) {
|
||||||
return node.style.margin;
|
return node.style.margin;
|
||||||
@@ -22,29 +41,25 @@ function computeLayout(node) {
|
|||||||
var crossAxis = mainAxis === 'row' ? 'column' : 'row';
|
var crossAxis = mainAxis === 'row' ? 'column' : 'row';
|
||||||
|
|
||||||
var mainPos = 0;
|
var mainPos = 0;
|
||||||
|
|
||||||
var children = [];
|
var children = [];
|
||||||
(node.children || []).forEach(function(child) {
|
(node.children || []).forEach(function(child) {
|
||||||
var offset = {};
|
var offset = {};
|
||||||
offset[pos[mainAxis]] = mainPos;
|
offset[pos[mainAxis]] = mainPos;
|
||||||
offset[pos[crossAxis]] = 0;
|
offset[pos[crossAxis]] = 0;
|
||||||
children.push(layoutNode(child, offset));
|
layoutNode(child, offset);
|
||||||
|
|
||||||
mainPos += child.style[dim[mainAxis]] + 2 * getMargin(child);
|
mainPos += child.layout[dim[mainAxis]] + 2 * getMargin(child);
|
||||||
});
|
});
|
||||||
|
|
||||||
var result = {
|
node.layout.width = node.style.width;
|
||||||
width: node.style.width,
|
node.layout.height = node.style.height;
|
||||||
height: node.style.height,
|
node.layout.top = getMargin(node) + parent.top;
|
||||||
top: getMargin(node) + parent.top,
|
node.layout.left = getMargin(node) + parent.left;
|
||||||
left: getMargin(node) + parent.left
|
|
||||||
};
|
|
||||||
|
|
||||||
if (children.length > 0) {
|
|
||||||
result.children = children;
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return layoutNode(node, {top: 0, left: 0});
|
fillNodes(node);
|
||||||
|
layoutNode(node, {top: 0, left: 0});
|
||||||
|
return extractNodes(node);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user