1
0
mirror of synced 2024-11-26 06:46:04 +03:00

Reverted changes to the scroll detection

This reverts the changes (from 3.5.x) to the scroll detection, so
Select2 will still reposition itself every time the window is
scrolled or resized. This has the benefit of fixing the issue when
the screen jumps if the dropdown is too high, which would previously
close the dropdown.

Select2 will also correctly set the positioning if it is pushed out
of the top of the viewport. Due to an unexpected bug, Select2 would
previously still display above the container, but would be styled
as if it were below it.

This closes https://github.com/select2/select2/issues/2961.
This closes https://github.com/select2/select2/issues/2956.
This commit is contained in:
Kevin Brown 2015-01-22 17:35:08 -05:00
parent 981c4065af
commit 6488551374
8 changed files with 129 additions and 34 deletions

View File

@ -826,15 +826,10 @@ define('select2/selection/base',[
$element.select2('close'); $element.select2('close');
}); });
}); });
$(window).on('scroll.select2.' + container.id, function (e) {
self.trigger('close');
});
}; };
BaseSelection.prototype._detachCloseHandler = function (container) { BaseSelection.prototype._detachCloseHandler = function (container) {
$(document.body).off('mousedown.select2.' + container.id); $(document.body).off('mousedown.select2.' + container.id);
$(window).off('scroll.select2.' + container.id);
}; };
BaseSelection.prototype.position = function ($selection, $container) { BaseSelection.prototype.position = function ($selection, $container) {
@ -3274,6 +3269,7 @@ define('select2/dropdown/attachBody',[
container.on('open', function () { container.on('open', function () {
self._showDropdown(); self._showDropdown();
self._attachPositioningHandler(container);
if (!setupResultsEvents) { if (!setupResultsEvents) {
setupResultsEvents = true; setupResultsEvents = true;
@ -3290,6 +3286,7 @@ define('select2/dropdown/attachBody',[
container.on('close', function () { container.on('close', function () {
self._hideDropdown(); self._hideDropdown();
self._detachPositioningHandler(container);
}); });
this.$dropdownContainer.on('mousedown', function (evt) { this.$dropdownContainer.on('mousedown', function (evt) {
@ -3329,6 +3326,27 @@ define('select2/dropdown/attachBody',[
this.$dropdownContainer.detach(); this.$dropdownContainer.detach();
}; };
AttachBody.prototype._attachPositioningHandler = function (container) {
var self = this;
var scrollEvent = 'scroll.select2.' + container.id;
var resizeEvent = 'resize.select2.' + container.id;
var orientationEvent = 'orientationchange.select2.' + container.id;
$(window).on(scrollEvent + ' ' + resizeEvent + ' ' + orientationEvent,
function (e) {
self._positionDropdown();
});
};
AttachBody.prototype._detachPositioningHandler = function (container) {
var scrollEvent = 'scroll.select2.' + container.id;
var resizeEvent = 'resize.select2.' + container.id;
var orientationEvent = 'orientationchange.select2.' + container.id;
$(window).off(scrollEvent + ' ' + resizeEvent + ' ' + orientationEvent);
};
AttachBody.prototype._positionDropdown = function () { AttachBody.prototype._positionDropdown = function () {
var $window = $(window); var $window = $(window);
@ -3376,7 +3394,8 @@ define('select2/dropdown/attachBody',[
newDirection = 'below'; newDirection = 'below';
} }
if (newDirection == 'above' || isCurrentlyAbove) { if (newDirection == 'above' ||
(isCurrentlyAbove && newDirection !== 'below')) {
css.top = container.top - dropdown.height; css.top = container.top - dropdown.height;
} }

View File

@ -826,15 +826,10 @@ define('select2/selection/base',[
$element.select2('close'); $element.select2('close');
}); });
}); });
$(window).on('scroll.select2.' + container.id, function (e) {
self.trigger('close');
});
}; };
BaseSelection.prototype._detachCloseHandler = function (container) { BaseSelection.prototype._detachCloseHandler = function (container) {
$(document.body).off('mousedown.select2.' + container.id); $(document.body).off('mousedown.select2.' + container.id);
$(window).off('scroll.select2.' + container.id);
}; };
BaseSelection.prototype.position = function ($selection, $container) { BaseSelection.prototype.position = function ($selection, $container) {
@ -3274,6 +3269,7 @@ define('select2/dropdown/attachBody',[
container.on('open', function () { container.on('open', function () {
self._showDropdown(); self._showDropdown();
self._attachPositioningHandler(container);
if (!setupResultsEvents) { if (!setupResultsEvents) {
setupResultsEvents = true; setupResultsEvents = true;
@ -3290,6 +3286,7 @@ define('select2/dropdown/attachBody',[
container.on('close', function () { container.on('close', function () {
self._hideDropdown(); self._hideDropdown();
self._detachPositioningHandler(container);
}); });
this.$dropdownContainer.on('mousedown', function (evt) { this.$dropdownContainer.on('mousedown', function (evt) {
@ -3329,6 +3326,27 @@ define('select2/dropdown/attachBody',[
this.$dropdownContainer.detach(); this.$dropdownContainer.detach();
}; };
AttachBody.prototype._attachPositioningHandler = function (container) {
var self = this;
var scrollEvent = 'scroll.select2.' + container.id;
var resizeEvent = 'resize.select2.' + container.id;
var orientationEvent = 'orientationchange.select2.' + container.id;
$(window).on(scrollEvent + ' ' + resizeEvent + ' ' + orientationEvent,
function (e) {
self._positionDropdown();
});
};
AttachBody.prototype._detachPositioningHandler = function (container) {
var scrollEvent = 'scroll.select2.' + container.id;
var resizeEvent = 'resize.select2.' + container.id;
var orientationEvent = 'orientationchange.select2.' + container.id;
$(window).off(scrollEvent + ' ' + resizeEvent + ' ' + orientationEvent);
};
AttachBody.prototype._positionDropdown = function () { AttachBody.prototype._positionDropdown = function () {
var $window = $(window); var $window = $(window);
@ -3376,7 +3394,8 @@ define('select2/dropdown/attachBody',[
newDirection = 'below'; newDirection = 'below';
} }
if (newDirection == 'above' || isCurrentlyAbove) { if (newDirection == 'above' ||
(isCurrentlyAbove && newDirection !== 'below')) {
css.top = container.top - dropdown.height; css.top = container.top - dropdown.height;
} }

View File

@ -1264,15 +1264,10 @@ define('select2/selection/base',[
$element.select2('close'); $element.select2('close');
}); });
}); });
$(window).on('scroll.select2.' + container.id, function (e) {
self.trigger('close');
});
}; };
BaseSelection.prototype._detachCloseHandler = function (container) { BaseSelection.prototype._detachCloseHandler = function (container) {
$(document.body).off('mousedown.select2.' + container.id); $(document.body).off('mousedown.select2.' + container.id);
$(window).off('scroll.select2.' + container.id);
}; };
BaseSelection.prototype.position = function ($selection, $container) { BaseSelection.prototype.position = function ($selection, $container) {
@ -3712,6 +3707,7 @@ define('select2/dropdown/attachBody',[
container.on('open', function () { container.on('open', function () {
self._showDropdown(); self._showDropdown();
self._attachPositioningHandler(container);
if (!setupResultsEvents) { if (!setupResultsEvents) {
setupResultsEvents = true; setupResultsEvents = true;
@ -3728,6 +3724,7 @@ define('select2/dropdown/attachBody',[
container.on('close', function () { container.on('close', function () {
self._hideDropdown(); self._hideDropdown();
self._detachPositioningHandler(container);
}); });
this.$dropdownContainer.on('mousedown', function (evt) { this.$dropdownContainer.on('mousedown', function (evt) {
@ -3767,6 +3764,27 @@ define('select2/dropdown/attachBody',[
this.$dropdownContainer.detach(); this.$dropdownContainer.detach();
}; };
AttachBody.prototype._attachPositioningHandler = function (container) {
var self = this;
var scrollEvent = 'scroll.select2.' + container.id;
var resizeEvent = 'resize.select2.' + container.id;
var orientationEvent = 'orientationchange.select2.' + container.id;
$(window).on(scrollEvent + ' ' + resizeEvent + ' ' + orientationEvent,
function (e) {
self._positionDropdown();
});
};
AttachBody.prototype._detachPositioningHandler = function (container) {
var scrollEvent = 'scroll.select2.' + container.id;
var resizeEvent = 'resize.select2.' + container.id;
var orientationEvent = 'orientationchange.select2.' + container.id;
$(window).off(scrollEvent + ' ' + resizeEvent + ' ' + orientationEvent);
};
AttachBody.prototype._positionDropdown = function () { AttachBody.prototype._positionDropdown = function () {
var $window = $(window); var $window = $(window);
@ -3814,7 +3832,8 @@ define('select2/dropdown/attachBody',[
newDirection = 'below'; newDirection = 'below';
} }
if (newDirection == 'above' || isCurrentlyAbove) { if (newDirection == 'above' ||
(isCurrentlyAbove && newDirection !== 'below')) {
css.top = container.top - dropdown.height; css.top = container.top - dropdown.height;
} }

File diff suppressed because one or more lines are too long

31
dist/js/select2.js vendored
View File

@ -1264,15 +1264,10 @@ define('select2/selection/base',[
$element.select2('close'); $element.select2('close');
}); });
}); });
$(window).on('scroll.select2.' + container.id, function (e) {
self.trigger('close');
});
}; };
BaseSelection.prototype._detachCloseHandler = function (container) { BaseSelection.prototype._detachCloseHandler = function (container) {
$(document.body).off('mousedown.select2.' + container.id); $(document.body).off('mousedown.select2.' + container.id);
$(window).off('scroll.select2.' + container.id);
}; };
BaseSelection.prototype.position = function ($selection, $container) { BaseSelection.prototype.position = function ($selection, $container) {
@ -3712,6 +3707,7 @@ define('select2/dropdown/attachBody',[
container.on('open', function () { container.on('open', function () {
self._showDropdown(); self._showDropdown();
self._attachPositioningHandler(container);
if (!setupResultsEvents) { if (!setupResultsEvents) {
setupResultsEvents = true; setupResultsEvents = true;
@ -3728,6 +3724,7 @@ define('select2/dropdown/attachBody',[
container.on('close', function () { container.on('close', function () {
self._hideDropdown(); self._hideDropdown();
self._detachPositioningHandler(container);
}); });
this.$dropdownContainer.on('mousedown', function (evt) { this.$dropdownContainer.on('mousedown', function (evt) {
@ -3767,6 +3764,27 @@ define('select2/dropdown/attachBody',[
this.$dropdownContainer.detach(); this.$dropdownContainer.detach();
}; };
AttachBody.prototype._attachPositioningHandler = function (container) {
var self = this;
var scrollEvent = 'scroll.select2.' + container.id;
var resizeEvent = 'resize.select2.' + container.id;
var orientationEvent = 'orientationchange.select2.' + container.id;
$(window).on(scrollEvent + ' ' + resizeEvent + ' ' + orientationEvent,
function (e) {
self._positionDropdown();
});
};
AttachBody.prototype._detachPositioningHandler = function (container) {
var scrollEvent = 'scroll.select2.' + container.id;
var resizeEvent = 'resize.select2.' + container.id;
var orientationEvent = 'orientationchange.select2.' + container.id;
$(window).off(scrollEvent + ' ' + resizeEvent + ' ' + orientationEvent);
};
AttachBody.prototype._positionDropdown = function () { AttachBody.prototype._positionDropdown = function () {
var $window = $(window); var $window = $(window);
@ -3814,7 +3832,8 @@ define('select2/dropdown/attachBody',[
newDirection = 'below'; newDirection = 'below';
} }
if (newDirection == 'above' || isCurrentlyAbove) { if (newDirection == 'above' ||
(isCurrentlyAbove && newDirection !== 'below')) {
css.top = container.top - dropdown.height; css.top = container.top - dropdown.height;
} }

File diff suppressed because one or more lines are too long

View File

@ -16,6 +16,7 @@ define([
container.on('open', function () { container.on('open', function () {
self._showDropdown(); self._showDropdown();
self._attachPositioningHandler(container);
if (!setupResultsEvents) { if (!setupResultsEvents) {
setupResultsEvents = true; setupResultsEvents = true;
@ -32,6 +33,7 @@ define([
container.on('close', function () { container.on('close', function () {
self._hideDropdown(); self._hideDropdown();
self._detachPositioningHandler(container);
}); });
this.$dropdownContainer.on('mousedown', function (evt) { this.$dropdownContainer.on('mousedown', function (evt) {
@ -71,6 +73,27 @@ define([
this.$dropdownContainer.detach(); this.$dropdownContainer.detach();
}; };
AttachBody.prototype._attachPositioningHandler = function (container) {
var self = this;
var scrollEvent = 'scroll.select2.' + container.id;
var resizeEvent = 'resize.select2.' + container.id;
var orientationEvent = 'orientationchange.select2.' + container.id;
$(window).on(scrollEvent + ' ' + resizeEvent + ' ' + orientationEvent,
function (e) {
self._positionDropdown();
});
};
AttachBody.prototype._detachPositioningHandler = function (container) {
var scrollEvent = 'scroll.select2.' + container.id;
var resizeEvent = 'resize.select2.' + container.id;
var orientationEvent = 'orientationchange.select2.' + container.id;
$(window).off(scrollEvent + ' ' + resizeEvent + ' ' + orientationEvent);
};
AttachBody.prototype._positionDropdown = function () { AttachBody.prototype._positionDropdown = function () {
var $window = $(window); var $window = $(window);
@ -118,7 +141,8 @@ define([
newDirection = 'below'; newDirection = 'below';
} }
if (newDirection == 'above' || isCurrentlyAbove) { if (newDirection == 'above' ||
(isCurrentlyAbove && newDirection !== 'below')) {
css.top = container.top - dropdown.height; css.top = container.top - dropdown.height;
} }

View File

@ -100,15 +100,10 @@ define([
$element.select2('close'); $element.select2('close');
}); });
}); });
$(window).on('scroll.select2.' + container.id, function (e) {
self.trigger('close');
});
}; };
BaseSelection.prototype._detachCloseHandler = function (container) { BaseSelection.prototype._detachCloseHandler = function (container) {
$(document.body).off('mousedown.select2.' + container.id); $(document.body).off('mousedown.select2.' + container.id);
$(window).off('scroll.select2.' + container.id);
}; };
BaseSelection.prototype.position = function ($selection, $container) { BaseSelection.prototype.position = function ($selection, $container) {