Skip to content
This repository was archived by the owner on Oct 2, 2019. It is now read-only.

fix: close drop-down regardless of stopPropagation() calls #2003

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions src/uiSelectDirective.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
uis.directive('uiSelect',
['$document', 'uiSelectConfig', 'uiSelectMinErr', 'uisOffset', '$compile', '$parse', '$timeout',
function($document, uiSelectConfig, uiSelectMinErr, uisOffset, $compile, $parse, $timeout) {
['$document', 'uiSelectConfig', 'uiSelectMinErr', 'uisOffset', '$compile', '$parse', '$timeout', '$window',
function($document, uiSelectConfig, uiSelectMinErr, uisOffset, $compile, $parse, $timeout, $window) {

return {
restrict: 'EA',
Expand Down Expand Up @@ -206,11 +206,15 @@ uis.directive('uiSelect',
$select.clickTriggeredSelect = false;
}

// See Click everywhere but here event http://stackoverflow.com/questions/12931369
$document.on('click', onDocumentClick);
// See Click everywhere but here. Similar approach to http://stackoverflow.com/questions/12931369
// but using the capture phase instead of bubble phase of the event propagation.
//
// Using the capture phase avoids problems that araise when event.stopPropatagion()
// is called before the event reaches the `document`.
$window.document.addEventListener('click', onDocumentClick, true);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why should we not use $document.addEventListener instead of $window.document?

Copy link
Contributor Author

@Reico3 Reico3 Jun 8, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In uiSelectchoicesDirective.js#L51 $window.document is used, so i wanted to be consistent.

Also it is not possible to add a native addEventListener to the angular $document, which is a jQuery window.document wrapper.

jQuery's aim is browser compability and capture phase is not supported by older IE's. Because Angular only supports IE9+ this should not be an issue.


scope.$on('$destroy', function() {
$document.off('click', onDocumentClick);
$window.document.removeEventListener('click', onDocumentClick, true);
});

// Move transcluded elements to their correct position in main template
Expand Down
22 changes: 22 additions & 0 deletions test/select.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -740,6 +740,28 @@ describe('ui-select tests', function () {
el2.remove();
});

it('should close an opened select clicking outside with stopPropagation()', function () {
var el1 = createUiSelect();
var el2 = $('<div></div>');
el1.appendTo(document.body);
el2.appendTo(document.body);

el2.on('click', function (e) {
e.stopPropagation()
});

expect(isDropdownOpened(el1)).toEqual(false);
clickMatch(el1);
expect(isDropdownOpened(el1)).toEqual(true);

// Using a native dom click() to make sure the test fails when it should.
el2[0].click();

expect(isDropdownOpened(el1)).toEqual(false);
el1.remove();
el2.remove();
});

it('should bind model correctly (with object as source)', function () {
var el = compileTemplate(
'<ui-select ng-model="selection.selected"> \
Expand Down