@braze/web-sdk
Version:
Braze SDK for web sites and other JS platforms.
204 lines (203 loc) • 5.34 kB
JavaScript
import {
convertMsToSeconds as L,
dateFromUnixTimestamp as O,
} from "../../util/date-utils.js";
import { FEED_ANIMATION_DURATION as P } from "../../common/constants.js";
import u from "../../managers/subscription-manager.js";
import N from "../../../shared-lib/logger.js";
export default class Card {
constructor(t, i, s, h, n, l, e, r, u, E, o, T, I, a, c, N) {
(this.id = t),
(this.viewed = i),
(this.title = s),
(this.imageUrl = h),
(this.description = n),
(this.created = l),
(this.updated = e),
(this.categories = r),
(this.expiresAt = u),
(this.url = E),
(this.linkText = o),
(this.aspectRatio = T),
(this.extras = I),
(this.pinned = a),
(this.dismissible = c),
(this.clicked = N),
(this.id = t),
(this.viewed = i || !1),
(this.title = s || ""),
(this.imageUrl = h),
(this.description = n || ""),
(this.created = l || null),
(this.updated = e || null),
(this.categories = r || []),
(this.expiresAt = u || null),
(this.url = E),
(this.linkText = o),
null == T
? (this.aspectRatio = null)
: ((T = parseFloat(T.toString())),
(this.aspectRatio = isNaN(T) ? null : T)),
(this.extras = I || {}),
(this.pinned = a || !1),
(this.dismissible = c || !1),
(this.dismissed = !1),
(this.clicked = N || !1),
(this.isControl = !1),
(this.test = !1),
(this.ti = null),
(this.ii = null),
(this.si = null);
}
subscribeToClickedEvent(t) {
return this.hi().Ft(t);
}
subscribeToDismissedEvent(t) {
return this.ni().Ft(t);
}
removeSubscription(t) {
this.hi().removeSubscription(t), this.ni().removeSubscription(t);
}
removeAllSubscriptions() {
this.hi().removeAllSubscriptions(), this.ni().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.li));
}, P));
}
hi() {
return null == this.ti && (this.ti = new u()), this.ti;
}
ni() {
return null == this.ii && (this.ii = new u()), this.ii;
}
ts() {
const t = new Date().valueOf();
return (
!(null != this.si && t - this.si < Card.ei) &&
((this.si = t), (this.viewed = !0), !0)
);
}
$t() {
(this.viewed = !0), (this.clicked = !0), this.hi().X();
}
Qt() {
return (
!(!this.dismissible || this.dismissed) &&
((this.dismissed = !0), this.ni().X(), !0)
);
}
ri(t) {
if (null == t || t[Card.ui.ps] !== this.id) return !0;
if (t[Card.ui.Ei]) return !1;
if (
null != t[Card.ui.bs] &&
null != this.updated &&
parseInt(t[Card.ui.bs]) < L(this.updated.valueOf())
)
return !0;
if (
(t[Card.ui.us] && !this.viewed && (this.viewed = !0),
t[Card.ui.As] && !this.clicked && (this.clicked = t[Card.ui.As]),
null != t[Card.ui.ls] && (this.title = t[Card.ui.ls]),
null != t[Card.ui.fs] && (this.imageUrl = t[Card.ui.fs]),
null != t[Card.ui.xs] && (this.description = t[Card.ui.xs]),
null != t[Card.ui.bs])
) {
const i = O(t[Card.ui.bs]);
null != i && (this.updated = i);
}
if (null != t[Card.ui.zs]) {
let i;
(i = t[Card.ui.zs] === Card.oi ? null : O(t[Card.ui.zs])),
(this.expiresAt = i);
}
if (
(null != t[Card.ui.URL] && (this.url = t[Card.ui.URL]),
null != t[Card.ui.ks] && (this.linkText = t[Card.ui.ks]),
null != t[Card.ui.qs])
) {
const i = parseFloat(t[Card.ui.qs].toString());
this.aspectRatio = isNaN(i) ? null : i;
}
return (
null != t[Card.ui.vs] && (this.extras = t[Card.ui.vs]),
null != t[Card.ui.ws] && (this.pinned = t[Card.ui.ws]),
null != t[Card.ui.ys] && (this.dismissible = t[Card.ui.ys]),
null != t[Card.ui.Bs] && (this.test = t[Card.ui.Bs]),
!0
);
}
bt() {
N.error("Must be implemented in a subclass");
}
}
(Card.oi = -1),
(Card.ui = {
ps: "id",
us: "v",
ys: "db",
Ei: "r",
bs: "ca",
ws: "p",
zs: "ea",
vs: "e",
hs: "tp",
fs: "i",
ls: "tt",
xs: "ds",
URL: "u",
ks: "dm",
qs: "ar",
As: "cl",
Bs: "t",
}),
(Card.ds = {
cs: "captioned_image",
Ti: "text_announcement",
Ii: "short_news",
ai: "banner_image",
ci: "control",
}),
(Card.es = {
ps: "id",
us: "v",
ys: "db",
gs: "cr",
bs: "ca",
ws: "p",
js: "t",
zs: "ea",
vs: "e",
hs: "tp",
fs: "i",
ls: "tt",
xs: "ds",
URL: "u",
ks: "dm",
qs: "ar",
As: "cl",
Bs: "s",
}),
(Card.Ni = {
mi: "ADVERTISING",
Si: "ANNOUNCEMENTS",
Ai: "NEWS",
Di: "SOCIAL",
}),
(Card.li = 400),
(Card.ei = 1e4);