@braze/web-sdk
Version:
Braze SDK for web sites and other JS platforms.
78 lines (77 loc) • 2.69 kB
JavaScript
import r, { OPTIONS as z } from "../../managers/braze-instance.js";
import { ContentCards, subscribeToContentCardsUpdates } from "../index.js";
import Z from "../content-cards-provider-factory.js";
import { setCardHeight as tn } from "../../Card/display/card-display.js";
import { logger as N } from "../../../shared-lib/index.js";
import {
destroyContentCardsHtml as nn,
detectContentCardsImpressions as en,
contentCardsToHtml as on,
LAST_REQUESTED_REFRESH_DATA_ATTRIBUTE as rn,
refreshContentCardsUI as sn,
registerContentCardsSubscriptionId as an,
updateContentCards as cn,
} from "../../common/content-cards-display.js";
import { setupFeedUI as fn } from "../../ui/js/feed-css.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(z.tn) || !1,
s = Z.m().Ys(!1);
"function" == typeof t && cn(s, t(s.cards.slice()), s.lastUpdated, null, o);
const a = on(s, o, e),
i = Z.m(),
c = i.Es();
(null == s.lastUpdated ||
new Date().valueOf() - s.lastUpdated.valueOf() > ContentCards.ur) &&
(null == c || new Date().valueOf() - c > ContentCards.ur) &&
(N.info(
`Cached content cards were older than max TTL of ${ContentCards.ur} ms, requesting an update from the server.`,
),
sn(s, a),
i.Ps(new Date().valueOf()));
const f = new Date().valueOf(),
l = subscribeToContentCardsUpdates(function (n) {
const e = a.querySelectorAll(".ab-refresh-button")[0];
if (null != e) {
let n = 500,
t = (n -= new Date().valueOf() - f);
const o = a.getAttribute(rn);
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())),
cn(s, r, n.lastUpdated, a, o);
});
an(l, 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
? (nn(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(),
en(s, a),
tn(s.cards, n);
};
var d;
null != n
? u(n)
: (window.onload =
((d = window.onload),
function () {
"function" == typeof d && d(new Event("oldLoad")), u(document.body);
}));
}