Skip to content

Commit d926025

Browse files
authored
Fix infinite scroll when the scrollbar is not visible (select2#5575)
Ever since the 4.0.0 release of Select2, there has been a bug where if you enabled infinite scrolling but did not return enough results on the first load of AJAX to show a scrollbar, then infinite scrolling would not be enabled and you could not view anything other than the first page of results. The solution for this was first proposed in select2#3888 but it was closed off because of inactivity and missing tests. This fixes the issue by performing the check to see if more results should be loaded both on scroll and also when the results are first loaded. This solves the issue that we were seeing before, because the plugin knows it needs to load in more results, just it did not receive the scroll event before and thus was not able to actually load in the new results. This has the potential to trigger multiple AJAX requests to load in multiple pages of results if the user has the ability to see many options, but only a few are being loaded at a time. This also adds tests for infinite scrolling, both to ensure that it will attempt to load additional pages, even without the scrollbar, and to ensure that the regular behaviour of not loading additional pages when the scrollbar is visible is preserved. Fixes select2#3088
1 parent 8a5aeab commit d926025

5 files changed

Lines changed: 148 additions & 16 deletions

File tree

src/js/select2/dropdown/infiniteScroll.js

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ define([
1818

1919
if (this.showLoadingMore(data)) {
2020
this.$results.append(this.$loadingMore);
21+
this.loadMoreIfNeeded();
2122
}
2223
};
2324

@@ -36,25 +37,27 @@ define([
3637
self.loading = true;
3738
});
3839

39-
this.$results.on('scroll', function () {
40-
var isLoadMoreVisible = $.contains(
41-
document.documentElement,
42-
self.$loadingMore[0]
43-
);
40+
this.$results.on('scroll', this.loadMoreIfNeeded.bind(this));
41+
};
42+
43+
InfiniteScroll.prototype.loadMoreIfNeeded = function () {
44+
var isLoadMoreVisible = $.contains(
45+
document.documentElement,
46+
this.$loadingMore[0]
47+
);
4448

45-
if (self.loading || !isLoadMoreVisible) {
46-
return;
47-
}
49+
if (this.loading || !isLoadMoreVisible) {
50+
return;
51+
}
4852

49-
var currentOffset = self.$results.offset().top +
50-
self.$results.outerHeight(false);
51-
var loadingMoreOffset = self.$loadingMore.offset().top +
52-
self.$loadingMore.outerHeight(false);
53+
var currentOffset = this.$results.offset().top +
54+
this.$results.outerHeight(false);
55+
var loadingMoreOffset = this.$loadingMore.offset().top +
56+
this.$loadingMore.outerHeight(false);
5357

54-
if (currentOffset + 50 >= loadingMoreOffset) {
55-
self.loadMore();
56-
}
57-
});
58+
if (currentOffset + 50 >= loadingMoreOffset) {
59+
this.loadMore();
60+
}
5861
};
5962

6063
InfiniteScroll.prototype.loadMore = function () {
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
module('Results - Infinite scrolling');
2+
3+
test('loadingMore is triggered even without a scrollbar', function (assert) {
4+
assert.expect(1);
5+
6+
var $ = require('jquery');
7+
8+
var $select = $('<select></select>');
9+
10+
var $container = $('<span></span>');
11+
var container = new MockContainer();
12+
13+
var Utils = require('select2/utils');
14+
var Options = require('select2/options');
15+
16+
var Results = require('select2/results');
17+
var InfiniteScroll = require('select2/dropdown/infiniteScroll');
18+
19+
var InfiniteScrollResults = Utils.Decorate(Results, InfiniteScroll);
20+
21+
var results = new InfiniteScrollResults($select, new Options({}));
22+
23+
// Fake the data adapter for the `setClasses` method
24+
results.data = {};
25+
results.data.current = function (callback) {
26+
callback([{ id: 'test' }]);
27+
};
28+
29+
$('#qunit-fixture').append(results.render());
30+
31+
results.bind(container, $container);
32+
33+
results.on('query:append', function () {
34+
assert.ok(true, 'It tried to load more immediately');
35+
});
36+
37+
container.trigger('results:all', {
38+
data: {
39+
results: [
40+
{
41+
id: 'test',
42+
text: 'Test'
43+
}
44+
],
45+
pagination: {
46+
more: true
47+
}
48+
}
49+
});
50+
});
51+
52+
test('loadingMore is not triggered without scrolling', function (assert) {
53+
assert.expect(0);
54+
55+
var $ = require('jquery');
56+
57+
var $select = $('<select></select>');
58+
59+
var $container = $('<span></span>');
60+
var container = new MockContainer();
61+
62+
var Utils = require('select2/utils');
63+
var Options = require('select2/options');
64+
65+
var Results = require('select2/results');
66+
var InfiniteScroll = require('select2/dropdown/infiniteScroll');
67+
68+
var InfiniteScrollResults = Utils.Decorate(Results, InfiniteScroll);
69+
70+
var results = new InfiniteScrollResults($select, new Options({}));
71+
72+
// Fake the data adapter for the `setClasses` method
73+
results.data = {};
74+
results.data.current = function (callback) {
75+
callback([{ id: 'test' }]);
76+
};
77+
78+
var $results = results.render();
79+
80+
$('#qunit-fixture').append($results);
81+
$results.css('max-height', '100px');
82+
83+
results.bind(container, $container);
84+
85+
results.on('query:append', function () {
86+
assert.ok(false, 'It tried to load more immediately');
87+
});
88+
89+
container.trigger('results:all', {
90+
data: {
91+
results: [
92+
{
93+
id: 'test',
94+
text: 'Test'
95+
},
96+
{
97+
id: 'test',
98+
text: 'Test'
99+
},
100+
{
101+
id: 'test',
102+
text: 'Test'
103+
},
104+
{
105+
id: 'test',
106+
text: 'Test'
107+
},
108+
{
109+
id: 'test',
110+
text: 'Test'
111+
},
112+
{
113+
id: 'test',
114+
text: 'Test'
115+
},
116+
{
117+
id: 'test',
118+
text: 'Test'
119+
}
120+
],
121+
pagination: {
122+
more: true
123+
}
124+
}
125+
});
126+
});

tests/unit-jq1.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@
8282
<script src="options/width-tests.js" type="text/javascript"></script>
8383

8484
<script src="results/focusing-tests.js" type="text/javascript"></script>
85+
<script src="results/infiniteScroll-tests.js" type="text/javascript"></script>
8586
<script src="results/option-tests.js" type="text/javascript"></script>
8687

8788
<script src="selection/allowClear-tests.js" type="text/javascript"></script>

tests/unit-jq2.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@
8282
<script src="options/width-tests.js" type="text/javascript"></script>
8383

8484
<script src="results/focusing-tests.js" type="text/javascript"></script>
85+
<script src="results/infiniteScroll-tests.js" type="text/javascript"></script>
8586
<script src="results/option-tests.js" type="text/javascript"></script>
8687

8788
<script src="selection/allowClear-tests.js" type="text/javascript"></script>

tests/unit-jq3.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@
8282
<script src="options/width-tests.js" type="text/javascript"></script>
8383

8484
<script src="results/focusing-tests.js" type="text/javascript"></script>
85+
<script src="results/infiniteScroll-tests.js" type="text/javascript"></script>
8586
<script src="results/option-tests.js" type="text/javascript"></script>
8687

8788
<script src="selection/allowClear-tests.js" type="text/javascript"></script>

0 commit comments

Comments
 (0)