File tree Expand file tree Collapse file tree 2 files changed +38
-1
lines changed
Expand file tree Collapse file tree 2 files changed +38
-1
lines changed Original file line number Diff line number Diff line change @@ -31,6 +31,7 @@ class MapmlifyLayer extends HTMLElement {
3131 #sourceCodeTextarea = null ;
3232 #sourceCodeVisible = false ;
3333 #moveendHandler = null ;
34+ #lazyLoadObserver = null ;
3435
3536 set layerConfig ( value ) {
3637 this . #config = value ;
@@ -47,11 +48,42 @@ class MapmlifyLayer extends HTMLElement {
4748 if ( this . #config) {
4849 this . #initDefaults( ) ;
4950 this . #render( ) ;
51+ this . #setupLazyLoading( ) ;
5052 }
5153 }
5254
5355 disconnectedCallback ( ) {
5456 this . #removeViewer( ) ;
57+ if ( this . #lazyLoadObserver) {
58+ this . #lazyLoadObserver. disconnect ( ) ;
59+ this . #lazyLoadObserver = null ;
60+ }
61+ }
62+
63+ #setupLazyLoading( ) {
64+ const options = {
65+ root : null ,
66+ rootMargin : '100px' ,
67+ threshold : 0.1 ,
68+ } ;
69+
70+ this . #lazyLoadObserver = new IntersectionObserver ( ( entries ) => {
71+ entries . forEach ( ( entry ) => {
72+ if (
73+ entry . isIntersecting &&
74+ this . #layerCheckbox &&
75+ ! this . #layerCheckbox. checked
76+ ) {
77+ this . #layerCheckbox. checked = true ;
78+ this . #layerCheckbox. dispatchEvent ( new Event ( 'change' ) ) ;
79+
80+ this . #lazyLoadObserver. disconnect ( ) ;
81+ this . #lazyLoadObserver = null ;
82+ }
83+ } ) ;
84+ } , options ) ;
85+
86+ this . #lazyLoadObserver. observe ( this ) ;
5587 }
5688
5789 #initDefaults( ) {
Original file line number Diff line number Diff line change @@ -90,7 +90,12 @@ export function getLayerElements(page) {
9090 */
9191export async function activateLayer ( page , index ) {
9292 const layer = page . locator ( 'mapmlify-layer' ) . nth ( index ) ;
93- await layer . locator ( '.layer-checkbox' ) . check ( ) ;
93+ const cb = layer . locator ( '.layer-checkbox' ) ;
94+ // The IntersectionObserver may have already checked this box
95+ const alreadyChecked = await cb . isChecked ( ) ;
96+ if ( ! alreadyChecked ) {
97+ await cb . check ( ) ;
98+ }
9499 // Wait for the mapml-viewer to appear inside this layer
95100 await layer . locator ( 'mapml-viewer' ) . waitFor ( { timeout : 15000 } ) ;
96101}
You can’t perform that action at this time.
0 commit comments