UNPKG

@braze/web-sdk

Version:

Braze SDK for web sites and other JS platforms.

78 lines (77 loc) 2.65 kB
import r, { OPTIONS as w } from "../../managers/braze-instance.js"; import { ContentCards, subscribeToContentCardsUpdates } from "../index.js"; import W from "../content-cards-provider-factory.js"; import { destroyFeedHtml as ee, detectFeedImpressions as nn, feedToHtml as tn, LAST_REQUESTED_REFRESH_DATA_ATTRIBUTE as en, refreshFeed as on, registerFeedSubscriptionId as rn, updateFeedCards as an, } from "../../common/feed-display.js"; import { setCardHeight as sn } from "../../Card/display/card-display.js"; import { setupFeedUI as fn } from "../../ui/js/index.js"; import { logger as N } from "../../../shared-lib/index.js"; export function showContentCards(n, t) { if (!r.rr()) return; fn(); let e = !1; null == n && ((n = document.body), (e = !0)); const o = r.ee(w.tn) || r.ee(w.en) || !1, i = W.m().pi(!1); "function" == typeof t && an(i, t(i.cards.slice()), i.lastUpdated, null, o); const a = tn(i, o, e), s = W.m(), f = s.Gs(); (null == i.lastUpdated || new Date().valueOf() - i.lastUpdated.valueOf() > ContentCards.dr) && (null == f || new Date().valueOf() - f > ContentCards.dr) && (N.info( `Cached content cards were older than max TTL of ${ContentCards.dr} ms, requesting an update from the server.`, ), on(i, a), s.Hs(new Date().valueOf())); const l = new Date().valueOf(), c = subscribeToContentCardsUpdates(function (n) { const e = a.querySelectorAll(".ab-refresh-button")[0]; if (null != e) { let n = 500, t = (n -= new Date().valueOf() - l); const o = a.getAttribute(en); o && ((t = parseInt(o)), isNaN(t) || (n -= new Date().valueOf() - t)), setTimeout( function () { e.className = e.className.replace(/fa-spin/g, ""); }, Math.max(n, 0), ); } let r = n.cards; "function" == typeof t && (r = t(r.slice())), an(i, r, n.lastUpdated, a, o); }); rn(c, a); const u = function (n) { const t = n.querySelectorAll(".ab-feed"); let o = null; for (let e = 0; e < t.length; e++) t[e].parentNode === n && (o = t[e]); null != o ? (ee(o), null != o.parentNode && o.parentNode.replaceChild(a, o)) : n.appendChild(a), setTimeout(function () { a.className = a.className.replace("ab-hide", "ab-show"); }, 0), e && a.focus(), nn(i, a), sn(i.cards, n); }; var d; null != n ? u(n) : (window.onload = ((d = window.onload), function () { "function" == typeof d && d(new Event("oldLoad")), u(document.body); })); }