Fixed #1844 - Improve VirtualScroller implementation for Data components

pull/1846/head
mertsincan 2021-12-05 20:19:49 +03:00
parent 35d6cf003c
commit 55ba2551b5
1 changed files with 298 additions and 257 deletions

View File

@ -1,41 +1,50 @@
<template> <template>
<template v-if="!disabled"> <template v-if="!disabled">
<div :ref="elementRef" :class="containerClass" :style="style" @scroll="onScroll"> <div :ref="elementRef" :class="containerClass" :tabindex="0" :style="style" @scroll="onScroll">
<slot name="content" styleClass="p-virtualscroller-content" :contentRef="contentRef" :items="loadedItems" :getItemOptions="getOptions"> <slot name="content" :styleClass="contentClass" :items="loadedItems" :getItemOptions="getOptions" :loading="d_loading" :getLoaderOptions="getLoaderOptions" :itemSize="itemSize"
<div :ref="contentRef" class="p-virtualscroller-content"> :rows="loadedRows" :columns="loadedColumns" :contentRef="contentRef" :spacerStyle="spacerStyle" :contentStyle="contentStyle"
:vertical="isVertical()" :horizontal="isHorizontal()" :both="isBoth()">
<div :ref="contentRef" :class="contentClass" :style="contentStyle">
<template v-for="(item, index) of loadedItems" :key="index"> <template v-for="(item, index) of loadedItems" :key="index">
<slot name="item" :item="item" :options="getOptions(index)"></slot> <slot name="item" :item="item" :options="getOptions(index)"></slot>
</template> </template>
</div> </div>
</slot> </slot>
<div :ref="spacerRef" class="p-virtualscroller-spacer"></div> <div class="p-virtualscroller-spacer" :style="spacerStyle" v-if="showSpacer"></div>
<div :class="loaderClass" v-if="d_loading"> <div :class="loaderClass" v-if="!loaderDisabled && showLoader && d_loading">
<template v-for="(loadItem, index) of loaderArr" :key="index"> <template v-if="$slots && $slots.loader">
<slot name="loader" :options="getLoaderOptions(index)"> <template v-for="(_, index) of loaderArr" :key="index">
<i class="p-virtualscroller-loading-icon pi pi-spinner pi-spin"></i> <slot name="loader" :options="getLoaderOptions(index, isBoth() && { numCols: d_numItemsInViewport.cols })"></slot>
</slot> </template>
</template> </template>
<i v-else class="p-virtualscroller-loading-icon pi pi-spinner pi-spin"></i>
</div> </div>
</div> </div>
</template> </template>
<template v-else> <template v-else>
<slot></slot> <slot></slot>
<slot name="content" :items="items"></slot> <slot name="content" :items="items" :rows="items" :columns="loadedColumns"></slot>
</template> </template>
</template> </template>
<script> <script>
export default { export default {
name: 'VirtualScroller', name: 'VirtualScroller',
emits: ['update:numToleratedItems', 'scroll-index-change', 'lazy-load'], emits: ['update:numToleratedItems', 'scroll', 'scroll-index-change', 'lazy-load'],
props: { props: {
id: {
type: String,
default: null
},
style: null,
class: null,
items: { items: {
type: Array, type: Array,
default: null default: null
}, },
itemSize: { itemSize: {
type: [Number,Array], type: [Number,Array],
default: null default: 0
}, },
scrollHeight: null, scrollHeight: null,
scrollWidth: null, scrollWidth: null,
@ -55,17 +64,27 @@ export default {
type: Boolean, type: Boolean,
default: false default: false
}, },
showLoader: { disabled: {
type: Boolean, type: Boolean,
default: false default: false
}, },
loaderDisabled: {
type: Boolean,
default: false
},
columns: {
type: Array,
default: null
},
loading: { loading: {
type: Boolean, type: Boolean,
default: false default: false
}, },
style: null, showSpacer: {
class: null, type: Boolean,
disabled: { default: true
},
showLoader: {
type: Boolean, type: Boolean,
default: false default: false
} }
@ -78,15 +97,19 @@ export default {
lastScrollPos: this.isBoth() ? { top: 0, left: 0 } : 0, lastScrollPos: this.isBoth() ? { top: 0, left: 0 } : 0,
d_numToleratedItems: this.numToleratedItems, d_numToleratedItems: this.numToleratedItems,
d_loading: this.loading, d_loading: this.loading,
loaderArr: null loaderArr: [],
spacerStyle: {},
contentStyle: {}
} }
}, },
element: null, element: null,
content: null, content: null,
spacer: null, lastScrollPos: null,
scrollTimeout: null, scrollTimeout: null,
mounted() { mounted() {
this.init(); this.init();
this.lastScrollPos = this.isBoth() ? { top: 0, left: 0 } : 0;
}, },
watch: { watch: {
numToleratedItems(newValue) { numToleratedItems(newValue) {
@ -95,67 +118,185 @@ export default {
loading(newValue) { loading(newValue) {
this.d_loading = newValue; this.d_loading = newValue;
}, },
items(newValue, oldVal) { items(newValue, oldValue) {
if (!oldVal || oldVal.length !== (newValue || []).length) { if (!oldValue || oldValue.length !== (newValue || []).length) {
this.init(); this.init();
} }
},
orientation() {
this.lastScrollPos = this.isBoth() ? { top: 0, left: 0 } : 0;
} }
}, },
methods: { methods: {
init() { init() {
if (!this.disabled) { this.setSize();
this.setSize(); this.calculateOptions();
this.calculateOptions(); this.setSpacerSize();
this.setSpacerSize(); },
isVertical() {
return this.orientation === 'vertical';
},
isHorizontal() {
return this.orientation === 'horizontal';
},
isBoth() {
return this.orientation === 'both';
},
scrollTo(options) {
this.element && this.element.scrollTo(options);
},
scrollToIndex(index, behavior = 'auto') {
const both = this.isBoth();
const horizontal = this.isHorizontal();
const contentPos = this.getContentPosition();
const calculateFirst = (_index = 0, _numT) => (_index <= _numT ? 0 : _index);
const calculateCoord = (_first, _size, _cpos) => (_first * _size) + _cpos;
const scrollTo = (left = 0, top = 0) => this.scrollTo({ left, top, behavior });
if (both) {
const newFirst = { rows: calculateFirst(index[0], this.d_numToleratedItems[0]), cols: calculateFirst(index[1], this.d_numToleratedItems[1]) };
if (newFirst.rows !== this.first.rows || newFirst.cols !== this.first.cols) {
scrollTo(calculateCoord(newFirst.cols, this.itemSize[1], contentPos.left), calculateCoord(newFirst.rows, this.itemSize[0], contentPos.top))
this.first = newFirst;
}
}
else {
const newFirst = calculateFirst(index, this.d_numToleratedItems);
if (newFirst !== this.first) {
horizontal ? scrollTo(calculateCoord(newFirst, this.itemSize, contentPos.left), 0) : scrollTo(0, calculateCoord(newFirst, this.itemSize, contentPos.top));
this.first = newFirst;
}
} }
}, },
getLast(last, isCols) { scrollInView(index, to, behavior = 'auto') {
return this.items ? Math.min((isCols ? this.items[0].length : this.items.length), last) : 0; if (to) {
const both = this.isBoth();
const horizontal = this.isHorizontal();
const { first, viewport } = this.getRenderedRange();
const scrollTo = (left = 0, top = 0) => this.scrollTo({ left, top, behavior });
const isToStart = to === 'to-start';
const isToEnd = to === 'to-end';
if (isToStart) {
if (both) {
if (viewport.first.rows - first.rows > index[0]) {
scrollTo(viewport.first.cols * this.itemSize[1], (viewport.first.rows - 1) * this.itemSize[0]);
}
else if (viewport.first.cols - first.cols > index[1]) {
scrollTo((viewport.first.cols - 1) * this.itemSize[1], viewport.first.rows * this.itemSize[0]);
}
}
else {
if (viewport.first - first > index) {
const pos = (viewport.first - 1) * this.itemSize;
horizontal ? scrollTo(pos, 0) : scrollTo(0, pos);
}
}
}
else if (isToEnd) {
if (both) {
if (viewport.last.rows - first.rows <= index[0] + 1) {
scrollTo(viewport.first.cols * this.itemSize[1], (viewport.first.rows + 1) * this.itemSize[0]);
}
else if (viewport.last.cols - first.cols <= index[1] + 1) {
scrollTo((viewport.first.cols + 1) * this.itemSize[1], viewport.first.rows * this.itemSize[0]);
}
}
else {
if (viewport.last - first <= index + 1) {
const pos = (viewport.first + 1) * this.itemSize;
horizontal ? scrollTo(pos, 0) : scrollTo(0, pos);
}
}
}
}
else {
this.scrollToIndex(index, behavior);
}
}, },
calculateOptions() { getRenderedRange() {
const isBoth = this.isBoth(); const calculateFirstInViewport = (_pos, _size) => Math.floor(_pos / (_size || _pos));
const isHorizontal = this.isHorizontal();
const first = this.first;
const itemSize = this.itemSize;
const contentPadding = this.getContentPadding();
const contentWidth = this.element ? this.element.offsetWidth - contentPadding.left : 0;
const contentHeight = this.element ? this.element.offsetHeight - contentPadding.top : 0;
const calculateNumItemsInViewport = (_contentSize, _itemSize) => Math.ceil(_contentSize / (_itemSize || _contentSize));
const numItemsInViewport = isBoth ?
{ rows: calculateNumItemsInViewport(contentHeight, itemSize[0]), cols: calculateNumItemsInViewport(contentWidth, itemSize[1]) } :
calculateNumItemsInViewport((isHorizontal ? contentWidth : contentHeight), itemSize);
let numToleratedItems = this.d_numToleratedItems || Math.ceil((isBoth ? numItemsInViewport.rows : numItemsInViewport) / 2); let firstInViewport = this.first;
const calculateLast = (_first, _num, _isCols) => this.getLast(_first + _num + ((_first < numToleratedItems ? 2 : 3) * numToleratedItems), _isCols); let lastInViewport = 0;
const last = isBoth ?
{ rows: calculateLast(first.rows, numItemsInViewport.rows), cols: calculateLast(first.cols, numItemsInViewport.cols, true) } :
calculateLast(first, numItemsInViewport);
this.d_numToleratedItems = numToleratedItems; if (this.element) {
this.$emit('update:numToleratedItems', this.d_numToleratedItems); const both = this.isBoth();
this.last = last; const horizontal = this.isHorizontal();
this.numItemsInViewport = numItemsInViewport; const scrollTop = this.element.scrollTop;
const scrollLeft = this.element.scrollLeft;
if (this.showLoader) { if (both) {
if (this.$slots && this.$slots.loader) { firstInViewport = { rows: calculateFirstInViewport(scrollTop, this.itemSize[0]), cols: calculateFirstInViewport(scrollLeft, this.itemSize[1]) };
this.loaderArr = Array.from({ length: (isBoth ? numItemsInViewport.rows : numItemsInViewport) }); lastInViewport = { rows: firstInViewport.rows + this.numItemsInViewport.rows, cols: firstInViewport.cols + this.numItemsInViewport.cols };
} }
else { else {
this.loaderArr = Array.from({ length: 1}); const scrollPos = horizontal ? scrollLeft : scrollTop;
firstInViewport = calculateFirstInViewport(scrollPos, this.itemSize);
lastInViewport = firstInViewport + this.numItemsInViewport;
} }
} }
return {
first: this.first,
last: this.last,
viewport: {
first: firstInViewport,
last: lastInViewport
}
};
},
calculateOptions() {
const both = this.isBoth();
const horizontal = this.isHorizontal();
const contentPos = this.getContentPosition();
const contentWidth = this.element ? this.element.offsetWidth - contentPos.left : 0;
const contentHeight = this.element ? this.element.offsetHeight - contentPos.top : 0;
const calculateNumItemsInViewport = (_contentSize, _itemSize) => Math.ceil(_contentSize / (_itemSize || _contentSize));
const calculateNumToleratedItems = (_numItems) => Math.ceil(_numItems / 2);
const numItemsInViewport = both ?
{ rows: calculateNumItemsInViewport(contentHeight, this.itemSize[0]), cols: calculateNumItemsInViewport(contentWidth, this.itemSize[1]) } :
calculateNumItemsInViewport((horizontal ? contentWidth : contentHeight), this.itemSize);
let numToleratedItems = this.d_numToleratedItems || (both ?
[calculateNumToleratedItems(numItemsInViewport.rows), calculateNumToleratedItems(numItemsInViewport.cols)] :
calculateNumToleratedItems(numItemsInViewport));
const calculateLast = (_first, _num, _numT, _isCols) => this.getLast(_first + _num + ((_first < _numT ? 2 : 3) * _numT), _isCols);
const last = both ?
{ rows: calculateLast(this.first.rows, numItemsInViewport.rows, numToleratedItems[0]), cols: calculateLast(this.first.cols, numItemsInViewport.cols, numToleratedItems[1], true) } :
calculateLast(this.first, numItemsInViewport, numToleratedItems);
this.last = last;
this.numItemsInViewport = numItemsInViewport;
this.d_numToleratedItems = numToleratedItems;
this.$emit('update:numToleratedItems', this.d_numToleratedItems);
if (this.showLoader) {
this.loaderArr = both ?
Array.from({ length: numItemsInViewport.rows }).map(() => Array.from({ length: numItemsInViewport.cols })) :
Array.from({ length: numItemsInViewport });
}
if (this.lazy) { if (this.lazy) {
this.$emit('lazy-load', { first, last }); this.$emit('lazy-load', { first: this.first, last });
} }
}, },
getContentPadding() { getLast(last = 0, isCols) {
if (this.items) {
return Math.min((isCols ? (this.columns || this.items[0]).length : this.items.length), last);
}
return 0;
},
getContentPosition() {
if (this.content) { if (this.content) {
const style = getComputedStyle(this.content); const style = getComputedStyle(this.content);
const left = parseInt(parseFloat(style.paddingLeft.slice(0, -2)), 10); const left = parseInt(style.paddingLeft, 10) + Math.max(parseInt(style.left, 10), 0);
const right = parseInt(parseFloat(style.paddingRight.slice(0, -2)), 10); const right = parseInt(style.paddingRight, 10) + Math.max(parseInt(style.right, 10), 0);
const top = parseInt(parseFloat(style.paddingTop.slice(0, -2)), 10); const top = parseInt(style.paddingTop, 10) + Math.max(parseInt(style.top, 10), 0);
const bottom = parseInt(parseFloat(style.paddingBottom.slice(0, -2)), 10); const bottom = parseInt(style.paddingBottom, 10) + Math.max(parseInt(style.bottom, 10), 0);
return { left, right, top, bottom, x: left + right, y: top + bottom }; return { left, right, top, bottom, x: left + right, y: top + bottom };
} }
@ -164,126 +305,125 @@ export default {
}, },
setSize() { setSize() {
if (this.element) { if (this.element) {
const isBoth = this.isBoth(); const both = this.isBoth();
const isHorizontal = this.isHorizontal(); const horizontal = this.isHorizontal();
const parentElement = this.element.parentElement; const parentElement = this.element.parentElement;
const width = this.scrollWidth || `${(this.element.offsetWidth || parentElement.offsetWidth)}px`; const width = this.scrollWidth || `${(this.element.offsetWidth || parentElement.offsetWidth)}px`;
const height = this.scrollHeight || `${(this.element.offsetHeight || parentElement.offsetHeight)}px`; const height = this.scrollHeight || `${(this.element.offsetHeight || parentElement.offsetHeight)}px`;
const setProp = (_name, _value) => this.element.style[_name] = _value; const setProp = (_name, _value) => this.element.style[_name] = _value;
if (isBoth) { if (both || horizontal) {
setProp('height', height); setProp('height', height);
setProp('width', width); setProp('width', width);
} }
else { else {
isHorizontal ? setProp('width', width) : setProp('height', height); setProp('height', height);
} }
} }
}, },
setSpacerSize() { setSpacerSize() {
const items = this.items; const items = this.items;
if (this.spacer && items) { if (items) {
const isBoth = this.isBoth(); const both = this.isBoth();
const isHorizontal = this.isHorizontal(); const horizontal = this.isHorizontal();
const itemSize = this.itemSize; const contentPos = this.getContentPosition();
const contentPadding = this.getContentPadding(); const setProp = (_name, _value, _size, _cpos = 0) => this.spacerStyle = { ...this.spacerStyle, ...{ [`${_name}`]: (((_value || []).length * _size) + _cpos) + 'px' } };
const setProp = (_name, _value, _size, _padding = 0) => this.spacer.style[_name] = (((_value || []).length * _size) + _padding) + 'px';
if (isBoth) { if (both) {
setProp('height', items[0], itemSize[0], contentPadding.y); setProp('height', items, this.itemSize[0], contentPos.y);
setProp('width', items[1], itemSize[1], contentPadding.x); setProp('width', (this.columns || items[1]), this.itemSize[1], contentPos.x);
} }
else { else {
isHorizontal ? setProp('width', items, itemSize, contentPadding.x) : setProp('height', items, itemSize, contentPadding.y); horizontal ? setProp('width', (this.columns || items), this.itemSize, contentPos.x) : setProp('height', items, this.itemSize, contentPos.y);
} }
} }
}, },
setContentPosition(pos) { setContentPosition(pos) {
if (this.content) { if (this.content) {
const isBoth = this.isBoth(); const both = this.isBoth();
const isHorizontal = this.isHorizontal(); const horizontal = this.isHorizontal();
const content = this.content;
const first = pos ? pos.first : this.first; const first = pos ? pos.first : this.first;
const itemSize = this.itemSize;
const calculateTranslateVal = (_first, _size) => (_first * _size); const calculateTranslateVal = (_first, _size) => (_first * _size);
const setTransform = (_x = 0, _y = 0) => content.style.transform = `translate3d(${_x}px, ${_y}px, 0)`; const setTransform = (_x = 0, _y = 0) => {
this.contentStyle = { ...this.contentStyle, ...{ transform: `translate3d(${_x}px, ${_y}px, 0)` } };
};
if (isBoth) { if (both) {
setTransform(calculateTranslateVal(first.cols, itemSize[1]), calculateTranslateVal(first.rows, itemSize[0])); setTransform(calculateTranslateVal(first.cols, this.itemSize[1]), calculateTranslateVal(first.rows, this.itemSize[0]));
} }
else { else {
const translateVal = calculateTranslateVal(first, itemSize); const translateVal = calculateTranslateVal(first, this.itemSize);
isHorizontal ? setTransform(translateVal, 0) : setTransform(0, translateVal); horizontal ? setTransform(translateVal, 0) : setTransform(0, translateVal);
} }
} }
}, },
onScrollPositionChange(event) { onScrollPositionChange(event) {
const target = event.target; const target = event.target;
const isBoth = this.isBoth(); const both = this.isBoth();
const isHorizontal = this.isHorizontal(); const horizontal = this.isHorizontal();
const itemSize = this.itemSize; const contentPos = this.getContentPosition();
const contentPadding = this.getContentPadding(); const calculateScrollPos = (_pos, _cpos) => _pos ? (_pos > _cpos ? _pos - _cpos : _pos) : 0;
const calculateScrollPos = (_pos, _padding) => _pos ? (_pos > _padding ? _pos - _padding : _pos) : 0;
const calculateCurrentIndex = (_pos, _size) => Math.floor(_pos / (_size || _pos)); const calculateCurrentIndex = (_pos, _size) => Math.floor(_pos / (_size || _pos));
const calculateTriggerIndex = (_currentIndex, _first, _last, _num, _isScrollDownOrRight) => { const calculateTriggerIndex = (_currentIndex, _first, _last, _num, _numT, _isScrollDownOrRight) => {
return (_currentIndex <= this.d_numToleratedItems ? this.d_numToleratedItems : (_isScrollDownOrRight ? (_last - _num - this.d_numToleratedItems) : (_first + this.d_numToleratedItems - 1))) return (_currentIndex <= _numT ? _numT : (_isScrollDownOrRight ? (_last - _num - _numT) : (_first + _numT - 1)))
}; };
const calculateFirst = (_currentIndex, _triggerIndex, _first, _last, _num, _isScrollDownOrRight) => { const calculateFirst = (_currentIndex, _triggerIndex, _first, _last, _num, _numT, _isScrollDownOrRight) => {
if (_currentIndex <= this.d_numToleratedItems) if (_currentIndex <= _numT)
return 0; return 0;
else else
return _isScrollDownOrRight ? return Math.max(0, _isScrollDownOrRight ?
(_currentIndex < _triggerIndex ? _first : _currentIndex - this.d_numToleratedItems) : (_currentIndex < _triggerIndex ? _first : _currentIndex - _numT) :
(_currentIndex > _triggerIndex ? _first : _currentIndex - (2 * this.d_numToleratedItems)); (_currentIndex > _triggerIndex ? _first : _currentIndex - (2 * _numT)));
}; };
const calculateLast = (_currentIndex, _first, _last, _num, _isCols) => { const calculateLast = (_currentIndex, _first, _last, _num, _numT, _isCols) => {
let lastValue = _first + _num + (2 * this.d_numToleratedItems); let lastValue = _first + _num + (2 * _numT);
if (_currentIndex >= this.d_numToleratedItems) { if (_currentIndex >= _numT) {
lastValue += (this.d_numToleratedItems + 1); lastValue += (_numT + 1);
} }
return this.getLast(lastValue, _isCols); return this.getLast(lastValue, _isCols);
}; };
const scrollTop = calculateScrollPos(target.scrollTop, contentPadding.top); const scrollTop = calculateScrollPos(target.scrollTop, contentPos.top);
const scrollLeft = calculateScrollPos(target.scrollLeft, contentPadding.left); const scrollLeft = calculateScrollPos(target.scrollLeft, contentPos.left);
let newFirst = 0; let newFirst = 0;
let newLast = this.last; let newLast = this.last;
let isRangeChanged = false; let isRangeChanged = false;
if (isBoth) { if (both) {
const isScrollDown = this.lastScrollPos.top <= scrollTop; const isScrollDown = this.lastScrollPos.top <= scrollTop;
const isScrollRight = this.lastScrollPos.left <= scrollLeft; const isScrollRight = this.lastScrollPos.left <= scrollLeft;
const currentIndex = { rows: calculateCurrentIndex(scrollTop, itemSize[0]), cols: calculateCurrentIndex(scrollLeft, itemSize[1]) }; const currentIndex = { rows: calculateCurrentIndex(scrollTop, this.itemSize[0]), cols: calculateCurrentIndex(scrollLeft, this.itemSize[1]) };
const triggerIndex = { const triggerIndex = {
rows: calculateTriggerIndex(currentIndex.rows, this.first.rows, this.last.rows, this.numItemsInViewport.rows, isScrollDown), rows: calculateTriggerIndex(currentIndex.rows, this.first.rows, this.last.rows, this.numItemsInViewport.rows, this.d_numToleratedItems[0], isScrollDown),
cols: calculateTriggerIndex(currentIndex.cols, this.first.cols, this.last.cols, this.numItemsInViewport.cols, isScrollRight) cols: calculateTriggerIndex(currentIndex.cols, this.first.cols, this.last.cols, this.numItemsInViewport.cols, this.d_numToleratedItems[1], isScrollRight)
}; };
newFirst = { newFirst = {
rows: calculateFirst(currentIndex.rows, triggerIndex.rows, this.first.rows, this.last.rows, this.numItemsInViewport.rows, isScrollDown), rows: calculateFirst(currentIndex.rows, triggerIndex.rows, this.first.rows, this.last.rows, this.numItemsInViewport.rows, this.d_numToleratedItems[0], isScrollDown),
cols: calculateFirst(currentIndex.cols, triggerIndex.cols, this.first.cols, this.last.cols, this.numItemsInViewport.cols, isScrollRight) cols: calculateFirst(currentIndex.cols, triggerIndex.cols, this.first.cols, this.last.cols, this.numItemsInViewport.cols, this.d_numToleratedItems[1], isScrollRight)
}; };
newLast = { newLast = {
rows: calculateLast(currentIndex.rows, newFirst.rows, this.last.rows, this.numItemsInViewport.rows), rows: calculateLast(currentIndex.rows, newFirst.rows, this.last.rows, this.numItemsInViewport.rows, this.d_numToleratedItems[0]),
cols: calculateLast(currentIndex.cols, newFirst.cols, this.last.cols, this.numItemsInViewport.cols, true) cols: calculateLast(currentIndex.cols, newFirst.cols, this.last.cols, this.numItemsInViewport.cols, this.d_numToleratedItems[1], true)
}; };
isRangeChanged = (newFirst.rows !== this.first.rows || newFirst.cols !== this.first.cols) || (newLast.rows !== this.last.rows || newLast.cols !== this.last.cols);
isRangeChanged = (newFirst.rows !== this.first.rows && newLast.rows !== this.last.rows) || (newFirst.cols !== this.first.cols && newLast.cols !== this.last.cols);
this.lastScrollPos = { top: scrollTop, left: scrollLeft }; this.lastScrollPos = { top: scrollTop, left: scrollLeft };
} }
else { else {
const scrollPos = isHorizontal ? scrollLeft : scrollTop; const scrollPos = horizontal ? scrollLeft : scrollTop;
const isScrollDownOrRight = this.lastScrollPos <= scrollPos; const isScrollDownOrRight = this.lastScrollPos <= scrollPos;
const currentIndex = calculateCurrentIndex(scrollPos, itemSize); const currentIndex = calculateCurrentIndex(scrollPos, this.itemSize);
const triggerIndex = calculateTriggerIndex(currentIndex, this.first, this.last, this.numItemsInViewport, isScrollDownOrRight); const triggerIndex = calculateTriggerIndex(currentIndex, this.first, this.last, this.numItemsInViewport, this.d_numToleratedItems, isScrollDownOrRight);
newFirst = calculateFirst(currentIndex, triggerIndex, this.first, this.last, this.numItemsInViewport, isScrollDownOrRight); newFirst = calculateFirst(currentIndex, triggerIndex, this.first, this.last, this.numItemsInViewport, this.d_numToleratedItems, isScrollDownOrRight);
newLast = calculateLast(currentIndex, newFirst, this.last, this.numItemsInViewport); newLast = calculateLast(currentIndex, newFirst, this.last, this.numItemsInViewport, this.d_numToleratedItems);
isRangeChanged = newFirst !== this.first || newLast !== this.last; isRangeChanged = newFirst !== this.first && newLast !== this.last;
this.lastScrollPos = scrollPos; this.lastScrollPos = scrollPos;
} }
@ -302,27 +442,27 @@ export default {
this.setContentPosition(newState); this.setContentPosition(newState);
if (this.lazy) {
this.$emit('lazy-load', { first, last });
}
this.first = first; this.first = first;
this.last = last; this.last = last;
this.$emit('scroll-index-change', { first, last }); this.$emit('scroll-index-change', newState);
if (this.lazy) {
this.$emit('lazy-load', newState);
}
} }
}, },
onScroll(event) { onScroll(event) {
if (this.delay && !this.lazy) { this.$emit('scroll', event);
if (this.delay) {
if (this.scrollTimeout) { if (this.scrollTimeout) {
clearTimeout(this.scrollTimeout); clearTimeout(this.scrollTimeout);
} }
if (!this.d_loading && this.showLoader) { if (!this.d_loading && this.showLoader) {
const { isRangeChanged: changed } = this.onScrollPositionChange(event); const { isRangeChanged: changed } = this.onScrollPositionChange(event);
changed && (this.d_loading = true);
if (changed) {
this.d_loading = true;
}
} }
this.scrollTimeout = setTimeout(() => { this.scrollTimeout = setTimeout(() => {
@ -338,8 +478,8 @@ export default {
} }
}, },
getOptions(renderedIndex) { getOptions(renderedIndex) {
let count = this.items.length; const count = (this.items || []).length;
let index = this.isBoth() ? this.first.rows + renderedIndex : this.first + renderedIndex; const index = this.isBoth() ? this.first.rows + renderedIndex : this.first + renderedIndex;
return { return {
index, index,
count, count,
@ -349,139 +489,23 @@ export default {
odd: index % 2 !== 0 odd: index % 2 !== 0
}; };
}, },
getLoaderOptions(index) { getLoaderOptions(index, extOptions) {
let count = this.loaderArr.length; let count = this.loaderArr.length;
return { return {
loading: this.d_loading, index,
count,
first: index === 0, first: index === 0,
last: index === (count - 1), last: index === (count - 1),
even: index % 2 === 0, even: index % 2 === 0,
odd: index % 2 !== 0 odd: index % 2 !== 0,
...extOptions
} }
}, },
isHorizontal() {
return this.orientation === 'horizontal';
},
isBoth() {
return this.orientation === 'both';
},
scrollTo(options) {
if (this.element) {
this.element.scrollTo(options);
}
},
scrollToIndex(index, behavior = 'auto') {
const isBoth = this.isBoth();
const isHorizontal = this.isHorizontal();
const itemSize = this.itemSize;
const contentPadding = this.getContentPadding();
const calculateFirst = (_index = 0) => (_index <= this.d_numToleratedItems ? 0 : _index);
const calculateCoord = (_first, _size, _padding) => (_first * _size) + _padding;
const scrollTo = (left = 0, top = 0) => this.scrollTo({ left, top, behavior });
if (isBoth) {
const newFirst = { rows: calculateFirst(index[0]), cols: calculateFirst(index[1]) };
if (newFirst.rows !== this.first.rows || newFirst.cols !== this.first.cols)
scrollTo(calculateCoord(newFirst.cols, itemSize[1], contentPadding.left),calculateCoord(newFirst.rows, itemSize[0], contentPadding.top));
}
else {
const newFirst = calculateFirst(index);
if (newFirst !== this.first) {
isHorizontal ? scrollTo(calculateCoord(newFirst, itemSize, contentPadding.left), 0) : scrollTo(0, calculateCoord(newFirst, itemSize, contentPadding.top));
}
this.first = newFirst;
}
},
scrollInView(index, to, behavior = 'auto') {
if (to) {
const isBoth = this.isBoth();
const isHorizontal = this.isHorizontal();
const { first, viewport } = this.getRenderedRange();
const itemSize = this.itemSize;
const scrollTo = (left = 0, top = 0) => this.scrollTo({ left, top, behavior });
const isToStart = to === 'to-start';
const isToEnd = to === 'to-end';
if (isToStart) {
if (isBoth) {
if (viewport.first.rows - first.rows > index[0]) {
scrollTo(viewport.first.cols * itemSize[1], (viewport.first.rows - 1) * itemSize[0]);
}
else if (viewport.first.cols - first.cols > index[1]) {
scrollTo((viewport.first.cols - 1) * itemSize[1], viewport.first.rows * itemSize[0]);
}
}
else {
if (viewport.first - first > index) {
const pos = (viewport.first - 1) * itemSize;
isHorizontal ? scrollTo(pos, 0) : scrollTo(0, pos);
}
}
}
else if (isToEnd) {
if (isBoth) {
if (viewport.last.rows - first.rows <= index[0] + 1) {
scrollTo(viewport.first.cols * itemSize[1], (viewport.first.rows + 1) * itemSize[0]);
}
else if (viewport.last.cols - first.cols <= index[1] + 1) {
scrollTo((viewport.first.cols + 1) * itemSize[1], viewport.first.rows * itemSize[0]);
}
}
else {
if (viewport.last - first <= index + 1) {
const pos = (viewport.first + 1) * itemSize;
isHorizontal ? scrollTo(pos, 0) : scrollTo(0, pos);
}
}
}
}
else {
this.scrollToIndex(index, behavior);
}
},
getRenderedRange() {
const isBoth = this.isBoth();
const isHorizontal = this.isHorizontal();
const itemSize = this.itemSize;
const calculateFirstInViewport = (_pos, _size) => Math.floor(_pos / (_size || _pos));
let firstInViewport = this.first;
let lastInViewport = 0;
if (this.element) {
const scrollTop = this.element.scrollTop;
const scrollLeft = this.element.scrollLeft;
if (isBoth) {
firstInViewport = { rows: calculateFirstInViewport(scrollTop, itemSize[0]), cols: calculateFirstInViewport(scrollLeft, itemSize[1]) };
lastInViewport = { rows: firstInViewport.rows + this.numItemsInViewport.rows, cols: firstInViewport.cols + this.numItemsInViewport.cols };
}
else {
const scrollPos = isHorizontal ? scrollLeft : scrollTop;
firstInViewport = calculateFirstInViewport(scrollPos, itemSize);
lastInViewport = firstInViewport + this.numItemsInViewport;
}
}
return {
first: this.first,
last: this.last,
viewport: {
first: firstInViewport,
last: lastInViewport
}
};
},
elementRef(el) { elementRef(el) {
this.element = el; this.element = el;
}, },
contentRef(el) { contentRef(el) {
this.content = el; this.content = el;
},
spacerRef(el) {
this.spacer = el;
} }
}, },
computed: { computed: {
@ -491,6 +515,11 @@ export default {
'p-horizontal-scroll': this.isHorizontal() 'p-horizontal-scroll': this.isHorizontal()
}, this.class]; }, this.class];
}, },
contentClass() {
return ['p-virtualscroller-content', {
'p-virtualscroller-loading': this.d_loading
}];
},
loaderClass() { loaderClass() {
return ['p-virtualscroller-loader', { return ['p-virtualscroller-loader', {
'p-component-overlay': !this.$slots.loader 'p-component-overlay': !this.$slots.loader
@ -499,22 +528,33 @@ export default {
loadedItems() { loadedItems() {
const items = this.items; const items = this.items;
if (items && !this.d_loading) { if (items && !this.d_loading) {
const isBoth = this.isBoth(); if (this.isBoth()) {
return items.slice(this.first.rows, this.last.rows).map(item => this.columns ? item : item.slice(this.first.cols, this.last.cols));
if (isBoth) {
return items.slice(this.first.rows, this.last.rows).map((item) => {
const items = item.slice(this.first.cols, this.last.cols);
return items;
});
}
else {
return items.slice(this.first, this.last).map((item) => {
return item;
});
} }
else if (this.isHorizontal() && this.columns)
return items;
else
return items.slice(this.first, this.last);
} }
return []; return [];
},
loadedRows() {
return this.d_loading ? (this.loaderDisabled ? this.loaderArr : []) : this.loadedItems;
},
loadedColumns() {
if (this.columns) {
const both = this.isBoth();
const horizontal = this.isHorizontal();
if (both || horizontal) {
return this.d_loading && this.loaderDisabled ?
(both ? this.loaderArr[0] : this.loaderArr):
this.columns.slice((both ? this.first.cols : this.first), (both ? this.last.cols : this.last));
}
}
return this.columns;
} }
} }
} }
@ -527,6 +567,7 @@ export default {
contain: strict; contain: strict;
transform: translateZ(0); transform: translateZ(0);
will-change: scroll-position; will-change: scroll-position;
outline: 0 none;
} }
.p-virtualscroller-content { .p-virtualscroller-content {