UNPKG

@braze/web-sdk

Version:

Braze SDK for web sites and other JS platforms.

333 lines (332 loc) 8.97 kB
import a 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 X } from "../util/date-utils.js"; import { isURIJavascriptOrData as Z } from "../util/url-utils.js"; import { newCardFromContentCardsJson as tt, newCardFromSerializedValue as st, } 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 l } from "../util/math.js"; import { logger as E, IndexedDBAdapter as it } from "../../shared-lib/index.js"; export default class W extends t { constructor(t, s, i, e, h) { super(), (this.Cs = t), (this.j = s), (this.h = i), (this.vs = e), (this.B = h), (this.Cs = t), (this.j = s), (this.h = i), (this.vs = e), (this.B = h), (this.ws = new u()), r.q(this.ws), (this.ys = 0), (this.Ss = 0), (this.cards = []), this.Ts(); const n = it.Us.Rs; new it(n, E).Ds(n.Fs.As, (t) => { this.Ls(t); }), (this.Ns = null), (this.D = null), (this.Js = null), (this.Ms = null), (this.$s = 10); } Bs() { return this.Ns; } Es(t) { this.Ns = t; } jt() { return this.D; } yt(t) { this.D = t; } Ts() { if (!this.j) return; const t = this.j.lt(s.ct.Ps) || [], i = []; for (let s = 0; s < t.length; s++) { const e = st(t[s]); null != e && i.push(e); } (this.cards = this.Xs(this._s(i, !1))), (this.ys = this.j.lt(s.ct.Gs) || this.ys), (this.Ss = this.j.lt(s.ct.Hs) || this.Ss); } Is(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 = tt(e); null != h && h.viewed && t && (t.viewed = !0), null != t && r.push(t); } else if (null == h) { const t = tt(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.Xs(this._s(r, i))), this.Ks(), (this.ys = e), (this.Ss = h), this.j && (this.j.ft(s.ct.Gs, this.ys), this.j.ft(s.ct.Hs, this.Ss)); } N(t) { if (this.Os() && null != t && t.cards) { this.j && this.j.ft(s.ct.Qs, r.Vs()); const i = t.full_sync; i || this.Ts(), this.Is(t.cards, i, t.last_full_sync_at, t.last_card_updated_at), this.ws.L(this.Ws(!0)); } } Ys(t) { this.Zs(), (this.Js = t); } Ls(t) { var s; if (!this.Os()) return; this.Ts(); 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 = tt(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.Xs(this._s(i, !1))), this.Ks(), this.ws.L(this.Ws(!0)); } _s(t, i) { let e = {}, h = {}, r = {}; this.j && ((e = this.j.lt(s.ct.Et) || {}), (h = this.j.lt(s.ct.Pt) || {}), (r = this.j.lt(s.ct.Lt) || {})); 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.j && (this.j.ft(s.ct.Et, n), this.j.ft(s.ct.Pt, o), this.j.ft(s.ct.Lt, l)), t ); } Xs(t) { const i = [], e = new Date(); let h = {}; this.j && (h = this.j.lt(s.ct.Lt) || {}); let r = !1; for (let s = 0; s < t.length; s++) { const n = t[s].url; if (!this.vs && n && Z(n)) { E.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.j && this.j.ft(s.ct.Lt, h), i; } Ks() { var t; const i = []; for (let t = 0; t < this.cards.length; t++) i.push(this.cards[t].dt()); null === (t = this.j) || void 0 === t || t.ft(s.ct.Ps, i); } Zs() { this.Js && (clearTimeout(this.Js), (this.Js = null)); } ar(t, i, e = !1) { var n; const o = this.B, u = this.j; if (!o || !u) return void ("function" == typeof i && i()); if ((e && (h.fi(u, h.H.Ci), this.Zs()), !this.Os())) return void ( null === (n = this.h) || void 0 === n || n.bi(() => { this.ar(t, i, !0); }) ); const c = o.$({}, !0); u.lt(s.ct.Qs) !== r.Vs() && this.vi(), (c.last_full_sync_at = this.ys), (c.last_card_updated_at = this.Ss); const f = o.A(c, h.H.Ci, e); let d = !1; o.J( c, (s = -1) => { if (this.j) { const t = new Date().valueOf(); h.K(this.j, h.H.Ci, t); } -1 !== s && f.push(["X-Braze-Req-Tokens-Remaining", s.toString()]), a.O({ url: `${o.V()}/content_cards/sync`, data: c, headers: f, W: (s) => { if (!o.Y(c, s, f)) return (d = !0), void ("function" == typeof i && i()); o.Z(), this.N(s), (d = !1), "function" == typeof t && t(); }, error: (t) => { o._(t, "retrieving content cards"), (d = !0), "function" == typeof i && i(); }, tt: (s, e) => { let r; if (d) { let t = this.Ms; (null == t || t < 1e3 * this.$s) && (t = 1e3 * this.$s), (r = Math.min(3e5, l(1e3 * this.$s, 3 * t))); } o.st( e, () => { this.ar(t, i, !1); }, h.H.Ci, (t) => this.Ys(t), () => this.Zs(), r, ); }, }); }, h.H.Ci, i, ); } Ws(t) { t || this.Ts(); const i = this.Xs(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.Ss || 0, this.ys || 0); return ( 0 === e && (e = void 0), this.j && this.j.lt(s.ct.Hs) === this.Ss && void 0 === e && (e = this.Ss), new ContentCards(i, X(e)) ); } St(t) { return this.ws.wt(t); } vi() { (this.ys = 0), (this.Ss = 0), this.j && (this.j.Nt(s.ct.Gs), this.j.Nt(s.ct.Hs)); } changeUser(t) { t || ((this.cards = []), this.ws.L(new ContentCards(this.cards.slice(), null)), this.j && (this.j.Nt(s.ct.Ps), this.j.Nt(s.ct.Et), this.j.Nt(s.ct.Pt), this.j.Nt(s.ct.Lt))), this.Zs(), this.vi(); } clearData(t) { (this.ys = 0), (this.Ss = 0), (this.cards = []), this.ws.L(new ContentCards(this.cards.slice(), null)), t && this.j && (this.j.Nt(s.ct.Ps), this.j.Nt(s.ct.Et), this.j.Nt(s.ct.Pt), this.j.Nt(s.ct.Lt), this.j.Nt(s.ct.Gs), this.j.Nt(s.ct.Hs)), this.Zs(); } Os() { return !!this.h && (!!this.h.wi() || (0 !== this.h.Ft() && this.gi(), !1)); } gi() { this.ws.L(new ContentCards([], new Date())), this.j && this.j.Nt(s.ct.Ps); } }