@benshi.ai/js-sdk
Version:
Benshi SDK
93 lines • 4.48 kB
JavaScript
;
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.ImpressionObservedTypes = void 0;
var events_1 = require("events");
var ImpressionObservedTypes;
(function (ImpressionObservedTypes) {
ImpressionObservedTypes["View"] = "view";
ImpressionObservedTypes["Hide"] = "hide";
})(ImpressionObservedTypes = exports.ImpressionObservedTypes || (exports.ImpressionObservedTypes = {}));
var BsViewableImpressionObserver = /** @class */ (function (_super) {
__extends(BsViewableImpressionObserver, _super);
function BsViewableImpressionObserver(intersectionThreshold) {
var _this = _super.call(this) || this;
_this.mutationObserver = null;
_this.intersectionObserver = null;
_this.containerSelector = '';
_this.itemSelector = '';
var intersectionObserverOptions = {
root: null,
rootMargin: "0px",
threshold: intersectionThreshold
};
_this.intersectionObserver = new IntersectionObserver(function (entries, observer) { return _this.intersectionHandler(entries, observer); }, intersectionObserverOptions);
return _this;
}
BsViewableImpressionObserver.prototype.intersectionHandler = function (entries, observer) {
var _this = this;
entries.forEach(function (entry) {
if (entry.isIntersecting) {
_this.emit(ImpressionObservedTypes.View, entry.target.dataset.logId, entry.target.dataset);
}
else {
_this.emit(ImpressionObservedTypes.Hide, entry.target.dataset.logId);
}
});
};
BsViewableImpressionObserver.prototype.start = function (containerClassname, itemClassname) {
var _this = this;
this.containerSelector = ".".concat(containerClassname);
this.itemSelector = ".".concat(itemClassname);
var addItemsToIntersectionObserver = function () {
document.querySelectorAll("".concat(_this.containerSelector, " ").concat(_this.itemSelector))
.forEach(function (element) {
_this.intersectionObserver.observe(element);
});
};
var target = document.querySelector(this.containerSelector);
if (target === null) {
throw new Error('container-does-not-exist');
}
// if the items are already there when this function is called, they will not
// be added to intersection observer, so we need to do it manually
addItemsToIntersectionObserver();
this.mutationObserver = new MutationObserver(function (mutations) {
// we don't care specific changes per mutation,
// just know what are the new items
_this.intersectionObserver.disconnect(); // to avoid having more than one observer per item
// within the container we are only interested in those child elements
// that matches the itemSelector
addItemsToIntersectionObserver();
});
var config = {
attributes: true,
childList: true,
characterData: true
};
this.mutationObserver.observe(target, config);
};
BsViewableImpressionObserver.prototype.stop = function () {
var _this = this;
this.mutationObserver.disconnect();
document.querySelectorAll("".concat(this.containerSelector, " ").concat(this.itemSelector))
.forEach(function (element) { return _this.intersectionObserver.unobserve(element); });
};
return BsViewableImpressionObserver;
}(events_1.EventEmitter));
exports.default = BsViewableImpressionObserver;
//# sourceMappingURL=ViewableImpressionObserver.js.map