@eka-care/medassist-widget-embed 0.2.34 → 0.2.36
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +22 -8
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
"use strict";const getCurrentScript=()=>{if(typeof document=="undefined")return console.error("document is not defined"),null;const{currentScript:e}=document;if(e instanceof HTMLScriptElement)return e;const t=document.getElementsByTagName("script");return t.length?t[t.length-1]:null},getEnvironment=e=>{if(e==="production"||e==="development"||e==="staging")return e},scriptEl=getCurrentScript(),DEFAULT_WIDGET_ASSET_BASE=(()=>{var e;return!((e=scriptEl==null?void 0:scriptEl.src)===null||e===void 0)&&e.includes("@dev")?"https://unpkg.com/@eka-care/medassist-widget@dev/dist/":"https://unpkg.com/@eka-care/medassist-widget@latest/dist/"})();function mapAgentConfigThemeToWidgetTheme(e){if(!e)return;const t=e.mode==="dark"?"white":e.mode==="light"?"black":void 0;return{...e.background_img&&{backgroundImage:e.background_img},...e.accent&&{primary:e.accent},...t&&{textColor:t},...e.mode&&{mode:e.mode},...e.header_tinted!==void 0&&{headerTinted:e.header_tinted},...e.title_img&&{titleImg:e.title_img},...e.tagline&&{tagline:e.tagline}}}function preloadImage(e){if(!(!e||typeof document=="undefined"))try{const t=document.createElement("link");t.rel="preload",t.as="image",t.href=e,document.head.appendChild(t)}catch{const t=new Image;t.src=e}}async function fetchAgentConfig(e,t){const
|
|
1
|
+
"use strict";const getCurrentScript=()=>{if(typeof document=="undefined")return console.error("document is not defined"),null;const{currentScript:e}=document;if(e instanceof HTMLScriptElement)return e;const t=document.getElementsByTagName("script");return t.length?t[t.length-1]:null},getEnvironment=e=>{if(e==="production"||e==="development"||e==="staging")return e},scriptEl=getCurrentScript(),DEFAULT_WIDGET_ASSET_BASE=(()=>{var e;return!((e=scriptEl==null?void 0:scriptEl.src)===null||e===void 0)&&e.includes("@dev")?"https://unpkg.com/@eka-care/medassist-widget@dev/dist/":"https://unpkg.com/@eka-care/medassist-widget@latest/dist/"})();function mapAgentConfigThemeToWidgetTheme(e){if(!e)return;const t=e.mode==="dark"?"white":e.mode==="light"?"black":void 0;return{...e.background_img&&{backgroundImage:e.background_img},...e.accent&&{primary:e.accent},...t&&{textColor:t},...e.mode&&{mode:e.mode},...e.header_tinted!==void 0&&{headerTinted:e.header_tinted},...e.title_img&&{titleImg:e.title_img},...e.tagline&&{tagline:e.tagline}}}function preloadImage(e){if(!(!e||typeof document=="undefined"))try{const t=document.createElement("link");t.rel="preload",t.as="image",t.href=e,document.head.appendChild(t)}catch{const t=new Image;t.src=e}}async function fetchAgentConfig(e,t){const n=`${e.replace(/\/$/,"")}/med-assist/agent-config/${t}`,o=await fetch(n,{headers:{"ngrok-skip-browser-warning":"69420"}});if(!o.ok)return;const i=await o.json();if(i!=null&&i.success&&(i!=null&&i.data))return i.data}const NUDGE_STORAGE_KEY="eka-medassist-nudges";function getNudgeStore(){try{const e=localStorage.getItem(NUDGE_STORAGE_KEY);return e?JSON.parse(e):{}}catch{return{}}}function setNudgeStore(e){try{localStorage.setItem(NUDGE_STORAGE_KEY,JSON.stringify(e))}catch{}}function normalizePath(e){let t=e.replace(/\/+$/,"");return t.startsWith("/")||(t="/"+t),t||"/"}function findCachedNudge(e,t){const o=getNudgeStore()[e];if(!o)return null;let i;try{i=new URL(t)}catch{return null}const s=i.host,d=o[s];if(!d)return null;const r=i.pathname.split("/").filter(Boolean),c=[];for(let a=r.length;a>=0;a--)c.push(a===0?"/":"/"+r.slice(0,a).join("/"));for(const a of c){if(d[a]!==void 0)return{domain:s,path:a,data:d[a]};const u=a==="/"?"/*":a+"/*";if(d[u]!==void 0)return{domain:s,path:u,data:d[u]}}return null}function clearNudgeForPath(e,t,n){const o=getNudgeStore(),i=o[e];!i||!i[t]||(delete i[t][n],setNudgeStore(o))}function storeNudgeResponse(e,t){var n,o,i,s;const d=(n=t==null?void 0:t.url_pattern)===null||n===void 0?void 0:n.domain,r=(o=t==null?void 0:t.url_pattern)===null||o===void 0?void 0:o.path;if(!(!((i=t.nudges)===null||i===void 0)&&i.length)||!t.expiry||!d||!r)return;const c=getNudgeStore();c[e]||(c[e]={}),c[e][d]||(c[e][d]={}),c[e][d][r]={nudges:t.nudges,expiry:t.expiry,delay:(s=t.delay)!==null&&s!==void 0?s:5},setNudgeStore(c)}const NUDGE_COOKIE_PREFIX="eka-nudge";function setNudgeCookie(e,t){const n=new Date(Date.now()+864e5).toUTCString();document.cookie=`${NUDGE_COOKIE_PREFIX}-${e}=${t}; expires=${n}; path=/; SameSite=Lax`}function getNudgeCookie(e){const t=document.cookie.split(";").map(o=>o.trim()).find(o=>o.startsWith(`${NUDGE_COOKIE_PREFIX}-${e}=`));if(!t)return null;const n=t.split("=")[1];return n==="open"||n==="closed"?n:null}async function fetchNudgeData(e,t){const n=`${t}/med-assist/user-nudge`,o=[];typeof document!="undefined"&&document.querySelectorAll("meta").forEach(i=>{var s,d,r;const c=(s=i.getAttribute("name"))!==null&&s!==void 0?s:void 0,a=(d=i.getAttribute("property"))!==null&&d!==void 0?d:void 0,u=(r=i.getAttribute("content"))!==null&&r!==void 0?r:void 0;(c||a)&&o.push({name:c,property:a,content:u})});try{const i=await fetch(n,{method:"POST",headers:{"Content-Type":"application/json","x-agent-id":e,referer:typeof window!="undefined"?window.location.href:""},body:JSON.stringify({meta_tags:o,url:typeof window!="undefined"?window.location.href:""})});return i.ok?await i.json():void 0}catch{return}}const globalMedAssistConfig={},getWidgetElement=()=>typeof document=="undefined"?null:document.querySelector("eka-medassist-widget");if(typeof window!="undefined"){const e=window;e.__ekaMedAssistConfig__=globalMedAssistConfig,e.EkaMedAssist={init(t){var n,o,i;Object.assign(globalMedAssistConfig,t);const s=getWidgetElement();if(s){if(t.agentId&&s.setAttribute("agent-id",t.agentId),t.title&&s.setAttribute("title",t.title),t.iconUrl&&s.setAttribute("icon-url",t.iconUrl),t.baseUrl&&s.setAttribute("base-url",t.baseUrl),t.resize!==void 0&&s.setAttribute("resize",String(t.resize)),(t.displayMode==="full"||t.displayMode==="widget")&&s.setAttribute("display-mode",t.displayMode),t.context)try{s.setAttribute("context",JSON.stringify(t.context))}catch{console.warn("Failed to stringify context passed to init")}!((n=t.theme)===null||n===void 0)&&n.backgroundImage&&preloadImage(t.theme.backgroundImage),(i=(o=s).openFromBridge)===null||i===void 0||i.call(o)}}}}const scriptBaseUrl=(()=>{if(!scriptEl)return"";try{return new URL(".",scriptEl.src||window.location.href).href}catch{return""}})(),widgetAssetBaseUrl=(()=>{if(!scriptEl)return DEFAULT_WIDGET_ASSET_BASE;const e=scriptEl.dataset.widgetAssets;if(e==="local")return`${scriptBaseUrl}src/`;if(e&&e.length>0)try{return new URL(e,scriptBaseUrl).href}catch{return e.endsWith("/")?e:`${e}/`}return DEFAULT_WIDGET_ASSET_BASE})(),WIDGET_JS_URL=`${widgetAssetBaseUrl}medassist-widget.js`,WIDGET_CSS_URL=`${widgetAssetBaseUrl}medassist-widget.css`;let widgetScriptPromise=null,widgetCssTextPromise=null;function getWidgetCssTextPromise(){return widgetCssTextPromise||(widgetCssTextPromise=fetch(WIDGET_CSS_URL).then(e=>{if(!e.ok)throw new Error(`Unable to fetch widget styles from ${WIDGET_CSS_URL}`);return e.text()})),widgetCssTextPromise}class MedAssistWidgetLoader extends HTMLElement{getAgentConfig(){if(!this.agentConfigPromise){const t=(typeof window!="undefined"?window.__ekaMedAssistConfig__:void 0)||{},n=t.baseUrl||this.getAttribute("base-url")||"https://matrix.eka.care/reloaded",o=t.agentId||this.getAttribute("agent-id")||"";if(!o)return Promise.resolve(void 0);this.agentConfigPromise=fetchAgentConfig(n,o).catch(()=>{})}return this.agentConfigPromise}constructor(){super(),this.reactRoot=null,this.agentConfigPromise=null,this.attachShadow({mode:"open"}),this.defaultIconUrl="https://cdn.eka.care/bot-icon.svg",this.widgetLoaded=!1,this.displayMode=this.getAttribute("display-mode")==="full"?"full":"widget",this.setupAuthExpirationListener()}static get observedAttributes(){return["icon-url","display-mode"]}connectedCallback(){this.displayMode==="full"?(this.initializeFullMode(),this.loadWidgetCss().then(()=>this.loadWidgetScript()).then(()=>this.loadAndRender()).catch(t=>{console.error("Failed to load MedAssist widget in full mode",t)})):(this.renderButton(),this.preloadWidgetAssets(),this.initNudge())}attributeChangedCallback(t){if(t==="icon-url"&&this.displayMode==="widget"&&this.renderButton(),t==="display-mode"){const n=this.getAttribute("display-mode")==="full"?"full":"widget";n!==this.displayMode&&(this.displayMode=n,n==="full"?this.initializeFullMode():this.renderButton())}}preloadWidgetAssets(){getWidgetCssTextPromise(),this.loadWidgetScript(),this.preloadBackgroundImage()}preloadBackgroundImage(){var t;const n=typeof window!="undefined"?window.__ekaMedAssistConfig__:void 0,o=this.getAttribute("theme")?(()=>{try{return JSON.parse(this.getAttribute("theme")||"{}")}catch{return}})():void 0,i=(t=n==null?void 0:n.theme)===null||t===void 0?void 0:t.backgroundImage,s=o==null?void 0:o.backgroundImage;i&&preloadImage(i),s&&s!==i&&preloadImage(s),this.getAgentConfig().then(d=>{var r;const c=(r=d==null?void 0:d.theme)===null||r===void 0?void 0:r.background_img;c&&preloadImage(c)}).catch(()=>{})}setupAuthExpirationListener(){if(typeof window=="undefined")return;const t=n=>{var o;((o=n.data)===null||o===void 0?void 0:o.type)==="AUTH_EXPIRED"&&window.parent!==window&&window.parent.postMessage({type:"AUTH_EXPIRED",message:"Authentication expired"},"*")};window.addEventListener("message",t)}openFromBridge(){this.displayMode=this.getAttribute("display-mode")==="full"?"full":"widget",this.displayMode==="full"?this.initializeFullMode():this.renderButton(),this.loadWidgetCss().then(()=>this.loadWidgetScript()).then(()=>this.loadAndRender()).catch(t=>{console.error("Failed to open MedAssist widget from bridge",t)})}async initNudge(){var t,n,o,i;const s=typeof window!="undefined"?window.__ekaMedAssistConfig__:void 0,d=(s==null?void 0:s.agentId)||this.getAttribute("agent-id")||"";if(!d)return;const r=await this.getAgentConfig();if(!(r!=null&&r.nudge))return;const c=(n=(t=r==null?void 0:r.theme)===null||t===void 0?void 0:t.nudge_color)!==null&&n!==void 0?n:"#ffffff",a=typeof window!="undefined"?window.location.href:"";let u=findCachedNudge(d,a);if(u){const f=Math.floor(Date.now()/1e3);u.data.expiry<=f&&(clearNudgeForPath(d,u.domain,u.path),u=null)}if(!u){const f=typeof window!="undefined"?window.__ekaMedAssistConfig__:void 0,y=(f==null?void 0:f.baseUrl)||this.getAttribute("base-url")||"https://matrix.eka.care/reloaded",p=await fetchNudgeData(d,y).catch(()=>{});if(!(!((o=p==null?void 0:p.nudges)===null||o===void 0)&&o.length)||(storeNudgeResponse(d,p),u=findCachedNudge(d,a),!u))return}const m=u.data.nudges[Math.floor(Math.random()*u.data.nudges.length)],h=(i=u.data.delay)!==null&&i!==void 0?i:5;setTimeout(()=>{this.showNudgePopup(m,c)},h*1e3)}showNudgePopup(t,n){var o,i;const s=this.shadowRoot;if(!s||!(!((o=s.getElementById("medassist-widget-root"))===null||o===void 0)&&o.classList.contains("hidden")))return;const r=((typeof window!="undefined"?window.__ekaMedAssistConfig__:void 0)||{}).agentId||this.getAttribute("agent-id")||"";if(r&&getNudgeCookie(r)!==null||s.getElementById("medassist-nudge-popup"))return;const a=document.createElement("div");a.id="medassist-nudge-popup",a.innerHTML=`
|
|
2
2
|
<style>
|
|
3
3
|
@keyframes nudge-slide-up {
|
|
4
4
|
from { opacity: 0; transform: translateY(12px) scale(0.97); }
|
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
bottom: 76px;
|
|
10
10
|
right: 0;
|
|
11
11
|
width: 272px;
|
|
12
|
-
background: ${
|
|
12
|
+
background: ${n};
|
|
13
13
|
border-radius: 16px;
|
|
14
14
|
box-shadow: 0 8px 32px rgba(0,0,0,0.14), 0 2px 8px rgba(0,0,0,0.08);
|
|
15
15
|
padding: 16px 16px 14px 16px;
|
|
@@ -29,7 +29,7 @@
|
|
|
29
29
|
right: 20px;
|
|
30
30
|
width: 16px;
|
|
31
31
|
height: 10px;
|
|
32
|
-
background: ${
|
|
32
|
+
background: ${n};
|
|
33
33
|
clip-path: polygon(0 0, 100% 0, 50% 100%);
|
|
34
34
|
}
|
|
35
35
|
#medassist-nudge-popup {
|
|
@@ -79,7 +79,7 @@
|
|
|
79
79
|
<p class="nudge-text">${t.replace(/</g,"<").replace(/>/g,">")}</p>
|
|
80
80
|
<button class="nudge-close" aria-label="Dismiss">✕</button>
|
|
81
81
|
</div>
|
|
82
|
-
`,(
|
|
82
|
+
`,(i=a.querySelector(".nudge-close"))===null||i===void 0||i.addEventListener("click",u=>{u.stopPropagation(),r&&setNudgeCookie(r,"closed"),this.dismissNudgePopup()}),a.addEventListener("click",()=>{r&&setNudgeCookie(r,"closed"),this.dismissNudgePopup(),this.loadAndRender()}),s.appendChild(a)}dismissNudgePopup(){var t;const n=(t=this.shadowRoot)===null||t===void 0?void 0:t.getElementById("medassist-nudge-popup");n==null||n.remove()}renderButton(){var t;const n=this.getAttribute("icon-url")||this.defaultIconUrl,o=n==null?void 0:n.toLowerCase().endsWith(".svg"),i=this.shadowRoot;if(!i){console.error("Shadow root was not created");return}if(!i.querySelector("button"))i.innerHTML=`
|
|
83
83
|
<style>
|
|
84
84
|
#medassist-open-btn {
|
|
85
85
|
width: 60px;
|
|
@@ -97,8 +97,22 @@
|
|
|
97
97
|
#medassist-open-btn img {
|
|
98
98
|
width: 60px;
|
|
99
99
|
height: 60px;
|
|
100
|
-
border-radius: 50%;
|
|
101
100
|
object-fit: cover;
|
|
101
|
+
/* Use clip-path (vector clip) instead of border-radius
|
|
102
|
+
(which iOS implements via a raster mask and blurs SVGs). */
|
|
103
|
+
-webkit-clip-path: circle(50%);
|
|
104
|
+
clip-path: circle(50%);
|
|
105
|
+
}
|
|
106
|
+
/* iOS picks a raster buffer for SVG <img> based on box size, so we
|
|
107
|
+
render the image at 2\xD7 and scale it down. The translate3d also
|
|
108
|
+
promotes the element to its own compositing layer. */
|
|
109
|
+
#medassist-open-btn img.is-svg {
|
|
110
|
+
width: 120px;
|
|
111
|
+
height: 120px;
|
|
112
|
+
-webkit-transform: translate3d(0, 0, 0) scale(0.5);
|
|
113
|
+
transform: translate3d(0, 0, 0) scale(0.5);
|
|
114
|
+
transform-origin: center center;
|
|
115
|
+
shape-rendering: geometricPrecision;
|
|
102
116
|
}
|
|
103
117
|
:host {
|
|
104
118
|
position: fixed;
|
|
@@ -118,10 +132,10 @@
|
|
|
118
132
|
</style>
|
|
119
133
|
|
|
120
134
|
<button id="medassist-open-btn">
|
|
121
|
-
<img src="${
|
|
135
|
+
<img src="${n}" alt="MedAssist Icon"${o?' class="is-svg"':""}>
|
|
122
136
|
</button>
|
|
123
137
|
<div id="medassist-widget-root" class="hidden"></div>
|
|
124
|
-
`,(t=i.getElementById("medassist-open-btn"))===null||t===void 0||t.addEventListener("click",()=>{const d=((typeof window!="undefined"?window.__ekaMedAssistConfig__:void 0)||{}).agentId||this.getAttribute("agent-id")||"";d&&setNudgeCookie(d,"closed"),this.dismissNudgePopup(),this.loadAndRender()});else{const
|
|
138
|
+
`,(t=i.getElementById("medassist-open-btn"))===null||t===void 0||t.addEventListener("click",()=>{const d=((typeof window!="undefined"?window.__ekaMedAssistConfig__:void 0)||{}).agentId||this.getAttribute("agent-id")||"";d&&setNudgeCookie(d,"closed"),this.dismissNudgePopup(),this.loadAndRender()});else{const s=i.querySelector("#medassist-open-btn img");s&&(s.src=n,s.classList.toggle("is-svg",!!o))}}initializeFullMode(){const t=this.shadowRoot;if(!t){console.error("Shadow root is not available");return}t.getElementById("medassist-widget-root")||(t.innerHTML=`
|
|
125
139
|
<style>
|
|
126
140
|
:host {
|
|
127
141
|
display: block;
|
|
@@ -138,4 +152,4 @@
|
|
|
138
152
|
}
|
|
139
153
|
</style>
|
|
140
154
|
<div id="medassist-widget-root"></div>
|
|
141
|
-
`)}async loadAndRender(){var t,o
|
|
155
|
+
`)}async loadAndRender(){var t,n,o;const i=this.shadowRoot;if(!i){console.error("Shadow root is not available");return}this.style.display="";const s=i.getElementById("medassist-open-btn"),d=i.getElementById("medassist-widget-root"),r=this.getAttribute("agent-id");let c=this.getAttribute("icon-url")||this.defaultIconUrl;const a=(t=getEnvironment(this.getAttribute("environment")))!==null&&t!==void 0?t:"production",u=this.getAttribute("connectivity"),m=u==="sse"||u==="socket"?u:void 0;let h=this.getAttribute("title")||"Medi Clinic";const f=this.getAttribute("base-url")||"https://matrix.eka.care/reloaded",p=this.getAttribute("display-mode")==="full"?"full":"widget",E=this.getAttribute("context")?JSON.parse(this.getAttribute("context")||"{}"):void 0,S=this.getAttribute("theme")?(()=>{try{return JSON.parse(this.getAttribute("theme")||"{}")}catch{return}})():void 0,g=(typeof window!="undefined"?window.__ekaMedAssistConfig__:void 0)||{},M={...E||{},...g.context||{}},I=g.auth||void 0,w=g.agentId||r,U=g.baseUrl||f,b=typeof window!="undefined"?(n=window.EkaMedAssist)===null||n===void 0?void 0:n.onClose:void 0;if(!w){console.error("Agent ID is required");return}if(!d){console.error("Widget root element is missing");return}s&&s.classList.add("hidden"),d.classList.remove("hidden");let x,A,_,k=!1;try{const l=await this.getAgentConfig();h=(l==null?void 0:l.name)||h,c=((o=l==null?void 0:l.theme)===null||o===void 0?void 0:o.icon_img)||c,x=mapAgentConfigThemeToWidgetTheme((l==null?void 0:l.theme)||void 0),A=l==null?void 0:l.allowed,_=l==null?void 0:l.connectivity,k=(l==null?void 0:l.resize)||(g==null?void 0:g.resize)||!1}catch{}const L=_||m||"socket",C={title:g.title||h,iconUrl:c||g.iconUrl,allowed:A,environment:a,connectionType:L,onClose:()=>{var l;(l=this.reactRoot)===null||l===void 0||l.unmount(),this.reactRoot=null,b&&typeof b=="function"&&b(),p==="full"?this.style.display="none":(s&&s.classList.remove("hidden"),d.classList.add("hidden"))},baseUrl:U,context:M,displayMode:p,resize:k||this.getAttribute("resize")==="true",auth:I,theme:{...x||{},...S||{},...g.theme||{}}},v=window;if(this.widgetLoaded){this.reactRoot=v.renderMedAssist(d,w,C);return}try{if(await Promise.all([this.loadWidgetCss(),this.loadWidgetScript()]),typeof v.renderMedAssist!="function")throw new Error("renderMedAssist is not available on window");this.reactRoot=v.renderMedAssist(d,w,C),this.widgetLoaded=!0}catch(l){console.error("Failed to load MedAssist widget",l),s&&s.classList.remove("hidden"),d.classList.add("hidden")}}async loadWidgetCss(){const t=this.shadowRoot;if(!t)throw new Error("Shadow root is not available");if(t.querySelector("[data-medassist-style='true']"))return;const n=await getWidgetCssTextPromise(),o=document.createElement("style");o.setAttribute("data-medassist-style","true"),o.textContent=n,t.appendChild(o)}loadWidgetScript(){return widgetScriptPromise||(widgetScriptPromise=new Promise((t,n)=>{const o=document.querySelector(`script[src="${WIDGET_JS_URL}"]`);if(o){if(o.dataset.loaded==="true"){t();return}o.addEventListener("load",()=>t()),o.addEventListener("error",s=>n(s.error||new Error("Unknown script loading error")));return}const i=document.createElement("script");i.src=WIDGET_JS_URL,i.async=!0,i.dataset.widget="medassist",i.onload=()=>{i.dataset.loaded="true",t()},i.onerror=s=>{if(s instanceof ErrorEvent&&s.error){n(s.error);return}n(new Error(`Failed to load ${WIDGET_JS_URL}`))},document.head.appendChild(i)})),widgetScriptPromise}}customElements.define("eka-medassist-widget",MedAssistWidgetLoader);
|