-
Notifications
You must be signed in to change notification settings - Fork 107
/
Copy pathviewport.js
216 lines (182 loc) · 7.07 KB
/
viewport.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
import Padding from './padding';
export default function Viewport(elementRoutines, buffer, element, viewportController, $rootScope, padding) {
let topPadding = null;
let bottomPadding = null;
const viewport = viewportController && viewportController.viewport ? viewportController.viewport : angular.element(window);
const container = viewportController && viewportController.container ? viewportController.container : undefined;
const scope = viewportController && viewportController.scope ? viewportController.scope : $rootScope;
viewport.css({
'overflow-anchor': 'none',
'overflow-y': 'auto',
'display': 'block'
});
function bufferPadding() {
return viewport.outerHeight() * padding; // some extra space to initiate preload
}
Object.assign(viewport, {
getScope() {
return scope;
},
createPaddingElements(template) {
topPadding = new Padding(template);
bottomPadding = new Padding(template);
element.before(topPadding.element);
element.after(bottomPadding.element);
topPadding.height(0);
bottomPadding.height(0);
},
applyContainerStyle() {
if (!container) {
return true;
}
if(container !== viewport) {
viewport.css('height', window.getComputedStyle(container[0]).height);
}
return viewport.height() > 0;
},
bottomDataPos() {
let scrollHeight = viewport[0].scrollHeight;
scrollHeight = scrollHeight != null ? scrollHeight : viewport[0].document.documentElement.scrollHeight;
return scrollHeight - bottomPadding.height();
},
topDataPos() {
return topPadding.height();
},
bottomVisiblePos() {
return viewport.scrollTop() + viewport.outerHeight();
},
topVisiblePos() {
return viewport.scrollTop();
},
insertElement(e, sibling) {
return elementRoutines.insertElement(e, sibling || topPadding.element);
},
insertElementAnimated(e, sibling) {
return elementRoutines.insertElementAnimated(e, sibling || topPadding.element);
},
shouldLoadBottom() {
return !buffer.eof && viewport.bottomDataPos() < viewport.bottomVisiblePos() + bufferPadding();
},
clipBottom() {
// clip the invisible items off the bottom
let overage = 0;
let overageHeight = 0;
let itemHeight = 0;
let emptySpaceHeight = viewport.bottomDataPos() - viewport.bottomVisiblePos() - bufferPadding();
for (let i = buffer.length - 1; i >= 0; i--) {
itemHeight = buffer[i].element.outerHeight(true);
if (overageHeight + itemHeight > emptySpaceHeight) {
break;
}
bottomPadding.cache.add(buffer[i]);
overageHeight += itemHeight;
overage++;
}
if (overage > 0) {
buffer.eof = false;
buffer.remove(buffer.length - overage, buffer.length);
buffer.next -= overage;
viewport.adjustPaddings();
}
},
shouldLoadTop() {
return !buffer.bof && (viewport.topDataPos() > viewport.topVisiblePos() - bufferPadding());
},
clipTop() {
// clip the invisible items off the top
let overage = 0;
let overageHeight = 0;
let itemHeight = 0;
let emptySpaceHeight = viewport.topVisiblePos() - viewport.topDataPos() - bufferPadding();
for (let i = 0; i < buffer.length; i++) {
itemHeight = buffer[i].element.outerHeight(true);
if (overageHeight + itemHeight > emptySpaceHeight) {
break;
}
topPadding.cache.add(buffer[i]);
overageHeight += itemHeight;
overage++;
}
if (overage > 0) {
// we need to adjust top padding element before items are removed from top
// to avoid strange behaviour of scroll bar during remove top items when we are at the very bottom
topPadding.height(topPadding.height() + overageHeight);
buffer.bof = false;
buffer.remove(0, overage);
buffer.first += overage;
}
},
adjustPaddings() {
if (!buffer.length) {
return;
}
// precise heights calculation based on items that are in buffer or that were in buffer once
const visibleItemsHeight = buffer.reduce((summ, item) => summ + item.element.outerHeight(true), 0);
let topPaddingHeight = 0, topCount = 0;
topPadding.cache.forEach(item => {
if(item.index < buffer.first) {
topPaddingHeight += item.height;
topCount++;
}
});
let bottomPaddingHeight = 0, bottomCount = 0;
bottomPadding.cache.forEach(item => {
if(item.index >= buffer.next) {
bottomPaddingHeight += item.height;
bottomCount++;
}
});
const totalHeight = visibleItemsHeight + topPaddingHeight + bottomPaddingHeight;
const averageItemHeight = totalHeight / (topCount + bottomCount + buffer.length);
// average heights calculation, items that have never been reached
let adjustTopPadding = buffer.minIndexUser !== null && buffer.minIndex > buffer.minIndexUser;
let adjustBottomPadding = buffer.maxIndexUser !== null && buffer.maxIndex < buffer.maxIndexUser;
let topPaddingHeightAdd = adjustTopPadding ? (buffer.minIndex - buffer.minIndexUser) * averageItemHeight : 0;
let bottomPaddingHeightAdd = adjustBottomPadding ? (buffer.maxIndexUser - buffer.maxIndex) * averageItemHeight : 0;
// paddings combine adjustment
topPadding.height(topPaddingHeight + topPaddingHeightAdd);
bottomPadding.height(bottomPaddingHeight + bottomPaddingHeightAdd);
},
onAfterMinIndexSet(topPaddingHeightOld) {
// additional scrollTop adjustment in case of datasource.minIndex external set
if (buffer.minIndexUser !== null && buffer.minIndex > buffer.minIndexUser) {
let diff = topPadding.height() - topPaddingHeightOld;
viewport.scrollTop(viewport.scrollTop() + diff);
while((diff -= viewport.scrollTop()) > 0) {
bottomPadding.height(bottomPadding.height() + diff);
viewport.scrollTop(viewport.scrollTop() + diff);
}
}
},
onAfterPrepend(updates) {
if (!updates.prepended.length)
return;
const height = buffer.effectiveHeight(updates.prepended);
const paddingHeight = topPadding.height() - height;
if (paddingHeight >= 0) {
topPadding.height(paddingHeight);
}
else {
topPadding.height(0);
viewport.scrollTop(viewport.scrollTop() - paddingHeight);
}
},
resetTopPadding() {
topPadding.height(0);
topPadding.cache.clear();
},
resetBottomPadding() {
bottomPadding.height(0);
bottomPadding.cache.clear();
},
removeCacheItem(item, isTop) {
topPadding.cache.remove(item, isTop);
bottomPadding.cache.remove(item, isTop);
},
removeItem(item) {
this.removeCacheItem(item);
return buffer.remove(item);
}
});
return viewport;
}