From d6853c436e5acc0374cd73ce32d8e51c5b44f553 Mon Sep 17 00:00:00 2001 From: Guillermo Alvarez Date: Wed, 13 Aug 2014 14:40:38 +0300 Subject: Backspace in modal doesn't make browser navigation (#13180) In a modal window if focus is not in an editable component the backspace default action is prevented. Also when focus is on bottom or top components. Change-Id: I53f0922e5ddff142e2d540be52e70c7d23d1b585 --- client/src/com/vaadin/client/Util.java | 19 +++++++++++++++++++ client/src/com/vaadin/client/ui/VWindow.java | 22 ++++++++++++++-------- 2 files changed, 33 insertions(+), 8 deletions(-) (limited to 'client') diff --git a/client/src/com/vaadin/client/Util.java b/client/src/com/vaadin/client/Util.java index 306f26b1af..1cdd8fb458 100644 --- a/client/src/com/vaadin/client/Util.java +++ b/client/src/com/vaadin/client/Util.java @@ -1261,6 +1261,25 @@ public class Util { return getFocusedElement(); } + /** + * Gets currently focused element and checks if it's editable + * + * @return true if focused element is editable + */ + public static boolean isFocusedElementEditable() { + Element focusedElement = Util.getFocusedElement(); + if (focusedElement != null) { + String tagName = focusedElement.getTagName(); + String contenteditable = focusedElement + .getAttribute("contenteditable"); + + return "textarea".equalsIgnoreCase(tagName) + || "input".equalsIgnoreCase(tagName) + || "true".equalsIgnoreCase(contenteditable); + } + return false; + } + /** * Kind of stronger version of isAttached(). In addition to std isAttached, * this method checks that this widget nor any of its parents is hidden. Can diff --git a/client/src/com/vaadin/client/ui/VWindow.java b/client/src/com/vaadin/client/ui/VWindow.java index 7223e4ac83..58e7a83012 100644 --- a/client/src/com/vaadin/client/ui/VWindow.java +++ b/client/src/com/vaadin/client/ui/VWindow.java @@ -16,6 +16,8 @@ package com.vaadin.client.ui; +import static com.vaadin.client.Util.isFocusedElementEditable; + import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; @@ -178,8 +180,6 @@ public class VWindow extends VOverlay implements ShortcutActionHandlerOwner, // Prevents leaving the window with the Tab key when true private boolean doTabStop; - private boolean hasFocus; - /** * If centered (via UIDL), the window should stay in the centered -mode * until a position is received from the server, or the user moves or @@ -420,6 +420,7 @@ public class VWindow extends VOverlay implements ShortcutActionHandlerOwner, Id.of(headerText)); // Handlers to Prevent tab to leave the window + // and backspace to cause browser navigation topEventBlocker = new NativePreviewHandler() { @Override public void onPreviewNativeEvent(NativePreviewEvent event) { @@ -429,6 +430,10 @@ public class VWindow extends VOverlay implements ShortcutActionHandlerOwner, && nativeEvent.getShiftKey()) { nativeEvent.preventDefault(); } + if (nativeEvent.getEventTarget().cast() == topTabStop + && nativeEvent.getKeyCode() == KeyCodes.KEY_BACKSPACE) { + nativeEvent.preventDefault(); + } } }; @@ -441,6 +446,10 @@ public class VWindow extends VOverlay implements ShortcutActionHandlerOwner, && !nativeEvent.getShiftKey()) { nativeEvent.preventDefault(); } + if (nativeEvent.getEventTarget().cast() == bottomTabStop + && nativeEvent.getKeyCode() == KeyCodes.KEY_BACKSPACE) { + nativeEvent.preventDefault(); + } } }; } @@ -1302,10 +1311,11 @@ public class VWindow extends VOverlay implements ShortcutActionHandlerOwner, @Override public void onKeyDown(KeyDownEvent event) { - if (hasFocus && event.getNativeKeyCode() == KeyCodes.KEY_BACKSPACE) { + if (vaadinModality + && event.getNativeKeyCode() == KeyCodes.KEY_BACKSPACE + && !isFocusedElementEditable()) { event.preventDefault(); } - if (shortcutHandler != null) { shortcutHandler .handleKeyboardEvent(Event.as(event.getNativeEvent())); @@ -1322,8 +1332,6 @@ public class VWindow extends VOverlay implements ShortcutActionHandlerOwner, @Override public void onBlur(BlurEvent event) { - hasFocus = false; - if (client.hasEventListeners(this, EventId.BLUR)) { client.updateVariable(id, EventId.BLUR, "", true); } @@ -1331,8 +1339,6 @@ public class VWindow extends VOverlay implements ShortcutActionHandlerOwner, @Override public void onFocus(FocusEvent event) { - hasFocus = true; - if (client.hasEventListeners(this, EventId.FOCUS)) { client.updateVariable(id, EventId.FOCUS, "", true); } -- cgit v1.2.3