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
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-04-18 09:56:57 -07:00
|
|
|
function isUndefined(value) {
|
2014-04-22 13:18:05 -07:00
|
|
|
return value === undefined;
|
2014-04-18 09:56:57 -07:00
|
|
|
}
|
|
|
|
|
2014-04-16 12:51:55 -07:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2014-04-16 12:51:55 -07:00
|
|
|
function getPadding(node, location) {
|
2014-04-22 11:51:04 -07:00
|
|
|
return getPositiveSpacing(node, 'padding', '', location);
|
2014-04-22 11:40:31 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
function getBorder(node, location) {
|
2014-04-22 11:51:04 -07:00
|
|
|
return getPositiveSpacing(node, 'border', 'Width', location);
|
2014-04-22 11:40:31 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
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]);
|
|
|
|
}
|
|
|
|
|
2014-04-22 11:40:31 -07:00
|
|
|
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';
|
|
|
|
}
|
|
|
|
|
2014-04-06 21:34:41 -07:00
|
|
|
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';
|
|
|
|
}
|
|
|
|
|
2014-04-21 14:29:17 -07:00
|
|
|
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-23 14:18:18 -07:00
|
|
|
function isFlex(node) {
|
|
|
|
return getPositionType(node) === CSS_POSITION_RELATIVE && getFlex(node);
|
|
|
|
}
|
|
|
|
|
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-25 13:28:56 -07:00
|
|
|
// When the user specifically sets a value for width or height
|
|
|
|
function setDimensionFromStyle(node, axis) {
|
2014-04-23 15:03:17 -07:00
|
|
|
// The parent already computed us a width or height. We just skip it
|
|
|
|
if (!isUndefined(node.layout[dim[axis]])) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// We only run if there's a width or height defined
|
|
|
|
if (!isDimDefined(node, axis)) {
|
|
|
|
return;
|
2014-04-23 14:21:24 -07:00
|
|
|
}
|
2014-04-23 15:03:17 -07:00
|
|
|
|
|
|
|
// The dimensions can never be smaller than the padding and border
|
|
|
|
node.layout[dim[axis]] = fmaxf(
|
|
|
|
node.style[dim[axis]],
|
|
|
|
getPaddingAndBorderAxis(node, axis)
|
|
|
|
);
|
2014-04-23 14:21:24 -07:00
|
|
|
}
|
|
|
|
|
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-27 12:37:26 -07:00
|
|
|
return function layoutNode(node) {
|
2014-04-18 11:53:28 -07:00
|
|
|
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-25 13:28:56 -07:00
|
|
|
// Handle width and height style attributes
|
|
|
|
setDimensionFromStyle(node, mainAxis);
|
|
|
|
setDimensionFromStyle(node, crossAxis);
|
2014-04-06 21:34:41 -07:00
|
|
|
|
2014-04-25 13:28:56 -07:00
|
|
|
// The position is set by the parent, but we need to complete it with a
|
|
|
|
// delta composed of the margin and left/top/right/bottom
|
|
|
|
node.layout[leading[mainAxis]] += getMargin(node, leading[mainAxis]) +
|
|
|
|
getRelativePosition(node, mainAxis);
|
|
|
|
node.layout[leading[crossAxis]] += getMargin(node, leading[crossAxis]) +
|
|
|
|
getRelativePosition(node, crossAxis);
|
2014-04-23 15:03:17 -07:00
|
|
|
|
2014-04-26 12:46:21 -07:00
|
|
|
if ('measure' in node.style) {
|
2014-04-26 19:02:16 -07:00
|
|
|
var width;
|
2014-04-28 11:06:29 -07:00
|
|
|
if (isDimDefined(node, CSS_FLEX_DIRECTION_ROW)) {
|
2014-04-26 19:02:16 -07:00
|
|
|
width = node.style.width;
|
2014-04-28 12:34:04 -07:00
|
|
|
} else if (getPositionType(node) === CSS_POSITION_ABSOLUTE) {
|
2014-04-26 19:02:16 -07:00
|
|
|
width = 'shrink';
|
|
|
|
} else {
|
|
|
|
width = 'grow';
|
|
|
|
}
|
|
|
|
var dimensions = node.style.measure(width);
|
2014-04-28 11:06:29 -07:00
|
|
|
if (!isDimDefined(node, CSS_FLEX_DIRECTION_ROW)) {
|
|
|
|
node.layout.width = dimensions.width +
|
|
|
|
getPaddingAndBorderAxis(node, CSS_FLEX_DIRECTION_ROW);
|
2014-04-26 17:11:22 -07:00
|
|
|
}
|
2014-04-28 11:06:29 -07:00
|
|
|
if (!isDimDefined(node, CSS_FLEX_DIRECTION_COLUMN)) {
|
|
|
|
node.layout.height = dimensions.height +
|
|
|
|
getPaddingAndBorderAxis(node, CSS_FLEX_DIRECTION_COLUMN);
|
2014-04-26 17:11:22 -07:00
|
|
|
}
|
2014-04-26 12:16:27 -07:00
|
|
|
return;
|
|
|
|
}
|
2014-04-23 15:03:17 -07:00
|
|
|
|
2014-04-24 10:03:05 -07:00
|
|
|
// <Loop A> Layout non flexible children and count children by type
|
2014-04-23 15:03:17 -07:00
|
|
|
|
|
|
|
// mainContentDim is accumulation of the dimensions and margin of all the
|
|
|
|
// non flexible children. This will be used in order to either set the
|
|
|
|
// dimensions of the node if none already exist, or to compute the
|
|
|
|
// remaining space left for the flexible children.
|
2014-04-18 11:53:28 -07:00
|
|
|
var/*float*/ mainContentDim = 0;
|
2014-04-23 15:03:17 -07:00
|
|
|
|
|
|
|
// There are three kind of children, non flexible, flexible and absolute.
|
|
|
|
// We need to know how many there are in order to distribute the space.
|
2014-04-18 13:17:47 -07:00
|
|
|
var/*int*/ flexibleChildrenCount = 0;
|
2014-04-23 15:03:17 -07:00
|
|
|
var/*int*/ nonFlexibleChildrenCount = 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-23 15:03:17 -07:00
|
|
|
|
|
|
|
// It only makes sense to consider a child flexible if we have a computed
|
|
|
|
// dimension for the node.
|
|
|
|
if (!isUndefined(node.layout[dim[mainAxis]]) && isFlex(child)) {
|
|
|
|
flexibleChildrenCount++;
|
|
|
|
|
|
|
|
// Even if we don't know its exact size yet, we already know the padding,
|
|
|
|
// border and margin. We'll use this partial information to compute the
|
|
|
|
// remaining space.
|
|
|
|
mainContentDim += getPaddingAndBorderAxis(child, mainAxis) +
|
|
|
|
getMarginAxis(child, mainAxis);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
// This is the main recursive call. We layout non flexible children.
|
2014-04-18 11:53:28 -07:00
|
|
|
layoutNode(child);
|
2014-04-23 15:03:17 -07:00
|
|
|
|
|
|
|
// Absolute positioned elements do not take part of the layout, so we
|
|
|
|
// don't use them to compute mainContentDim
|
2014-04-22 13:18:05 -07:00
|
|
|
if (getPositionType(child) === CSS_POSITION_RELATIVE) {
|
2014-04-23 15:03:17 -07:00
|
|
|
nonFlexibleChildrenCount++;
|
|
|
|
// At this point we know the final size and margin of the element.
|
2014-04-21 14:29:17 -07:00
|
|
|
mainContentDim += getDimWithMargin(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-26 12:16:27 -07:00
|
|
|
|
2014-04-24 10:03:05 -07:00
|
|
|
// <Loop B> Layout flexible children and allocate empty space
|
|
|
|
|
|
|
|
// In order to position the elements in the main axis, we have two
|
|
|
|
// controls. The space between the beginning and the first element
|
|
|
|
// and the space between each two elements.
|
2014-04-18 11:53:28 -07:00
|
|
|
var/*float*/ leadingMainDim = 0;
|
|
|
|
var/*float*/ betweenMainDim = 0;
|
2014-04-24 10:03:05 -07:00
|
|
|
|
|
|
|
// If the dimensions of the current node is defined by its children, they
|
|
|
|
// are all going to be packed together and we don't need to compute
|
|
|
|
// anything.
|
2014-04-18 11:53:28 -07:00
|
|
|
if (!isUndefined(node.layout[dim[mainAxis]])) {
|
2014-04-24 10:03:05 -07:00
|
|
|
|
|
|
|
// The remaining available space that's needs to be allocated
|
2014-04-18 11:53:28 -07:00
|
|
|
var/*float*/ remainingMainDim = node.layout[dim[mainAxis]] -
|
2014-04-22 11:40:31 -07:00
|
|
|
getPaddingAndBorderAxis(node, mainAxis) -
|
2014-04-18 11:53:28 -07:00
|
|
|
mainContentDim;
|
|
|
|
|
2014-04-24 10:03:05 -07:00
|
|
|
// If there are flexible children in the mix, they are going to fill the
|
|
|
|
// remaining space
|
2014-04-18 11:53:28 -07:00
|
|
|
if (flexibleChildrenCount) {
|
|
|
|
var/*float*/ flexibleMainDim = remainingMainDim / flexibleChildrenCount;
|
2014-04-24 10:03:05 -07:00
|
|
|
|
|
|
|
// The non flexible children can overflow the container, in this case
|
|
|
|
// we should just assume that there is no space available.
|
2014-04-22 09:56:48 -07:00
|
|
|
if (flexibleMainDim < 0) {
|
|
|
|
flexibleMainDim = 0;
|
|
|
|
}
|
2014-04-24 10:03:05 -07:00
|
|
|
// We iterate over the full array and only apply the action on flexible
|
|
|
|
// children. This is faster than actually allocating a new array that
|
|
|
|
// contains only flexible children.
|
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-23 14:18:18 -07:00
|
|
|
if (isFlex(child)) {
|
2014-04-24 10:03:05 -07:00
|
|
|
// At this point we know the final size of the element in the main
|
|
|
|
// dimension
|
|
|
|
child.layout[dim[mainAxis]] = flexibleMainDim +
|
|
|
|
getPaddingAndBorderAxis(child, mainAxis);
|
|
|
|
|
|
|
|
// And we recursively call the layout algorithm for this child
|
2014-04-18 11:53:28 -07:00
|
|
|
layoutNode(child);
|
|
|
|
}
|
|
|
|
}
|
2014-04-24 10:03:05 -07:00
|
|
|
|
|
|
|
// We use justifyContent to figure out how to allocate the remaining
|
|
|
|
// space available
|
2014-04-18 11:53:28 -07:00
|
|
|
} 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) {
|
2014-04-24 10:03:05 -07:00
|
|
|
betweenMainDim = remainingMainDim /
|
|
|
|
(flexibleChildrenCount + nonFlexibleChildrenCount - 1);
|
2014-04-22 13:18:05 -07:00
|
|
|
} else if (justifyContent === CSS_JUSTIFY_SPACE_AROUND) {
|
2014-04-24 10:03:05 -07:00
|
|
|
// Space on the edges is half of the space between elements
|
|
|
|
betweenMainDim = remainingMainDim /
|
|
|
|
(flexibleChildrenCount + nonFlexibleChildrenCount);
|
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-24 17:14:55 -07:00
|
|
|
|
|
|
|
// <Loop C> Position elements in the main axis and compute dimensions
|
|
|
|
|
|
|
|
// At this point, all the children have their dimensions set. We need to
|
|
|
|
// find their position. In order to do that, we accumulate data in
|
|
|
|
// variables that are also useful to compute the total dimensions of the
|
|
|
|
// container!
|
2014-04-18 11:53:28 -07:00
|
|
|
var/*float*/ crossDim = 0;
|
2014-04-24 17:14:55 -07:00
|
|
|
var/*float*/ mainDim = leadingMainDim +
|
|
|
|
getPaddingAndBorder(node, leading[mainAxis]);
|
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-24 17:14:55 -07:00
|
|
|
|
|
|
|
if (getPositionType(child) === CSS_POSITION_ABSOLUTE &&
|
|
|
|
isPosDefined(child, leading[mainAxis])) {
|
|
|
|
// In case the child is position absolute and has left/top being
|
|
|
|
// defined, we override the position to whatever the user said
|
|
|
|
// (and margin/border).
|
2014-04-21 18:45:01 -07:00
|
|
|
child.layout[pos[mainAxis]] = getPosition(child, leading[mainAxis]) +
|
2014-04-22 11:40:31 -07:00
|
|
|
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 {
|
2014-04-24 17:14:55 -07:00
|
|
|
// If the child is position absolute (without top/left) or relative,
|
|
|
|
// we put it at the current accumulated offset.
|
|
|
|
child.layout[pos[mainAxis]] += mainDim;
|
2014-04-21 18:34:28 -07:00
|
|
|
}
|
2014-04-18 11:53:28 -07:00
|
|
|
|
2014-04-24 17:14:55 -07:00
|
|
|
// Now that we placed the element, we need to update the variables
|
|
|
|
// We only need to do that for relative elements. Absolute elements
|
|
|
|
// do not take part in that phase.
|
|
|
|
if (getPositionType(child) === CSS_POSITION_RELATIVE) {
|
|
|
|
// The main dimension is the sum of all the elements dimension plus
|
|
|
|
// the spacing.
|
|
|
|
mainDim += betweenMainDim + getDimWithMargin(child, mainAxis);
|
|
|
|
// The cross dimension is the max of the elements dimension since there
|
|
|
|
// can only be one element in that cross dimension.
|
|
|
|
crossDim = fmaxf(crossDim, getDimWithMargin(child, crossAxis));
|
2014-04-09 19:15:46 -07:00
|
|
|
}
|
2014-04-18 09:59:20 -07:00
|
|
|
}
|
2014-04-22 17:33:08 -07:00
|
|
|
|
2014-04-25 13:28:56 -07:00
|
|
|
// If the user didn't specify a width or height, and it has not been set
|
|
|
|
// by the container, then we set it via the children.
|
2014-04-24 17:14:55 -07:00
|
|
|
if (isUndefined(node.layout[dim[mainAxis]])) {
|
2014-04-25 13:28:56 -07:00
|
|
|
node.layout[dim[mainAxis]] = fmaxf(
|
|
|
|
// We're missing the last padding at this point to get the final
|
|
|
|
// dimension
|
|
|
|
mainDim + getPaddingAndBorder(node, trailing[mainAxis]),
|
|
|
|
// We can never assign a width smaller than the padding and borders
|
|
|
|
getPaddingAndBorderAxis(node, mainAxis)
|
|
|
|
);
|
2014-04-18 11:53:28 -07:00
|
|
|
}
|
2014-04-24 17:14:55 -07:00
|
|
|
|
2014-04-18 11:53:28 -07:00
|
|
|
if (isUndefined(node.layout[dim[crossAxis]])) {
|
2014-04-25 13:28:56 -07:00
|
|
|
node.layout[dim[crossAxis]] = fmaxf(
|
|
|
|
// For the cross dim, we add both sides at the end because the value
|
|
|
|
// is aggregate via a max function. Intermediate negative values
|
|
|
|
// can mess this computation otherwise
|
|
|
|
crossDim + getPaddingAndBorderAxis(node, crossAxis),
|
|
|
|
getPaddingAndBorderAxis(node, crossAxis)
|
|
|
|
);
|
2014-04-18 11:53:28 -07:00
|
|
|
}
|
|
|
|
|
2014-04-24 17:14:55 -07:00
|
|
|
|
|
|
|
// <Loop D> Position elements in the cross axis
|
|
|
|
|
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-25 15:13:18 -07:00
|
|
|
if (getPositionType(child) === CSS_POSITION_ABSOLUTE &&
|
|
|
|
isPosDefined(child, leading[crossAxis])) {
|
|
|
|
// In case the child is absolutely positionned and has a
|
|
|
|
// top/left/bottom/right being set, we override all the previously
|
|
|
|
// computed positions to set it correctly.
|
|
|
|
child.layout[pos[crossAxis]] = getPosition(child, leading[crossAxis]) +
|
|
|
|
getBorder(node, leading[crossAxis]) +
|
|
|
|
getMargin(child, leading[crossAxis]);
|
2014-04-21 14:58:44 -07:00
|
|
|
|
2014-04-25 15:13:18 -07:00
|
|
|
} else {
|
2014-04-22 11:40:31 -07:00
|
|
|
var/*float*/ leadingCrossDim = getPaddingAndBorder(node, leading[crossAxis]);
|
2014-04-25 15:13:18 -07:00
|
|
|
|
|
|
|
// For a relative children, we're either using alignItems (parent) or
|
|
|
|
// alignSelf (child) in order to determine the position in the cross axis
|
|
|
|
if (getPositionType(child) === CSS_POSITION_RELATIVE) {
|
|
|
|
var/*css_align_t*/ alignItem = getAlignItem(node, child);
|
|
|
|
if (alignItem === CSS_ALIGN_FLEX_START) {
|
|
|
|
// Do nothing
|
|
|
|
} else if (alignItem === CSS_ALIGN_STRETCH) {
|
|
|
|
// You can only stretch if the dimension has not already been set
|
|
|
|
// previously.
|
|
|
|
if (!isDimDefined(child, crossAxis)) {
|
|
|
|
child.layout[dim[crossAxis]] = fmaxf(
|
|
|
|
node.layout[dim[crossAxis]] -
|
|
|
|
getPaddingAndBorderAxis(node, crossAxis) -
|
|
|
|
getMarginAxis(child, crossAxis),
|
|
|
|
// You never want to go smaller than padding
|
|
|
|
getPaddingAndBorderAxis(child, crossAxis)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// The remaining space between the parent dimensions+padding and child
|
|
|
|
// dimensions+margin.
|
|
|
|
var/*float*/ remainingCrossDim = node.layout[dim[crossAxis]] -
|
|
|
|
getPaddingAndBorderAxis(node, crossAxis) -
|
|
|
|
getDimWithMargin(child, crossAxis);
|
|
|
|
|
|
|
|
if (alignItem === CSS_ALIGN_CENTER) {
|
|
|
|
leadingCrossDim += remainingCrossDim / 2;
|
|
|
|
} else { // CSS_ALIGN_FLEX_END
|
|
|
|
leadingCrossDim += remainingCrossDim;
|
|
|
|
}
|
2014-04-21 17:16:32 -07:00
|
|
|
}
|
2014-04-21 14:58:44 -07:00
|
|
|
}
|
2014-04-25 15:13:18 -07:00
|
|
|
|
|
|
|
// And we apply the position
|
2014-04-21 14:58:44 -07:00
|
|
|
child.layout[pos[crossAxis]] += leadingCrossDim;
|
2014-04-18 11:53:28 -07:00
|
|
|
}
|
2014-04-18 09:59:20 -07:00
|
|
|
}
|
2014-03-30 19:18:06 -07:00
|
|
|
}
|
2014-04-18 11:53:28 -07:00
|
|
|
})();
|
|
|
|
|
|
|
|
|