Files
OpenMediaCenter/src/elements/VideoContainer/VideoContainer.tsx

98 lines
2.6 KiB
TypeScript
Raw Normal View History

import React from 'react';
import Preview from '../Preview/Preview';
import style from './VideoContainer.module.css';
import {VideoTypes} from '../../types/ApiTypes';
2020-12-29 19:39:30 +00:00
interface props {
data: VideoTypes.VideoUnloadedType[]
2020-12-29 19:39:30 +00:00
}
interface state {
loadeditems: VideoTypes.VideoUnloadedType[];
2020-12-29 19:39:30 +00:00
selectionnr: number;
}
/**
* A videocontainer storing lots of Preview elements
* includes scroll handling and loading of preview infos
*/
2020-12-29 19:39:30 +00:00
class VideoContainer extends React.Component<props, state> {
// stores current index of loaded elements
2020-12-29 19:39:30 +00:00
loadindex: number = 0;
2020-12-29 19:39:30 +00:00
constructor(props: props) {
super(props);
this.state = {
loadeditems: [],
2020-12-29 19:39:30 +00:00
selectionnr: 0
};
}
2020-06-07 22:17:55 +02:00
2020-12-29 19:39:30 +00:00
componentDidMount(): void {
document.addEventListener('scroll', this.trackScrolling);
this.loadPreviewBlock(16);
}
2020-12-29 19:39:30 +00:00
render(): JSX.Element {
return (
<div className={style.maincontent}>
{this.state.loadeditems.map(elem => (
<Preview
key={elem.movie_id}
name={elem.movie_name}
movie_id={elem.movie_id}/>
))}
2020-06-07 22:17:55 +02:00
{/*todo css for no items to show*/}
{this.state.loadeditems.length === 0 ?
'no items to show!' : null}
{this.props.children}
</div>
);
}
2020-12-29 19:39:30 +00:00
componentWillUnmount(): void {
this.setState({});
// unbind scroll listener when unmounting component
document.removeEventListener('scroll', this.trackScrolling);
}
/**
* load previews to the container
* @param nr number of previews to load
*/
2020-12-29 19:39:30 +00:00
loadPreviewBlock(nr: number): void {
console.log('loadpreviewblock called ...');
let ret = [];
for (let i = 0; i < nr; i++) {
// only add if not end
2020-12-29 19:39:30 +00:00
if (this.props.data.length > this.loadindex + i) {
ret.push(this.props.data[this.loadindex + i]);
}
}
this.setState({
loadeditems: [
...this.state.loadeditems,
...ret
]
});
this.loadindex += nr;
}
/**
* scroll event handler -> load new previews if on bottom
*/
2020-12-29 19:39:30 +00:00
trackScrolling = (): void => {
// comparison if current scroll position is on bottom --> 200 is bottom offset to trigger load
if (window.innerHeight + document.documentElement.scrollTop + 200 >= document.documentElement.offsetHeight) {
this.loadPreviewBlock(8);
}
};
}
2020-06-07 22:17:55 +02:00
export default VideoContainer;