Skip to content
This repository was archived by the owner on Sep 5, 2024. It is now read-only.

Commit 2d8eb6d

Browse files
crisbetoThomasBurleson
authored andcommitted
fix(datepicker): prevent calendar from closing immediately on mobile with md-open-on-focus
* Fixes the calendar being closed immediately on mobile, if md-open-on-focus is set. This was due to the fact that mobile browsers zoom in on the input and dispatch a `resize` event. This change switches to the more appropriate `orientationchange` event. * Exposes the `isIos` and `isAndroid` variables in the gesture service. Fixes #9170. Closes #9202
1 parent 64bc5b9 commit 2d8eb6d

File tree

2 files changed

+17
-8
lines changed

2 files changed

+17
-8
lines changed

src/components/datepicker/js/datepickerDirective.js

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@
218218
* @ngInject @constructor
219219
*/
220220
function DatePickerCtrl($scope, $element, $attrs, $window, $mdConstant,
221-
$mdTheming, $mdUtil, $mdDateLocale, $$mdDateUtil, $$rAF) {
221+
$mdTheming, $mdUtil, $mdDateLocale, $$mdDateUtil, $$rAF, $mdGesture) {
222222

223223
/** @final */
224224
this.$window = $window;
@@ -312,8 +312,15 @@
312312
/** Pre-bound click handler is saved so that the event listener can be removed. */
313313
this.bodyClickHandler = angular.bind(this, this.handleBodyClick);
314314

315-
/** Pre-bound resize handler so that the event listener can be removed. */
316-
this.windowResizeHandler = $mdUtil.debounce(angular.bind(this, this.closeCalendarPane), 100);
315+
/**
316+
* Name of the event that will trigger a close. Necessary to sniff the browser, because
317+
* the resize event doesn't make sense on mobile and can have a negative impact since it
318+
* triggers whenever the browser zooms in on a focused input.
319+
*/
320+
this.windowEventName = ($mdGesture.isIos || $mdGesture.isAndroid) ? 'orientationchange' : 'resize';
321+
322+
/** Pre-bound close handler so that the event listener can be removed. */
323+
this.windowEventHandler = $mdUtil.debounce(angular.bind(this, this.closeCalendarPane), 100);
317324

318325
/** Pre-bound handler for the window blur event. Allows for it to be removed later. */
319326
this.windowBlurHandler = angular.bind(this, this.handleWindowBlur);
@@ -690,7 +697,7 @@
690697
self.documentElement.on('click touchstart', self.bodyClickHandler);
691698
}, false);
692699

693-
window.addEventListener('resize', this.windowResizeHandler);
700+
window.addEventListener(this.windowEventName, this.windowEventHandler);
694701
}
695702
};
696703

@@ -704,7 +711,7 @@
704711
self.evalAttr('ngBlur');
705712

706713
self.documentElement.off('click touchstart', self.bodyClickHandler);
707-
window.removeEventListener('resize', self.windowResizeHandler);
714+
window.removeEventListener(self.windowEventName, self.windowEventHandler);
708715

709716
self.calendarPaneOpenedFrom.focus();
710717
self.calendarPaneOpenedFrom = null;

src/core/services/gesture/gesture.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ var HANDLERS = {};
55
* It contains normalized x and y coordinates from DOM events,
66
* as well as other information abstracted from the DOM.
77
*/
8-
8+
99
var pointer, lastPointer, forceSkipClickHijack = false;
1010

1111
/**
@@ -79,6 +79,8 @@ function MdGesture($$MdGestureHandler, $$rAF, $timeout) {
7979
var self = {
8080
handler: addHandler,
8181
register: register,
82+
isIos: isIos,
83+
isAndroid: isAndroid,
8284
// On mobile w/out jQuery, we normally intercept clicks. Should we skip that?
8385
isHijackingClicks: (isIos || isAndroid) && !hasJQuery && !forceSkipClickHijack
8486
};
@@ -170,7 +172,7 @@ function MdGesture($$MdGestureHandler, $$rAF, $timeout) {
170172
* Register handlers. These listen to touch/start/move events, interpret them,
171173
* and dispatch gesture events depending on options & conditions. These are all
172174
* instances of GestureHandler.
173-
* @see GestureHandler
175+
* @see GestureHandler
174176
*/
175177
return self
176178
/*
@@ -528,7 +530,7 @@ function attachToDocument( $mdGesture, $$MdGestureHandler ) {
528530
* click event will be sent ~400ms after a touchend event happens.
529531
* The only way to know if this click is real is to prevent any normal
530532
* click events, and add a flag to events sent by material so we know not to prevent those.
531-
*
533+
*
532534
* Two exceptions to click events that should be prevented are:
533535
* - click events sent by the keyboard (eg form submit)
534536
* - events that originate from an Ionic app

0 commit comments

Comments
 (0)