Skip to content

Commit be3414f

Browse files
committed
Moved the body click handlers out
This should allow us to start working on the body mask handler, and make future implementations easier to work with.
1 parent 85593de commit be3414f

7 files changed

Lines changed: 143 additions & 103 deletions

File tree

dist/js/select2.amd.full.js

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -684,25 +684,7 @@ define('select2/selection/base',[
684684
// When the dropdown is open, aria-expanded="true"
685685
self.$selection.attr('aria-expanded', 'true');
686686

687-
$(document.body).on('mousedown.select2.' + container.id, function (e) {
688-
var $target = $(e.target);
689-
690-
var $select = $target.closest('.select2');
691-
692-
var $all = $('.select2.select2-container--open');
693-
694-
$all.each(function () {
695-
var $this = $(this);
696-
697-
if (this == $select[0]) {
698-
return;
699-
}
700-
701-
var $element = $this.data('element');
702-
703-
$element.select2('close');
704-
});
705-
});
687+
self._attachCloseHandler(container);
706688
});
707689

708690
container.on('close', function () {
@@ -712,10 +694,36 @@ define('select2/selection/base',[
712694

713695
self.$selection.focus();
714696

715-
$(document.body).off('mousedown.select2.' + container.id);
697+
self._detachCloseHandler(container);
716698
});
717699
};
718700

701+
BaseSelection.prototype._attachCloseHandler = function (container) {
702+
$(document.body).on('mousedown.select2.' + container.id, function (e) {
703+
var $target = $(e.target);
704+
705+
var $select = $target.closest('.select2');
706+
707+
var $all = $('.select2.select2-container--open');
708+
709+
$all.each(function () {
710+
var $this = $(this);
711+
712+
if (this == $select[0]) {
713+
return;
714+
}
715+
716+
var $element = $this.data('element');
717+
718+
$element.select2('close');
719+
});
720+
});
721+
};
722+
723+
BaseSelection.prototype._detachCloseHandler = function (container) {
724+
$(document.body).off('mousedown.select2.' + container.id);
725+
};
726+
719727
BaseSelection.prototype.destroy = function () {
720728
// Unbind the dropdown click handler if it exists
721729
$(document.body).off('.select2.' + this.container.id);

dist/js/select2.amd.js

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -684,25 +684,7 @@ define('select2/selection/base',[
684684
// When the dropdown is open, aria-expanded="true"
685685
self.$selection.attr('aria-expanded', 'true');
686686

687-
$(document.body).on('mousedown.select2.' + container.id, function (e) {
688-
var $target = $(e.target);
689-
690-
var $select = $target.closest('.select2');
691-
692-
var $all = $('.select2.select2-container--open');
693-
694-
$all.each(function () {
695-
var $this = $(this);
696-
697-
if (this == $select[0]) {
698-
return;
699-
}
700-
701-
var $element = $this.data('element');
702-
703-
$element.select2('close');
704-
});
705-
});
687+
self._attachCloseHandler(container);
706688
});
707689

708690
container.on('close', function () {
@@ -712,10 +694,36 @@ define('select2/selection/base',[
712694

713695
self.$selection.focus();
714696

715-
$(document.body).off('mousedown.select2.' + container.id);
697+
self._detachCloseHandler(container);
716698
});
717699
};
718700

701+
BaseSelection.prototype._attachCloseHandler = function (container) {
702+
$(document.body).on('mousedown.select2.' + container.id, function (e) {
703+
var $target = $(e.target);
704+
705+
var $select = $target.closest('.select2');
706+
707+
var $all = $('.select2.select2-container--open');
708+
709+
$all.each(function () {
710+
var $this = $(this);
711+
712+
if (this == $select[0]) {
713+
return;
714+
}
715+
716+
var $element = $this.data('element');
717+
718+
$element.select2('close');
719+
});
720+
});
721+
};
722+
723+
BaseSelection.prototype._detachCloseHandler = function (container) {
724+
$(document.body).off('mousedown.select2.' + container.id);
725+
};
726+
719727
BaseSelection.prototype.destroy = function () {
720728
// Unbind the dropdown click handler if it exists
721729
$(document.body).off('.select2.' + this.container.id);

dist/js/select2.full.js

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10219,25 +10219,7 @@ define('select2/selection/base',[
1021910219
// When the dropdown is open, aria-expanded="true"
1022010220
self.$selection.attr('aria-expanded', 'true');
1022110221

10222-
$(document.body).on('mousedown.select2.' + container.id, function (e) {
10223-
var $target = $(e.target);
10224-
10225-
var $select = $target.closest('.select2');
10226-
10227-
var $all = $('.select2.select2-container--open');
10228-
10229-
$all.each(function () {
10230-
var $this = $(this);
10231-
10232-
if (this == $select[0]) {
10233-
return;
10234-
}
10235-
10236-
var $element = $this.data('element');
10237-
10238-
$element.select2('close');
10239-
});
10240-
});
10222+
self._attachCloseHandler(container);
1024110223
});
1024210224

1024310225
container.on('close', function () {
@@ -10247,10 +10229,36 @@ define('select2/selection/base',[
1024710229

1024810230
self.$selection.focus();
1024910231

10250-
$(document.body).off('mousedown.select2.' + container.id);
10232+
self._detachCloseHandler(container);
10233+
});
10234+
};
10235+
10236+
BaseSelection.prototype._attachCloseHandler = function (container) {
10237+
$(document.body).on('mousedown.select2.' + container.id, function (e) {
10238+
var $target = $(e.target);
10239+
10240+
var $select = $target.closest('.select2');
10241+
10242+
var $all = $('.select2.select2-container--open');
10243+
10244+
$all.each(function () {
10245+
var $this = $(this);
10246+
10247+
if (this == $select[0]) {
10248+
return;
10249+
}
10250+
10251+
var $element = $this.data('element');
10252+
10253+
$element.select2('close');
10254+
});
1025110255
});
1025210256
};
1025310257

10258+
BaseSelection.prototype._detachCloseHandler = function (container) {
10259+
$(document.body).off('mousedown.select2.' + container.id);
10260+
};
10261+
1025410262
BaseSelection.prototype.destroy = function () {
1025510263
// Unbind the dropdown click handler if it exists
1025610264
$(document.body).off('.select2.' + this.container.id);

dist/js/select2.full.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.

dist/js/select2.js

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1112,25 +1112,7 @@ define('select2/selection/base',[
11121112
// When the dropdown is open, aria-expanded="true"
11131113
self.$selection.attr('aria-expanded', 'true');
11141114

1115-
$(document.body).on('mousedown.select2.' + container.id, function (e) {
1116-
var $target = $(e.target);
1117-
1118-
var $select = $target.closest('.select2');
1119-
1120-
var $all = $('.select2.select2-container--open');
1121-
1122-
$all.each(function () {
1123-
var $this = $(this);
1124-
1125-
if (this == $select[0]) {
1126-
return;
1127-
}
1128-
1129-
var $element = $this.data('element');
1130-
1131-
$element.select2('close');
1132-
});
1133-
});
1115+
self._attachCloseHandler(container);
11341116
});
11351117

11361118
container.on('close', function () {
@@ -1140,10 +1122,36 @@ define('select2/selection/base',[
11401122

11411123
self.$selection.focus();
11421124

1143-
$(document.body).off('mousedown.select2.' + container.id);
1125+
self._detachCloseHandler(container);
11441126
});
11451127
};
11461128

1129+
BaseSelection.prototype._attachCloseHandler = function (container) {
1130+
$(document.body).on('mousedown.select2.' + container.id, function (e) {
1131+
var $target = $(e.target);
1132+
1133+
var $select = $target.closest('.select2');
1134+
1135+
var $all = $('.select2.select2-container--open');
1136+
1137+
$all.each(function () {
1138+
var $this = $(this);
1139+
1140+
if (this == $select[0]) {
1141+
return;
1142+
}
1143+
1144+
var $element = $this.data('element');
1145+
1146+
$element.select2('close');
1147+
});
1148+
});
1149+
};
1150+
1151+
BaseSelection.prototype._detachCloseHandler = function (container) {
1152+
$(document.body).off('mousedown.select2.' + container.id);
1153+
};
1154+
11471155
BaseSelection.prototype.destroy = function () {
11481156
// Unbind the dropdown click handler if it exists
11491157
$(document.body).off('.select2.' + this.container.id);

dist/js/select2.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.

src/js/select2/selection/base.js

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -45,25 +45,7 @@ define([
4545
// When the dropdown is open, aria-expanded="true"
4646
self.$selection.attr('aria-expanded', 'true');
4747

48-
$(document.body).on('mousedown.select2.' + container.id, function (e) {
49-
var $target = $(e.target);
50-
51-
var $select = $target.closest('.select2');
52-
53-
var $all = $('.select2.select2-container--open');
54-
55-
$all.each(function () {
56-
var $this = $(this);
57-
58-
if (this == $select[0]) {
59-
return;
60-
}
61-
62-
var $element = $this.data('element');
63-
64-
$element.select2('close');
65-
});
66-
});
48+
self._attachCloseHandler(container);
6749
});
6850

6951
container.on('close', function () {
@@ -73,10 +55,36 @@ define([
7355

7456
self.$selection.focus();
7557

76-
$(document.body).off('mousedown.select2.' + container.id);
58+
self._detachCloseHandler(container);
7759
});
7860
};
7961

62+
BaseSelection.prototype._attachCloseHandler = function (container) {
63+
$(document.body).on('mousedown.select2.' + container.id, function (e) {
64+
var $target = $(e.target);
65+
66+
var $select = $target.closest('.select2');
67+
68+
var $all = $('.select2.select2-container--open');
69+
70+
$all.each(function () {
71+
var $this = $(this);
72+
73+
if (this == $select[0]) {
74+
return;
75+
}
76+
77+
var $element = $this.data('element');
78+
79+
$element.select2('close');
80+
});
81+
});
82+
};
83+
84+
BaseSelection.prototype._detachCloseHandler = function (container) {
85+
$(document.body).off('mousedown.select2.' + container.id);
86+
};
87+
8088
BaseSelection.prototype.destroy = function () {
8189
// Unbind the dropdown click handler if it exists
8290
$(document.body).off('.select2.' + this.container.id);

0 commit comments

Comments
 (0)