UNPKG

cluedin-widget

Version:

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

62 lines (47 loc) 1.25 kB
var Row = function( w ) { this.widgets = []; if ( w ) { this.widgets.push( w ); } }; Row.prototype.calculate = function() { var total = 0; this.widgets.forEach( function( w ) { total += w.size; }, this ); return total; }; Row.prototype.placeLeft = function() { return 12 - this.calculate(); }; Row.prototype.addWidget = function( w ) { this.widgets.push( w ); }; Row.prototype.hasSpace = function( w ) { return (this.placeLeft() >= w.size); }; var Layout = function( widgets ) { this.widgets = widgets; this.rows = []; this.rows.push( new Row() ); }; Layout.prototype.generate = function() { this.widgets.forEach( this.placeWidget, this ); }; Layout.prototype.placeWidget = function( w ) { var isPlaced = false; this.rows.forEach( function( r ) { if ( !isPlaced ) { if ( r.hasSpace( w ) ) { r.addWidget( w ); isPlaced = true; } } } ); if ( !isPlaced ) { this.rows.push( new Row( w ) ); } }; var widgets = [ { size: 4 }, { size: 8 }, { size: 4 }, { size: 4 }, { size: 4 }, { size: 8 }, { size: 12 } ]; var layout = new Layout( widgets ); layout.generate();