Skip to content

Allow multiple rows inside single table #34

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions lib/componentFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,29 @@ module.exports = function(element) {
// <column>
case this.components.columns:
return this.makeColumn(element, 'columns');

// <column2>
case this.components.columns2:
return this.makeColumn2(element, 'columns2');

// <innerrow>
case this.components.innerrow:
var classes = ['innerrow'];
if (element.attr('class')) {
classes = classes.concat(element.attr('class').split(' '));
}

return format('<tr class="%s">%s</tr>', classes.join(' '), inner);

// <row2>
case this.components.row2:
var classes = ['row2'];
if (element.attr('class')) {
classes = classes.concat(element.attr('class').split(' '));
}

return format('<table class="%s"><tbody>%s</tbody></table>', classes.join(' '), inner);

// <row>
case this.components.row:
var classes = ['row'];
Expand Down
7 changes: 6 additions & 1 deletion lib/inky.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ function Inky(options) {
menuItem: 'item',
center: 'center',
spacer: 'spacer',
wrapper: 'wrapper'
wrapper: 'wrapper',
columns2: 'columns2',
innerrow: 'innerrow',
row2: 'row2'
}, options.components || {});

// Column count for grid
Expand Down Expand Up @@ -62,3 +65,5 @@ Inky.prototype.releaseTheKraken = function($) {
Inky.prototype.componentFactory = require('./componentFactory');

Inky.prototype.makeColumn = require('./makeColumn');

Inky.prototype.makeColumn2 = require('./makeColumn2');
57 changes: 57 additions & 0 deletions lib/makeColumn2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
var format = require('util').format;
var multiline = require('multiline');
var $ = require('cheerio');

/**
* Returns output for column elements.
* @todo This could be refactored to handle both cols and subcols.
* @param {string} col - Column to format.
* @returns {string} Column HTML.
*/
module.exports = function(col) {
var output = '';
var inner = $(col).html();
var classes = [];
var expander = '';

// Add 1 to include current column
var colCount = $(col).siblings().length + 1;

// Inherit classes from the <column> tag
if ($(col).attr('class')) {
classes = classes.concat($(col).attr('class').split(' '));
}

// Check for sizes. If no attribute is provided, default to small-12. Divide evenly for large columns
var smallSize = $(col).attr('small') || this.columnCount;
var largeSize = $(col).attr('large') || $(col).attr('small') || Math.floor(this.columnCount / colCount);

classes.push(format('small-%s', smallSize));
classes.push(format('large-%s', largeSize));

// Add the basic "columns" class also
classes.push('columns');

// Determine if it's the first or last column, or both
if (!$(col).prev(this.components.columns).length) classes.push('first');
if (!$(col).next(this.components.columns).length) classes.push('last');

// If the column contains a nested row, the .expander class should not be used
// The == on the first check is because we're comparing a string pulled from $.attr() to a number
if (largeSize == this.columnCount && col.find('.innerrow, innerrow').length === 0) {
expander = '\n<th class="expander"></th>';
}

// Final HTML output
output = multiline(function() {/*
<th class="%s">
<table>
<tr>
<th>%s</th>%s
</tr>
</table>
</th>
*/});

return format(output, classes.join(' '), inner, expander);
}