Skip to content

Commit 3b575ad

Browse files
committed
Register the click handler in the container
This shouldn't be in the core, it's much easier to override things in the container.
1 parent 130358e commit 3b575ad

8 files changed

Lines changed: 133 additions & 103 deletions

File tree

dist/js/select2.amd.full.js

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,32 @@ define('select2/selection/base',[
534534
container.on('selection:update', function (params) {
535535
self.update(params.data);
536536
});
537+
538+
container.on('open', function () {
539+
$(document.body).on('mousedown.select2.' + container.id, function (e) {
540+
var $target = $(e.target);
541+
542+
var $select = $target.closest('.select2');
543+
544+
var $all = $('.select2.open');
545+
546+
$all.each(function () {
547+
var $this = $(this);
548+
549+
if (this == $select[0]) {
550+
return;
551+
}
552+
553+
var $element = $this.data('element');
554+
555+
$element.select2('close');
556+
});
557+
});
558+
559+
container.on('close', function () {
560+
$(document.body).off('mousedown.select2.' + container.id);
561+
});
562+
});
537563
};
538564

539565
BaseSelection.prototype.update = function (data) {
@@ -1746,26 +1772,6 @@ define('select2/core',[
17461772
Select2.prototype._registerDomEvents = function () {
17471773
var self = this;
17481774

1749-
$(document.body).on('mousedown', function (e) {
1750-
var $target = $(e.target);
1751-
1752-
var $select = $target.closest('.select2');
1753-
1754-
var $all = $('.select2.open');
1755-
1756-
$all.each(function () {
1757-
var $this = $(this);
1758-
1759-
if (this == $select[0]) {
1760-
return;
1761-
}
1762-
1763-
var $element = $this.data('element');
1764-
1765-
$element.select2('close');
1766-
});
1767-
});
1768-
17691775
this.$element.on('change', function () {
17701776
self.data.current(function (data) {
17711777
self.trigger('selection:update', {

dist/js/select2.amd.js

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,32 @@ define('select2/selection/base',[
534534
container.on('selection:update', function (params) {
535535
self.update(params.data);
536536
});
537+
538+
container.on('open', function () {
539+
$(document.body).on('mousedown.select2.' + container.id, function (e) {
540+
var $target = $(e.target);
541+
542+
var $select = $target.closest('.select2');
543+
544+
var $all = $('.select2.open');
545+
546+
$all.each(function () {
547+
var $this = $(this);
548+
549+
if (this == $select[0]) {
550+
return;
551+
}
552+
553+
var $element = $this.data('element');
554+
555+
$element.select2('close');
556+
});
557+
});
558+
559+
container.on('close', function () {
560+
$(document.body).off('mousedown.select2.' + container.id);
561+
});
562+
});
537563
};
538564

539565
BaseSelection.prototype.update = function (data) {
@@ -1746,26 +1772,6 @@ define('select2/core',[
17461772
Select2.prototype._registerDomEvents = function () {
17471773
var self = this;
17481774

1749-
$(document.body).on('mousedown', function (e) {
1750-
var $target = $(e.target);
1751-
1752-
var $select = $target.closest('.select2');
1753-
1754-
var $all = $('.select2.open');
1755-
1756-
$all.each(function () {
1757-
var $this = $(this);
1758-
1759-
if (this == $select[0]) {
1760-
return;
1761-
}
1762-
1763-
var $element = $this.data('element');
1764-
1765-
$element.select2('close');
1766-
});
1767-
});
1768-
17691775
this.$element.on('change', function () {
17701776
self.data.current(function (data) {
17711777
self.trigger('selection:update', {

dist/js/select2.full.js

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10069,6 +10069,32 @@ define('select2/selection/base',[
1006910069
container.on('selection:update', function (params) {
1007010070
self.update(params.data);
1007110071
});
10072+
10073+
container.on('open', function () {
10074+
$(document.body).on('mousedown.select2.' + container.id, function (e) {
10075+
var $target = $(e.target);
10076+
10077+
var $select = $target.closest('.select2');
10078+
10079+
var $all = $('.select2.open');
10080+
10081+
$all.each(function () {
10082+
var $this = $(this);
10083+
10084+
if (this == $select[0]) {
10085+
return;
10086+
}
10087+
10088+
var $element = $this.data('element');
10089+
10090+
$element.select2('close');
10091+
});
10092+
});
10093+
10094+
container.on('close', function () {
10095+
$(document.body).off('mousedown.select2.' + container.id);
10096+
});
10097+
});
1007210098
};
1007310099

1007410100
BaseSelection.prototype.update = function (data) {
@@ -11281,26 +11307,6 @@ define('select2/core',[
1128111307
Select2.prototype._registerDomEvents = function () {
1128211308
var self = this;
1128311309

11284-
$(document.body).on('mousedown', function (e) {
11285-
var $target = $(e.target);
11286-
11287-
var $select = $target.closest('.select2');
11288-
11289-
var $all = $('.select2.open');
11290-
11291-
$all.each(function () {
11292-
var $this = $(this);
11293-
11294-
if (this == $select[0]) {
11295-
return;
11296-
}
11297-
11298-
var $element = $this.data('element');
11299-
11300-
$element.select2('close');
11301-
});
11302-
});
11303-
1130411310
this.$element.on('change', function () {
1130511311
self.data.current(function (data) {
1130611312
self.trigger('selection:update', {

dist/js/select2.full.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/js/select2.js

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -962,6 +962,32 @@ define('select2/selection/base',[
962962
container.on('selection:update', function (params) {
963963
self.update(params.data);
964964
});
965+
966+
container.on('open', function () {
967+
$(document.body).on('mousedown.select2.' + container.id, function (e) {
968+
var $target = $(e.target);
969+
970+
var $select = $target.closest('.select2');
971+
972+
var $all = $('.select2.open');
973+
974+
$all.each(function () {
975+
var $this = $(this);
976+
977+
if (this == $select[0]) {
978+
return;
979+
}
980+
981+
var $element = $this.data('element');
982+
983+
$element.select2('close');
984+
});
985+
});
986+
987+
container.on('close', function () {
988+
$(document.body).off('mousedown.select2.' + container.id);
989+
});
990+
});
965991
};
966992

967993
BaseSelection.prototype.update = function (data) {
@@ -2174,26 +2200,6 @@ define('select2/core',[
21742200
Select2.prototype._registerDomEvents = function () {
21752201
var self = this;
21762202

2177-
$(document.body).on('mousedown', function (e) {
2178-
var $target = $(e.target);
2179-
2180-
var $select = $target.closest('.select2');
2181-
2182-
var $all = $('.select2.open');
2183-
2184-
$all.each(function () {
2185-
var $this = $(this);
2186-
2187-
if (this == $select[0]) {
2188-
return;
2189-
}
2190-
2191-
var $element = $this.data('element');
2192-
2193-
$element.select2('close');
2194-
});
2195-
});
2196-
21972203
this.$element.on('change', function () {
21982204
self.data.current(function (data) {
21992205
self.trigger('selection:update', {

dist/js/select2.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/js/select2/core.js

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -128,26 +128,6 @@ define([
128128
Select2.prototype._registerDomEvents = function () {
129129
var self = this;
130130

131-
$(document.body).on('mousedown', function (e) {
132-
var $target = $(e.target);
133-
134-
var $select = $target.closest('.select2');
135-
136-
var $all = $('.select2.open');
137-
138-
$all.each(function () {
139-
var $this = $(this);
140-
141-
if (this == $select[0]) {
142-
return;
143-
}
144-
145-
var $element = $this.data('element');
146-
147-
$element.select2('close');
148-
});
149-
});
150-
151131
this.$element.on('change', function () {
152132
self.data.current(function (data) {
153133
self.trigger('selection:update', {

src/js/select2/selection/base.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,32 @@ define([
2020
container.on('selection:update', function (params) {
2121
self.update(params.data);
2222
});
23+
24+
container.on('open', function () {
25+
$(document.body).on('mousedown.select2.' + container.id, function (e) {
26+
var $target = $(e.target);
27+
28+
var $select = $target.closest('.select2');
29+
30+
var $all = $('.select2.open');
31+
32+
$all.each(function () {
33+
var $this = $(this);
34+
35+
if (this == $select[0]) {
36+
return;
37+
}
38+
39+
var $element = $this.data('element');
40+
41+
$element.select2('close');
42+
});
43+
});
44+
45+
container.on('close', function () {
46+
$(document.body).off('mousedown.select2.' + container.id);
47+
});
48+
});
2349
};
2450

2551
BaseSelection.prototype.update = function (data) {

0 commit comments

Comments
 (0)