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