| 1 |
(function () { |
| 2 |
const MIN_WIDGET_WIDTH = 1024; |
| 3 |
const currentScript = document.currentScript; |
| 4 |
|
| 5 |
const config = { |
| 6 |
apiBaseUrl: { |
| 7 |
core: "https://gateway.hamsalam.ir/core", |
| 8 |
conversation: "https://gateway.hamsalam.ir/conversation", |
| 9 |
auth: "https://gateway.hamsalam.ir/auth", |
| 10 |
review: "https://gateway.hamsalam.ir/review", |
| 11 |
story: "https://gateway.hamsalam.ir/story", |
| 12 |
userActivity: "https://gateway.hamsalam.ir/user-activity", |
| 13 |
featureFlag: "https://gateway.hamsalam.ir/feature-flag", |
| 14 |
orderProcessing: "https://gateway.hamsalam.ir/order-processing", |
| 15 |
uploadio: "https://gateway.hamsalam.ir/uploadio", |
| 16 |
automation: "https://gateway.hamsalam.ir/automation", |
| 17 |
voucher: "https://gateway.hamsalam.ir/voucher", |
| 18 |
notification: "https://gateway.hamsalam.ir/notification", |
| 19 |
appsApi: "https://gateway.hamsalam.ir/apps-api", |
| 20 |
chat: "https://gateway.hamsalam.ir/chat", |
| 21 |
orderTest2: "https://gateway.hamsalam.ir/order-test2", |
| 22 |
}, |
| 23 |
forcedDeviceType: "mobile", |
| 24 |
buttonText: "Chat", |
| 25 |
buttonIcon: null, |
| 26 |
buttonSize: "60px", |
| 27 |
buttonColor: "#ff5c35", |
| 28 |
buttonTextColor: "#fff", |
| 29 |
clickOutsideClose: true, |
| 30 |
widgetStyle: { |
| 31 |
left : "20px", |
| 32 |
right: "auto", |
| 33 |
height: "630px", |
| 34 |
}, |
| 35 |
...window.dalanWidgetConfig, |
| 36 |
}; |
| 37 |
|
| 38 |
for (const attr of currentScript.attributes) { |
| 39 |
if (attr.name === "src") continue; |
| 40 |
config[attr.name] = attr.value; |
| 41 |
} |
| 42 |
|
| 43 |
window.dalanWidgetConfig = config; |
| 44 |
|
| 45 |
const scriptSrc = currentScript.src; |
| 46 |
const basePath = scriptSrc.substring(0, scriptSrc.lastIndexOf("/") + 1); |
| 47 |
|
| 48 |
if (!config.buttonIcon) { |
| 49 |
config.buttonIcon = basePath + "../icons/chat.svg"; |
| 50 |
} |
| 51 |
|
| 52 |
let assetsLoaded = false; |
| 53 |
let scriptLoaded = false; |
| 54 |
let widgetButton = null; |
| 55 |
|
| 56 |
function isDesktopViewport() { |
| 57 |
return window.innerWidth >= MIN_WIDGET_WIDTH; |
| 58 |
} |
| 59 |
|
| 60 |
function getMountFunction() { |
| 61 |
return ( |
| 62 |
window.mountChatWidget || |
| 63 |
(window.ChatWidget && window.ChatWidget.mountChatWidget) |
| 64 |
); |
| 65 |
} |
| 66 |
|
| 67 |
function getUnmountFunction() { |
| 68 |
return ( |
| 69 |
window.unmountChatWidget || |
| 70 |
(window.ChatWidget && window.ChatWidget.unmountChatWidget) |
| 71 |
); |
| 72 |
} |
| 73 |
|
| 74 |
function removeWidget() { |
| 75 |
const isOpen = !!document.querySelector("#dalan-chat-root"); |
| 76 |
const unmount = getUnmountFunction(); |
| 77 |
|
| 78 |
if (isOpen && unmount) { |
| 79 |
unmount(); |
| 80 |
} |
| 81 |
|
| 82 |
if (widgetButton) { |
| 83 |
widgetButton.remove(); |
| 84 |
widgetButton = null; |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
function createWidgetButton() { |
| 89 |
if (!isDesktopViewport() || widgetButton || !scriptLoaded) { |
| 90 |
return; |
| 91 |
} |
| 92 |
|
| 93 |
const button = document.createElement("button"); |
| 94 |
button.id = "dalan-chat-button"; |
| 95 |
|
| 96 |
if (config.buttonIcon) { |
| 97 |
const icon = document.createElement("img"); |
| 98 |
icon.src = config.buttonIcon; |
| 99 |
icon.alt = "Chat"; |
| 100 |
Object.assign(icon.style, { |
| 101 |
width: "30px", |
| 102 |
height: "30px", |
| 103 |
display: "block", |
| 104 |
}); |
| 105 |
button.appendChild(icon); |
| 106 |
} else { |
| 107 |
button.innerText = config.buttonText; |
| 108 |
} |
| 109 |
|
| 110 |
Object.assign(button.style, { |
| 111 |
position: "fixed", |
| 112 |
bottom: config.buttonBottom || "20px", |
| 113 |
left: config.buttonRight || "20px", |
| 114 |
borderRadius: "50%", |
| 115 |
width: config.buttonSize, |
| 116 |
height: config.buttonSize, |
| 117 |
fontSize: "24px", |
| 118 |
background: config.buttonColor, |
| 119 |
color: config.buttonTextColor, |
| 120 |
border: "none", |
| 121 |
cursor: "pointer", |
| 122 |
zIndex: 9999, |
| 123 |
display: "flex", |
| 124 |
alignItems: "center", |
| 125 |
justifyContent: "center", |
| 126 |
}); |
| 127 |
|
| 128 |
button.onclick = () => { |
| 129 |
const isOpen = !!document.querySelector("#dalan-chat-root"); |
| 130 |
const unmount = getUnmountFunction(); |
| 131 |
const mount = getMountFunction(); |
| 132 |
|
| 133 |
if (isOpen && unmount) { |
| 134 |
unmount(); |
| 135 |
} else if (mount) { |
| 136 |
mount(window.dalanWidgetConfig); |
| 137 |
} else { |
| 138 |
console.error("mountChatWidget function not found!"); |
| 139 |
} |
| 140 |
}; |
| 141 |
|
| 142 |
document.body.appendChild(button); |
| 143 |
widgetButton = button; |
| 144 |
} |
| 145 |
|
| 146 |
function initWidget() { |
| 147 |
if (assetsLoaded) { |
| 148 |
createWidgetButton(); |
| 149 |
return; |
| 150 |
} |
| 151 |
|
| 152 |
assetsLoaded = true; |
| 153 |
|
| 154 |
const cssLink = document.createElement("link"); |
| 155 |
cssLink.rel = "stylesheet"; |
| 156 |
cssLink.href = basePath + "dalan.css"; |
| 157 |
document.head.appendChild(cssLink); |
| 158 |
|
| 159 |
const script = document.createElement("script"); |
| 160 |
script.src = basePath + "chat-app-bundle.js"; |
| 161 |
|
| 162 |
script.onerror = (error) => { |
| 163 |
console.error("Failed to load chat widget bundle:", error); |
| 164 |
console.error("Script source:", script.src); |
| 165 |
}; |
| 166 |
|
| 167 |
script.onload = () => { |
| 168 |
scriptLoaded = true; |
| 169 |
createWidgetButton(); |
| 170 |
}; |
| 171 |
|
| 172 |
document.body.appendChild(script); |
| 173 |
} |
| 174 |
|
| 175 |
function updateWidgetVisibility() { |
| 176 |
if (!isDesktopViewport()) { |
| 177 |
removeWidget(); |
| 178 |
return; |
| 179 |
} |
| 180 |
|
| 181 |
initWidget(); |
| 182 |
} |
| 183 |
|
| 184 |
if (document.readyState === "loading") { |
| 185 |
document.addEventListener("DOMContentLoaded", updateWidgetVisibility); |
| 186 |
} else { |
| 187 |
updateWidgetVisibility(); |
| 188 |
} |
| 189 |
|
| 190 |
if (window.matchMedia) { |
| 191 |
const desktopMediaQuery = window.matchMedia( |
| 192 |
`(min-width: ${MIN_WIDGET_WIDTH}px)` |
| 193 |
); |
| 194 |
|
| 195 |
if (desktopMediaQuery.addEventListener) { |
| 196 |
desktopMediaQuery.addEventListener("change", updateWidgetVisibility); |
| 197 |
} else if (desktopMediaQuery.addListener) { |
| 198 |
desktopMediaQuery.addListener(updateWidgetVisibility); |
| 199 |
} |
| 200 |
} else { |
| 201 |
window.addEventListener("resize", updateWidgetVisibility); |
| 202 |
} |
| 203 |
})(); |
| 204 |
|