Files
yoga/src/Layout.js

360 lines
11 KiB
JavaScript
Raw Normal View History

2014-03-30 17:12:38 -07:00
2014-04-18 11:53:28 -07:00
var computeLayout = (function() {
2014-03-30 19:33:24 -07:00
function fillNodes(node) {
node.layout = {
width: undefined,
2014-04-05 22:23:00 -07:00
height: undefined,
top: 0,
left: 0
};
2014-04-18 10:37:01 -07:00
if (!node.style) {
node.style = {};
}
if (!node.children) {
node.children = [];
}
node.children.forEach(fillNodes);
}
function extractNodes(node) {
var layout = node.layout;
delete node.layout;
if (node.children.length > 0) {
layout.children = node.children.map(extractNodes);
} else {
delete node.children;
}
return layout;
}
2014-04-06 10:19:53 -07:00
function capitalizeFirst(str) {
return str.charAt(0).toUpperCase() + str.slice(1);
}
2014-04-22 11:31:42 -07:00
function getSpacing(node, type, suffix, location) {
var key = type + capitalizeFirst(location) + suffix;
2014-04-06 10:19:53 -07:00
if (key in node.style) {
return node.style[key];
}
2014-04-22 11:31:42 -07:00
key = type + suffix;
if (key in node.style) {
return node.style[key];
2014-03-30 19:51:14 -07:00
}
2014-04-06 10:19:53 -07:00
2014-03-30 19:51:14 -07:00
return 0;
}
2014-04-22 11:51:04 -07:00
function getPositiveSpacing(node, type, suffix, location) {
var key = type + capitalizeFirst(location) + suffix;
if (key in node.style && node.style[key] >= 0) {
return node.style[key];
}
key = type + suffix;
if (key in node.style && node.style[key] >= 0) {
return node.style[key];
}
return 0;
}
function isUndefined(value) {
2014-04-22 13:18:05 -07:00
return value === undefined;
}
function getMargin(node, location) {
2014-04-22 11:31:42 -07:00
return getSpacing(node, 'margin', '', location);
2014-04-15 18:04:11 -07:00
}
function getPadding(node, location) {
2014-04-22 11:51:04 -07:00
return getPositiveSpacing(node, 'padding', '', location);
}
function getBorder(node, location) {
2014-04-22 11:51:04 -07:00
return getPositiveSpacing(node, 'border', 'Width', location);
}
function getPaddingAndBorder(node, location) {
return getPadding(node, location) + getBorder(node, location);
2014-04-22 11:31:42 -07:00
}
function getMarginAxis(node, axis) {
return getMargin(node, leading[axis]) + getMargin(node, trailing[axis]);
}
function getPaddingAndBorderAxis(node, axis) {
return getPaddingAndBorder(node, leading[axis]) + getPaddingAndBorder(node, trailing[axis]);
2014-04-15 18:04:11 -07:00
}
2014-04-06 17:39:30 -07:00
function getJustifyContent(node) {
if ('justifyContent' in node.style) {
return node.style.justifyContent;
}
return 'flex-start';
}
function getAlignItem(node, child) {
if ('alignSelf' in child.style) {
return child.style.alignSelf;
}
if ('alignItems' in node.style) {
return node.style.alignItems;
}
return 'flex-start';
}
2014-04-06 21:49:09 -07:00
function getFlexDirection(node) {
if ('flexDirection' in node.style) {
return node.style.flexDirection;
}
return 'column';
}
function getPositionType(node) {
if ('position' in node.style) {
return node.style.position;
}
return 'relative';
}
2014-04-15 16:39:42 -07:00
function getFlex(node) {
2014-04-22 13:18:05 -07:00
return node.style.flex === 1;
2014-04-15 16:39:42 -07:00
}
2014-04-15 16:44:24 -07:00
function getDimWithMargin(node, axis) {
2014-04-22 11:31:42 -07:00
return node.layout[dim[axis]] + getMarginAxis(node, axis);
2014-04-15 16:44:24 -07:00
}
2014-04-18 10:11:37 -07:00
function isDimDefined(node, axis) {
2014-04-22 17:19:21 -07:00
return !isUndefined(node.style[dim[axis]]) && node.style[dim[axis]] >= 0;
2014-04-18 10:11:37 -07:00
}
2014-04-21 18:34:28 -07:00
function isPosDefined(node, pos) {
return !isUndefined(node.style[pos]);
}
2014-04-16 15:26:15 -07:00
function getPosition(node, pos) {
if (pos in node.style) {
return node.style[pos];
}
return 0;
}
2014-04-16 16:31:38 -07:00
// If both left and right are defined, then use left. Otherwise return
// +left or -right depending on which is defined.
function getRelativePosition(node, axis) {
if (leading[axis] in node.style) {
return getPosition(node, leading[axis]);
}
return -getPosition(node, trailing[axis]);
}
2014-04-06 10:19:53 -07:00
var leading = {
row: 'left',
column: 'top'
};
var trailing = {
row: 'right',
column: 'bottom'
};
2014-03-31 11:09:33 -07:00
var pos = {
row: 'left',
column: 'top'
};
var dim = {
row: 'width',
column: 'height'
};
2014-04-22 13:18:05 -07:00
function fmaxf(a, b) {
if (a > b) {
return a;
}
return b;
}
2014-04-18 10:08:16 -07:00
var CSS_FLEX_DIRECTION_ROW = 'row';
var CSS_FLEX_DIRECTION_COLUMN = 'column';
var CSS_JUSTIFY_FLEX_START = 'flex-start';
var CSS_JUSTIFY_CENTER = 'center';
var CSS_JUSTIFY_FLEX_END = 'flex-end';
var CSS_JUSTIFY_SPACE_BETWEEN = 'space-between';
var CSS_JUSTIFY_SPACE_AROUND = 'space-around';
var CSS_ALIGN_FLEX_START = 'flex-start';
var CSS_ALIGN_CENTER = 'center';
var CSS_ALIGN_FLEX_END = 'flex-end';
var CSS_ALIGN_STRETCH = 'stretch';
2014-04-22 13:18:05 -07:00
var CSS_POSITION_RELATIVE = 'relative';
var CSS_POSITION_ABSOLUTE = 'absolute';
2014-04-18 11:53:28 -07:00
function layoutNode(node) {
var/*css_flex_direction_t*/ mainAxis = getFlexDirection(node);
2014-04-22 13:18:05 -07:00
var/*css_flex_direction_t*/ crossAxis = mainAxis === CSS_FLEX_DIRECTION_ROW ?
2014-04-18 11:53:28 -07:00
CSS_FLEX_DIRECTION_COLUMN :
CSS_FLEX_DIRECTION_ROW;
2014-03-31 11:09:33 -07:00
2014-04-18 11:53:28 -07:00
var/*bool*/ mainDimInStyle = isDimDefined(node, mainAxis);
if (isUndefined(node.layout[dim[mainAxis]]) && mainDimInStyle) {
2014-04-22 13:18:05 -07:00
node.layout[dim[mainAxis]] = fmaxf(
node.style[dim[mainAxis]],
getPaddingAndBorderAxis(node, mainAxis)
);
2014-04-18 11:53:28 -07:00
}
2014-04-18 11:53:28 -07:00
var/*bool*/ crossDimInStyle = isDimDefined(node, crossAxis);
if (isUndefined(node.layout[dim[crossAxis]]) && crossDimInStyle) {
2014-04-22 13:18:05 -07:00
node.layout[dim[crossAxis]] = fmaxf(
node.style[dim[crossAxis]],
getPaddingAndBorderAxis(node, crossAxis)
);
2014-04-18 11:53:28 -07:00
}
2014-04-18 11:53:28 -07:00
var/*float*/ mainContentDim = 0;
2014-04-18 13:17:47 -07:00
var/*int*/ flexibleChildrenCount = 0;
var/*int*/ absoluteChildrenCount = 0;
2014-04-18 13:17:47 -07:00
for (var/*int*/ i = 0; i < node.children.length; ++i) {
2014-04-18 11:53:28 -07:00
var/*css_node_t**/ child = node.children[i];
2014-04-22 10:02:33 -07:00
if (isUndefined(node.layout[dim[mainAxis]]) ||
2014-04-22 13:18:05 -07:00
getPositionType(child) === CSS_POSITION_ABSOLUTE ||
2014-04-22 10:02:33 -07:00
!getFlex(child)) {
2014-04-18 11:53:28 -07:00
layoutNode(child);
2014-04-22 13:18:05 -07:00
if (getPositionType(child) === CSS_POSITION_RELATIVE) {
mainContentDim += getDimWithMargin(child, mainAxis);
} else {
absoluteChildrenCount++;
}
2014-04-18 11:53:28 -07:00
} else {
flexibleChildrenCount++;
mainContentDim += getPaddingAndBorderAxis(child, mainAxis) + getMarginAxis(child, mainAxis);
2014-04-18 11:53:28 -07:00
}
2014-04-18 10:37:01 -07:00
}
2014-04-05 22:23:00 -07:00
2014-04-18 11:53:28 -07:00
var/*float*/ leadingMainDim = 0;
var/*float*/ betweenMainDim = 0;
if (!isUndefined(node.layout[dim[mainAxis]])) {
var/*float*/ remainingMainDim = node.layout[dim[mainAxis]] -
getPaddingAndBorderAxis(node, mainAxis) -
2014-04-18 11:53:28 -07:00
mainContentDim;
if (flexibleChildrenCount) {
var/*float*/ flexibleMainDim = remainingMainDim / flexibleChildrenCount;
2014-04-22 09:56:48 -07:00
if (flexibleMainDim < 0) {
flexibleMainDim = 0;
}
2014-04-18 13:17:47 -07:00
for (var/*int*/ i = 0; i < node.children.length; ++i) {
2014-04-18 11:53:28 -07:00
var/*css_node_t**/ child = node.children[i];
2014-04-22 13:18:05 -07:00
if (getPositionType(child) === CSS_POSITION_RELATIVE && getFlex(child)) {
child.layout[dim[mainAxis]] = flexibleMainDim + getPaddingAndBorderAxis(child, mainAxis);
2014-04-18 11:53:28 -07:00
layoutNode(child);
}
}
} else {
var/*css_justify_t*/ justifyContent = getJustifyContent(node);
2014-04-22 13:18:05 -07:00
if (justifyContent === CSS_JUSTIFY_FLEX_START) {
2014-04-18 11:53:28 -07:00
// Do nothing
2014-04-22 13:18:05 -07:00
} else if (justifyContent === CSS_JUSTIFY_CENTER) {
2014-04-18 11:53:28 -07:00
leadingMainDim = remainingMainDim / 2;
2014-04-22 13:18:05 -07:00
} else if (justifyContent === CSS_JUSTIFY_FLEX_END) {
2014-04-18 11:53:28 -07:00
leadingMainDim = remainingMainDim;
2014-04-22 13:18:05 -07:00
} else if (justifyContent === CSS_JUSTIFY_SPACE_BETWEEN) {
betweenMainDim = remainingMainDim / (node.children.length - absoluteChildrenCount - 1);
2014-04-22 13:18:05 -07:00
} else if (justifyContent === CSS_JUSTIFY_SPACE_AROUND) {
betweenMainDim = remainingMainDim / (node.children.length - absoluteChildrenCount);
2014-04-18 11:53:28 -07:00
leadingMainDim = betweenMainDim / 2;
2014-04-06 17:39:30 -07:00
}
2014-04-18 10:37:01 -07:00
}
2014-04-06 17:39:30 -07:00
}
2014-03-31 11:09:33 -07:00
2014-04-18 11:53:28 -07:00
var/*float*/ crossDim = 0;
var/*float*/ mainPos = getPaddingAndBorder(node, leading[mainAxis]) + leadingMainDim;
2014-04-18 13:17:47 -07:00
for (var/*int*/ i = 0; i < node.children.length; ++i) {
2014-04-18 11:53:28 -07:00
var/*css_node_t**/ child = node.children[i];
2014-04-22 13:18:05 -07:00
if (getPositionType(child) === CSS_POSITION_ABSOLUTE && isPosDefined(child, leading[mainAxis])) {
2014-04-21 18:45:01 -07:00
child.layout[pos[mainAxis]] = getPosition(child, leading[mainAxis]) +
getBorder(node, leading[mainAxis]) +
2014-04-21 18:45:01 -07:00
getMargin(child, leading[mainAxis]);
2014-04-21 18:34:28 -07:00
} else {
child.layout[pos[mainAxis]] += mainPos;
}
2014-04-22 13:18:05 -07:00
if (getPositionType(child) === CSS_POSITION_RELATIVE) {
mainPos += getDimWithMargin(child, mainAxis) + betweenMainDim;
2014-04-18 11:53:28 -07:00
if (!isUndefined(child.layout[dim[crossAxis]])) {
var/*float*/ childCrossDim = getDimWithMargin(child, crossAxis);
if (childCrossDim > crossDim) {
crossDim = childCrossDim;
}
2014-04-18 11:53:28 -07:00
}
2014-04-09 19:15:46 -07:00
}
2014-04-18 09:59:20 -07:00
}
mainPos += getPaddingAndBorder(node, trailing[mainAxis]);
crossDim += getPaddingAndBorderAxis(node, crossAxis);
2014-04-16 16:12:24 -07:00
2014-04-18 11:53:28 -07:00
if (isUndefined(node.layout[dim[mainAxis]]) && !mainDimInStyle) {
node.layout[dim[mainAxis]] = fmaxf(mainPos, getPaddingAndBorderAxis(node, mainAxis));
2014-04-18 11:53:28 -07:00
}
if (isUndefined(node.layout[dim[crossAxis]])) {
node.layout[dim[crossAxis]] = fmaxf(crossDim, getPaddingAndBorderAxis(node, crossAxis));
2014-04-18 11:53:28 -07:00
}
2014-04-18 13:17:47 -07:00
for (var/*int*/ i = 0; i < node.children.length; ++i) {
2014-04-18 11:53:28 -07:00
var/*css_node_t**/ child = node.children[i];
2014-04-21 14:58:44 -07:00
2014-04-22 13:18:05 -07:00
if (getPositionType(child) === CSS_POSITION_RELATIVE) {
2014-04-21 14:58:44 -07:00
var/*css_align_t*/ alignItem = getAlignItem(node, child);
var/*float*/ remainingCrossDim = node.layout[dim[crossAxis]] -
getDimWithMargin(child, crossAxis) -
getPaddingAndBorderAxis(node, crossAxis);
2014-04-21 14:58:44 -07:00
var/*float*/ leadingCrossDim = getPaddingAndBorder(node, leading[crossAxis]);
2014-04-22 13:18:05 -07:00
if (alignItem === CSS_ALIGN_FLEX_START) {
2014-04-21 14:58:44 -07:00
// Do nothing
2014-04-22 13:18:05 -07:00
} else if (alignItem === CSS_ALIGN_CENTER) {
2014-04-21 14:58:44 -07:00
leadingCrossDim += remainingCrossDim / 2;
2014-04-22 13:18:05 -07:00
} else if (alignItem === CSS_ALIGN_FLEX_END) {
2014-04-21 14:58:44 -07:00
leadingCrossDim += remainingCrossDim;
2014-04-22 13:18:05 -07:00
} else if (alignItem === CSS_ALIGN_STRETCH) {
if (!isDimDefined(child, crossAxis)) {
child.layout[dim[crossAxis]] = fmaxf(
node.layout[dim[crossAxis]] -
getPaddingAndBorderAxis(node, crossAxis) -
getMarginAxis(child, crossAxis),
getPaddingAndBorderAxis(child, crossAxis)
);
}
2014-04-21 14:58:44 -07:00
}
child.layout[pos[crossAxis]] += leadingCrossDim;
} else {
2014-04-21 18:40:00 -07:00
if (isPosDefined(child, leading[crossAxis])) {
2014-04-21 18:45:57 -07:00
child.layout[pos[crossAxis]] = getPosition(child, leading[crossAxis]) +
getBorder(node, leading[crossAxis]) +
2014-04-21 18:45:57 -07:00
getMargin(child, leading[crossAxis]);
2014-04-21 18:40:00 -07:00
} else {
child.layout[pos[crossAxis]] += getPaddingAndBorder(node, leading[crossAxis]);
2014-04-21 18:40:00 -07:00
}
2014-04-18 11:53:28 -07:00
}
2014-04-18 09:59:20 -07:00
}
2014-04-18 11:53:28 -07:00
node.layout[leading[mainAxis]] += getMargin(node, leading[mainAxis]) +
getRelativePosition(node, mainAxis);
node.layout[leading[crossAxis]] += getMargin(node, leading[crossAxis]) +
getRelativePosition(node, crossAxis);
2014-03-30 19:18:06 -07:00
}
2014-03-30 19:33:24 -07:00
2014-04-18 11:53:28 -07:00
var fn = function(node) {
fillNodes(node);
layoutNode(node);
return extractNodes(node);
};
fn.layoutNode = layoutNode;
return fn;
})();