2022-09-06 12:03:37 +00:00
|
|
|
<template>
|
2023-03-24 15:29:46 +00:00
|
|
|
<div ref="container" v-bind="ptm('root')">
|
2022-09-06 12:03:37 +00:00
|
|
|
<slot v-if="loaded"></slot>
|
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script>
|
2023-03-31 21:51:41 +00:00
|
|
|
import BaseComponent from 'primevue/basecomponent';
|
2023-10-02 10:46:09 +00:00
|
|
|
import DeferredContentStyle from 'primevue/deferredcontent/style';
|
2023-03-24 15:29:46 +00:00
|
|
|
|
2022-09-06 12:03:37 +00:00
|
|
|
export default {
|
|
|
|
name: 'DeferredContent',
|
2023-03-31 21:51:41 +00:00
|
|
|
extends: BaseComponent,
|
2022-09-06 12:03:37 +00:00
|
|
|
emits: ['load'],
|
2023-10-02 10:46:09 +00:00
|
|
|
style: DeferredContentStyle,
|
2022-09-06 12:03:37 +00:00
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
loaded: false
|
2022-09-14 11:26:01 +00:00
|
|
|
};
|
2022-09-06 12:03:37 +00:00
|
|
|
},
|
|
|
|
mounted() {
|
|
|
|
if (!this.loaded) {
|
2022-09-14 11:26:01 +00:00
|
|
|
if (this.shouldLoad()) this.load();
|
|
|
|
else this.bindScrollListener();
|
2022-09-06 12:03:37 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
beforeUnmount() {
|
|
|
|
this.unbindScrollListener();
|
|
|
|
},
|
|
|
|
methods: {
|
|
|
|
bindScrollListener() {
|
|
|
|
this.documentScrollListener = () => {
|
|
|
|
if (this.shouldLoad()) {
|
|
|
|
this.load();
|
|
|
|
this.unbindScrollListener();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
window.addEventListener('scroll', this.documentScrollListener);
|
|
|
|
},
|
|
|
|
unbindScrollListener() {
|
|
|
|
if (this.documentScrollListener) {
|
|
|
|
window.removeEventListener('scroll', this.documentScrollListener);
|
|
|
|
this.documentScrollListener = null;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
shouldLoad() {
|
|
|
|
if (this.loaded) {
|
|
|
|
return false;
|
2022-09-14 11:26:01 +00:00
|
|
|
} else {
|
2022-09-06 12:03:37 +00:00
|
|
|
const rect = this.$refs.container.getBoundingClientRect();
|
|
|
|
const docElement = document.documentElement;
|
|
|
|
const winHeight = docElement.clientHeight;
|
|
|
|
|
2022-09-14 11:26:01 +00:00
|
|
|
return winHeight >= rect.top;
|
2022-09-06 12:03:37 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
load(event) {
|
|
|
|
this.loaded = true;
|
|
|
|
this.$emit('load', event);
|
|
|
|
}
|
|
|
|
}
|
2022-09-14 11:26:01 +00:00
|
|
|
};
|
2022-09-06 12:03:37 +00:00
|
|
|
</script>
|