Updated the public API to no-longer extract nodes

This commit is contained in:
Colin Eberhardt
2015-08-13 08:39:28 +01:00
parent 0de0186fbd
commit f9bb74aedf
9 changed files with 121 additions and 88 deletions

View File

@@ -11,22 +11,68 @@ The JavaScript version has been implemented in a way that can be easily transpil
Usage Usage
----- -----
A single function `computeLayout` is exposed and A single function `computeLayout` is exposed that
- takes a tree of nodes: `{ style: { ... }, children: [ nodes ] }` - takes a tree of nodes: `{ style: { ... }, children: [ nodes ] }`
- returns a tree of rectangles: `{ width: ..., height: ..., top: ..., left: ..., children: [ rects ] }` - computes the layout and writes it back to the node tree.
For example, For example,
```javascript ```javascript
computeLayout( // create an initial tree of nodes
{style: {padding: 50}, children: [ var nodeTree = {
{style: {padding: 10, alignSelf: 'stretch'}} "style": {
]} "padding": 50
); },
// => "children": [
{width: 120, height: 120, top: 0, left: 0, children: [ {
{width: 20, height: 20, top: 50, left: 50} "style": {
]} "padding": 10,
"alignSelf": "stretch"
}
}
]
};
// compute the layout
computeLayout(tree);
// the layout information is written back to the node tree, with
// each node now having a layout property:
// JSON.stringify(tree, null, 2);
{
"style": {
"padding": 50
},
"children": [
{
"style": {
"padding": 10,
"alignSelf": "stretch"
},
"layout": {
"width": 20,
"height": 20,
"top": 50,
"left": 50,
"right": 50,
"bottom": 50,
"direction": "ltr"
},
"children": [],
"lineIndex": 0
}
],
"layout": {
"width": 120,
"height": 120,
"top": 0,
"left": 0,
"right": 0,
"bottom": 0,
"direction": "ltr"
}
}
``` ```
Supported Attributes Supported Attributes

BIN
dist/css-layout.jar vendored

Binary file not shown.

51
dist/css-layout.js vendored
View File

