@@ -26,29 +26,98 @@ qx.Class.define("osparc.dashboard.NewPlusMenu", {
2626 this . set ( {
2727 position : "bottom-left" ,
2828 padding : 10 ,
29+ allowGrowX : true ,
2930 } ) ;
3031
3132 this . __addItems ( ) ;
3233 } ,
3334
35+ events : {
36+ "newStudyFromTemplateClicked" : "qx.event.type.Data" ,
37+ } ,
38+
39+ statics : {
40+ createMenuButton : function ( label , icon ) {
41+ const menuButton = new qx . ui . menu . Button ( ) . set ( {
42+ label,
43+ icon : icon || null ,
44+ font : "text-14" ,
45+ padding : 4 ,
46+ } ) ;
47+ menuButton . getChildControl ( "label" ) . set ( {
48+ rich : true ,
49+ } ) ;
50+ return menuButton ;
51+ } ,
52+ } ,
53+
3454 members : {
3555 _createChildControlImpl : function ( id ) {
3656 let control ;
3757 switch ( id ) {
3858 case "new-folder" :
39- control = new qx . ui . menu . Button ( ) . set ( {
40- label : this . tr ( "New Folder" ) ,
41- icon : osparc . dashboard . CardBase . NEW_ICON + "14" ,
42- font : "text-14" ,
43- } ) ;
59+ control = this . self ( ) . createMenuButton ( this . tr ( "New Folder" ) , osparc . dashboard . CardBase . NEW_ICON + "14" ) ;
60+ osparc . utils . Utils . setIdToWidget ( control , "newFolderButton" ) ;
61+ this . add ( control ) ;
62+ break ;
63+ case "more-entry" :
64+ control = this . self ( ) . createMenuButton ( this . tr ( "More" ) ) ;
4465 this . add ( control ) ;
4566 break ;
4667 }
4768 return control || this . base ( arguments , id ) ;
4869 } ,
4970
50- __addItems : function ( ) {
71+ __addItems : async function ( ) {
5172 this . getChildControl ( "new-folder" ) ;
73+ this . addSeparator ( ) ;
74+ await this . __fetchReferencedTemplates ( ) ;
75+ const permissions = osparc . data . Permissions . getInstance ( ) ;
76+ if ( permissions . canDo ( "dashboard.templates.read" ) || permissions . canDo ( "dashboard.services.read" ) ) {
77+ this . addSeparator ( ) ;
78+ const moreMenu = new qx . ui . menu . Menu ( ) ;
79+ const moreEntry = this . getChildControl ( "more-entry" ) ;
80+ moreEntry . setMenu ( moreMenu ) ;
81+ if ( permissions . canDo ( "dashboard.templates.read" ) ) {
82+ moreMenu . add ( this . self ( ) . createMenuButton ( this . tr ( "Templates" ) ) ) ;
83+ }
84+ if ( permissions . canDo ( "dashboard.services.read" ) ) {
85+ moreMenu . add ( this . self ( ) . createMenuButton ( this . tr ( "Services" ) ) ) ;
86+ }
87+ }
88+ } ,
89+
90+ __fetchReferencedTemplates : async function ( ) {
91+ await osparc . utils . Utils . fetchJSON ( "/resource/osparc/new_studies.json" )
92+ . then ( newStudiesData => {
93+ const product = osparc . product . Utils . getProductName ( )
94+ if ( product in newStudiesData ) {
95+ osparc . data . Resources . get ( "templates" )
96+ . then ( templates => {
97+ if ( templates ) {
98+ const referencedTemplates = newStudiesData [ product ] ;
99+ if ( referencedTemplates [ "linkedResource" ] === "templates" ) {
100+ this . __addReferencedTemplateButtons ( referencedTemplates , templates ) ;
101+ }
102+ }
103+ } ) ;
104+ }
105+ } ) ;
106+ } ,
107+
108+ __addReferencedTemplateButtons : function ( referencedTemplates , templates ) {
109+ const displayTemplates = referencedTemplates [ "resources" ] . filter ( referencedTemplate => {
110+ if ( referencedTemplate . showDisabled ) {
111+ return true ;
112+ }
113+ return templates . find ( t => t . name === referencedTemplate . expectedTemplateLabel ) ;
114+ } ) ;
115+ displayTemplates . forEach ( displayTemplate => {
116+ const menuButton = this . self ( ) . createMenuButton ( displayTemplate . title ) ;
117+ osparc . utils . Utils . setIdToWidget ( menuButton , displayTemplate . idToWidget ) ;
118+ menuButton . addListener ( "tap" , ( ) => this . fireDataEvent ( "newStudyFromTemplateClicked" , displayTemplate ) ) ;
119+ this . add ( menuButton ) ;
120+ } ) ;
52121 }
53122 } ,
54123} ) ;
0 commit comments