UNPKG

@braze/web-sdk

Version:

Braze SDK for web sites and other JS platforms.

333 lines (332 loc) 8.98 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 V } from "../util/date-utils.js"; import { isURIJavascriptOrData as tt } from "../util/url-utils.js"; import { newCardFromContentCardsJson as st, newCardFromSerializedValue as it, } 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 et } from "../../shared-lib/index.js"; export default class Y extends t { constructor(t, s, i, e, h) { super(), (this.Cs = t), (this.C = s), (this.B = i), (this.ws = e), (this.S = h), (this.Cs = t), (this.C = s), (this.B = i), (this.ws = e), (this.S = h), (this.ys = new u()), r.F(this.ys), (this.Ss = 0), (this.Ts = 0), (this.cards = []), this.Rs(); const n = et.Ds.Us; new et(n, N).As(n.Ls.Fs, (t) => { this.Ns(t); }), (this.Js = null), (this.N = null), (this.Ms = null), (this.$s = null), (this.Bs = 10); } Es() { return this.Js; } Ps(t) { this.Js = t; } Ct() { return this.N; } Tt(t) { this.N = t; } Rs() { if (!this.C) return; const t = this.C.ft(s.gt.Xs) || [], i = []; for (let s = 0; s < t.length; s++) { const e = it(t[s]); null != e && i.push(e); } (this.cards = this._s(this.Gs(i, !1))), (this.Ss = this.C.ft(s.gt.Hs) || this.Ss), (this.Ts = this.C.ft(s.gt.Is) || this.Ts); } Ks(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 = st(e); null != h && h.viewed && t && (t.viewed = !0), null != t && r.push(t); } else if (null == h) { const t = st(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._s(this.Gs(r, i))), this.Os(), (this.Ss = e), (this.Ts = h), this.C && (this.C.Bt(s.gt.Hs, this.Ss), this.C.Bt(s.gt.Is, this.Ts)); } U(t) { if (this.Qs() && null != t && t.cards) { this.C && this.C.Bt(s.gt.Vs, r.Ws()); const i = t.full_sync; i || this.Rs(), this.Ks(t.cards, i, t.last_full_sync_at, t.last_card_updated_at), this.ys.X(this.Ys(!0)); } } Zs(t) { this.fi(), (this.Ms = t); } Ns(t) { var s; if (!this.Qs()) return; this.Rs(); 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 = st(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._s(this.Gs(i, !1))), this.Os(), this.ys.X(this.Ys(!0)); } Gs(t, i) { let e = {}, h = {}, r = {}; this.C && ((e = this.C.ft(s.gt.Kt) || {}), (h = this.C.ft(s.gt.Xt) || {}), (r = this.C.ft(s.gt.Qt) || {})); 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.Xt, o), this.C.Bt(s.gt.Qt, l)), t ); } _s(t) { const i = [], e = new Date(); let h = {}; this.C && (h = this.C.ft(s.gt.Qt) || {}); let r = !1; for (let s = 0; s < t.length; s++) { const n = t[s].url; if (!this.ws && n && tt(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.Qt, h), i; } Os() { 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.Xs, i); } fi() { this.Ms && (clearTimeout(this.Ms), (this.Ms = null)); } Ci(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.bi(u, h.O.vi), this.fi()), !this.Qs())) return void ( null === (n = this.B) || void 0 === n || n.wi(() => { this.Ci(t, i, !0); }) ); const c = o.H({}, !0); u.ft(s.gt.Vs) !== r.Ws() && this.gi(), (c.last_full_sync_at = this.Ss), (c.last_card_updated_at = this.Ts); const f = o.J(c, h.O.vi, e); let d = !1; o.V( c, (s = -1) => { if (this.C) { const t = new Date().valueOf(); h.W(this.C, h.O.vi, 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.$s; (null == t || t < 1e3 * this.Bs) && (t = 1e3 * this.Bs), (r = Math.min(3e5, a(1e3 * this.Bs, 3 * t))); } o.rt( e, () => { this.Ci(t, i, !1); }, h.O.vi, (t) => this.Zs(t), () => this.fi(), r, ); }, }); }, h.O.vi, i, ); } Ys(t) { t || this.Rs(); const i = this._s(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.Ts || 0, this.Ss || 0); return ( 0 === e && (e = void 0), this.C && this.C.ft(s.gt.Is) === this.Ts && void 0 === e && (e = this.Ts), new ContentCards(i, V(e)) ); } qt(t) { return this.ys.Dt(t); } gi() { (this.Ss = 0), (this.Ts = 0), this.C && (this.C.Ut(s.gt.Hs), this.C.Ut(s.gt.Is)); } changeUser(t) { t || ((this.cards = []), this.ys.X(new ContentCards(this.cards.slice(), null)), this.C && (this.C.Ut(s.gt.Xs), this.C.Ut(s.gt.Kt), this.C.Ut(s.gt.Xt), this.C.Ut(s.gt.Qt))), this.fi(), this.gi(); } clearData(t) { (this.Ss = 0), (this.Ts = 0), (this.cards = []), this.ys.X(new ContentCards(this.cards.slice(), null)), t && this.C && (this.C.Ut(s.gt.Xs), this.C.Ut(s.gt.Kt), this.C.Ut(s.gt.Xt), this.C.Ut(s.gt.Qt), this.C.Ut(s.gt.Hs), this.C.Ut(s.gt.Is)), this.fi(); } Qs() { return !!this.B && (!!this.B.ji() || (0 !== this.B.Et() && this.yi(), !1)); } yi() { this.ys.X(new ContentCards([], new Date())), this.C && this.C.Ut(s.gt.Xs); } }