Golden-layout: How to prevent to close last Tab?

Created on 25 Jul 2017  路  3Comments  路  Source: golden-layout/golden-layout

I added a "Js confirm" when user tries to close last Tab, but I have some trouble when user clicks "no" because Confirm is actually triggered at itemDestroyed event. So, in this case, Confirm is displayed after that "destroy" is already triggered.

I'd need something like beforeDestroy or any other method to abort "destroy" in this cases.

Thanks

Question

Most helpful comment

@jcford73 sparked a memory of doing this in a project before, here is a cut down version of my solution -

myLayout.on('tabCreated', function(tab){
   tab.closeElement.off( 'click' ).click(function(){
      if( confirm( 'You have unsaved changes, are you sure you want to close this tab' ) ) {
         tab.contentItem.remove();
      }
   })
})

You can look in the tabs parent (the stack) and see how many tabs are there and if this is the last one, then prompt before closing the tab.

tab.closeElement.off( 'click' ) will unbind the event listener for the click and then you assign your own function in the .click()

All 3 comments

Might be able to use isClosable as a work around and then manually closing the tab.
Not sure if there is any other methods however

Replace the tab close button with your own implementation. I had to do something similar to give the component within the tab an opportunity to interrupt the close action, so the user could be prompted to save changes.

@jcford73 sparked a memory of doing this in a project before, here is a cut down version of my solution -

myLayout.on('tabCreated', function(tab){
   tab.closeElement.off( 'click' ).click(function(){
      if( confirm( 'You have unsaved changes, are you sure you want to close this tab' ) ) {
         tab.contentItem.remove();
      }
   })
})

You can look in the tabs parent (the stack) and see how many tabs are there and if this is the last one, then prompt before closing the tab.

tab.closeElement.off( 'click' ) will unbind the event listener for the click and then you assign your own function in the .click()

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Sedawa picture Sedawa  路  5Comments

JohnKiller picture JohnKiller  路  5Comments

soh1988 picture soh1988  路  6Comments

ldetomi picture ldetomi  路  5Comments

panyam picture panyam  路  4Comments