Skip to content

Commit

Permalink
fix(virtual-list): empty list crashes
Browse files Browse the repository at this point in the history
fixes #11093
  • Loading branch information
manucorporat committed Apr 6, 2017
1 parent 26d10c4 commit eb9de60
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 35 deletions.
12 changes: 8 additions & 4 deletions src/components/virtual-scroll/test/basic/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@ export class E2EPage {
counter: number = 0;

constructor(plt: Platform, public navCtrl: NavController) {
for (var i = 0; i < 200; i++) {
this.addItem();
}

if (plt.is('ios')) {
if (plt.testUserAgent('Safari')) {
this.webview = ': iOS Safari';
Expand All @@ -29,6 +25,14 @@ export class E2EPage {
}
}

addItems() {
if (this.items.length === 0) {
for (var i = 0; i < 200; i++) {
this.addItem();
}
}
}

headerFn(record: any, index: number, records: any[]) {
if (index % 4 === 0) {
return index + ' is divisible by 4';
Expand Down
4 changes: 4 additions & 0 deletions src/components/virtual-scroll/test/basic/main.html
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@

</ion-list>

<div padding>
<button ion-button (click)="addItems()">Add items</button>
</div>

<div padding>
<button ion-button (click)="pushPage()">Push Virtual Scroll Page</button>
</div>
Expand Down
65 changes: 34 additions & 31 deletions src/components/virtual-scroll/virtual-util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -317,40 +317,43 @@ export function updateDimensions(plt: Platform, nodes: VirtualNode[], cells: Vir
data.topViewCell = totalCells;
data.bottomViewCell = 0;

// completely realign position to ensure they're all accurately placed
cell = cells[0];
previousCell = {
row: 0,
width: 0,
height: 0,
top: cell.top,
left: 0,
tmpl: -1
};
for (var i = 0; i < totalCells; i++) {
cell = cells[i];

if (previousCell.left + previousCell.width + cell.width > data.viewWidth) {
// new row
cell.row++;
cell.top = (previousCell.top + previousCell.height);
cell.left = 0;

} else {
// same row
cell.row = previousCell.row;
cell.top = previousCell.top;
cell.left = (previousCell.left + previousCell.width);
}
if (totalCells > 0) {
// completely realign position to ensure they're all accurately placed
cell = cells[0];
previousCell = {
row: 0,
width: 0,
height: 0,
top: cell.top,
left: 0,
tmpl: -1
};

for (var i = 0; i < totalCells; i++) {
cell = cells[i];

// figure out which cells are viewable within the viewport
if (cell.top + cell.height > data.scrollTop && i < data.topViewCell) {
data.topViewCell = i;
if (previousCell.left + previousCell.width + cell.width > data.viewWidth) {
// new row
cell.row++;
cell.top = (previousCell.top + previousCell.height);
cell.left = 0;

} else {
// same row
cell.row = previousCell.row;
cell.top = previousCell.top;
cell.left = (previousCell.left + previousCell.width);
}

// figure out which cells are viewable within the viewport
if (cell.top + cell.height > data.scrollTop && i < data.topViewCell) {
data.topViewCell = i;

} else if (cell.top < viewableBottom && i > data.bottomViewCell) {
data.bottomViewCell = i;
} else if (cell.top < viewableBottom && i > data.bottomViewCell) {
data.bottomViewCell = i;
}
previousCell = cell;
}
previousCell = cell;
}

}
Expand Down

0 comments on commit eb9de60

Please sign in to comment.