Skip to content

Commit dd81ad0

Browse files
committed
shiftTop instead of _op = 'isTop'
1 parent 249ba17 commit dd81ad0

File tree

5 files changed

+29
-27
lines changed

5 files changed

+29
-27
lines changed

src/modules/adapter.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { OPERATIONS } from './utils';
2+
13
class Adapter {
24

35
constructor($scope, $parse, $attr, viewport, buffer, doAdjust, reload) {
@@ -168,16 +170,17 @@ class Adapter {
168170
}
169171
let position = this.buffer.indexOf(wrapper);
170172
if (!newItems.reverse().some(newItem => newItem === wrapper.item)) {
171-
wrapper.op = 'remove';
173+
wrapper.op = OPERATIONS.REMOVE;
174+
// try to catch "first" edge case on remove
172175
if (!options.immutableTop && position === 0 && !newItems.length) {
173-
wrapper._op = 'isTop'; // to catch "first" edge case on remove
176+
wrapper.shiftTop = true;
174177
}
175178
}
176179
newItems.forEach((newItem) => {
177180
if (newItem === wrapper.item) {
178181
position--;
179182
} else {
180-
// 3 parameter (isTop) is to catch "first" edge case on insert
183+
// 3 parameter (shiftTop) is to catch "first" edge case on insert
181184
this.buffer.insert(position + 1, newItem, !options.immutableTop && position === -1);
182185
}
183186
});

src/modules/buffer.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,15 @@ export default function ScrollBuffer(elementRoutines, bufferSize, startIndex) {
4545
* for insert the number is the index for the buffer element the new one have to be inserted after
4646
* operations: 'append', 'prepend', 'insert', 'remove', 'none'
4747
*/
48-
insert(operation, item, isTop) {
48+
insert(operation, item, shiftTop) {
4949
const wrapper = {
5050
item: item
5151
};
5252

5353
if (operation % 1 === 0) { // it is an insert
5454
wrapper.op = OPERATIONS.INSERT;
5555
buffer.splice(operation, 0, wrapper);
56-
if(isTop) {
56+
if (shiftTop) {
5757
buffer.first--;
5858
}
5959
else {
@@ -81,21 +81,21 @@ export default function ScrollBuffer(elementRoutines, bufferSize, startIndex) {
8181
}
8282
return buffer.splice(arg1, arg2 - arg1);
8383
}
84-
// removes single item(wrapper) from the buffer
84+
// removes single item (wrapper) from the buffer
8585
buffer.splice(buffer.indexOf(arg1), 1);
86-
if(arg1._op === 'isTop' && buffer.first === this.getAbsMinIndex()) {
86+
if (arg1.shiftTop && buffer.first === this.getAbsMinIndex()) {
8787
this.incrementMinIndex();
8888
}
8989
else {
9090
this.decrementMaxIndex();
9191
}
92-
if(arg1._op === 'isTop') {
92+
if (arg1.shiftTop) {
9393
buffer.first++;
9494
}
9595
else {
9696
buffer.next--;
9797
}
98-
if(!buffer.length) {
98+
if (!buffer.length) {
9999
buffer.first = 1;
100100
buffer.next = 1;
101101
}
@@ -104,34 +104,34 @@ export default function ScrollBuffer(elementRoutines, bufferSize, startIndex) {
104104
},
105105

106106
incrementMinIndex() {
107-
if(buffer.minIndexUser !== null) {
108-
if(buffer.minIndex > buffer.minIndexUser) {
107+
if (buffer.minIndexUser !== null) {
108+
if (buffer.minIndex > buffer.minIndexUser) {
109109
buffer.minIndexUser++;
110110
return;
111111
}
112-
if(buffer.minIndex === buffer.minIndexUser) {
112+
if (buffer.minIndex === buffer.minIndexUser) {
113113
buffer.minIndexUser++;
114114
}
115115
}
116116
buffer.minIndex++;
117117
},
118118

119119
decrementMaxIndex() {
120-
if(buffer.maxIndexUser !== null && buffer.maxIndex <= buffer.maxIndexUser) {
120+
if (buffer.maxIndexUser !== null && buffer.maxIndex <= buffer.maxIndexUser) {
121121
buffer.maxIndexUser--;
122122
}
123123
buffer.maxIndex--;
124124
},
125125

126126
getAbsMinIndex() {
127-
if(buffer.minIndexUser !== null) {
127+
if (buffer.minIndexUser !== null) {
128128
return Math.min(buffer.minIndexUser, buffer.minIndex);
129129
}
130130
return buffer.minIndex;
131131
},
132132

133133
getAbsMaxIndex() {
134-
if(buffer.maxIndexUser !== null) {
134+
if (buffer.maxIndexUser !== null) {
135135
return Math.max(buffer.maxIndexUser, buffer.maxIndex);
136136
}
137137
return buffer.maxIndex;

src/modules/padding.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,16 @@ class CacheProto {
1515
this.sort((a, b) => ((a.index < b.index) ? -1 : ((a.index > b.index) ? 1 : 0)));
1616
}
1717

18-
remove(argument, _isTop) {
18+
remove(argument, _shiftTop) {
1919
const index = argument % 1 === 0 ? argument : argument.scope.$index;
20-
const isTop = argument % 1 === 0 ? _isTop : argument._op === 'isTop';
20+
const shiftTop = argument % 1 === 0 ? _shiftTop : argument.shiftTop;
2121
for (let i = this.length - 1; i >= 0; i--) {
2222
if (this[i].index === index) {
2323
this.splice(i, 1);
2424
break;
2525
}
2626
}
27-
if(!isTop) {
27+
if (!shiftTop) {
2828
for (let i = this.length - 1; i >= 0; i--) {
2929
if (this[i].index > index) {
3030
this[i].index--;
@@ -50,7 +50,7 @@ Object.getOwnPropertyNames(CacheProto.prototype).forEach(methodName =>
5050
);
5151

5252
function generateElement(template) {
53-
if(template.nodeType !== Node.ELEMENT_NODE) {
53+
if (template.nodeType !== Node.ELEMENT_NODE) {
5454
throw new Error('ui-scroll directive requires an Element node for templating the view');
5555
}
5656
let element;

src/modules/utils.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
export const OPERATIONS = {
2-
NONE: 'none',
32
PREPEND: 'prepend',
43
APPEND: 'append',
54
INSERT: 'insert',
65
REMOVE: 'remove',
7-
REMOVE_AND_SHIFT_TOP: 'remove and shift top index'
6+
NONE: 'none'
87
};

src/modules/viewport.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export default function Viewport(elementRoutines, buffer, element, viewportContr
3535
if (!container) {
3636
return true;
3737
}
38-
if(container !== viewport) {
38+
if (container !== viewport) {
3939
viewport.css('height', window.getComputedStyle(container[0]).height);
4040
}
4141
return viewport.height() > 0;
@@ -137,15 +137,15 @@ export default function Viewport(elementRoutines, buffer, element, viewportContr
137137

138138
let topPaddingHeight = 0, topCount = 0;
139139
topPadding.cache.forEach(item => {
140-
if(item.index < buffer.first) {
140+
if (item.index < buffer.first) {
141141
topPaddingHeight += item.height;
142142
topCount++;
143143
}
144144
});
145145

146146
let bottomPaddingHeight = 0, bottomCount = 0;
147147
bottomPadding.cache.forEach(item => {
148-
if(item.index >= buffer.next) {
148+
if (item.index >= buffer.next) {
149149
bottomPaddingHeight += item.height;
150150
bottomCount++;
151151
}
@@ -201,9 +201,9 @@ export default function Viewport(elementRoutines, buffer, element, viewportContr
201201
bottomPadding.cache.clear();
202202
},
203203

204-
removeCacheItem(item, isTop) {
205-
topPadding.cache.remove(item, isTop);
206-
bottomPadding.cache.remove(item, isTop);
204+
removeCacheItem(item, shiftTop) {
205+
topPadding.cache.remove(item, shiftTop);
206+
bottomPadding.cache.remove(item, shiftTop);
207207
},
208208

209209
removeItem(item) {

0 commit comments

Comments
 (0)