-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
56791e8
commit d861d41
Showing
6 changed files
with
224 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import React, { useState, useEffect, useRef } from "react"; | ||
|
||
const SvEmbedIframe = (params) => { | ||
const iframeRef = useRef(null); | ||
const [iframeSrc, setIframeSrc] = useState(`/svEmbed`); | ||
|
||
|
||
// Function to send updated params via postMessage to the iframe | ||
const sendMessageToIframe = () => { | ||
let passableParams = { | ||
nm: params.nm, | ||
npz: params.npz, | ||
showRoadLabels: params.showRoadLabels , | ||
lat: params.lat || null, | ||
long: params.long || null, | ||
showAnswer: params.showAnswer || false, | ||
hidden: false, onLoad: undefined }; | ||
if (iframeRef.current) { | ||
iframeRef.current.contentWindow.postMessage({ type: "updateProps", props: passableParams }, "*"); | ||
} | ||
}; | ||
|
||
useEffect(() => { | ||
// reload iframe when lat or long changes | ||
console.log("lat or long changed", params.lat, params.long); | ||
setIframeSrc(`/svEmbed?nm=${params.nm}&npz=${params.npz}&showRoadLabels=${params.showRoadLabels}&lat=${params.lat}&long=${params.long}&showAnswer=${params.showAnswer}&hidden=false`); | ||
}, [params?.lat, params?.long]); | ||
|
||
useEffect(() => { | ||
sendMessageToIframe(); | ||
|
||
}, [JSON.stringify(params)]); | ||
|
||
useEffect(() => { | ||
// listen to events from iframe (onLoad) call params.onLoad | ||
const handleMessage = (event) => { | ||
console.log("Received message from iframe", event.data); | ||
if (event.data && typeof event.data === "object" && event.data.type === "onLoad") { | ||
params.onLoad(); | ||
} | ||
}; | ||
|
||
if (typeof window !== "undefined") { | ||
window.onmessage = handleMessage; | ||
} | ||
return () => { | ||
window.onmessage = null; | ||
} | ||
}, []); | ||
|
||
// Watch for window.reloadLoc to reload the iframe | ||
useEffect(() => { | ||
window.reloadLoc = () => { | ||
if (iframeRef.current) { | ||
// Force reload by changing iframe src | ||
iframeRef.current.src = iframeRef.current.src; | ||
} | ||
} | ||
return () => { | ||
window.reloadLoc = null; | ||
} | ||
|
||
}, [JSON.stringify(params)]); | ||
|
||
|
||
|
||
if(!params.lat && !params.long) { | ||
return null; | ||
} | ||
|
||
return ( | ||
<div> | ||
<iframe | ||
ref={iframeRef} | ||
src={iframeSrc} // Dynamically update the iframe src | ||
width="100%" | ||
height="100%" | ||
className={`svframe ${params?.hidden ? "svhidden" : ""}`} | ||
style={{ border: "none", position: "fixed", top: 0, left: 0}} | ||
title="Street View Embed" | ||
frameBorder="0" | ||
onLoad={() => sendMessageToIframe()} | ||
></iframe> | ||
</div> | ||
); | ||
}; | ||
|
||
export default SvEmbedIframe; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import React, { useState, useEffect } from "react"; | ||
import StreetView from "../components/streetview/streetView"; | ||
import Head from "next/head"; | ||
|
||
const SvEmbed = () => { | ||
const [props, setProps] = useState({ | ||
nm: false, | ||
npz: false, | ||
showRoadLabels: true, | ||
lat: null, | ||
long: null, | ||
showAnswer: false, | ||
hidden: false, | ||
}); | ||
|
||
useEffect(() => { | ||
if (typeof window !== "undefined") { | ||
const searchParams = new URLSearchParams(window.location.search); | ||
setProps({ | ||
nm: searchParams.get("nm") === "true", | ||
npz: searchParams.get("npz") === "true", | ||
showRoadLabels: searchParams.get("showRoadLabels") !== "false", | ||
lat: parseFloat(searchParams.get("lat")), | ||
long: parseFloat(searchParams.get("long")), | ||
showAnswer: searchParams.get("showAnswer") === "true", | ||
hidden: searchParams.get("hidden") === "true", | ||
}); | ||
} | ||
}, []); | ||
|
||
// PostMessage listener to update props dynamically | ||
useEffect(() => { | ||
const handleMessage = (event) => { | ||
if (event.data && typeof event.data === "object" && event.data.type === "updateProps") { | ||
console.log("Received message from parent", event.data); | ||
const newProps = { ...props, ...event.data.props }; | ||
setProps(newProps); | ||
} | ||
}; | ||
|
||
if (typeof window !== "undefined") { | ||
window.addEventListener("message", handleMessage); | ||
} | ||
|
||
return () => { | ||
if (typeof window !== "undefined") { | ||
window.removeEventListener("message", handleMessage); | ||
} | ||
}; | ||
}, [props]); | ||
|
||
return ( | ||
<> | ||
<Head> | ||
<script | ||
src="https://maps.googleapis.com/maps/api/js?v=weekly" | ||
defer | ||
></script> | ||
</Head> | ||
<StreetView | ||
nm={props.nm} | ||
npz={props.npz} | ||
showRoadLabels={props.showRoadLabels} | ||
lat={props.lat} | ||
long={props.long} | ||
showAnswer={props.showAnswer} | ||
hidden={props.hidden} | ||
onLoad={() => { | ||
console.log("StreetView Loaded"); | ||
// send to parent window that the iframe has loaded | ||
if (typeof window !== "undefined") { | ||
window.parent.postMessage({ type: "onLoad" }, "*"); | ||
} | ||
}} | ||
/> | ||
</> | ||
); | ||
}; | ||
|
||
export default SvEmbed; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters