Veto Selection Change event possible?

We are evaluating GoJS right now, one of the things we do with the nodes is that we have a user-editable properties panel associated with each node. To prevent user from losing their changes when a diagram selection changes, we prompt the user before proceeding with the selection change. I have tried going through the samples, docs and forum topics, and couldn’t find an example of how to do this, is this possible in GoJS?

I tried listening to the “ChangingSelection” event and setting the event.cancel = true but to no avail.

Any help is appreciated.



It seems to me that you should detect when your properties panel loses focus in order to prompt the user. Such a policy is what is normally used and applies to pages that have nothing to do with diagrams and selection within a diagram. And you have that issue when the user tries to change pages, too.

Yes, the “ChangingSelection” and “ChangedSelection” DiagramEvents do not allow you to modify the selection or to prevent selection changes from happening. Extending the “ChangingSelection” DiagramEvent to heed the DiagramEvent.cancel property would be difficult. For example, CommandHandler.deleteSelection() changes the selection because it makes sense to deselect deleted Parts. Should cancelling the selection change at that time also not do the deletion? The same issue applies to other commands. And what about calls to or Diagram.selectCollection? Who knows what code will be executed next? How can the “ChangingSelection” DiagramEvent sensibly affect the behavior of that code? Worse, what about calls to UndoManager.undo() or redo()? [Sorry if this is going into too much detail.]

A Part’s selection status is simply the value of the Part.isSelected property. If some code modifies the property, that’s that. But you can prevent the user from selecting other Parts – just set Diagram.allowSelect = false. You’ll note that code can still modify the value of Part.isSelected on any Node or Link. And Parts can become unselected, for example by deleting them (although of course you can prevent the user from doing that, if you choose).

Thanks for replying Walter.
The ‘lose focus’ detection you suggested won’t work in this case because the properties panel is supposed to change only when the diagram selection changes, and we don’t want to prompt user until it’s being replaced/refreshed. User should be able to click on other parts of the screen w/o being warned if the properties panel remains unchanged.

I guess we don’t need to veto the selection change event, but need to provide information about the old selection so that the old selections can be re-selected later. Right now, I believe I’ll need to keep track of the old selection with this approach. Let me know if you have better suggestion.



Yes, your “ChangingSelection” DiagramEvent listener could make a copy of the Diagram.selection collection and remember it. Then only when you are ready for it could you update your properties panel based on the newly selected parts. Caution: there might be multiple pairs of these “Changing/ChangedSelection” events before you decide you’re ready.