Refactor on VirtualScroller

pull/1453/head
mertsincan 2021-08-13 16:53:11 +03:00
parent 5243d5ba00
commit efc8b36523
1 changed files with 40 additions and 22 deletions

View File

@ -1,10 +1,13 @@
<template> <template>
<template v-if="!disabled">
<div :ref="elementRef" :class="containerClass" :style="style" @scroll="onScroll"> <div :ref="elementRef" :class="containerClass" :style="style" @scroll="onScroll">
<slot name="content" styleClass="p-virtualscroller-content" :contentRef="contentRef" :items="loadedItems" :getItemOptions="getOptions">
<div :ref="contentRef" class="p-virtualscroller-content"> <div :ref="contentRef" class="p-virtualscroller-content">
<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>
<div :ref="spacerRef" class="p-virtualscroller-spacer"></div> <div :ref="spacerRef" class="p-virtualscroller-spacer"></div>
<div :class="loaderClass" v-if="d_loading"> <div :class="loaderClass" v-if="d_loading">
<template v-for="(loadItem, index) of loaderArr" :key="index"> <template v-for="(loadItem, index) of loaderArr" :key="index">
@ -15,13 +18,21 @@
</div> </div>
</div> </div>
</template> </template>
<template v-else>
<slot></slot>
<slot name="content"></slot>
</template>
</template>
<script> <script>
export default { export default {
name: 'VirtualScroller', name: 'VirtualScroller',
emits: ['update:numToleratedItems', 'scroll-index-change', 'lazy-load'], emits: ['update:numToleratedItems', 'scroll-index-change', 'lazy-load'],
props: { props: {
items: null, items: {
type: Array,
default: null
},
itemSize: { itemSize: {
type: [Number,Array], type: [Number,Array],
default: null default: null
@ -53,7 +64,11 @@ export default {
default: false default: false
}, },
style: null, style: null,
class: null class: null,
disabled: {
type: Boolean,
default: false
}
}, },
data() { data() {
return { return {
@ -71,9 +86,7 @@ export default {
spacer: null, spacer: null,
scrollTimeout: null, scrollTimeout: null,
mounted() { mounted() {
this.$nextTick(function () {
this.init(); this.init();
});
}, },
watch: { watch: {
numToleratedItems(newValue) { numToleratedItems(newValue) {
@ -81,13 +94,18 @@ export default {
}, },
loading(newValue) { loading(newValue) {
this.d_loading = newValue; this.d_loading = newValue;
},
items() {
this.init();
} }
}, },
methods: { methods: {
init() { init() {
if (!this.disabled) {
this.setSize(); this.setSize();
this.calculateOptions(); this.calculateOptions();
this.setSpacerSize(); this.setSpacerSize();
}
}, },
getLast(last, isCols) { getLast(last, isCols) {
return this.items ? Math.min((isCols ? this.items[0].length : this.items.length), last) : 0; return this.items ? Math.min((isCols ? this.items[0].length : this.items.length), last) : 0;