Skip to content

Commit 7d316b2

Browse files
committed
finalize deletion out of buffer
1 parent d472df8 commit 7d316b2

File tree

6 files changed

+100
-51
lines changed

6 files changed

+100
-51
lines changed

demo/adapterSync/adapterSync.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ <h1 class="page-header page-header-exapmle">Adapter: append, prepend and remove
6565
<button ng-click="mainCtrl.prepend()">Prepend one item</button>
6666
<button ng-click="mainCtrl.append()">Append one item</button>
6767
<!--button ng-click="mainCtrl.removeAll()">Clear the viewport</button-->
68+
<button ng-click="mainCtrl.removeFirst()">Remove first</button>
69+
<button ng-click="mainCtrl.removeLast()">Remove last</button>
6870
</div>
6971

7072
<br/>

demo/adapterSync/adapterSync.js

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,13 +83,30 @@ app.factory('Server', [
8383
return this.returnDeferredResult(newItem);
8484
},
8585

86+
removeFirst: function () {
87+
var firstItem = this.data.find(i => i.index === this.firstIndex);
88+
if(!firstItem) {
89+
return $q.reject();
90+
}
91+
return this.removeItemById(firstItem.id);
92+
},
93+
94+
removeLast: function () {
95+
var lastItem = this.data.find(i => i.index === this.lastIndex);
96+
if(!lastItem) {
97+
return $q.reject();
98+
}
99+
return this.removeItemById(lastItem.id);
100+
},
101+
86102
removeItemById: function (itemId) {
87103
var length = this.data.length;
88104
for (var i = 0; i < length; i++) {
89105
if (this.data[i].id === itemId) {
106+
var indexRemoved = this.data[i].index;
90107
this.data.splice(i, 1);
91108
this.setIndicies();
92-
return this.returnDeferredResult(true);
109+
return this.returnDeferredResult(indexRemoved);
93110
}
94111
}
95112
return this.returnDeferredResult(false);
@@ -171,7 +188,7 @@ app.controller('mainController', [
171188

172189
ctrl.remove = function (itemRemove) {
173190
Server.removeItemById(itemRemove.id).then(function (result) {
174-
if (result) {
191+
if (result !== false) {
175192
ctrl.adapter.applyUpdates(function (item) {
176193
if (item.id === itemRemove.id) {
177194
return [];
@@ -181,5 +198,20 @@ app.controller('mainController', [
181198
});
182199
};
183200

201+
ctrl.removeFirst = function () {
202+
Server.removeFirst().then(function (indexRemoved) {
203+
if (indexRemoved !== false) {
204+
ctrl.adapter.applyUpdates(indexRemoved, []);
205+
}
206+
});
207+
};
208+
209+
ctrl.removeLast = function () {
210+
Server.removeLast().then(function (indexRemoved) {
211+
if (indexRemoved !== false) {
212+
ctrl.adapter.applyUpdates(indexRemoved, []);
213+
}
214+
});
215+
};
184216
}
185217
]);

dist/ui-scroll.js

Lines changed: 20 additions & 27 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/ui-scroll.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/modules/adapter.js

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -145,32 +145,26 @@ class Adapter {
145145
}
146146

147147
applyUpdatesIndex(index, newItems) {
148-
if (index % 1 !== 0) { // checking if it is an integer
149-
throw new Error('applyUpdates - ' + index + ' is not a valid index');
148+
if (index % 1 !== 0) {
149+
throw new Error('applyUpdates - ' + index + ' is not a valid index (should be an integer)');
150150
}
151151
const _index = index - this.buffer.first;
152+
// apply updates only within buffer
152153
if (_index >= 0 && _index < this.buffer.length) {
153154
this.applyUpdate(this.buffer[_index], newItems);
154155
}
156+
// out-of-buffer case: deletion may affect Paddings
155157
else if(index >= this.buffer.minIndex && index <= this.buffer.maxIndex) {
156-
this.applyUpdateBuffer(index, newItems);
157-
}
158-
}
159-
160-
applyUpdateBuffer(index, newItems) {
161-
if (!angular.isArray(newItems)) {
162-
return;
163-
}
164-
// remove single item
165-
if(!newItems.length) {
166-
var isTop = index === this.buffer.minIndex;
167-
if(isTop) {
168-
this.buffer.minIndex++;
169-
}
170-
else {
171-
this.buffer.maxIndex--;
158+
if(angular.isArray(newItems) && !newItems.length) {
159+
var isTop = index === this.buffer.minIndex;
160+
if(isTop) {
161+
this.buffer.minIndex++;
162+
}
163+
else {
164+
this.buffer.maxIndex--;
165+
}
166+
this.viewport.removeCacheItem(index, isTop);
172167
}
173-
this.viewport.removeCacheItem(index, isTop);
174168
}
175169
}
176170

test/PaddingCacheSpec.js

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,34 @@ describe('uiScroll Paddings cache', function () {
6969
);
7070
});
7171

72+
it('should delete last row and then the next after last, when out of buffer', function () {
73+
var removeLastItem;
74+
inject(function(myResponsiveDatasource) {
75+
var datasource = myResponsiveDatasource;
76+
removeLastItem = function() {
77+
datasource.data.slice(-1, 1);
78+
datasource.max--;
79+
};
80+
});
81+
runTest(scrollSettings,
82+
function (viewport, scope) {
83+
84+
scrollBottom(viewport, 3);
85+
scrollTop(viewport);
86+
87+
var initialBottomHeight = getBottomPaddingHeight(viewport);
88+
removeLastItem();
89+
scope.adapter.applyUpdates(itemsCount, []);
90+
removeLastItem();
91+
scope.adapter.applyUpdates(itemsCount - 1, []);
92+
expect(getBottomPaddingHeight(viewport)).toBe(initialBottomHeight - itemHeight * 2);
93+
94+
scrollBottom(viewport, 3);
95+
expect(viewport.scrollTop()).toBe(itemsCount * itemHeight - viewportHeight - itemHeight * 2);
96+
}
97+
);
98+
});
99+
72100
it('should delete first row when out of buffer', function () {
73101
var removeFirstItem;
74102
inject(function(myResponsiveDatasource) {
@@ -94,7 +122,7 @@ describe('uiScroll Paddings cache', function () {
94122
);
95123
});
96124

97-
it('should delete first row and then second row when out of buffer', function () {
125+
it('should delete first row and then the next after first, when out of buffer', function () {
98126
var removeFirstItem;
99127
inject(function(myResponsiveDatasource) {
100128
var datasource = myResponsiveDatasource;
@@ -109,10 +137,10 @@ describe('uiScroll Paddings cache', function () {
109137
scrollBottom(viewport, 3);
110138

111139
var initialTopHeight = getTopPaddingHeight(viewport);
140+
removeFirstItem();
112141
scope.adapter.applyUpdates(1, []);
113142
removeFirstItem();
114143
scope.adapter.applyUpdates(2, []);
115-
removeFirstItem();
116144
expect(getTopPaddingHeight(viewport)).toBe(initialTopHeight - itemHeight * 2);
117145

118146
scrollTop(viewport);

0 commit comments

Comments
 (0)