@braze/web-sdk
Version:
Braze SDK for web sites and other JS platforms.
210 lines (209 loc) • 5.55 kB
JavaScript
import {
convertMsToSeconds as X,
dateFromUnixTimestamp as Y,
} from "../../util/date-utils.js";
import { FEED_ANIMATION_DURATION as Z } from "../../common/constants.js";
import f from "../../managers/subscription-manager.js";
import b from "../../../shared-lib/logger.js";
export default class Card {
constructor(t, i, s, h, l, n, e, r, u, E, T, o, a, I, N, A) {
(this.id = t),
(this.viewed = i),
(this.title = s),
(this.imageUrl = h),
(this.description = l),
(this.updated = n),
(this.expiresAt = e),
(this.url = r),
(this.linkText = u),
(this.aspectRatio = E),
(this.extras = T),
(this.pinned = o),
(this.dismissible = a),
(this.clicked = I),
(this.language = N),
(this.altImageText = A),
(this.id = t),
(this.viewed = i || !1),
(this.title = s || ""),
(this.imageUrl = h),
(this.description = l || ""),
(this.updated = n || null),
(this.expiresAt = e || null),
(this.url = r),
(this.linkText = u),
null == E
? (this.aspectRatio = null)
: ((E = parseFloat(E.toString())),
(this.aspectRatio = isNaN(E) ? null : E)),
(this.extras = T || {}),
(this.pinned = o || !1),
(this.dismissible = a || !1),
(this.dismissed = !1),
(this.clicked = I || !1),
(this.isControl = !1),
(this.language = N || null),
(this.altImageText = A || null),
(this.test = !1),
(this.ti = null),
(this.es = null),
(this.ii = null);
}
subscribeToClickedEvent(t) {
return this.si().Ut(t);
}
subscribeToDismissedEvent(t) {
return this.ns().Ut(t);
}
removeSubscription(t) {
this.si().removeSubscription(t), this.ns().removeSubscription(t);
}
removeAllSubscriptions() {
this.si().removeAllSubscriptions(), this.ns().removeAllSubscriptions();
}
dismissCard() {
if (!this.dismissible || this.dismissed) return;
"function" == typeof this.logCardDismissal && this.logCardDismissal();
let t = this.te;
!t && this.id && (t = document.getElementById(this.id)),
t &&
((t.style.height = t.offsetHeight + "px"),
(t.className = t.className + " ab-hide"),
setTimeout(function () {
t &&
t.parentNode &&
((t.style.height = "0"),
(t.style.margin = "0"),
setTimeout(function () {
t && t.parentNode && t.parentNode.removeChild(t);
}, Card.hi));
}, Z));
}
si() {
return null == this.ti && (this.ti = new f()), this.ti;
}
ns() {
return null == this.es && (this.es = new f()), this.es;
}
js() {
const t = new Date().valueOf();
return (
!(null != this.ii && t - this.ii < Card.li) &&
((this.ii = t), (this.viewed = !0), !0)
);
}
Yt() {
(this.viewed = !0), (this.clicked = !0), this.si().A();
}
Ft() {
return (
!(!this.dismissible || this.dismissed) &&
((this.dismissed = !0), this.ns().A(), !0)
);
}
ni(t) {
if (null == t || t[Card.ei.qs] !== this.id) return !0;
if (t[Card.ei.ri]) return !1;
if (
null != t[Card.ei.Es] &&
null != this.updated &&
parseInt(t[Card.ei.Es]) < X(this.updated.valueOf())
)
return !0;
if (
(t[Card.ei.ys] && !this.viewed && (this.viewed = !0),
t[Card.ei.Ls] && !this.clicked && (this.clicked = t[Card.ei.Ls]),
null != t[Card.ei.As] && (this.title = t[Card.ei.As]),
null != t[Card.ei.Bs] && (this.imageUrl = t[Card.ei.Bs]),
null != t[Card.ei.Ds] && (this.description = t[Card.ei.Ds]),
null != t[Card.ei.Es])
) {
const i = Y(t[Card.ei.Es]);
null != i && (this.updated = i);
}
if (null != t[Card.ei.Fs]) {
let i;
(i = t[Card.ei.Fs] === Card.ui ? null : Y(t[Card.ei.Fs])),
(this.expiresAt = i);
}
if (
(null != t[Card.ei.URL] && (this.url = t[Card.ei.URL]),
null != t[Card.ei.Gs] && (this.linkText = t[Card.ei.Gs]),
null != t[Card.ei.Hs])
) {
const i = parseFloat(t[Card.ei.Hs].toString());
this.aspectRatio = isNaN(i) ? null : i;
}
return (
null != t[Card.ei.Is] && (this.extras = t[Card.ei.Is]),
null != t[Card.ei.Js] && (this.pinned = t[Card.ei.Js]),
null != t[Card.ei.Ks] && (this.dismissible = t[Card.ei.Ks]),
null != t[Card.ei.Ms] && (this.language = t[Card.ei.Ms]),
null != t[Card.ei.Ns] && (this.altImageText = t[Card.ei.Ns]),
null != t[Card.ei.Os] && (this.test = t[Card.ei.Os]),
!0
);
}
qt() {
b.error("Must be implemented in a subclass");
}
}
(Card.ui = -1),
(Card.ei = {
qs: "id",
ys: "v",
Ks: "db",
ri: "r",
Es: "ca",
Js: "p",
Fs: "ea",
Is: "e",
xs: "tp",
Bs: "i",
As: "tt",
Ds: "ds",
URL: "u",
Gs: "dm",
Hs: "ar",
Ls: "cl",
Os: "t",
Ms: "language",
Ns: "image_alt",
}),
(Card.ks = {
zs: "captioned_image",
Ei: "text_announcement",
Ti: "short_news",
oi: "banner_image",
ai: "control",
}),
(Card.bs = {
qs: "id",
ys: "v",
Ks: "db",
Ii: "cr",
Es: "ca",
Js: "p",
Ni: "t",
Fs: "ea",
Is: "e",
xs: "tp",
Bs: "i",
As: "tt",
Ds: "ds",
URL: "u",
Gs: "dm",
Hs: "ar",
Ls: "cl",
Os: "s",
Ms: "l",
Ns: "ia",
}),
(Card.Ai = {
ci: "ADVERTISING",
mi: "ANNOUNCEMENTS",
Si: "NEWS",
Di: "SOCIAL",
}),
(Card.hi = 400),
(Card.li = 1e4);