From 1281118d8f332c8ae04ba84a0db2400dc9aebb28 Mon Sep 17 00:00:00 2001 From: Tomasz Dziezyk Date: Mon, 28 Oct 2024 13:36:53 +0100 Subject: [PATCH] KAW-8201 Add lazy loading for map --- blocks/dealer-locator/dealer-locator.js | 85 ++++++++++++++----------- 1 file changed, 49 insertions(+), 36 deletions(-) diff --git a/blocks/dealer-locator/dealer-locator.js b/blocks/dealer-locator/dealer-locator.js index b34a736..f04b6e0 100644 --- a/blocks/dealer-locator/dealer-locator.js +++ b/blocks/dealer-locator/dealer-locator.js @@ -118,44 +118,57 @@ export default async function decorate(block) { }, }; - loadScript('/blocks/dealer-locator/vendor/jquery.min.js', { type: 'text/javascript', charset: 'UTF-8' }) - .then(() => { - // these scripts depend on jquery: - loadScript('//webapp.woosmap.com/webapp.js', { type: 'text/javascript' }).then(() => { - function loadWebApp() { - const publicKey = 'woos-50c04d89-af71-3a3b-b0b9-0786ee130532'; - // eslint-disable-next-line no-undef - const webapp = new window.WebApp('dealer-locator', publicKey); - const config = { - maps: { - provider: 'woosmap', - channel: '', - localities: { - language, - data: 'advanced', + const loadDealerLocator = () => { + loadScript('/blocks/dealer-locator/vendor/jquery.min.js', { type: 'text/javascript', charset: 'UTF-8' }) + .then(() => { + // these scripts depend on jquery: + loadScript('//webapp.woosmap.com/webapp.js', { type: 'text/javascript' }).then(() => { + function loadWebApp() { + const publicKey = 'woos-50c04d89-af71-3a3b-b0b9-0786ee130532'; + // eslint-disable-next-line no-undef + const webapp = new window.WebApp('dealer-locator', publicKey); + const config = { + maps: { + provider: 'woosmap', + channel: '', + localities: { + language, + data: 'advanced', + }, }, - }, - theme: { - primaryColor: '#ed1d24', - }, - datasource: { - maxResponses: 5, - maxDistance: 1000000, - useDistanceMatrix: true, - distanceMatrixProvider: 'woosmap', - }, - internationalization: { - lang: language, - }, - woosmapview: isOneLocationVariant ? selectedLocationConfig : defaultLocationConfig, - }; - webapp.setConf(config); - if (isOneLocationVariant) { - webapp.setInitialStateToSelectedStore('990002'); + theme: { + primaryColor: '#ed1d24', + }, + datasource: { + maxResponses: 5, + maxDistance: 1000000, + useDistanceMatrix: true, + distanceMatrixProvider: 'woosmap', + }, + internationalization: { + lang: language, + }, + woosmapview: isOneLocationVariant ? selectedLocationConfig : defaultLocationConfig, + }; + webapp.setConf(config); + if (isOneLocationVariant) { + webapp.setInitialStateToSelectedStore('990002'); + } + webapp.render(); } - webapp.render(); - } - loadWebApp(); + loadWebApp(); + }); }); + }; + + const observer = new IntersectionObserver((entries) => { + entries.forEach((entry) => { + if (entry.intersectionRatio > 0) { + loadDealerLocator(); + observer.disconnect(); + } }); + }, { threshold: [0.1], rootMargin: '500px' }); + + observer.observe(block); }