cluedin-widget
Version:
This is the project for creating and managing widgets in CluedIn.
68 lines (56 loc) • 2.37 kB
JavaScript
module.exports = function( bookshelf ) {
var models = require( 'cluedin-widget-models' )( bookshelf ).models;
var Page = models.application.Page;
var Tab = models.application.Tab;
var WidgetConfiguration = models.application.WidgetConfiguration;
return {
getAppropriateWidgetList: function( code, cb ) {
if ( !code ) {
cb( {
message: 'wrong code'
} );
}
var type = code.split( '-' )[ 0 ];
var uniqueId = code.split( '-' )[ 1 ];
if ( !type ) {
cb( {
message: 'wrong code with no type'
} );
}
if ( !uniqueId ) {
cb( {
message: 'wrong code with no unique id'
} )
}
switch( type ) {
case 'page':
Page.forge( { uuid: uniqueId } ).fetch( { withRelated: [ 'tabs', 'tabs.widgets', 'tabs.widgets.original' ] } ).then( function( pageModel ) {
if ( !pageModel ) {
cb( {
message: 'could not find page'
} );
}
var widgets = [];
var tabs = pageModel.related( 'tabs' );
tabs.forEach( function( tab ) {
widgets = widgets.concat( tab.related( 'widgets' ).toJSON() );
} );
cb( null, widgets.map( function( w ) {
return w.original.name
} ) );
} );
break;
case 'tab':
Tab.forge( { uuid: uniqueId } ).fetch( { withRelated: [ 'widgets', 'widgets.original' ] } ).then( function( tabModel ) {
cb( null, tabModel.widgets );
} );
break;
case 'widget':
WidgetConfiguration.forge( { uuid: uniqueId } ).fetch( { withRelated: [ 'widgets.original' ] } ).then( function( widget ) {
cb( [ widget ] )
} );
break;
}
}
}
};