UNPKG

cluedin-widget

Version:

This is the project for creating and managing widgets in CluedIn.

72 lines (57 loc) 1.57 kB
var Row = function( w ) { this.total = w.size; this.widgets = [ w ]; }; Row.prototype.addWidget = function( w ) { this.widgets.push( w ); this.total += w.size; }; Row.prototype.isSpaceFree = function( size ) { if ( !size ) { return this.total <= 12; } return (this.total + size <= 12); }; var Grid = function( widgets ) { this.widgets = widgets; this.rows = []; }; Grid.prototype.numberOfFullRowsAtFirst = function() { var allTwelves = this.rows.map( function( rows ) { return rows.total === 12; } ); var result = 0; var found = false; allTwelves.forEach( function( twelve, index ) { if ( !found && !twelve ) { result = index; found = true; } } ); return result; }; Grid.prototype.addWidgetToRow = function( w ) { var self = this; if ( this.rows.length > 0 ) { this.rows.forEach( function( r, index ) { if ( r.isSpaceFree( w.size ) && !w.isAdded ) { r.addWidget( w ); w.isAdded = true; } if ( (index + 1) === ( self.rows.length ) && !w.isAdded ) { self.rows.push( new Row( w ) ); w.isAdded = true; } } ); } else { this.rows.push( new Row( w ) ); w.isAdded = true; } }; Grid.prototype.getLength = function() { return this.rows.length; }; Grid.prototype.generateRows = function() { this.widgets.forEach( this.addWidgetToRow, this ); }; module.exports = Grid;