video-ad-sdk
Version:
VAST/VPAID SDK that allows video ads to be played on top of any player
48 lines (47 loc) • 2.35 kB
JavaScript
var __rest = (this && this.__rest) || function (s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
t[p[i]] = s[p[i]];
}
return t;
};
import { renderIcons } from './renderIcons';
const firstRenderPending = Symbol('firstRenderPending');
const noop = () => { };
const hasPendingIconRedraws = (icons, videoElement) => {
const currentTimeInMs = videoElement.currentTime * 1000;
const videoDurationInMs = videoElement.duration * 1000;
const iconsPendingToRedraw = icons.filter((icon) => !icon.offset || icon.offset < currentTimeInMs);
const iconsPendingToBeRemoved = icons.filter((icon) => icon.duration && icon.duration < videoDurationInMs);
return iconsPendingToRedraw.length > 0 || iconsPendingToBeRemoved.length > 0;
};
const removeDrawnIcons = (icons) => icons
.filter(({ element }) => Boolean(element === null || element === void 0 ? void 0 : element.parentNode))
.forEach(({ element }) => { var _a; return (_a = element === null || element === void 0 ? void 0 : element.parentNode) === null || _a === void 0 ? void 0 : _a.removeChild(element); });
export const addIcons = (icons, _a) => {
var { videoAdContainer, onIconView = noop, onIconClick = noop } = _a, rest = __rest(_a, ["videoAdContainer", "onIconView", "onIconClick"]);
const { videoElement, element } = videoAdContainer;
const drawIcons = async () => {
const drawnIcons = await renderIcons(icons, Object.assign({ onIconClick,
videoAdContainer }, rest));
element.dispatchEvent(new CustomEvent('iconsDrawn'));
drawnIcons.forEach((icon) => {
if (icon[firstRenderPending]) {
onIconView(icon);
icon[firstRenderPending] = false;
}
});
};
icons.forEach((icon) => {
icon[firstRenderPending] = true;
});
return {
drawIcons,
hasPendingIconRedraws: () => hasPendingIconRedraws(icons, videoElement),
removeIcons: () => removeDrawnIcons(icons)
};
};