Skip to content

Commit 01d6f7c

Browse files
committed
startIndex rounding
1 parent 05896d8 commit 01d6f7c

File tree

2 files changed

+31
-4
lines changed

2 files changed

+31
-4
lines changed

src/ui-scroll.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,13 @@ angular.module('ui.scroll', [])
6060
throw new Error('Expected uiScroll in form of \'_item_ in _datasource_\' but got \'' + $attr.uiScroll + '\'');
6161
}
6262

63+
function getIntegerNumber(value, defaultValue = 1) {
64+
return isNaN(value) ? defaultValue : Math.floor(value);
65+
}
66+
6367
function parseNumericAttr(value, defaultValue) {
6468
const result = $parse(value)($scope);
65-
return isNaN(result) ? defaultValue : result;
69+
return getIntegerNumber(result, defaultValue);
6670
}
6771

6872
const BUFFER_MIN = 3;
@@ -78,7 +82,7 @@ angular.module('ui.scroll', [])
7882
const viewportController = controllers[0];
7983
const bufferSize = Math.max(BUFFER_MIN, parseNumericAttr($attr.bufferSize, BUFFER_DEFAULT));
8084
const padding = Math.max(PADDING_MIN, parseNumericAttr($attr.padding, PADDING_DEFAULT));
81-
let startIndex = parseNumericAttr($attr.startIndex, 1);
85+
let startIndex = parseNumericAttr($attr.startIndex);
8286
let ridActual = 0; // current data revision id
8387
let pending = [];
8488

@@ -235,7 +239,7 @@ angular.module('ui.scroll', [])
235239
viewport.resetTopPadding();
236240
viewport.resetBottomPadding();
237241
if (arguments.length) {
238-
startIndex = arguments[0];
242+
startIndex = getIntegerNumber(arguments[0]);
239243
}
240244
buffer.reset(startIndex);
241245
persistDatasourceIndex(datasource, 'minIndex');

test/UserIndicesSpec.js

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,29 @@ describe('uiScroll main/max indices', function() {
115115
);
116116
});
117117

118+
it('should round numbers for startIndex', () => {
119+
const startIndex = 22.4;
120+
runTest(Object.assign({}, scrollSettings, { startIndex }),
121+
(viewport, scope) => {
122+
expect(scope.adapter.topVisible).toBe('item' + Math.floor(startIndex));
123+
124+
const newStartIndex = 92.7;
125+
scope.adapter.reload(newStartIndex);
126+
expect(scope.adapter.topVisible).toBe('item' + Math.floor(newStartIndex));
127+
}
128+
);
129+
});
130+
131+
it('should validate startIndex argument on reload', () => {
132+
runTest(Object.assign({}, scrollSettings),
133+
(viewport, scope) => {
134+
const newStartIndex = 'invalid number';
135+
scope.adapter.reload(newStartIndex);
136+
expect(scope.adapter.topVisible).toBe('item1');
137+
}
138+
);
139+
});
140+
118141
});
119142

120143
describe('Reload\n', () => {
@@ -175,4 +198,4 @@ describe('uiScroll main/max indices', function() {
175198

176199
});
177200

178-
});
201+
});

0 commit comments

Comments
 (0)