@@ -191,7 +191,9 @@ class PlexMeetsHomeAssistant extends HTMLElement {
191191 window . innerHeight + window . scrollY > height + getOffset ( this . content ) . top - 300 &&
192192 this . renderedItems > 0 &&
193193 this . renderedItems < this . data [ this . config . libraryName ] . length &&
194- ( ! this . maxRows || this . renderedRows < this . config . maxRows )
194+ ( ! this . maxCount || this . renderedItems < this . maxCount ) &&
195+ ( ! this . maxRows || this . renderedRows < this . config . maxRows ) &&
196+ _ . isEmpty ( this . searchValue )
195197 ) {
196198 this . maxRenderCount = this . renderedItems + this . columnsCount * ( loadAdditionalRowsCount * 2 ) ;
197199 this . renderMovieElems ( ) ;
@@ -272,7 +274,6 @@ class PlexMeetsHomeAssistant extends HTMLElement {
272274 }
273275 }
274276 }
275-
276277 this . renderNewElementsIfNeeded ( ) ;
277278 } ) ;
278279 window . addEventListener ( 'resize' , ( ) => {
@@ -600,23 +601,32 @@ class PlexMeetsHomeAssistant extends HTMLElement {
600601 } ;
601602
602603 renderMovieElems = ( ) : void => {
603- if ( this . data [ this . config . libraryName ] && this . renderedItems < this . data [ this . config . libraryName ] . length ) {
604- let count = 0 ;
605- // eslint-disable-next-line consistent-return
606- const searchValues = _ . split ( this . searchValue , ' ' ) ;
607- // eslint-disable-next-line consistent-return
608- let lastRowTop = 0 ;
604+ const renderElements = (
605+ render : boolean ,
606+ hasEpisodesResult : any ,
607+ searchValues : Array < string > ,
608+ itemsPerRow : number
609+ ) : Record < string , any > => {
610+ const origRenderedRows = this . renderedRows ;
611+ const origRenderedItems = this . renderedItems ;
612+ const origColumnsCount = this . columnsCount ;
609613
610614 const loadAdditionalRowsCount = 2 ; // todo: make this configurable
611- this . renderedRows = 0 ;
612- this . columnsCount = 0 ;
613- const hasEpisodesResult = hasEpisodes ( this . data [ this . config . libraryName ] ) ;
615+ let lastRowTop = 0 ;
616+ this . contentContainer . style . width = '' ;
617+ let containerWidth = 0 ;
618+ let renderMore =
619+ ( ! this . maxCount || this . renderedItems < this . maxCount ) &&
620+ ( ! this . maxRenderCount || this . renderedItems < this . maxRenderCount ) &&
621+ ( ! this . maxRows || this . renderedRows <= this . maxRows ) ;
622+
623+ let count = 0 ;
614624 _ . forEach ( this . data [ this . config . libraryName ] , ( movieData : Record < string , any > ) => {
615- if (
625+ renderMore =
616626 ( ! this . maxCount || this . renderedItems < this . maxCount ) &&
617627 ( ! this . maxRenderCount || this . renderedItems < this . maxRenderCount ) &&
618- ( ! this . maxRows || this . renderedRows <= this . maxRows )
619- ) {
628+ ( ! this . maxRows || this . renderedRows <= this . maxRows ) ;
629+ if ( renderMore ) {
620630 const movieElem = this . getMovieElement ( movieData , hasEpisodesResult ) ;
621631 let shouldRender = false ;
622632 if ( this . looseSearch ) {
@@ -644,25 +654,33 @@ class PlexMeetsHomeAssistant extends HTMLElement {
644654 ) {
645655 shouldRender = true ;
646656 }
657+
647658 if ( shouldRender ) {
648- count += 1 ;
659+ count += 1 ; // keeps track of already rendered items for progressive scroll
649660 if ( count > this . renderedItems ) {
650- this . contentContainer . appendChild ( movieElem ) ;
661+ if ( render ) {
662+ this . contentContainer . appendChild ( movieElem ) ;
663+ }
651664 if ( this . useHorizontalScroll ) {
665+ if ( this . renderedItems > 0 && this . renderedItems % itemsPerRow === 0 ) {
666+ this . renderedRows += 1 ;
667+ movieElem . style . clear = 'both' ;
668+ }
652669 const marginRight = 10 ;
653- if ( _ . isEmpty ( this . contentContainer . style . width ) ) {
654- this . contentContainer . style . width = `${ parseFloat ( movieElem . style . width ) + marginRight } px` ;
655- } else {
656- this . contentContainer . style . width = `${ parseFloat ( this . contentContainer . style . width ) +
657- parseFloat ( movieElem . style . width ) +
658- marginRight } px`;
670+ if ( this . renderedRows < 2 || ! this . maxRows || this . maxRows < 2 ) {
671+ containerWidth += parseFloat ( movieElem . style . width ) + marginRight ;
659672 }
660673 }
661674
662675 this . renderedItems += 1 ;
663676 }
664677 }
665- if ( shouldRender && lastRowTop !== movieElem . getBoundingClientRect ( ) . top && ! this . useHorizontalScroll ) {
678+ if (
679+ render &&
680+ shouldRender &&
681+ lastRowTop !== movieElem . getBoundingClientRect ( ) . top &&
682+ ! this . useHorizontalScroll
683+ ) {
666684 this . renderedRows += 1 ;
667685 if ( lastRowTop !== 0 && this . columnsCount === 0 ) {
668686 this . columnsCount = this . renderedItems - 1 ;
@@ -675,10 +693,54 @@ class PlexMeetsHomeAssistant extends HTMLElement {
675693 if ( this . maxRows && this . renderedRows > this . maxRows && ! this . useHorizontalScroll ) {
676694 movieElem . remove ( ) ;
677695 }
696+
678697 return true ;
679698 }
680699 return false ;
681700 } ) ;
701+ const returnObj = {
702+ renderedItems : this . renderedItems
703+ } ;
704+ if ( ! render ) {
705+ this . renderedRows = origRenderedRows ;
706+ this . renderedItems = origRenderedItems ;
707+ this . columnsCount = origColumnsCount ;
708+ }
709+ if ( render && containerWidth > 0 ) {
710+ this . contentContainer . style . width = `${ containerWidth } px` ;
711+ }
712+ return returnObj ;
713+ } ;
714+
715+ const renderMore =
716+ ( ! this . maxCount || this . renderedItems < this . maxCount ) &&
717+ ( ! this . maxRenderCount || this . renderedItems < this . maxRenderCount ) &&
718+ ( ! this . maxRows || this . renderedRows <= this . maxRows ) ;
719+ if (
720+ this . data [ this . config . libraryName ] &&
721+ this . renderedItems < this . data [ this . config . libraryName ] . length &&
722+ renderMore
723+ ) {
724+ let maxRenderedItems = this . data [ this . config . libraryName ] . length ;
725+ let itemsPerRow = this . data [ this . config . libraryName ] . length ;
726+ if ( this . maxCount ) {
727+ maxRenderedItems = this . maxCount ;
728+ }
729+ itemsPerRow = maxRenderedItems ;
730+ if ( this . maxRows ) {
731+ itemsPerRow = Math . ceil ( maxRenderedItems / this . maxRows ) ;
732+ }
733+ const searchValues = _ . split ( this . searchValue , ' ' ) ;
734+
735+ const hasEpisodesResult = hasEpisodes ( this . data [ this . config . libraryName ] ) ;
736+
737+ const { renderedItems } = renderElements ( false , hasEpisodesResult , searchValues , itemsPerRow ) ;
738+ itemsPerRow = renderedItems ;
739+ if ( this . maxRows ) {
740+ itemsPerRow = Math . ceil ( renderedItems / this . maxRows ) ;
741+ }
742+
743+ renderElements ( true , hasEpisodesResult , searchValues , itemsPerRow ) ;
682744 }
683745
684746 const contentbg = this . getElementsByClassName ( 'contentbg' ) [ 0 ] as HTMLElement ;
@@ -715,6 +777,7 @@ class PlexMeetsHomeAssistant extends HTMLElement {
715777 }
716778
717779 this . renderedItems = 0 ;
780+ this . renderedRows = 0 ;
718781 // this.columnsCount = 0;
719782
720783 const spinner = document . createElement ( 'div' ) ;
@@ -989,11 +1052,11 @@ class PlexMeetsHomeAssistant extends HTMLElement {
9891052 // todo: figure out why interval is needed here and do it properly
9901053 const setLeftOffsetsInterval = setInterval ( ( ) => {
9911054 this . movieElems = this . getElementsByClassName ( 'movieElem' ) ;
1055+
9921056 for ( let i = 0 ; i < this . movieElems . length ; i += 1 ) {
9931057 if ( this . movieElems [ i ] . offsetLeft === 0 ) {
9941058 break ;
9951059 } else {
996- this . resizeHandler ( ) ;
9971060 clearInterval ( setLeftOffsetsInterval ) ;
9981061 }
9991062 }
0 commit comments