Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

web parameters support #1035

Merged
merged 7 commits into from
Sep 22, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ import { MESSAGE_IN_APP } from "../../Personalization/constants/schema";
import { TEXT_HTML } from "../../Personalization/constants/contentType";

export default (id, type, detail) => {
// TODO: add webParameters when available from the authoring UI in detail
const { html, mobileParameters } = detail;

// TODO: Remove it once we have webParameters
const webParameters = { info: "this is a placeholder" };

return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,73 +11,19 @@ governing permissions and limitations under the License.
*/

import { getNonce } from "../../dom-actions/dom";
import { parseAnchor, removeElementById } from "../utils";
import { createElement, parseAnchor, removeElementById } from "../utils";
import { TEXT_HTML } from "../../constants/contentType";
import { INTERACT } from "../../constants/eventType";
import { assign } from "../../../../utils";

const ELEMENT_TAG_CLASSNAME = "alloy-messaging-container";
const ELEMENT_TAG_ID = "alloy-messaging-container";

const OVERLAY_TAG_CLASSNAME = "alloy-overlay-container";
const OVERLAY_TAG_ID = "alloy-overlay-container";
const ALLOY_MESSAGING_CONTAINER_ID = "alloy-messaging-container";
const ALLOY_OVERLAY_CONTAINER_ID = "alloy-overlay-container";
const ALLOY_IFRAME_ID = "alloy-content-iframe";

const dismissMessage = () =>
[ELEMENT_TAG_ID, OVERLAY_TAG_ID].forEach(removeElementById);

// eslint-disable-next-line no-unused-vars
export const buildStyleFromParameters = (mobileParameters, webParameters) => {
const {
verticalAlign,
width,
horizontalAlign,
backdropColor,
height,
cornerRadius,
horizontalInset,
verticalInset,
uiTakeover = false
} = mobileParameters;

const style = {
width: width ? `${width}%` : "100%",
backgroundColor: backdropColor || "rgba(0, 0, 0, 0.5)",
borderRadius: cornerRadius ? `${cornerRadius}px` : "0px",
border: "none",
position: uiTakeover ? "fixed" : "relative",
overflow: "hidden"
};
if (horizontalAlign === "left") {
style.left = horizontalInset ? `${horizontalInset}%` : "0";
} else if (horizontalAlign === "right") {
style.right = horizontalInset ? `${horizontalInset}%` : "0";
} else if (horizontalAlign === "center") {
style.left = "50%";
style.transform = "translateX(-50%)";
}

if (verticalAlign === "top") {
style.top = verticalInset ? `${verticalInset}%` : "0";
} else if (verticalAlign === "bottom") {
style.position = "fixed";
style.bottom = verticalInset ? `${verticalInset}%` : "0";
} else if (verticalAlign === "center") {
style.top = "50%";
style.transform = `${
horizontalAlign === "center" ? `${style.transform} ` : ""
}translateY(-50%)`;
style.display = "flex";
style.alignItems = "center";
style.justifyContent = "center";
}

if (height) {
style.height = `${height}vh`;
} else {
style.height = "100%";
}
return style;
};
[ALLOY_MESSAGING_CONTAINER_ID, ALLOY_OVERLAY_CONTAINER_ID].forEach(
removeElementById
);

const setWindowLocationHref = link => {
window.location.assign(link);
Expand Down Expand Up @@ -132,13 +78,6 @@ export const createIframe = (htmlContent, clickHandler) => {
new Blob([htmlDocument.documentElement.outerHTML], { type: TEXT_HTML })
);
element.id = ALLOY_IFRAME_ID;

Object.assign(element.style, {
border: "none",
width: "100%",
height: "100%"
});

element.addEventListener("load", () => {
const { addEventListener } =
element.contentDocument || element.contentWindow.document;
Expand All @@ -148,60 +87,145 @@ export const createIframe = (htmlContent, clickHandler) => {
return element;
};

export const createContainerElement = settings => {
const { mobileParameters = {}, webParameters = {} } = settings;
const element = document.createElement("div");
element.id = ELEMENT_TAG_ID;
element.className = `${ELEMENT_TAG_CLASSNAME}`;
Object.assign(
element.style,
buildStyleFromParameters(mobileParameters, webParameters)
);
const displayBasedOnWebParams = (iframe, webParameters, container, overlay) => {
const { style: iframeStyle = {} } = webParameters[ALLOY_IFRAME_ID];
const {
style: messagingStyle = {},
params: messagingParams = {}
} = webParameters[ALLOY_MESSAGING_CONTAINER_ID];

return element;
const {
style: overlayStyle = {},
params: overlayParams = {}
} = webParameters[ALLOY_OVERLAY_CONTAINER_ID];

assign(iframe.style, iframeStyle);
assign(container.style, messagingStyle);

if (overlayParams.enabled) {
shandilya3 marked this conversation as resolved.
Show resolved Hide resolved
assign(overlay.style, overlayStyle);
document.body.appendChild(overlay);
document.body.style.overflow = "hidden";
}

const {
paramElement = "body",
insertionMethod = "appendChild"
} = messagingParams;

const element = document.querySelector(paramElement);
element[insertionMethod](container);
};

export const createOverlayElement = parameter => {
const element = document.createElement("div");
const backdropOpacity = parameter.backdropOpacity || 0.5;
const backdropColor = parameter.backdropColor || "#FFFFFF";
element.id = OVERLAY_TAG_ID;
element.className = `${OVERLAY_TAG_CLASSNAME}`;
export const buildStyleFromMobileParameters = mobileParameters => {
const {
verticalAlign,
width,
horizontalAlign,
backdropColor,
height,
cornerRadius,
horizontalInset,
verticalInset,
uiTakeover = false
} = mobileParameters;

const style = {
width: width ? `${width}%` : "100%",
backgroundColor: backdropColor || "rgba(0, 0, 0, 0.5)",
borderRadius: cornerRadius ? `${cornerRadius}px` : "0px",
border: "none",
position: uiTakeover ? "fixed" : "relative",
overflow: "hidden"
};
if (horizontalAlign === "left") {
style.left = horizontalInset ? `${horizontalInset}%` : "0";
} else if (horizontalAlign === "right") {
style.right = horizontalInset ? `${horizontalInset}%` : "0";
} else if (horizontalAlign === "center") {
style.left = "50%";
style.transform = "translateX(-50%)";
}

if (verticalAlign === "top") {
style.top = verticalInset ? `${verticalInset}%` : "0";
} else if (verticalAlign === "bottom") {
style.position = "fixed";
style.bottom = verticalInset ? `${verticalInset}%` : "0";
} else if (verticalAlign === "center") {
style.top = "50%";
style.transform = `${
horizontalAlign === "center" ? `${style.transform} ` : ""
}translateY(-50%)`;
style.display = "flex";
style.alignItems = "center";
style.justifyContent = "center";
}

if (height) {
style.height = `${height}vh`;
} else {
style.height = "100%";
}
return style;
};

Object.assign(element.style, {
export const mobileOverlay = (overlay, mobileParameters) => {
shandilya3 marked this conversation as resolved.
Show resolved Hide resolved
const { backdropOpacity, backdropColor } = mobileParameters;
const opacity = backdropOpacity || 0.5;
const color = backdropColor || "#FFFFFF";
assign(overlay.style, {
position: "fixed",
top: "0",
left: "0",
width: "100%",
height: "100%",
background: "transparent",
opacity: backdropOpacity,
backgroundColor: backdropColor
opacity,
backgroundColor: color
});
document.body.appendChild(overlay);
document.body.style.overflow = "hidden";
};

return element;
const displayBasedOnMobileParams = (
iframe,
container,
mobileParameters,
overlay
) => {
assign(iframe.style, {
border: "none",
width: "100%",
height: "100%"
});
assign(container.style, buildStyleFromMobileParameters(mobileParameters));
if (mobileParameters.uiTakeover) {
mobileOverlay(overlay, mobileParameters);
}
document.body.appendChild(container);
};

export const displayHTMLContentInIframe = (settings, interact) => {
dismissMessage();
const { content, contentType, mobileParameters } = settings;
const { content, contentType, mobileParameters, webParameters } = settings;

if (contentType !== TEXT_HTML) {
return;
}

const container = createContainerElement(settings);
const container = createElement(ALLOY_MESSAGING_CONTAINER_ID);

const iframe = createIframe(content, createIframeClickHandler(interact));

container.appendChild(iframe);
const overlay = createElement(ALLOY_OVERLAY_CONTAINER_ID);

if (mobileParameters.uiTakeover) {
const overlay = createOverlayElement(mobileParameters);
document.body.appendChild(overlay);
document.body.style.overflow = "hidden";
container.appendChild(iframe);
if (webParameters && webParameters.info !== "this is a placeholder") {
shandilya3 marked this conversation as resolved.
Show resolved Hide resolved
displayBasedOnWebParams(iframe, webParameters, container, overlay);
} else {
displayBasedOnMobileParams(iframe, container, mobileParameters, overlay);
}
document.body.appendChild(container);
};

export default (settings, collect) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,8 @@ export const parseAnchor = anchor => {
uuid
};
};
export const createElement = elementTagId => {
const element = document.createElement("div");
element.id = elementTagId;
return element;
};

This file was deleted.

Loading