diff --git a/src/components/dialog/dialog.js b/src/components/dialog/dialog.js index 715ce47be1..f0ae52a48f 100644 --- a/src/components/dialog/dialog.js +++ b/src/components/dialog/dialog.js @@ -638,7 +638,7 @@ function MdDialogProvider($$interimElementProvider) { /* @ngInject */ function dialogDefaultOptions($mdDialog, $mdAria, $mdUtil, $mdConstant, $animate, $document, $window, $rootElement, - $log, $injector, $mdTheming, $interpolate) { + $log, $injector, $mdTheming, $interpolate, $mdInteraction) { return { hasBackdrop: true, @@ -798,7 +798,10 @@ function MdDialogProvider($$interimElementProvider) { // Exposed cleanup function from the $mdCompiler. options.cleanupElement(); - if (!options.$destroy) options.origin.focus(); + // Restores the focus to the origin element if the last interaction upon opening was a keyboard. + if (!options.$destroy && options.originInteraction === 'keyboard') { + options.origin.focus(); + } } } @@ -850,7 +853,8 @@ function MdDialogProvider($$interimElementProvider) { options.openFrom = getBoundingClientRect(getDomElement(options.openFrom)); if ( options.targetEvent ) { - options.origin = getBoundingClientRect(options.targetEvent.target, options.origin); + options.origin = getBoundingClientRect(options.targetEvent.target, options.origin); + options.originInteraction = $mdInteraction.getLastInteractionType(); } diff --git a/src/components/dialog/dialog.spec.js b/src/components/dialog/dialog.spec.js index 68eaddee51..e8598a7029 100644 --- a/src/components/dialog/dialog.spec.js +++ b/src/components/dialog/dialog.spec.js @@ -1134,6 +1134,82 @@ describe('$mdDialog', function() { expect($document.activeElement).toBe(parent[0].querySelector('#focus-target')); })); + it('should restore the focus to the origin upon close', inject(function($mdDialog, $compile, $rootScope) { + var scope = $rootScope.$new(); + var body = angular.element(document.body); + var parent = angular.element('
'); + var button = $compile('')(scope); + + // Append the button to the body, because otherwise the dialog is not able to determine + // the origin rectangle. + document.body.appendChild(button[0]); + + scope.openDialog = function($event) { + $mdDialog.show({ + parent: parent, + template: 'Test', + targetEvent: $event, + scope: scope.$new() + }); + }; + + // Emit a keyboard event to fake a keyboard interaction. + body.triggerHandler('keydown'); + button.triggerHandler('click'); + + runAnimation(); + + expect(parent.find('md-dialog').length).toBe(1); + expect(document.activeElement).not.toBe(button[0]); + + + $mdDialog.hide(); + runAnimation(); + + expect(parent.find('md-dialog').length).toBe(0); + expect(document.activeElement).toBe(button[0]); + + button.remove(); + })); + + it('should not restore the focus without keyboard interaction', inject(function($mdDialog, $compile, $rootScope) { + var scope = $rootScope.$new(); + var body = angular.element(document.body); + var parent = angular.element('
'); + var button = $compile('')(scope); + + // Append the button to the body, because otherwise the dialog is not able to determine + // the origin rectangle. + document.body.appendChild(button[0]); + + scope.openDialog = function($event) { + $mdDialog.show({ + parent: parent, + template: 'Test', + targetEvent: $event, + scope: scope.$new() + }); + }; + + // Emit a keyboard event to fake a mouse interaction. + body.triggerHandler('mousedown'); + button.triggerHandler('click'); + + runAnimation(); + + expect(parent.find('md-dialog').length).toBe(1); + expect(document.activeElement).not.toBe(button[0]); + + + $mdDialog.hide(); + runAnimation(); + + expect(parent.find('md-dialog').length).toBe(0); + expect(document.activeElement).not.toBe(button[0]); + + button.remove(); + })); + it('should focus the dialog element if no actions are set', inject(function($mdDialog, $rootScope, $document) { jasmine.mockElementFocus(this);