Skip to content

Commit a957d07

Browse files
authored
Merge pull request #238 from angular-ui/inertia-fix
Inertia fix
2 parents a07daf0 + 1294c0b commit a957d07

19 files changed

+140
-125
lines changed

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,10 @@ Pull Rerquest should include source code (./scr) changes, may include tests (./t
477477

478478
## Change log
479479

480+
### v1.8.0
481+
* Reconsidered scroll event handling
482+
* Fixed inertia scrolling issues
483+
480484
### v1.7.6
481485
* Added immutableTop option for applyUpdates and prepend Adapter methods.
482486

bower.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "angular-ui-scroll",
33
"description": "AngularJS infinite scrolling module",
4-
"version": "1.7.6",
4+
"version": "1.8.0",
55
"main": "./dist/ui-scroll.js",
66
"homepage": "https://github.com/angular-ui/ui-scroll.git",
77
"license": "MIT",

dist/ui-scroll-grid.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/ui-scroll-grid.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/ui-scroll-grid.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/ui-scroll-grid.min.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/ui-scroll.js

+55-5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/ui-scroll.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/ui-scroll.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/ui-scroll.min.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package-lock.json

+5-13
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "angular-ui-scroll",
33
"description": "AngularJS infinite scrolling module",
4-
"version": "1.7.6",
4+
"version": "1.8.0",
55
"src": "./src/",
66
"public": "./dist/",
77
"main": "./dist/ui-scroll.js",

src/modules/viewport.js

+8-5
Original file line numberDiff line numberDiff line change
@@ -178,17 +178,20 @@ export default function Viewport(elementRoutines, buffer, element, viewportContr
178178
},
179179

180180
onAfterPrepend(updates) {
181-
if (!updates.prepended.length)
181+
if (!updates.prepended.length) {
182182
return;
183+
}
183184
const height = buffer.effectiveHeight(updates.prepended);
184185
const paddingHeight = topPadding.height() - height;
185186
if (paddingHeight >= 0) {
186187
topPadding.height(paddingHeight);
188+
return;
187189
}
188-
else {
189-
topPadding.height(0);
190-
viewport.scrollTop(viewport.scrollTop() - paddingHeight);
191-
}
190+
const position = viewport.scrollTop();
191+
const newPosition = position - paddingHeight;
192+
viewport.synthetic = { previous: position, next: newPosition };
193+
topPadding.height(0);
194+
viewport.scrollTop(newPosition);
192195
},
193196

194197
resetTopPadding() {

src/ui-scroll.js

+32
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,16 @@ angular.module('ui.scroll', [])
7373
return parseNumber(result, defaultValue, isFloat);
7474
}
7575

76+
function parseBooleanAttr(value, defaultValue) {
77+
const result = $parse(value)($scope);
78+
return typeof result === 'boolean' ? result : defaultValue;
79+
}
80+
7681
const BUFFER_MIN = 3;
7782
const BUFFER_DEFAULT = 10;
7883
const PADDING_MIN = 0.3;
7984
const PADDING_DEFAULT = 0.5;
85+
const HANDLE_INERTIA_DEFAULT = true;
8086
const START_INDEX_DEFAULT = 1;
8187
const MAX_VIEWPORT_DELAY = 500;
8288
const VIEWPORT_POLLING_INTERVAL = 50;
@@ -87,6 +93,7 @@ angular.module('ui.scroll', [])
8793
const viewportController = controllers[0];
8894
const bufferSize = Math.max(BUFFER_MIN, parseNumericAttr($attr.bufferSize, BUFFER_DEFAULT));
8995
const padding = Math.max(PADDING_MIN, parseNumericAttr($attr.padding, PADDING_DEFAULT, true));
96+
const handleInertia = parseBooleanAttr($attr.handleInertia, HANDLE_INERTIA_DEFAULT);
9097
let startIndex = parseNumericAttr($attr.startIndex, START_INDEX_DEFAULT);
9198
let ridActual = 0; // current data revision id
9299
let pending = [];
@@ -485,7 +492,32 @@ angular.module('ui.scroll', [])
485492
}
486493
}
487494

495+
function fixInertia() {
496+
if (!viewport.synthetic) {
497+
return;
498+
}
499+
const oldPosition = viewport.synthetic.previous;
500+
const newPosition = viewport.synthetic.next;
501+
if (viewport.scrollTop() !== newPosition) {
502+
requestAnimationFrame(() => {
503+
const position = viewport.scrollTop();
504+
const diff = oldPosition - position;
505+
if (diff > 0) { // inertia over synthetic
506+
viewport.scrollTop(newPosition - diff);
507+
} else {
508+
viewport.scrollTop(newPosition);
509+
}
510+
viewport.synthetic = null;
511+
});
512+
return true;
513+
}
514+
viewport.synthetic = null;
515+
}
516+
488517
function resizeAndScrollHandler() {
518+
if (handleInertia && fixInertia()) {
519+
return;
520+
}
489521
if (!$rootScope.$$phase && !adapter.isLoading && !adapter.disabled) {
490522

491523
enqueueFetch(ridActual);

0 commit comments

Comments
 (0)