@@ -99,42 +99,33 @@ var computeLayout = (function() {
return 0; return 0;
} }
// When transpiled to Java / C the node type has layout, children and style
// properties. For the JavaScript version this function adds these properties
// if they don't already exist.
function fillNodes(node) { function fillNodes(node) {
node.layout = { if (!node.layout) {
width: undefined, node.layout = {
height: undefined, width: undefined,
top: 0, height: undefined,
left: 0, top: 0,
right: 0, left: 0,
bottom: 0 right: 0,
}; bottom: 0
};
}
if (!node.style) { if (!node.style) {
node.style = {}; node.style = {};
} }
if (!node.children || node.style.measure) { if (!node.children) {
node.children = []; node.children = [];
} }
node.children.forEach(fillNodes); node.children.forEach(fillNodes);
return node; return node;
} }
function extractNodes(node) {
var layout = node.layout;
delete node.layout;
if (node.children && node.children.length > 0) {
layout.children = node.children.map(extractNodes);
} else {
delete node.children;
}
delete layout.right;
delete layout.bottom;
delete layout.direction;
return layout;
}
function getPositiveSpacing(node, type, suffix, locations) { function getPositiveSpacing(node, type, suffix, locations) {
for (var i = 0; i < locations.length; ++i) { for (var i = 0; i < locations.length; ++i) {
var location = locations[i]; var location = locations[i];
@@ -1000,19 +991,19 @@ var computeLayout = (function() {
return { return {
computeLayout: layoutNode, computeLayout: layoutNode,
fillNodes: fillNodes, fillNodes: fillNodes
extractNodes: extractNodes
}; };
})(); })();
// This module export is only used for the purposes of unit testing this file. When
// the library is packaged this file is included within css-layout.js which forms
// the public API.
if (typeof exports === 'object') { if (typeof exports === 'object') {
module.exports = computeLayout; module.exports = computeLayout;
} }
return function(node) { return function(node) {
node = computeLayout.fillNodes(node); computeLayout.fillNodes(node);
computeLayout.computeLayout(node); computeLayout.computeLayout(node);
node = computeLayout.extractNodes(node);
return node;
}; };
})); }));

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@@ -114,10 +114,25 @@ var layoutTestUtils = (function() {
if (typeof computeLayout === 'object') { if (typeof computeLayout === 'object') {
var fillNodes = computeLayout.fillNodes; var fillNodes = computeLayout.fillNodes;
var extractNodes = computeLayout.extractNodes;
var realComputeLayout = computeLayout.computeLayout; var realComputeLayout = computeLayout.computeLayout;
} }
function extractNodes(node) {
var layout = node.layout;
delete node.layout;
if (node.children && node.children.length > 0) {
layout.children = node.children.map(extractNodes);
} else {
delete node.children;
}
delete layout.right;
delete layout.bottom;
delete layout.direction;
return layout;
}
function roundLayout(layout) { function roundLayout(layout) {
// Chrome rounds all the numbers with a precision of 1/64 // Chrome rounds all the numbers with a precision of 1/64
// Reproduce the same behavior // Reproduce the same behavior

View File

@@ -80,42 +80,33 @@ var computeLayout = (function() {
return 0; return 0;
} }
// When transpiled to Java / C the node type has layout, children and style
// properties. For the JavaScript version this function adds these properties
// if they don't already exist.
function fillNodes(node) { function fillNodes(node) {
node.layout = { if (!node.layout) {
width: undefined, node.layout = {
height: undefined, width: undefined,
top: 0, height: undefined,
left: 0, top: 0,
right: 0, left: 0,
bottom: 0 right: 0,
}; bottom: 0
};
}
if (!node.style) { if (!node.style) {
node.style = {}; node.style = {};
} }
if (!node.children || node.style.measure) { if (!node.children) {
node.children = []; node.children = [];
} }
node.children.forEach(fillNodes); node.children.forEach(fillNodes);
return node; return node;
} }
function extractNodes(node) {
var layout = node.layout;
delete node.layout;
if (node.children && node.children.length > 0) {
layout.children = node.children.map(extractNodes);
} else {
delete node.children;
}
delete layout.right;
delete layout.bottom;
delete layout.direction;
return layout;
}
function getPositiveSpacing(node, type, suffix, locations) { function getPositiveSpacing(node, type, suffix, locations) {
for (var i = 0; i < locations.length; ++i) { for (var i = 0; i < locations.length; ++i) {
var location = locations[i]; var location = locations[i];
@@ -981,11 +972,13 @@ var computeLayout = (function() {
return { return {
computeLayout: layoutNode, computeLayout: layoutNode,
fillNodes: fillNodes, fillNodes: fillNodes
extractNodes: extractNodes
}; };
})(); })();
// This module export is only used for the purposes of unit testing this file. When
// the library is packaged this file is included within css-layout.js which forms
// the public API.
if (typeof exports === 'object') { if (typeof exports === 'object') {
module.exports = computeLayout; module.exports = computeLayout;
} }

View File

@@ -31,20 +31,10 @@ describe('Javascript Only', function() {
]} ]}
); );
}); });
it('should pull out just the layout object from root', function() { it('should not replace user-provided layout information', function() {
testExtractNodes( testFillNodes(
{layout: {width: undefined, height: undefined, top: 0, left: 0}}, {layout: {width: 200}},
{width: undefined, height: undefined, top: 0, left: 0} {layout: {width: 200}, style: {}, children: []}
);
});
it('should pull out just the layout object from root and children', function() {
testExtractNodes(
{layout: {width: undefined, height: undefined, top: 0, left: 0}, children: [
{layout: {width: undefined, height: undefined, top: 0, left: 0}}
]},
{width: undefined, height: undefined, top: 0, left: 0, children: [
{width: undefined, height: undefined, top: 0, left: 0}
]}
); );
}); });
}); });

View File

@@ -20,9 +20,7 @@
// @@include('./Layout.js') // @@include('./Layout.js')
return function(node) { return function(node) {
node = computeLayout.fillNodes(node); computeLayout.fillNodes(node);
computeLayout.computeLayout(node); computeLayout.computeLayout(node);
node = computeLayout.extractNodes(node);
return node;
}; };
})); }));