define([], function () {
function computeLayout(node, box) {
var totalSize,
x = box.x,
y = box.y;
if(node.children) {
totalSize = sum(node.children, size);
return _.reduce(node.children, function (layoutElements, child) {
var childBox = {x: x, y: y};
if (node.orientation === 'vertical') {
childBox.width = box.width * size(child) / totalSize;
childBox.height = box.height;
x += childBox.width;
} else if (node.orientation === 'horizontal') {
childBox.width = box.width;
childBox.height = box.height * size(child) / totalSize;
y += childBox.height;
}
return layoutElements.concat(computeLayout(child, childBox));
}, []);
} else {
return [{ name: node.name, box: box }];
}
}
function sum(arr, fn) {
return _.reduce(arr, function(memo, item){
return memo + fn(item);
}, 0);
}