From acfd75ee2ec8ad97f8dd54e2ae454257e7284159 Mon Sep 17 00:00:00 2001 From: Ahmed Ashour Date: Thu, 23 Nov 2017 06:54:27 +0100 Subject: DateField not to fire focus/blur event when going to sub-components (#10246) Fixes `DateField blur event fires when focus gets to the "calendar button"` #1008 --- .../vaadin/client/ui/VAbstractCalendarPanel.java | 1 - .../vaadin/client/ui/VAbstractPopupCalendar.java | 28 ++++++----- .../com/vaadin/client/ui/VAbstractTextualDate.java | 54 ++++++++++++++++++---- 3 files changed, 60 insertions(+), 23 deletions(-) (limited to 'client') diff --git a/client/src/main/java/com/vaadin/client/ui/VAbstractCalendarPanel.java b/client/src/main/java/com/vaadin/client/ui/VAbstractCalendarPanel.java index 70bec08e4f..f009f483e0 100644 --- a/client/src/main/java/com/vaadin/client/ui/VAbstractCalendarPanel.java +++ b/client/src/main/java/com/vaadin/client/ui/VAbstractCalendarPanel.java @@ -1997,7 +1997,6 @@ public abstract class VAbstractCalendarPanel> renderCalendar(); } } - } /** diff --git a/client/src/main/java/com/vaadin/client/ui/VAbstractPopupCalendar.java b/client/src/main/java/com/vaadin/client/ui/VAbstractPopupCalendar.java index d894653609..4df4d0e325 100644 --- a/client/src/main/java/com/vaadin/client/ui/VAbstractPopupCalendar.java +++ b/client/src/main/java/com/vaadin/client/ui/VAbstractPopupCalendar.java @@ -377,7 +377,6 @@ public abstract class VAbstractPopupCalendar= 0) { - return desiredLeftPosition; - } else { - return left; } + return left + calendarToggle.getOffsetWidth(); } + int[] margins = style.getMargin(); + int desiredLeftPosition = calendarToggle.getAbsoluteLeft() - width + - margins[1] - margins[3]; + if (desiredLeftPosition >= 0) { + return desiredLeftPosition; + } + return left; } private boolean positionRightSide() { @@ -721,6 +720,11 @@ public abstract class VAbstractPopupCalendar> /** For internal use only. May be removed or replaced in the future. */ private TimeZone timeZone; + /** + * Specifies whether the group of components has focus or not. + */ + private boolean groupFocus; + public VAbstractTextualDate(R resoluton) { super(resoluton); text = new TextBox(); text.addChangeHandler(this); text.addFocusHandler( - event -> fireBlurFocusEvent(event, true, EventId.FOCUS)); + event -> fireBlurFocusEvent(event, true)); text.addBlurHandler( - event -> fireBlurFocusEvent(event, false, EventId.BLUR)); + event -> fireBlurFocusEvent(event, false)); if (BrowserInfo.get().isIE()) { addDomHandler(this, KeyDownEvent.getType()); } @@ -394,27 +400,54 @@ public abstract class VAbstractTextualDate> } private void fireBlurFocusEvent(DomEvent event, - boolean addFocusStyleName, String eventId) { + boolean focus) { String styleName = VTextField.CLASSNAME + "-" + VTextField.CLASSNAME_FOCUS; - if (addFocusStyleName) { + if (focus) { text.addStyleName(styleName); } else { text.removeStyleName(styleName); } - if (getClient() != null && connector.hasEventListener(eventId)) { - // may excessively send events if if focus went to another - // sub-component - if (EventId.FOCUS.equals(eventId)) { + + Scheduler.get().scheduleDeferred(() -> checkGroupFocus(focus)); + + // Needed for tooltip event handling + fireEvent(event); + } + + /** + * Checks if the group focus has changed, and sends to the server if needed. + * + * @param textFocus + * the focus of the {@link #text} + * @since + */ + protected void checkGroupFocus(boolean textFocus) { + boolean newGroupFocus = textFocus | hasChildFocus(); + if (getClient() != null + && connector.hasEventListener( + textFocus ? EventId.FOCUS : EventId.BLUR) + && groupFocus != newGroupFocus) { + + if (newGroupFocus) { rpc.focus(); } else { rpc.blur(); } sendBufferedValues(); + groupFocus = newGroupFocus; } + } - // Needed for tooltip event handling - fireEvent(event); + /** + * Returns whether any of the child components has focus. + * + * @return {@code true} if any of the child component has focus, + * {@code false} otherwise + * @since + */ + protected boolean hasChildFocus() { + return false; } /** @@ -479,4 +512,5 @@ public abstract class VAbstractTextualDate> private static Logger getLogger() { return Logger.getLogger(VAbstractTextualDate.class.getName()); } + } -- cgit v1.2.3