(scrollTop, init)
| 1225 | }); |
| 1226 | |
| 1227 | function scroll (scrollTop, init) { |
| 1228 | var size = that.selectpicker.current.elements.length, |
| 1229 | chunks = [], |
| 1230 | chunkSize, |
| 1231 | chunkCount, |
| 1232 | firstChunk, |
| 1233 | lastChunk, |
| 1234 | currentChunk, |
| 1235 | prevPositions, |
| 1236 | positionIsDifferent, |
| 1237 | previousElements, |
| 1238 | menuIsDifferent = true, |
| 1239 | isVirtual = that.isVirtual(); |
| 1240 | |
| 1241 | that.selectpicker.view.scrollTop = scrollTop; |
| 1242 | |
| 1243 | chunkSize = Math.ceil(that.sizeInfo.menuInnerHeight / that.sizeInfo.liHeight * 1.5); // number of options in a chunk |
| 1244 | chunkCount = Math.round(size / chunkSize) || 1; // number of chunks |
| 1245 | |
| 1246 | for (var i = 0; i < chunkCount; i++) { |
| 1247 | var endOfChunk = (i + 1) * chunkSize; |
| 1248 | |
| 1249 | if (i === chunkCount - 1) { |
| 1250 | endOfChunk = size; |
| 1251 | } |
| 1252 | |
| 1253 | chunks[i] = [ |
| 1254 | (i) * chunkSize + (!i ? 0 : 1), |
| 1255 | endOfChunk |
| 1256 | ]; |
| 1257 | |
| 1258 | if (!size) break; |
| 1259 | |
| 1260 | if (currentChunk === undefined && scrollTop - 1 <= that.selectpicker.current.data[endOfChunk - 1].position - that.sizeInfo.menuInnerHeight) { |
| 1261 | currentChunk = i; |
| 1262 | } |
| 1263 | } |
| 1264 | |
| 1265 | if (currentChunk === undefined) currentChunk = 0; |
| 1266 | |
| 1267 | prevPositions = [that.selectpicker.view.position0, that.selectpicker.view.position1]; |
| 1268 | |
| 1269 | // always display previous, current, and next chunks |
| 1270 | firstChunk = Math.max(0, currentChunk - 1); |
| 1271 | lastChunk = Math.min(chunkCount - 1, currentChunk + 1); |
| 1272 | |
| 1273 | that.selectpicker.view.position0 = isVirtual === false ? 0 : (Math.max(0, chunks[firstChunk][0]) || 0); |
| 1274 | that.selectpicker.view.position1 = isVirtual === false ? size : (Math.min(size, chunks[lastChunk][1]) || 0); |
| 1275 | |
| 1276 | positionIsDifferent = prevPositions[0] !== that.selectpicker.view.position0 || prevPositions[1] !== that.selectpicker.view.position1; |
| 1277 | |
| 1278 | if (that.activeIndex !== undefined) { |
| 1279 | prevActive = that.selectpicker.main.elements[that.prevActiveIndex]; |
| 1280 | active = that.selectpicker.main.elements[that.activeIndex]; |
| 1281 | selected = that.selectpicker.main.elements[that.selectedIndex]; |
| 1282 | |
| 1283 | if (init) { |
| 1284 | if (that.activeIndex !== that.selectedIndex) { |
no test coverage detected