Skip to content
This repository has been archived by the owner on Jun 15, 2019. It is now read-only.

Commit

Permalink
Fill in table
Browse files Browse the repository at this point in the history
  • Loading branch information
jamestalmage committed Dec 15, 2014
1 parent 2b0e131 commit 5ca7432
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 3 deletions.
47 changes: 44 additions & 3 deletions src/table-layout.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ var Cell = require('./cell');

function makeTableLayout(rows){
var cellRows = generateCells(rows);
fillInLayout(cellRows);
expandCells(cellRows);
return cellRows;
}

function fillInLayout(cellRows){
function expandCells(cellRows){
for(var rowIndex = cellRows.length-1; rowIndex >= 0; rowIndex--){
var cellColumns = cellRows[rowIndex];
for(var columnIndex = 0; columnIndex < cellColumns.length; columnIndex++){
Expand All @@ -28,6 +28,46 @@ function fillInLayout(cellRows){
}
}

function fillInTable(rows){
var height = rows.length;
var width = maxWidth(rows);
for(var rowIndex = 0; rowIndex < height; rowIndex++){
var row = rows[rowIndex];
for(var colIndex = 0; colIndex < width; colIndex++){
var cell = row[colIndex];
if(!cell){
var i = colIndex+1;
while(i < width && !row[i]){
row[i] = new Cell.NoOpCell();
i++;
}
var j = rowIndex + 1;
while(j < height && allBlank(rows[j],colIndex,i)){
for(var k = colIndex+1; k < i; k++){
rows[j][k] = new Cell.NoOpCell();
}
j++;
}
var rowSpan = j - rowIndex;
var blankCell = new Cell(
{colSpan:i-colIndex,rowSpan: rowSpan}
);
row[colIndex] = blankCell;
for(var n = 1; n < rowSpan; n++){
rows[rowIndex+n][colIndex] = new Cell.RowSpanCell(blankCell);
}
}
}
}
}

function allBlank(row,from,to){
for(var i = from; i < to; i++){
if(row[i]) return false;
}
return true;
}

function generateCells(rows){
return _.map(rows,function(row){
return _.map(row,function(cell){
Expand All @@ -44,5 +84,6 @@ function maxWidth(rows){

module.exports = {
makeTableLayout:makeTableLayout,
maxWidth:maxWidth
maxWidth:maxWidth,
fillInTable:fillInTable
};
62 changes: 62 additions & 0 deletions test/table-layout-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ describe('tableLayout', function () {
var tableLayout = require('../src/table-layout');
var makeTableLayout = tableLayout.makeTableLayout;
var maxWidth = tableLayout.maxWidth;
var fillInTable = tableLayout.fillInTable;
var chai = require('chai');
var expect = chai.expect;
var _ = require('lodash');
Expand Down Expand Up @@ -100,6 +101,67 @@ describe('tableLayout', function () {
expect(maxWidth([[1],[1,2],[1,2,3,4,5]])).to.equal(5);
});

describe('fillInTable',function(){
function mc(opts){
return new Cell(opts);
}

it('will blank out individual cells',function(){
var cells = [
[null, mc('a')],
[mc('b'), null]
];
fillInTable(cells);

checkLayout(cells,[
['', 'a'],
['b', '']
]);
});

it('will autospan to the right',function(){
var cells = [
[null, null],
[null, mc('a')]
];
fillInTable(cells);

checkLayout(cells,[
[{content:'',colSpan:2}, null],
['', 'a']
]);
});

it('will autospan down',function(){
var cells = [
[null, mc('a')],
[null, null]
];
fillInTable(cells);

checkLayout(cells,[
[{content:'',rowSpan:2}, 'a'],
[{spannerFor:[0,0]}, '']
]);
});

it('will autospan right and down',function(){
var cells = [
[null, null, mc('a')],
[null, null, null],
[null, mc('b'), null]
];
fillInTable(cells);

checkLayout(cells,[
[{content:'',colSpan:2, rowSpan:2}, null, 'a'],
[{spannerFor:[0,0]}, null, {content:'', colSpan:1, rowSpan:2}],
['','b',{spannerFor:[1,2]}]
]);
});

});

/**
* Provides a shorthand for validating a table of cells.
* To pass, both arrays must have the same dimensions, and each cell in `actualRows` must
Expand Down

0 comments on commit 5ca7432

Please sign in to comment.