ocular-widget-sdk
Version:
Ocular's widget SDK
170 lines (143 loc) • 4.45 kB
JavaScript
(function initGlobals() {
if (typeof window === "undefined") return;
if (!window.onOcularWidgetReady) {
window.onOcularWidgetReady = function () {
if (!window.ocularWidget && typeof ocularWidget !== "undefined") {
window.ocularWidget = ocularWidget;
}
window.ocularWidgetIsReady = true;
_dispatch("ready");
};
}
})();
const ocularEvents = typeof window !== "undefined" ? new EventTarget() : null;
function _dispatch(name, detail) {
if (!ocularEvents) return;
try {
ocularEvents.dispatchEvent(new CustomEvent(name, {
detail
}));
} catch {}
}
function waitForReady() {
if (typeof window === "undefined") {
return Promise.reject(new Error("Solo cliente"));
}
return new Promise((resolve, reject) => {
const timeout = 10000;
const interval = 400;
let elapsed = 0;
function check() {
if (window.ocularWidgetIsReady && window.ocularWidget) {
resolve();
} else if (elapsed >= timeout) {
reject(new Error("Timeout esperando ocularWidget"));
} else {
elapsed += interval;
setTimeout(check, interval);
}
}
check();
});
}
let _initCalled = false;
function initOcularWidget({
code
}) {
return new Promise((resolve, reject) => {
if (typeof window === "undefined") {
return reject(new Error("No disponible en servidor"));
}
if (_initCalled) {
return resolve("Ya inicializado");
}
_initCalled = true;
if (!code) {
const error = new Error("initOcularWidget: falta code");
console.warn(error.message);
return reject(error);
}
if (document.querySelector('script[name="ocular-solution-widget"]')) {
return resolve("Script ya existe");
}
const script = document.createElement("script");
script.setAttribute("name", "ocular-solution-widget");
script.setAttribute("data-modules", "call");
script.setAttribute("code", code);
script.type = "text/javascript";
script.src = "https://widget.ocularsolution.com/service/ocular/js/ocular-widget.js?v=1.1.1";
script.defer = true;
script.addEventListener("load", () => {
if (!window.ocularWidget && typeof ocularWidget !== "undefined") {
window.ocularWidget = ocularWidget;
}
if (!window.ocularWidgetIsReady && window.ocularWidget) {
window.ocularWidgetIsReady = true;
_dispatch("ready");
}
resolve("Script cargado exitosamente");
});
script.addEventListener("error", (error) => {
reject(new Error(`Error cargando script: ${error.message}`));
});
document.body.appendChild(script);
});
}
let _msgAttached = false;
function _attachMessageListener() {
if (_msgAttached || typeof window === "undefined") return;
_msgAttached = true;
window.addEventListener("message", (event) => {
if (typeof event.data !== "object") return;
if (event.data.type !== "ocular-widget-event") return;
_dispatch(event.data.msg, event.data);
_dispatch("widget-event", event.data);
if (event.data.msg === "is-ready") {
if (!window.ocularWidgetIsReady) {
window.ocularWidgetIsReady = true;
_dispatch("ready");
}
}
});
}
_attachMessageListener();
class OcularWidget {
on(event, callback) {
if (!ocularEvents) return
ocularEvents.addEventListener(event, (e) => {
const customEvent = e;
callback(customEvent.detail);
});
}
off(event) {
if (!ocularEvents) return
ocularEvents.removeEventListener(event, () => {});
}
async show(show = false) {
await waitForReady();
window.ocularWidget.show(show);
return "success";
}
async hide() {
await waitForReady();
window.ocularWidget.hide();
return "success";
}
async toggle() {
await waitForReady();
window.ocularWidget.toggle();
return "success";
}
async setCustomer(customer) {
await waitForReady();
window.ocularWidget.setCustomer(customer);
return "success";
}
async setLocale(locale) {
await waitForReady();
window.ocularWidget.setLocale(locale);
return "success";
}
}
const ocularWidgetInstance = new OcularWidget();
export { initOcularWidget, ocularEvents, ocularWidgetInstance, waitForReady };