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

Commit dee006e

Browse files
committed
Close ui-select when clicking outside
1 parent 731e2ec commit dee006e

File tree

2 files changed

+30
-10
lines changed

2 files changed

+30
-10
lines changed

dist/select.js

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,22 @@ angular.module('ui.select', [])
7979
scope.$select.selected = ngModelCtrl.$viewValue;
8080
};
8181

82-
$document.on('click', function(evt) {
83-
if (angular.element(evt.target).hasClass('ui-select-search')) {
84-
return;
82+
// See Click everywhere but here event http://stackoverflow.com/questions/12931369/click-everywhere-but-here-event
83+
$document.on('mousedown', function(evt) {
84+
var contains = false;
85+
86+
if (window.jQuery) {
87+
// Firefox 3.6 does not support element.contains()
88+
// See Node.contains https://developer.mozilla.org/en-US/docs/Web/API/Node.contains
89+
contains = $.contains(element[0], evt.target);
90+
} else {
91+
contains = element[0].contains(evt.target);
92+
}
93+
94+
if (!contains) {
95+
uiSelectCtrl.close();
96+
scope.$digest();
8597
}
86-
uiSelectCtrl.close(); // Close if clicking outside
87-
scope.$digest();
8898
});
8999

90100
// Move transcluded elements to their correct position on main template

src/select.js

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,22 @@ angular.module('ui.select', [])
7979
scope.$select.selected = ngModelCtrl.$viewValue;
8080
};
8181

82-
$document.on('click', function(evt) {
83-
if (angular.element(evt.target).hasClass('ui-select-search')) {
84-
return;
82+
// See Click everywhere but here event http://stackoverflow.com/questions/12931369/click-everywhere-but-here-event
83+
$document.on('mousedown', function(evt) {
84+
var contains = false;
85+
86+
if (window.jQuery) {
87+
// Firefox 3.6 does not support element.contains()
88+
// See Node.contains https://developer.mozilla.org/en-US/docs/Web/API/Node.contains
89+
contains = $.contains(element[0], evt.target);
90+
} else {
91+
contains = element[0].contains(evt.target);
92+
}
93+
94+
if (!contains) {
95+
uiSelectCtrl.close();
96+
scope.$digest();
8597
}
86-
uiSelectCtrl.close(); // Close if clicking outside
87-
scope.$digest();
8898
});
8999

90100
// Move transcluded elements to their correct position on main template

0 commit comments

Comments
 (0)