Is there a way to detect when changes have been made to a form's contents and the user is about to navigate away (or to a different record) without submitting the changes? For example, in the Office Supply Catalog sample app, if you click on an item and make changes in the Edit tab, but forget to click Save Item, and then you click on a different item you lose your edits. Can that condition be trapped to display a warning message?
Announcement
Collapse
No announcement yet.
X
-
This actually seems to work, but it takes a very long time to respond when focus is changed. Resetting focus to the form works fine, but the "return" to allow things to proceed as normal doesn't quite work. The newly selected item record does not populate the form until you click on it again. ??
Code:itemForm.addFocusChangedHandler(new FocusChangedHandler() { public void onFocusChanged(FocusChangedEvent event) { if (!event.getHasFocus()) { if (itemForm.valuesHaveChanged()) { SC.ask("Exit without saving your changes?", new BooleanCallback() { public void execute(Boolean value) { if (value) return; else itemForm.focus(); } }); } } } });
Comment
-
I gave up trying to find a solution to this but now it is being reported as a "bug" by users so I'm trying again. This time I tried adding a FocusChangedHandler as follows ...
Code:form.addFocusChangedHandler(new FocusChangedHandler() { @Override public void onFocusChanged(FocusChangedEvent event) { if (!event.getHasFocus() && form.valuesHaveChanged()) { SC.ask("Do you want to cancel the pending changes?",new BooleanCallback() { @Override public void execute(Boolean value) { if (!value) form.focus(); } }); } } });
Comment
-
Thanks for the sample code - we were able to reproduce this easily.
The problem is that when the focus changed handler fired and called "SC.ask" this forces focus into the ask dialog -- and by manipulating the focus in response to the focus changed event it was leading to an infinite loop.
The app would only come back to life when the thread hit some native stack-depth limit and threw an error (visible in the console, though without much useful info).
Anyway we've made some changes to the way focus / blur events are handled to fix this. So this chunk of code should work fine in the next nightly build.
If you need a workaround, you could also have your SC.ask call execute on a timer so it's in a separate thread, and the issue should go away
Comment
-
Thanks. That code is working now with the latest nightly.
One follow up question. Is there any way to get and save the new focus target before showing the SC.ask dialog? Ideally, if they agree to lose their pending changes, I want to reset the form fields and then set the focus to where it was headed before being interrupted. Is that possible?
Comment
-
Still having difficulties getting this to work. I have a row of buttons that are related to the form - Save, Delete, New, Reset. Initially these were separate from the form, just in a layout with the form. Of course the problem there was that clicking one of the buttons cause the FocusChangedHandler to fire as if you were navigating away from the form.
I've now moved those buttons into a CanvasItem and included that in form itself. However, the FocusChangedHandler still fires when I click any of the buttons and event.getHasFocus() is false. Why, when the buttons are in a CanvasItem in the form itself does it still appear that the form is losing focus?
Comment
-
I finally have this working ... almost. Instead of using event.getHasFocus() I switched to using form.containsFocus() as was mentioned earlier. That fixed the problem with SC.ask dialog popping up when you clicked a button on the form. Now when I click Save the record is saved and all is well.
However, I am also using setSaveOnEnter(true) on the form so the user can press Enter instead of clicking Save. I have a SubmitValuesHandler that calls the same method as the Save button's clickHandler. I've put log messages in place and can see that the same method is being called, but the focus handling is different.
When I click the Save button the FocusChangedHandler does not fire. But when I press Enter it does, and form.containsFocus() is false. Why does the Enter key change focus away from the form? And where does the focus go?
Comment
-
On focus in general, the Button is considered a distinct widget and distinct focus position regardless of whether it's incorporated into the DynamicForm for layout purposes.
You can enable the "nativeFocus" log category in the Developer Console to see how focus is moving around. This is a big verbose and technical, but it may help you understand what's going on.
The most likely place that focus is moving on submit is to the modal prompt that blocks interactivity while the submit is taking place.
Comment
Comment