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

Commit

Permalink
Merge pull request #2003 from nzzdev/changeEventPropagationMode-onDoc…
Browse files Browse the repository at this point in the history
…umentclick

fix: close drop-down regardless of `stopPropagation()` calls
  • Loading branch information
Jefiozie authored Aug 3, 2017
2 parents 50c6273 + 6f19d5f commit 2b0b17b
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
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);

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 @@ -746,6 +746,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

0 comments on commit 2b0b17b

Please sign in to comment.