Skip to content
Snippets Groups Projects
parent.ts 3.28 KiB
Newer Older
  • Learn to ignore specific revisions
  • Midhun Suresh's avatar
    Midhun Suresh committed
    import "./parent-style.css";
    
    Midhun Suresh's avatar
    Midhun Suresh committed
    let isIframeLoaded = false;
    
    const parentHostRoot = (document.querySelector("#chatterbox-script") as HTMLScriptElement).src;
    
    const hostRoot = new URL(parentHostRoot).origin;
    
    Midhun Suresh's avatar
    Midhun Suresh committed
    
    const sizeCollection = {
        "desktop": {
    
    RMidhunSuresh's avatar
    RMidhunSuresh committed
            "account-setup": { height: "334px", width: "375px" },
            "timeline": {height: "595px", width: "375px"}
    
    Midhun Suresh's avatar
    Midhun Suresh committed
        },
        "mobile": {
            "account-setup": { height: "100vh", width: "100vw" },
            "timeline": {height: "100vh", width: "100vw"}
        }
    }
    
    window.addEventListener("message", event => {
        const { action } = event.data;
        switch (action) {
            case "resize-iframe":
    
                resizeIframe(event.data);
    
    Midhun Suresh's avatar
    Midhun Suresh committed
                break;
            case "minimize":
                minimizeIframe();
                break;
        }
    });
    
    
    function isMobile() {
        return window.innerWidth <= 800 && window.innerHeight <= 930;
    }
    
    
    Midhun Suresh's avatar
    Midhun Suresh committed
    function renderStartButton() {
    
    Midhun Suresh's avatar
    Midhun Suresh committed
        const container = document.createElement("div");
        container.className = "start";
        const button = document.createElement("button");
    
        button.className = "start-chat-btn";
    
    Midhun Suresh's avatar
    Midhun Suresh committed
        button.onclick = () => isIframeLoaded? minimizeIframe() : loadChatterboxIframe();
        container.appendChild(button);
        document.body.appendChild(container);
    }
    
    
    function loadCSS() {
        const linkElement = document.createElement("link") as HTMLLinkElement;
        linkElement.rel = "stylesheet";
    
        linkElement.href = new URL("CSS_FILE_NAME", parentHostRoot).href;
    
    Midhun Suresh's avatar
    Midhun Suresh committed
        document.head.appendChild(linkElement);
    
    Midhun Suresh's avatar
    Midhun Suresh committed
    function loadChatterboxIframe() {
        const iframe = document.createElement("iframe");
    
        const configLocation = (window as any).CHATTERBOX_CONFIG_LOCATION;
    
    Midhun Suresh's avatar
    Midhun Suresh committed
        if (!configLocation) {
    
            throw new Error("CHATTERBOX_CONFIG_LOCATION is not set");
    
    Midhun Suresh's avatar
    Midhun Suresh committed
        }
    
        iframe.src = new URL("../chatterbox.html?config=" + configLocation, hostRoot).href;
    
    Midhun Suresh's avatar
    Midhun Suresh committed
        iframe.className = "chatterbox-iframe";
        document.body.appendChild(iframe);
        isIframeLoaded = true;
    
        document.querySelector(".start-chat-btn").classList.add("start-background-minimized");
    
        if (isMobile()) {
    
            (document.querySelector(".start") as HTMLDivElement).style.display = "none";
    
    Midhun Suresh's avatar
    Midhun Suresh committed
    }
    
    function minimizeIframe() {
        const iframeElement = document.querySelector(".chatterbox-iframe") as HTMLIFrameElement;
    
        const startButtonDiv = document.querySelector(".start") as HTMLDivElement;
    
    Midhun Suresh's avatar
    Midhun Suresh committed
        if (iframeElement.style.display !== "none") {
            iframeElement.style.display = "none";
    
            document.querySelector(".start-chat-btn").classList.remove("start-background-minimized");
    
            if (isMobile()) {
    
                startButtonDiv.style.display = "block";
    
    Midhun Suresh's avatar
    Midhun Suresh committed
        }
        else {
            iframeElement.style.display = "block";
    
            document.querySelector(".start-chat-btn").classList.add("start-background-minimized");
    
            if (isMobile()) {
    
                startButtonDiv.style.display = "none";
    
    Midhun Suresh's avatar
    Midhun Suresh committed
        }
    
    Midhun Suresh's avatar
    Midhun Suresh committed
    
    
    function resizeIframe(data) {
        const { view } = data;
        const type = isMobile()? "mobile": "desktop";
        const size = sizeCollection[type][view];
        if (!size) { return; }
        const { height, width } = size;
        const iframeElement = document.querySelector(".chatterbox-iframe") as HTMLIFrameElement;
        if (height) { iframeElement.style.height = height; }
        if (width) { iframeElement.style.width = width; }
    
    Midhun Suresh's avatar
    Midhun Suresh committed
    }
    
    renderStartButton();