UNPKG

@caspingus/lt

Version:

A utility library of helpers and extensions useful when working with Learnosity APIs.

75 lines (74 loc) 2.81 kB
import e from "../../logger.js"; import { r as t } from "../../extensionsFactory-hk5ijx1G.js"; //#region src/assessment/extensions/ui/networkStatus/index.js var n = { logPrefix: "LT Network Status: ", options: { iconWrapper: "top-right-wrapper", interval: 3e4, message: "Internet connection is currently down.", render: !0, uri: "https://questions.learnosity.com?latest-lts" } }; function r(e = {}) { l(e), i().catch(() => {}), setInterval(i, n.options.interval); } async function i() { let e = await o(), t = document.querySelector(".lt__networkStatus-indicator"), { render: r, iconWrapper: i, message: s } = n.options; c(e), r && (e ? t && t.remove() : a(t, i, s)); } function a(e, t, n) { if (e) return; let r = document.querySelector(`.${t}`); if (!r) return; let i = ` <div class="lt__networkStatus-indicator pos-left" role="status" aria-live="polite" aria-atomic="true" aria-relevant="all"> <span title="${n}" aria-hidden="true"> <svg xmlns="http://www.w3.org/2000/svg" height="16px" viewBox="0 -960 960 960" width="16px" fill="#333333"> <path d="M790-56 414-434q-47 11-87.5 33T254-346l-84-86q32-32 69-56t79-42l-90-90q-41 21-76.5 46.5T84-516L0-602q32-32 66.5-57.5T140-708l-84-84 56-56 736 736-58 56Zm-310-64q-42 0-71-29.5T380-220q0-42 29-71t71-29q42 0 71 29t29 71q0 41-29 70.5T480-120Zm236-238-29-29-29-29-144-144q81 8 151.5 41T790-432l-74 74Zm160-158q-77-77-178.5-120.5T480-680q-21 0-40.5 1.5T400-674L298-776q44-12 89.5-18t92.5-6q142 0 265 53t215 145l-84 86Z"/> </svg> </span> <span class="sr-only">${n}</span> </div> `; r.insertAdjacentHTML("afterbegin", i); } async function o() { let { uri: t } = n.options, r = new AbortController(), i = setTimeout(() => r.abort(), 3500); try { return (await fetch(`${t}${t.includes("?") ? "&" : "?"}_=${Date.now()}`, { method: "GET", cache: "no-store", credentials: "omit", signal: r.signal })).ok; } catch (t) { return e.error(`${n.logPrefix} offline`, t), !1; } finally { clearTimeout(i); } } function s() { return navigator?.connection ? `${navigator.connection.downlink} Mbps` : "Network Information API not supported"; } function c(e) { let t = new CustomEvent(e ? "LTNetworkOnline" : "LTNetworkOffline"); document.dispatchEvent(t); } function l(e) { e && typeof e == "object" && (n.options = { ...n.options, ...e }); } function u() { return "\n /* Learnosity render network status */\n .lt__networkStatus-indicator {\n width: 24px;\n text-align: center;\n position: relative;\n top: 9px;\n }\n "; } var d = t("networkStatus", r, { checkConnection: o, checkSpeed: s, getStyles: u }); //#endregion export { d as networkStatus };