How to close popup when screen changes?

Does anyone know how to close popup automatically when I change the view on perspective?
I do have many popups on each screen and I do not want to close them pressing button all the time.

You should be able to use the onShutdown event on a view, to send a message. Any popup that is open and listening for that message would then be able to close using system.perspective.closePopup()

Since the main responsibility of closing a Popup rests with the Popup itself there isn't truly a dedicated route for closing all Popups which are open.

If your users chnage pages by maniupating the URL bar directly, there's nothing you can do. If you're managing the navigation through Actions, then you should include a script along these lines anywhere which would perform navigation:

count_of_potenital_popups = 5
for i in range(count_of_potenital_popups):
    # specifying a blank string for the Popup id acts against the most recently focused Popup
    system.perspective.closePopup(id="", pageId=self.page.props.pageId)

Do I need to write all the popup in the script?

Do I need to write IDs for all the popup on a screen?

Perhaps the docs will be helpful for you,
https://docs.inductiveautomation.com/display/DOC81/system.perspective.closePopup

2 Likes

If you want to close all popups that are open, at some point you will have to indicate to the system to close that popup. There is no "close all popups" short cut.

In addition to the quick-and-dirty avenue I already supplied, there's two other ways to do this.

More correct:
In the onShutdown Event script for the View in question, close out all "known" Popups for the View.

known_popup_ids = ["a", "b", "c"]
for popup_id in known_popup_ids:
    system.perspective.closePopup(id=popup_id)

This requires you to always update the list of potential Popups, so this is inherently brittle as you develop and add more content. Additionally, there is always the possibility of other Views (Popups, Docked Views) opening their own Popups, and this View would know nothing about them.

Most correct:
In the onShutdown Event script for the View in question, broadcast that all Popups should be closed for the page.

system.perspective.sendMessage(messageType="CLOSEPOPUPS", scope="page")

This route requires that EVERY POPUP have at least one component which has a configured Message Handler listening at the page scope for a "CLOSEPOPUPS" message. It also requires that the Popup either knows its own ID (passed in as a parameter perhaps, or hard-coded as a custom prop). You are not able to rely on supplying an empty-string id because there could be other Popups which would be closed.

system.perspective.closePopup(id="MyKnownId")

This route is the "most correct" because there is no list of Popups to manage as development continues, and it allows the individual Popups to manage their own lifecycle. Also, this route will also act on Popups opened from other Views as long as they too have a configured Message Handler and ID.

7 Likes

... when screen changes?

To make things clear, you should edit your title to be when view changes. There are no "screens" in Perspective. The correct is always helpful.