From a370fc643b40ae7b44b87de1d777df1eba9a114c Mon Sep 17 00:00:00 2001 From: Christopher Chedeau Date: Sat, 5 Apr 2014 11:25:58 -0700 Subject: [PATCH] Mutate instead of trying to be functional --- src/Layout.js | 43 +++++++++++++++++++++++++++++-------------- 1 file changed, 29 insertions(+), 14 deletions(-) diff --git a/src/Layout.js b/src/Layout.js index 64a15906..20ff165b 100755 --- a/src/Layout.js +++ b/src/Layout.js @@ -1,6 +1,25 @@ 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) { if ('margin' in node.style) { return node.style.margin; @@ -22,29 +41,25 @@ function computeLayout(node) { var crossAxis = mainAxis === 'row' ? 'column' : 'row'; var mainPos = 0; + var children = []; (node.children || []).forEach(function(child) { var offset = {}; offset[pos[mainAxis]] = mainPos; 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 = { - width: node.style.width, - height: node.style.height, - top: getMargin(node) + parent.top, - left: getMargin(node) + parent.left - }; - - if (children.length > 0) { - result.children = children; - } - return result; + node.layout.width = node.style.width; + node.layout.height = node.style.height; + node.layout.top = getMargin(node) + parent.top; + node.layout.left = getMargin(node) + parent.left; } - return layoutNode(node, {top: 0, left: 0}); + fillNodes(node); + layoutNode(node, {top: 0, left: 0}); + return extractNodes(node); }