Skip to content

Commit 676d3d7

Browse files
authored
Merge pull request #219 from khajjit/startIndex-rounding
startIndex rounding
2 parents 05896d8 + a52b19e commit 676d3d7

File tree

3 files changed

+53
-6
lines changed

3 files changed

+53
-6
lines changed

src/ui-scroll.js

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

63-
function parseNumericAttr(value, defaultValue) {
63+
function parseNumber(value, defaultValue, isFloat) {
64+
if (!isFloat) {
65+
value = value === null ? defaultValue : Math.floor(value);
66+
}
67+
return isNaN(value) ? defaultValue : value;
68+
}
69+
70+
function parseNumericAttr(value, defaultValue, isFloat) {
6471
const result = $parse(value)($scope);
65-
return isNaN(result) ? defaultValue : result;
72+
return parseNumber(result, defaultValue, isFloat);
6673
}
6774

6875
const BUFFER_MIN = 3;
6976
const BUFFER_DEFAULT = 10;
7077
const PADDING_MIN = 0.3;
7178
const PADDING_DEFAULT = 0.5;
79+
const START_INDEX_DEFAULT = 1;
7280
const MAX_VIEWPORT_DELAY = 500;
7381
const VIEWPORT_POLLING_INTERVAL = 50;
7482

@@ -77,8 +85,8 @@ angular.module('ui.scroll', [])
7785
const datasourceName = match[2];
7886
const viewportController = controllers[0];
7987
const bufferSize = Math.max(BUFFER_MIN, parseNumericAttr($attr.bufferSize, BUFFER_DEFAULT));
80-
const padding = Math.max(PADDING_MIN, parseNumericAttr($attr.padding, PADDING_DEFAULT));
81-
let startIndex = parseNumericAttr($attr.startIndex, 1);
88+
const padding = Math.max(PADDING_MIN, parseNumericAttr($attr.padding, PADDING_DEFAULT, true));
89+
let startIndex = parseNumericAttr($attr.startIndex, START_INDEX_DEFAULT);
8290
let ridActual = 0; // current data revision id
8391
let pending = [];
8492

@@ -235,7 +243,7 @@ angular.module('ui.scroll', [])
235243
viewport.resetTopPadding();
236244
viewport.resetBottomPadding();
237245
if (arguments.length) {
238-
startIndex = arguments[0];
246+
startIndex = parseNumber(arguments[0], START_INDEX_DEFAULT, false);
239247
}
240248
buffer.reset(startIndex);
241249
persistDatasourceIndex(datasource, 'minIndex');

test/AdapterTestsSpec.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1096,6 +1096,45 @@ describe('uiScroll', function () {
10961096
);
10971097
});
10981098

1099+
it('should round numbers for startIndex', () => {
1100+
runTest(scrollSettings,
1101+
function (viewport, scope) {
1102+
scope.adapter.reload(23.4);
1103+
expect(scope.adapter.topVisible).toBe('item23');
1104+
1105+
scope.adapter.reload(-56.9);
1106+
expect(scope.adapter.topVisible).toBe('item-57');
1107+
}
1108+
);
1109+
});
1110+
1111+
it('should correctly convert string to number and round', () => {
1112+
runTest(scrollSettings,
1113+
function (viewport, scope) {
1114+
scope.adapter.reload('1001.14');
1115+
expect(scope.adapter.topVisible).toBe('item1001');
1116+
1117+
scope.adapter.reload('0');
1118+
expect(scope.adapter.topVisible).toBe('item0');
1119+
}
1120+
);
1121+
});
1122+
1123+
it('should set startIndex to default if number is invalid', () => {
1124+
runTest(scrollSettings,
1125+
function (viewport, scope) {
1126+
scope.adapter.reload('invalid number');
1127+
expect(scope.adapter.topVisible).toBe('item1');
1128+
1129+
scope.adapter.reload({});
1130+
expect(scope.adapter.topVisible).toBe('item1');
1131+
1132+
scope.adapter.reload(null);
1133+
expect(scope.adapter.topVisible).toBe('item1');
1134+
}
1135+
);
1136+
});
1137+
10991138
});
11001139

11011140
describe('adapter bof/eof/empty', function () {

test/UserIndicesSpec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,4 +175,4 @@ describe('uiScroll main/max indices', function() {
175175

176176
});
177177

178-
});
178+
});

0 commit comments

Comments
 (0)