UNPKG

@braze/web-sdk

Version:

Braze SDK for web sites and other JS platforms.

333 lines (332 loc) 8.97 kB
import l from "../util/net.js"; import t from "../common/base-provider.js"; import r from "../managers/braze-instance.js"; import ContentCards from "./content-cards.js"; import { dateFromUnixTimestamp as P } from "../util/date-utils.js"; import { isURIJavascriptOrData as Y } from "../util/url-utils.js"; import { newCardFromContentCardsJson as Z, newCardFromSerializedValue as tt, } from "../Card/util/card-factory.js"; import { STORAGE_KEYS as s } from "../managers/storage-manager.js"; import u from "../managers/subscription-manager.js"; import h from "../util/request-header-utils.js"; import { randomInclusive as a } from "../util/math.js"; import { logger as N, IndexedDBAdapter as st } from "../../shared-lib/index.js"; export default class Q extends t { constructor(t, s, i, e, h) { super(), (this.Cs = t), (this.C = s), (this.B = i), (this.Ss = e), (this.S = h), (this.Cs = t), (this.C = s), (this.B = i), (this.Ss = e), (this.S = h), (this.Ts = new u()), r.F(this.Ts), (this.Rs = 0), (this.Us = 0), (this.cards = []), this.Ds(); const n = st.Ls.Fs; new st(n, N).Ns(n.Ms.Js, (t) => { this.$s(t); }), (this.Bs = null), (this.N = null), (this.Es = null), (this.Ps = null), (this.Xs = 10); } _s() { return this.Bs; } Gs(t) { this.Bs = t; } Ct() { return this.N; } Tt(t) { this.N = t; } Ds() { if (!this.C) return; const t = this.C.ft(s.gt.Hs) || [], i = []; for (let s = 0; s < t.length; s++) { const e = tt(t[s]); null != e && i.push(e); } (this.cards = this.Is(this.Ks(i, !1))), (this.Rs = this.C.ft(s.gt.Os) || this.Rs), (this.Us = this.C.ft(s.gt.Qs) || this.Us); } Vs(t, i = !1, e = 0, h = 0) { let r; if (i) { r = []; for (const t of this.cards) t.test && r.push(t); } else r = this.cards.slice(); for (let s = 0; s < t.length; s++) { const e = t[s]; let h = null; for (let t = 0; t < this.cards.length; t++) if (e.id === this.cards[t].id) { h = this.cards[t]; break; } if (i) { const t = Z(e); null != h && h.viewed && t && (t.viewed = !0), null != t && r.push(t); } else if (null == h) { const t = Z(e); null != t && r.push(t); } else { if (!h.ri(e)) for (let t = 0; t < r.length; t++) if (e.id === r[t].id) { r.splice(t, 1); break; } } } (this.cards = this.Is(this.Ks(r, i))), this.Ws(), (this.Rs = e), (this.Us = h), this.C && (this.C.Bt(s.gt.Os, this.Rs), this.C.Bt(s.gt.Qs, this.Us)); } U(t) { if (this.Ys() && null != t && t.cards) { this.C && this.C.Bt(s.gt.Zs, r.fi()); const i = t.full_sync; i || this.Ds(), this.Vs(t.cards, i, t.last_full_sync_at, t.last_card_updated_at), this.Ts.X(this.di(!0)); } } pi(t) { this.Ci(), (this.Es = t); } $s(t) { var s; if (!this.Ys()) return; this.Ds(); const i = this.cards.slice(); let e = null; e = null === (s = this.Cs) || void 0 === s ? void 0 : s.getUserId(); for (let s = 0; s < t.length; s++) if (e === t[s].userId || (null == e && null == t[s].userId)) { const e = t[s].card; let h = null; for (let t = 0; t < this.cards.length; t++) if (e.id === this.cards[t].id) { h = this.cards[t]; break; } if (null == h) { const t = Z(e); null != t && i.push(t); } else { if (!h.ri(e)) for (let t = 0; t < i.length; t++) if (e.id === i[t].id) { i.splice(t, 1); break; } } } (this.cards = this.Is(this.Ks(i, !1))), this.Ws(), this.Ts.X(this.di(!0)); } Ks(t, i) { let e = {}, h = {}, r = {}; this.C && ((e = this.C.ft(s.gt.Kt) || {}), (h = this.C.ft(s.gt.Yt) || {}), (r = this.C.ft(s.gt.Vt) || {})); const n = {}, o = {}, l = {}; for (let s = 0; s < t.length; s++) { const i = t[s].id; i && (e[i] && ((t[s].clicked = !0), (n[i] = !0)), h[i] && ((t[s].viewed = !0), (o[i] = !0)), r[i] && ((t[s].dismissed = !0), (l[i] = !0))); } return ( i && this.C && (this.C.Bt(s.gt.Kt, n), this.C.Bt(s.gt.Yt, o), this.C.Bt(s.gt.Vt, l)), t ); } Is(t) { const i = [], e = new Date(); let h = {}; this.C && (h = this.C.ft(s.gt.Vt) || {}); let r = !1; for (let s = 0; s < t.length; s++) { const n = t[s].url; if (!this.Ss && n && Y(n)) { N.error( `Card with url ${n} will not be displayed because Javascript URLs are disabled. Use the "allowUserSuppliedJavascript" option for braze.initialize to enable this card.`, ); continue; } const o = t[s].expiresAt; let l = !0; if ((null != o && (l = o >= e), (l = l && !t[s].dismissed), l)) i.push(t[s]); else { const i = t[s].id; i && (h[i] = !0), (r = !0); } } return r && this.C && this.C.Bt(s.gt.Vt, h), i; } Ws() { var t; const i = []; for (let t = 0; t < this.cards.length; t++) i.push(this.cards[t].bt()); null === (t = this.C) || void 0 === t || t.Bt(s.gt.Hs, i); } Ci() { this.Es && (clearTimeout(this.Es), (this.Es = null)); } bi(t, i, e = !1) { var n; const o = this.S, u = this.C; if (!o || !u) return void ("function" == typeof i && i()); if ((e && (h.vi(u, h.O.wi), this.Ci()), !this.Ys())) return void ( null === (n = this.B) || void 0 === n || n.gi(() => { this.bi(t, i, !0); }) ); const c = o.H({}, !0); u.ft(s.gt.Zs) !== r.fi() && this.ji(), (c.last_full_sync_at = this.Rs), (c.last_card_updated_at = this.Us); const f = o.J(c, h.O.wi, e); let d = !1; o.V( c, (s = -1) => { if (this.C) { const t = new Date().valueOf(); h.W(this.C, h.O.wi, t); } -1 !== s && f.push(["X-Braze-Req-Tokens-Remaining", s.toString()]), l.Y({ url: `${o.Z()}/content_cards/sync`, data: c, headers: f, tt: (s) => { if (!o.st(c, s, f)) return (d = !0), void ("function" == typeof i && i()); o.it(), this.U(s), (d = !1), "function" == typeof t && t(); }, error: (t) => { o.et(t, "retrieving content cards"), (d = !0), "function" == typeof i && i(); }, nt: (s, e) => { let r; if (d) { let t = this.Ps; (null == t || t < 1e3 * this.Xs) && (t = 1e3 * this.Xs), (r = Math.min(3e5, a(1e3 * this.Xs, 3 * t))); } o.rt( e, () => { this.bi(t, i, !1); }, h.O.wi, (t) => this.pi(t), () => this.Ci(), r, ); }, }); }, h.O.wi, i, ); } di(t) { t || this.Ds(); const i = this.Is(this.cards); i.sort((t, s) => t.pinned && !s.pinned ? -1 : s.pinned && !t.pinned ? 1 : t.updated && s.updated && t.updated > s.updated ? -1 : t.updated && s.updated && s.updated > t.updated ? 1 : 0, ); let e = Math.max(this.Us || 0, this.Rs || 0); return ( 0 === e && (e = void 0), this.C && this.C.ft(s.gt.Qs) === this.Us && void 0 === e && (e = this.Us), new ContentCards(i, P(e)) ); } qt(t) { return this.Ts.Dt(t); } ji() { (this.Rs = 0), (this.Us = 0), this.C && (this.C.Ut(s.gt.Os), this.C.Ut(s.gt.Qs)); } changeUser(t) { t || ((this.cards = []), this.Ts.X(new ContentCards(this.cards.slice(), null)), this.C && (this.C.Ut(s.gt.Hs), this.C.Ut(s.gt.Kt), this.C.Ut(s.gt.Yt), this.C.Ut(s.gt.Vt))), this.Ci(), this.ji(); } clearData(t) { (this.Rs = 0), (this.Us = 0), (this.cards = []), this.Ts.X(new ContentCards(this.cards.slice(), null)), t && this.C && (this.C.Ut(s.gt.Hs), this.C.Ut(s.gt.Kt), this.C.Ut(s.gt.Yt), this.C.Ut(s.gt.Vt), this.C.Ut(s.gt.Os), this.C.Ut(s.gt.Qs)), this.Ci(); } Ys() { return !!this.B && (!!this.B.yi() || (0 !== this.B.Et() && this.Ri(), !1)); } Ri() { this.Ts.X(new ContentCards([], new Date())), this.C && this.C.Ut(s.gt.Hs); } }