UNPKG

@braze/web-sdk

Version:

Braze SDK for web sites and other JS platforms.

331 lines (330 loc) 8.94 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 O } from "../util/date-utils.js"; import { isURIJavascriptOrData as X } from "../util/url-utils.js"; import { newCardFromContentCardsJson as Y, newCardFromSerializedValue as Z, } 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 tt } 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.N(this.Ts), (this.Rs = 0), (this.Us = 0), (this.cards = []), this.Ds(); const n = tt.Ls.Fs; new tt(n, N).Ns(n.Ms.Js, (t) => { this.$s(t); }), (this.Es = null), (this.k = null), (this.Ps = null), (this.Xs = null), (this._s = 10); } Gs() { return this.Es; } Hs(t) { this.Es = t; } Ct() { return this.k; } Tt(t) { this.k = t; } Ds() { if (!this.C) return; const t = this.C.ft(s.gt.Is) || [], i = []; for (let s = 0; s < t.length; s++) { const e = Z(t[s]); null != e && i.push(e); } (this.cards = this.Ks(this.Os(i, !1))), (this.Rs = this.C.ft(s.gt.Qs) || this.Rs), (this.Us = this.C.ft(s.gt.Vs) || this.Us); } Ws(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 = Y(e); null != h && h.viewed && t && (t.viewed = !0), null != t && r.push(t); } else if (null == h) { const t = Y(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.Ks(this.Os(r, i))), this.Ys(), (this.Rs = e), (this.Us = h), this.C && (this.C.Bt(s.gt.Qs, this.Rs), this.C.Bt(s.gt.Vs, this.Us)); } U(t) { if (this.Zs() && null != t && t.cards) { this.C && this.C.Bt(s.gt.fi, r.di()); const i = t.full_sync; i || this.Ds(), this.Ws(t.cards, i, t.last_full_sync_at, t.last_card_updated_at), this.Ts.X(this.pi(!0)); } } Ci(t) { this.bi(), (this.Ps = t); } $s(t) { var s; if (!this.Zs()) 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 = Y(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.Ks(this.Os(i, !1))), this.Ys(), this.Ts.X(this.pi(!0)); } Os(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 ); } Ks(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 && X(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; } Ys() { 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.Is, i); } bi() { this.Ps && (clearTimeout(this.Ps), (this.Ps = null)); } vi(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.wi(u, h.O.gi), this.bi()), !this.Zs())) return void ( null === (n = this.B) || void 0 === n || n.ji(() => { this.vi(t, i, !0); }) ); const c = o.H({}, !0); u.ft(s.gt.fi) !== r.di() && this.yi(), (c.last_full_sync_at = this.Rs), (c.last_card_updated_at = this.Us); const f = o.J(c, h.O.gi, e); let d = !1; o.V( c, (s = -1) => { if (this.C) { const t = new Date().valueOf(); h.W(this.C, h.O.gi, 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.Xs; (null == t || t < 1e3 * this._s) && (t = 1e3 * this._s), (r = Math.min(3e5, a(1e3 * this._s, 3 * t))); } o.rt( e, () => { this.vi(t, i, !1); }, h.O.gi, (t) => this.Ci(t), () => this.bi(), r, ); }, }); }, h.O.gi, i, ); } pi(t) { t || this.Ds(); const i = this.Ks(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.Vs) === this.Us && void 0 === e && (e = this.Us), new ContentCards(i, O(e)) ); } qt(t) { return this.Ts.Ft(t); } yi() { (this.Rs = 0), (this.Us = 0), this.C && (this.C.Ut(s.gt.Qs), this.C.Ut(s.gt.Vs)); } changeUser(t) { t || ((this.cards = []), this.Ts.X(new ContentCards(this.cards.slice(), null)), this.C && (this.C.Ut(s.gt.Is), this.C.Ut(s.gt.Kt), this.C.Ut(s.gt.Yt), this.C.Ut(s.gt.Vt))), this.yi(); } 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.Is), this.C.Ut(s.gt.Kt), this.C.Ut(s.gt.Yt), this.C.Ut(s.gt.Vt), this.C.Ut(s.gt.Qs), this.C.Ut(s.gt.Vs)); } Zs() { return !!this.B && (!!this.B.Ri() || (0 !== this.B.Et() && this.Ui(), !1)); } Ui() { this.Ts.X(new ContentCards([], new Date())), this.C && this.C.Ut(s.gt.Is); } }