Prevent scrolling in scrollable containers
This prevents scrolling within scrollable containers (excluding the dropdown) when the dropdown is open. This fixes an issue where the dropdown would go out of sync with the parent container when the parent container was scrolled. We did not have this issue in past versions of Select2 because the mask prevented any scrolling. Now that we have removed the mask, we have to deal with scrolling from different areas of the page. We initially tried to hook into the `scroll` events of the parent containers, but because of a list of issues we decided against it. If the container scrolled out of view, the dropdown would still be left open and above everything else, even though the container wasn't visually connected to it. The `scroll` event does not bubble, so we need to attach the `scroll` handler to every parent element that is scrollable. Since it is surprisingly difficult to determine if an element is scrollable, we modified some CC-BY-SA code and use that to determine if the element has a scrollbar. The original `hasScroll` function can be found at http://codereview.stackexchange.com/q/13338, the same link left within the code, and was originally designed to be a sizzle selector. As Select2 does not require a sizzle-compatible version of jQuery, we converted it into a function that could be used with `.filter` to filter down the elements. This closes https://github.com/select2/select2/issues/2975.
This commit is contained in:
parent
000653498f
commit
003d6053a9
46
dist/js/select2.amd.full.js
vendored
46
dist/js/select2.amd.full.js
vendored
@ -193,6 +193,31 @@ define(['jquery'], function ($) {define('select2/utils',[], function () {
|
|||||||
return data;
|
return data;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Utils.hasScroll = function (index, el) {
|
||||||
|
// Adapted from the function created by @ShadowScripter
|
||||||
|
// and adapted by @BillBarry on the Stack Exchange Code Review website.
|
||||||
|
// The original code can be found at
|
||||||
|
// http://codereview.stackexchange.com/q/13338
|
||||||
|
// and was designed to be used with the Sizzle selector engine.
|
||||||
|
|
||||||
|
var $el = $(el);
|
||||||
|
var overflowX = el.style.overflowX;
|
||||||
|
var overflowY = el.style.overflowY;
|
||||||
|
|
||||||
|
//Check both x and y declarations
|
||||||
|
if (overflowX === overflowY &&
|
||||||
|
(overflowY === 'hidden' || overflowY === 'visible')) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (overflowX === 'scroll' || overflowY === 'scroll') {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return ($el.innerHeight() < el.scrollHeight ||
|
||||||
|
$el.innerWidth() < el.scrollWidth);
|
||||||
|
};
|
||||||
|
|
||||||
return Utils;
|
return Utils;
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -3252,8 +3277,9 @@ define('select2/dropdown/infiniteScroll',[
|
|||||||
});
|
});
|
||||||
|
|
||||||
define('select2/dropdown/attachBody',[
|
define('select2/dropdown/attachBody',[
|
||||||
'jquery'
|
'jquery',
|
||||||
], function ($) {
|
'../utils'
|
||||||
|
], function ($, Utils) {
|
||||||
function AttachBody (decorated, $element, options) {
|
function AttachBody (decorated, $element, options) {
|
||||||
this.$dropdownParent = options.get('dropdownParent') || document.body;
|
this.$dropdownParent = options.get('dropdownParent') || document.body;
|
||||||
|
|
||||||
@ -3333,6 +3359,19 @@ define('select2/dropdown/attachBody',[
|
|||||||
var resizeEvent = 'resize.select2.' + container.id;
|
var resizeEvent = 'resize.select2.' + container.id;
|
||||||
var orientationEvent = 'orientationchange.select2.' + container.id;
|
var orientationEvent = 'orientationchange.select2.' + container.id;
|
||||||
|
|
||||||
|
$watchers = this.$container.parents().filter(Utils.hasScroll);
|
||||||
|
$watchers.each(function () {
|
||||||
|
$(this).data('select2-scroll-position', {
|
||||||
|
x: $(this).scrollLeft(),
|
||||||
|
y: $(this).scrollTop()
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
$watchers.on(scrollEvent, function (ev) {
|
||||||
|
var position = $(this).data('select2-scroll-position');
|
||||||
|
$(this).scrollTop(position.y);
|
||||||
|
});
|
||||||
|
|
||||||
$(window).on(scrollEvent + ' ' + resizeEvent + ' ' + orientationEvent,
|
$(window).on(scrollEvent + ' ' + resizeEvent + ' ' + orientationEvent,
|
||||||
function (e) {
|
function (e) {
|
||||||
self._positionDropdown();
|
self._positionDropdown();
|
||||||
@ -3345,6 +3384,9 @@ define('select2/dropdown/attachBody',[
|
|||||||
var resizeEvent = 'resize.select2.' + container.id;
|
var resizeEvent = 'resize.select2.' + container.id;
|
||||||
var orientationEvent = 'orientationchange.select2.' + container.id;
|
var orientationEvent = 'orientationchange.select2.' + container.id;
|
||||||
|
|
||||||
|
$watchers = this.$container.parents().filter(Utils.hasScroll);
|
||||||
|
$watchers.off(scrollEvent);
|
||||||
|
|
||||||
$(window).off(scrollEvent + ' ' + resizeEvent + ' ' + orientationEvent);
|
$(window).off(scrollEvent + ' ' + resizeEvent + ' ' + orientationEvent);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
46
dist/js/select2.amd.js
vendored
46
dist/js/select2.amd.js
vendored
@ -193,6 +193,31 @@ define(['jquery'], function ($) {define('select2/utils',[], function () {
|
|||||||
return data;
|
return data;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Utils.hasScroll = function (index, el) {
|
||||||
|
// Adapted from the function created by @ShadowScripter
|
||||||
|
// and adapted by @BillBarry on the Stack Exchange Code Review website.
|
||||||
|
// The original code can be found at
|
||||||
|
// http://codereview.stackexchange.com/q/13338
|
||||||
|
// and was designed to be used with the Sizzle selector engine.
|
||||||
|
|
||||||
|
var $el = $(el);
|
||||||
|
var overflowX = el.style.overflowX;
|
||||||
|
var overflowY = el.style.overflowY;
|
||||||
|
|
||||||
|
//Check both x and y declarations
|
||||||
|
if (overflowX === overflowY &&
|
||||||
|
(overflowY === 'hidden' || overflowY === 'visible')) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (overflowX === 'scroll' || overflowY === 'scroll') {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return ($el.innerHeight() < el.scrollHeight ||
|
||||||
|
$el.innerWidth() < el.scrollWidth);
|
||||||
|
};
|
||||||
|
|
||||||
return Utils;
|
return Utils;
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -3252,8 +3277,9 @@ define('select2/dropdown/infiniteScroll',[
|
|||||||
});
|
});
|
||||||
|
|
||||||
define('select2/dropdown/attachBody',[
|
define('select2/dropdown/attachBody',[
|
||||||
'jquery'
|
'jquery',
|
||||||
], function ($) {
|
'../utils'
|
||||||
|
], function ($, Utils) {
|
||||||
function AttachBody (decorated, $element, options) {
|
function AttachBody (decorated, $element, options) {
|
||||||
this.$dropdownParent = options.get('dropdownParent') || document.body;
|
this.$dropdownParent = options.get('dropdownParent') || document.body;
|
||||||
|
|
||||||
@ -3333,6 +3359,19 @@ define('select2/dropdown/attachBody',[
|
|||||||
var resizeEvent = 'resize.select2.' + container.id;
|
var resizeEvent = 'resize.select2.' + container.id;
|
||||||
var orientationEvent = 'orientationchange.select2.' + container.id;
|
var orientationEvent = 'orientationchange.select2.' + container.id;
|
||||||
|
|
||||||
|
$watchers = this.$container.parents().filter(Utils.hasScroll);
|
||||||
|
$watchers.each(function () {
|
||||||
|
$(this).data('select2-scroll-position', {
|
||||||
|
x: $(this).scrollLeft(),
|
||||||
|
y: $(this).scrollTop()
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
$watchers.on(scrollEvent, function (ev) {
|
||||||
|
var position = $(this).data('select2-scroll-position');
|
||||||
|
$(this).scrollTop(position.y);
|
||||||
|
});
|
||||||
|
|
||||||
$(window).on(scrollEvent + ' ' + resizeEvent + ' ' + orientationEvent,
|
$(window).on(scrollEvent + ' ' + resizeEvent + ' ' + orientationEvent,
|
||||||
function (e) {
|
function (e) {
|
||||||
self._positionDropdown();
|
self._positionDropdown();
|
||||||
@ -3345,6 +3384,9 @@ define('select2/dropdown/attachBody',[
|
|||||||
var resizeEvent = 'resize.select2.' + container.id;
|
var resizeEvent = 'resize.select2.' + container.id;
|
||||||
var orientationEvent = 'orientationchange.select2.' + container.id;
|
var orientationEvent = 'orientationchange.select2.' + container.id;
|
||||||
|
|
||||||
|
$watchers = this.$container.parents().filter(Utils.hasScroll);
|
||||||
|
$watchers.off(scrollEvent);
|
||||||
|
|
||||||
$(window).off(scrollEvent + ' ' + resizeEvent + ' ' + orientationEvent);
|
$(window).off(scrollEvent + ' ' + resizeEvent + ' ' + orientationEvent);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
46
dist/js/select2.full.js
vendored
46
dist/js/select2.full.js
vendored
@ -631,6 +631,31 @@ define('select2/utils',[], function () {
|
|||||||
return data;
|
return data;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Utils.hasScroll = function (index, el) {
|
||||||
|
// Adapted from the function created by @ShadowScripter
|
||||||
|
// and adapted by @BillBarry on the Stack Exchange Code Review website.
|
||||||
|
// The original code can be found at
|
||||||
|
// http://codereview.stackexchange.com/q/13338
|
||||||
|
// and was designed to be used with the Sizzle selector engine.
|
||||||
|
|
||||||
|
var $el = $(el);
|
||||||
|
var overflowX = el.style.overflowX;
|
||||||
|
var overflowY = el.style.overflowY;
|
||||||
|
|
||||||
|
//Check both x and y declarations
|
||||||
|
if (overflowX === overflowY &&
|
||||||
|
(overflowY === 'hidden' || overflowY === 'visible')) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (overflowX === 'scroll' || overflowY === 'scroll') {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return ($el.innerHeight() < el.scrollHeight ||
|
||||||
|
$el.innerWidth() < el.scrollWidth);
|
||||||
|
};
|
||||||
|
|
||||||
return Utils;
|
return Utils;
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -3690,8 +3715,9 @@ define('select2/dropdown/infiniteScroll',[
|
|||||||
});
|
});
|
||||||
|
|
||||||
define('select2/dropdown/attachBody',[
|
define('select2/dropdown/attachBody',[
|
||||||
'jquery'
|
'jquery',
|
||||||
], function ($) {
|
'../utils'
|
||||||
|
], function ($, Utils) {
|
||||||
function AttachBody (decorated, $element, options) {
|
function AttachBody (decorated, $element, options) {
|
||||||
this.$dropdownParent = options.get('dropdownParent') || document.body;
|
this.$dropdownParent = options.get('dropdownParent') || document.body;
|
||||||
|
|
||||||
@ -3771,6 +3797,19 @@ define('select2/dropdown/attachBody',[
|
|||||||
var resizeEvent = 'resize.select2.' + container.id;
|
var resizeEvent = 'resize.select2.' + container.id;
|
||||||
var orientationEvent = 'orientationchange.select2.' + container.id;
|
var orientationEvent = 'orientationchange.select2.' + container.id;
|
||||||
|
|
||||||
|
$watchers = this.$container.parents().filter(Utils.hasScroll);
|
||||||
|
$watchers.each(function () {
|
||||||
|
$(this).data('select2-scroll-position', {
|
||||||
|
x: $(this).scrollLeft(),
|
||||||
|
y: $(this).scrollTop()
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
$watchers.on(scrollEvent, function (ev) {
|
||||||
|
var position = $(this).data('select2-scroll-position');
|
||||||
|
$(this).scrollTop(position.y);
|
||||||
|
});
|
||||||
|
|
||||||
$(window).on(scrollEvent + ' ' + resizeEvent + ' ' + orientationEvent,
|
$(window).on(scrollEvent + ' ' + resizeEvent + ' ' + orientationEvent,
|
||||||
function (e) {
|
function (e) {
|
||||||
self._positionDropdown();
|
self._positionDropdown();
|
||||||
@ -3783,6 +3822,9 @@ define('select2/dropdown/attachBody',[
|
|||||||
var resizeEvent = 'resize.select2.' + container.id;
|
var resizeEvent = 'resize.select2.' + container.id;
|
||||||
var orientationEvent = 'orientationchange.select2.' + container.id;
|
var orientationEvent = 'orientationchange.select2.' + container.id;
|
||||||
|
|
||||||
|
$watchers = this.$container.parents().filter(Utils.hasScroll);
|
||||||
|
$watchers.off(scrollEvent);
|
||||||
|
|
||||||
$(window).off(scrollEvent + ' ' + resizeEvent + ' ' + orientationEvent);
|
$(window).off(scrollEvent + ' ' + resizeEvent + ' ' + orientationEvent);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
4
dist/js/select2.full.min.js
vendored
4
dist/js/select2.full.min.js
vendored
File diff suppressed because one or more lines are too long
46
dist/js/select2.js
vendored
46
dist/js/select2.js
vendored
@ -631,6 +631,31 @@ define('select2/utils',[], function () {
|
|||||||
return data;
|
return data;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Utils.hasScroll = function (index, el) {
|
||||||
|
// Adapted from the function created by @ShadowScripter
|
||||||
|
// and adapted by @BillBarry on the Stack Exchange Code Review website.
|
||||||
|
// The original code can be found at
|
||||||
|
// http://codereview.stackexchange.com/q/13338
|
||||||
|
// and was designed to be used with the Sizzle selector engine.
|
||||||
|
|
||||||
|
var $el = $(el);
|
||||||
|
var overflowX = el.style.overflowX;
|
||||||
|
var overflowY = el.style.overflowY;
|
||||||
|
|
||||||
|
//Check both x and y declarations
|
||||||
|
if (overflowX === overflowY &&
|
||||||
|
(overflowY === 'hidden' || overflowY === 'visible')) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (overflowX === 'scroll' || overflowY === 'scroll') {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return ($el.innerHeight() < el.scrollHeight ||
|
||||||
|
$el.innerWidth() < el.scrollWidth);
|
||||||
|
};
|
||||||
|
|
||||||
return Utils;
|
return Utils;
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -3690,8 +3715,9 @@ define('select2/dropdown/infiniteScroll',[
|
|||||||
});
|
});
|
||||||
|
|
||||||
define('select2/dropdown/attachBody',[
|
define('select2/dropdown/attachBody',[
|
||||||
'jquery'
|
'jquery',
|
||||||
], function ($) {
|
'../utils'
|
||||||
|
], function ($, Utils) {
|
||||||
function AttachBody (decorated, $element, options) {
|
function AttachBody (decorated, $element, options) {
|
||||||
this.$dropdownParent = options.get('dropdownParent') || document.body;
|
this.$dropdownParent = options.get('dropdownParent') || document.body;
|
||||||
|
|
||||||
@ -3771,6 +3797,19 @@ define('select2/dropdown/attachBody',[
|
|||||||
var resizeEvent = 'resize.select2.' + container.id;
|
var resizeEvent = 'resize.select2.' + container.id;
|
||||||
var orientationEvent = 'orientationchange.select2.' + container.id;
|
var orientationEvent = 'orientationchange.select2.' + container.id;
|
||||||
|
|
||||||
|
$watchers = this.$container.parents().filter(Utils.hasScroll);
|
||||||
|
$watchers.each(function () {
|
||||||
|
$(this).data('select2-scroll-position', {
|
||||||
|
x: $(this).scrollLeft(),
|
||||||
|
y: $(this).scrollTop()
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
$watchers.on(scrollEvent, function (ev) {
|
||||||
|
var position = $(this).data('select2-scroll-position');
|
||||||
|
$(this).scrollTop(position.y);
|
||||||
|
});
|
||||||
|
|
||||||
$(window).on(scrollEvent + ' ' + resizeEvent + ' ' + orientationEvent,
|
$(window).on(scrollEvent + ' ' + resizeEvent + ' ' + orientationEvent,
|
||||||
function (e) {
|
function (e) {
|
||||||
self._positionDropdown();
|
self._positionDropdown();
|
||||||
@ -3783,6 +3822,9 @@ define('select2/dropdown/attachBody',[
|
|||||||
var resizeEvent = 'resize.select2.' + container.id;
|
var resizeEvent = 'resize.select2.' + container.id;
|
||||||
var orientationEvent = 'orientationchange.select2.' + container.id;
|
var orientationEvent = 'orientationchange.select2.' + container.id;
|
||||||
|
|
||||||
|
$watchers = this.$container.parents().filter(Utils.hasScroll);
|
||||||
|
$watchers.off(scrollEvent);
|
||||||
|
|
||||||
$(window).off(scrollEvent + ' ' + resizeEvent + ' ' + orientationEvent);
|
$(window).off(scrollEvent + ' ' + resizeEvent + ' ' + orientationEvent);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
4
dist/js/select2.min.js
vendored
4
dist/js/select2.min.js
vendored
File diff suppressed because one or more lines are too long
21
src/js/select2/dropdown/attachBody.js
vendored
21
src/js/select2/dropdown/attachBody.js
vendored
@ -1,6 +1,7 @@
|
|||||||
define([
|
define([
|
||||||
'jquery'
|
'jquery',
|
||||||
], function ($) {
|
'../utils'
|
||||||
|
], function ($, Utils) {
|
||||||
function AttachBody (decorated, $element, options) {
|
function AttachBody (decorated, $element, options) {
|
||||||
this.$dropdownParent = options.get('dropdownParent') || document.body;
|
this.$dropdownParent = options.get('dropdownParent') || document.body;
|
||||||
|
|
||||||
@ -80,6 +81,19 @@ define([
|
|||||||
var resizeEvent = 'resize.select2.' + container.id;
|
var resizeEvent = 'resize.select2.' + container.id;
|
||||||
var orientationEvent = 'orientationchange.select2.' + container.id;
|
var orientationEvent = 'orientationchange.select2.' + container.id;
|
||||||
|
|
||||||
|
$watchers = this.$container.parents().filter(Utils.hasScroll);
|
||||||
|
$watchers.each(function () {
|
||||||
|
$(this).data('select2-scroll-position', {
|
||||||
|
x: $(this).scrollLeft(),
|
||||||
|
y: $(this).scrollTop()
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
$watchers.on(scrollEvent, function (ev) {
|
||||||
|
var position = $(this).data('select2-scroll-position');
|
||||||
|
$(this).scrollTop(position.y);
|
||||||
|
});
|
||||||
|
|
||||||
$(window).on(scrollEvent + ' ' + resizeEvent + ' ' + orientationEvent,
|
$(window).on(scrollEvent + ' ' + resizeEvent + ' ' + orientationEvent,
|
||||||
function (e) {
|
function (e) {
|
||||||
self._positionDropdown();
|
self._positionDropdown();
|
||||||
@ -92,6 +106,9 @@ define([
|
|||||||
var resizeEvent = 'resize.select2.' + container.id;
|
var resizeEvent = 'resize.select2.' + container.id;
|
||||||
var orientationEvent = 'orientationchange.select2.' + container.id;
|
var orientationEvent = 'orientationchange.select2.' + container.id;
|
||||||
|
|
||||||
|
$watchers = this.$container.parents().filter(Utils.hasScroll);
|
||||||
|
$watchers.off(scrollEvent);
|
||||||
|
|
||||||
$(window).off(scrollEvent + ' ' + resizeEvent + ' ' + orientationEvent);
|
$(window).off(scrollEvent + ' ' + resizeEvent + ' ' + orientationEvent);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
25
src/js/select2/utils.js
vendored
25
src/js/select2/utils.js
vendored
@ -193,5 +193,30 @@ define([], function () {
|
|||||||
return data;
|
return data;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Utils.hasScroll = function (index, el) {
|
||||||
|
// Adapted from the function created by @ShadowScripter
|
||||||
|
// and adapted by @BillBarry on the Stack Exchange Code Review website.
|
||||||
|
// The original code can be found at
|
||||||
|
// http://codereview.stackexchange.com/q/13338
|
||||||
|
// and was designed to be used with the Sizzle selector engine.
|
||||||
|
|
||||||
|
var $el = $(el);
|
||||||
|
var overflowX = el.style.overflowX;
|
||||||
|
var overflowY = el.style.overflowY;
|
||||||
|
|
||||||
|
//Check both x and y declarations
|
||||||
|
if (overflowX === overflowY &&
|
||||||
|
(overflowY === 'hidden' || overflowY === 'visible')) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (overflowX === 'scroll' || overflowY === 'scroll') {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return ($el.innerHeight() < el.scrollHeight ||
|
||||||
|
$el.innerWidth() < el.scrollWidth);
|
||||||
|
};
|
||||||
|
|
||||||
return Utils;
|
return Utils;
|
||||||
});
|
});
|
||||||
|
Loading…
Reference in New Issue
Block a user