-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
header and footer web component (#378)
- Loading branch information
Showing
11 changed files
with
294 additions
and
271 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { defineCustomElement } from "../custom-elements"; | ||
import { endpointUrlWithParams } from "../helpers/urls"; | ||
|
||
class Footer extends HTMLElement { | ||
handleParamsUpdated = (e: CustomEvent) => { | ||
if (e.detail.params.language || e.detail.params.context) { | ||
fetch(endpointUrlWithParams("/footer")) | ||
.then((res) => res.text()) | ||
.then((footer) => (this.innerHTML = footer)); | ||
} | ||
}; | ||
|
||
connectedCallback() { | ||
window.addEventListener("paramsupdated", this.handleParamsUpdated); | ||
} | ||
|
||
disconnectedCallback() { | ||
window.addEventListener("paramsupdated", this.handleParamsUpdated); | ||
} | ||
} | ||
|
||
defineCustomElement("decorator-footer", Footer); |
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,62 @@ | ||
import { defineCustomElement } from "../custom-elements"; | ||
import { endpointUrlWithParams } from "../helpers/urls"; | ||
import { updateDecoratorParams } from "../params"; | ||
|
||
const msgSafetyCheck = (message: MessageEvent) => { | ||
const { origin, source } = message; | ||
return window.location.href.startsWith(origin) && source === window; | ||
}; | ||
|
||
class Header extends HTMLElement { | ||
handleMessage = (e: MessageEvent) => { | ||
if (!msgSafetyCheck(e)) { | ||
return; | ||
} else if ( | ||
e.data.source === "decoratorClient" && | ||
e.data.event === "ready" | ||
) { | ||
window.postMessage({ source: "decorator", event: "ready" }); | ||
} else if ( | ||
e.data.source === "decoratorClient" && | ||
e.data.event == "params" | ||
) { | ||
const payload = e.data.payload; | ||
|
||
[ | ||
"breadcrumbs", | ||
"availableLanguages", | ||
"utilsBackground", | ||
"language", | ||
"chatbotVisible", | ||
"context", | ||
].forEach((key) => { | ||
if (payload[key] !== undefined) { | ||
// TODO: validation | ||
updateDecoratorParams({ | ||
[key]: payload[key], | ||
}); | ||
} | ||
}); | ||
} | ||
}; | ||
|
||
handleParamsUpdated = (e: CustomEvent) => { | ||
if (e.detail.params.language) { | ||
fetch(endpointUrlWithParams("/header")) | ||
.then((res) => res.text()) | ||
.then((header) => (this.innerHTML = header)); | ||
} | ||
}; | ||
|
||
connectedCallback() { | ||
window.addEventListener("message", this.handleMessage); | ||
window.addEventListener("paramsupdated", this.handleParamsUpdated); | ||
} | ||
|
||
disconnectedCallback() { | ||
window.removeEventListener("message", this.handleMessage); | ||
window.addEventListener("paramsupdated", this.handleParamsUpdated); | ||
} | ||
} | ||
|
||
defineCustomElement("decorator-header", Header); |
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
Oops, something went wrong.