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

Commit 35e2f2a

Browse files
EladBezalelThomasBurleson
authored andcommitted
fix(dialog): Switched the click action to mouse down/up
Changed the click event to mousedown and mouseup so we can save the origin element on mouse down and close on mouseup if the origin and the target is the backdrop fixes #3873. closes #4972.
1 parent eae9eea commit 35e2f2a

File tree

2 files changed

+111
-11
lines changed

2 files changed

+111
-11
lines changed

src/components/dialog/dialog.js

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -651,22 +651,36 @@ function MdDialogProvider($$interimElementProvider) {
651651
}
652652
if (options.clickOutsideToClose) {
653653
var target = element;
654-
var clickHandler = function(ev) {
655-
// Only close if we click the flex container outside on the backdrop
656-
if (ev.target === target[0]) {
654+
var sourceElem;
655+
656+
// Keep track of the element on which the mouse originally went down
657+
// so that we can only close the backdrop when the 'click' started on it.
658+
// A simple 'click' handler does not work,
659+
// it sets the target object as the element the mouse went down on.
660+
var mousedownHandler = function(ev) {
661+
sourceElem = ev.target;
662+
};
663+
664+
// We check if our original element and the target is the backdrop
665+
// because if the original was the backdrop and the target was inside the dialog
666+
// we don't want to dialog to close.
667+
var mouseupHandler = function(ev) {
668+
if (sourceElem === target[0] && ev.target === target[0]) {
657669
ev.stopPropagation();
658670
ev.preventDefault();
659671

660672
smartClose();
661673
}
662674
};
663675

664-
// Add click listeners
665-
target.on('click', clickHandler);
676+
// Add listeners
677+
target.on('mousedown', mousedownHandler);
678+
target.on('mouseup', mouseupHandler);
666679

667680
// Queue remove listeners function
668681
removeListeners.push(function() {
669-
target.off('click', clickHandler);
682+
target.off('mousedown', mousedownHandler);
683+
target.off('mouseup', mouseupHandler);
670684
});
671685
}
672686

src/components/dialog/dialog.spec.js

Lines changed: 91 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ describe('$mdDialog', function() {
132132
expect($document.activeElement).toBe(parent[0].querySelector('md-dialog-content'));
133133
}));
134134

135-
it('should remove `md-dialog-container` on click and remove', inject(function($mdDialog, $rootScope, $timeout) {
135+
it('should remove `md-dialog-container` on mousedown mouseup and remove', inject(function($mdDialog, $rootScope, $timeout) {
136136
jasmine.mockElementFocus(this);
137137
var container, parent = angular.element('<div>');
138138

@@ -152,7 +152,11 @@ describe('$mdDialog', function() {
152152

153153
container = angular.element(parent[0].querySelector('.md-dialog-container'));
154154
container.triggerHandler({
155-
type: 'click',
155+
type: 'mousedown',
156+
target: container[0]
157+
});
158+
container.triggerHandler({
159+
type: 'mouseup',
156160
target: container[0]
157161
});
158162

@@ -290,7 +294,7 @@ describe('$mdDialog', function() {
290294
expect($document.activeElement).toBe(parent[0].querySelector('.dialog-close'));
291295
}));
292296

293-
it('should remove `md-dialog-container` after click outside', inject(function($mdDialog, $rootScope, $timeout, $animate) {
297+
it('should remove `md-dialog-container` after mousedown mouseup outside', inject(function($mdDialog, $rootScope, $timeout, $animate) {
294298
jasmine.mockElementFocus(this);
295299
var container, parent = angular.element('<div>');
296300

@@ -311,7 +315,11 @@ describe('$mdDialog', function() {
311315

312316
container = angular.element(parent[0].querySelector('.md-dialog-container'));
313317
container.triggerHandler({
314-
type: 'click',
318+
type: 'mousedown',
319+
target: container[0]
320+
});
321+
container.triggerHandler({
322+
type: 'mouseup',
315323
target: container[0]
316324
});
317325

@@ -322,6 +330,80 @@ describe('$mdDialog', function() {
322330
expect(container.length).toBe(0);
323331
}));
324332

333+
it('should not remove `md-dialog-container` after mousedown outside mouseup inside', inject(function($mdDialog, $rootScope, $timeout, $animate) {
334+
jasmine.mockElementFocus(this);
335+
var container, parent = angular.element('<div>');
336+
337+
$mdDialog.show(
338+
$mdDialog.confirm({
339+
template: '<md-dialog>' +
340+
'<md-dialog-content tabIndex="0">' +
341+
'<p>Muppets are the best</p>' +
342+
'</md-dialog-content>' +
343+
'</md-dialog>',
344+
parent: parent,
345+
clickOutsideToClose: true,
346+
ok: 'OK',
347+
cancel: 'CANCEL'
348+
})
349+
);
350+
runAnimation();
351+
352+
container = angular.element(parent[0].querySelector('.md-dialog-container'));
353+
var content = angular.element(parent[0].querySelector('md-dialog-content'));
354+
container.triggerHandler({
355+
type: 'mousedown',
356+
target: container[0]
357+
});
358+
content.triggerHandler({
359+
type: 'mouseup',
360+
target: content[0]
361+
});
362+
363+
runAnimation();
364+
runAnimation();
365+
366+
container = angular.element(parent[0].querySelector('.md-dialog-container'));
367+
expect(container.length).toBe(1);
368+
}));
369+
370+
it('should not remove `md-dialog-container` after mousedown inside mouseup outside', inject(function($mdDialog, $rootScope, $timeout, $animate) {
371+
jasmine.mockElementFocus(this);
372+
var container, parent = angular.element('<div>');
373+
374+
$mdDialog.show(
375+
$mdDialog.confirm({
376+
template: '<md-dialog>' +
377+
'<md-dialog-content tabIndex="0">' +
378+
'<p>Muppets are the best</p>' +
379+
'</md-dialog-content>' +
380+
'</md-dialog>',
381+
parent: parent,
382+
clickOutsideToClose: true,
383+
ok: 'OK',
384+
cancel: 'CANCEL'
385+
})
386+
);
387+
runAnimation();
388+
389+
container = angular.element(parent[0].querySelector('.md-dialog-container'));
390+
var content = angular.element(parent[0].querySelector('md-dialog-content'));
391+
content.triggerHandler({
392+
type: 'mousedown',
393+
target: content[0]
394+
});
395+
container.triggerHandler({
396+
type: 'mouseup',
397+
target: container[0]
398+
});
399+
400+
runAnimation();
401+
runAnimation();
402+
403+
container = angular.element(parent[0].querySelector('.md-dialog-container'));
404+
expect(container.length).toBe(1);
405+
}));
406+
325407
it('should remove `md-dialog-container` after ESCAPE key', inject(function($mdDialog, $rootScope, $timeout, $mdConstant) {
326408
jasmine.mockElementFocus(this);
327409
var container, parent = angular.element('<div>');
@@ -569,7 +651,11 @@ describe('$mdDialog', function() {
569651
expect(parent.find('md-dialog').length).toBe(1);
570652

571653
container.triggerHandler({
572-
type: 'click',
654+
type: 'mousedown',
655+
target: container[0]
656+
});
657+
container.triggerHandler({
658+
type: 'mouseup',
573659
target: container[0]
574660
});
575661
runAnimation();

0 commit comments

Comments
 (0)