@@ -154,6 +154,7 @@ qx.Class.define("osparc.study.Conversations", {
154154
155155 members : {
156156 __conversations : null ,
157+ __conversationPages : null ,
157158 __wsHandlers : null ,
158159
159160 _createChildControlImpl : function ( id ) {
@@ -248,57 +249,76 @@ qx.Class.define("osparc.study.Conversations", {
248249 }
249250 } ;
250251 osparc . data . Resources . fetch ( "conversations" , "getConversationsPage" , params )
251- . then ( conversations => this . __addConversations ( conversations , studyData ) )
252+ . then ( conversations => {
253+ this . __conversationPages = [ ] ;
254+ if ( conversations . length === 0 ) {
255+ conversations . forEach ( conversation => this . __addConversation ( conversation ) ) ;
256+ } else {
257+ this . __addTemporaryConversation ( ) ;
258+ }
259+ } )
252260 . finally ( ( ) => {
253261 loadMoreButton . setFetching ( false ) ;
254262 loadMoreButton . exclude ( ) ;
255263 } ) ;
256264 } ,
257265
258- __addConversations : function ( conversations , studyData ) {
259- const conversationPages = [ ] ;
266+ __createConversation : function ( conversationData ) {
267+ const studyData = this . getStudyData ( ) ;
268+ let conversation = null ;
269+ if ( conversationData ) {
270+ const conversationId = conversationData [ "conversationId" ] ;
271+ conversation = new osparc . conversation . Conversation ( studyData , conversationId ) ;
272+ conversation . setLabel ( conversationData [ "name" ] ) ;
273+ conversation . addListener ( "conversationDeleted" , ( ) => {
274+ console . log ( "Conversation deleted" ) ;
275+ } ) ;
276+ } else {
277+ // create a temporary conversation
278+ conversation = new osparc . conversation . Conversation ( studyData ) ;
279+ conversation . setLabel ( this . tr ( "new" ) ) ;
280+ }
281+ } ,
282+
283+ __addTemporaryConversation : function ( ) {
284+ const temporaryConversation = this . __createConversation ( ) ;
285+
260286 const conversationsLayout = this . getChildControl ( "conversations-layout" ) ;
287+ conversationsLayout . add ( temporaryConversation ) ;
261288
262- const newConversationButton = new qx . ui . form . Button ( ) . set ( {
263- icon : "@FontAwesome5Solid/plus/12" ,
264- toolTipText : this . tr ( "Add new conversation" ) ,
265- allowGrowX : false ,
266- backgroundColor : "transparent" ,
267- } ) ;
289+ this . __conversationPages . push ( temporaryConversation ) ;
290+ } ,
268291
269- const reloadConversations = ( ) => {
270- conversationPages . forEach ( conversationPage => conversationPage . fireDataEvent ( "close" , conversationPage ) ) ;
271- conversationsLayout . getChildControl ( "bar" ) . remove ( newConversationButton ) ;
272- this . fetchConversations ( studyData ) ;
273- } ;
292+ __addConversation : function ( conversationData ) {
293+ const conversation = this . __createConversation ( conversationData ) ;
274294
275- this . __conversations = [ ] ;
276- if ( conversations . length === 0 ) {
277- const noConversationTab = new osparc . conversation . Conversation ( studyData ) ;
278- conversationPages . push ( noConversationTab ) ;
279- noConversationTab . setLabel ( this . tr ( "new" ) ) ;
280- noConversationTab . addListener ( "conversationDeleted" , ( ) => reloadConversations ( ) ) ;
281- conversationsLayout . add ( noConversationTab ) ;
282- } else {
283- conversations . forEach ( conversationData => {
284- const conversationId = conversationData [ "conversationId" ] ;
285- const conversation = new osparc . conversation . Conversation ( studyData , conversationId ) ;
286- this . __conversations . push ( conversation ) ;
287- conversationPages . push ( conversation ) ;
288- conversation . setLabel ( conversationData [ "name" ] ) ;
289- conversation . addListener ( "conversationDeleted" , ( ) => reloadConversations ( ) ) ;
290- conversationsLayout . add ( conversation ) ;
291- } ) ;
292- }
295+ const conversationsLayout = this . getChildControl ( "conversations-layout" ) ;
296+ conversationsLayout . add ( conversation ) ;
293297
294- newConversationButton . addListener ( "execute" , ( ) => {
295- osparc . study . Conversations . addConversation ( studyData [ "uuid" ] , "new " + ( conversations . length + 1 ) )
296- . then ( ( ) => {
297- reloadConversations ( ) ;
298- } ) ;
299- } ) ;
300298
301- conversationsLayout . getChildControl ( "bar" ) . add ( newConversationButton ) ;
299+
300+ this . __conversations . push ( conversation ) ;
301+ this . __conversationPages . push ( conversation ) ;
302+
303+ if ( this . __newConversationButton === null ) {
304+ // initialize the new button only once
305+ const newConversationButton = this . __newConversationButton = new qx . ui . form . Button ( ) . set ( {
306+ icon : "@FontAwesome5Solid/plus/12" ,
307+ toolTipText : this . tr ( "Add new conversation" ) ,
308+ allowGrowX : false ,
309+ backgroundColor : "transparent" ,
310+ } ) ;
311+ newConversationButton . addListener ( "execute" , ( ) => {
312+ osparc . study . Conversations . addConversation ( studyData [ "uuid" ] , "new " + ( conversations . length + 1 ) )
313+ . then ( conversation => {
314+ this . __addConversation ( conversation ) ;
315+ } ) ;
316+ } ) ;
317+ conversationsLayout . getChildControl ( "bar" ) . add ( newConversationButton ) ;
318+ }
319+ // remove and add to move to last position
320+ conversationsLayout . getChildControl ( "bar" ) . remove ( this . __newConversationButton ) ;
321+ conversationsLayout . getChildControl ( "bar" ) . add ( this . __newConversationButton ) ;
302322 } ,
303323 } ,
304324
0 commit comments