nativescript-xz-ad-generation
Version:
NativeScript AdGeneration Plugin
163 lines (162 loc) • 6.18 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
var core_1 = require("@angular/core");
var page_1 = require("tns-core-modules/ui/page");
var xz_ad_controller_1 = require("../../../xz-ad-controller");
var DEFAULT_AD_WIDTH = 320;
var DEFAULT_AD_HEIGHT = 100;
var XzAdGLayoutComponent = (function () {
function XzAdGLayoutComponent(page, ngZone) {
this.page = page;
this.ngZone = ngZone;
this.updateAd = new core_1.EventEmitter();
this.ad = {};
}
XzAdGLayoutComponent.prototype.ngOnInit = function () {
this.loadAd();
};
XzAdGLayoutComponent.prototype.ngOnDestroy = function () {
this.dispose();
};
XzAdGLayoutComponent.prototype.ngOnChanges = function (changes) {
if ("locationId" in changes && !changes.locationId.firstChange) {
if (changes.locationId.currentValue) {
this.locationId = changes.locationId.currentValue;
this.dispose();
this.loadAd();
}
}
};
XzAdGLayoutComponent.prototype.dispose = function () {
if (this._adController) {
this._adController.off("receiveNativeAd", this.onReceiveNativeAd, this);
this._adController.off("fail", this.onFailReceiveAd, this);
this._adController.dispose();
}
if (this.ad) {
this.ad.nativeAd = null;
this.ad = {};
}
};
XzAdGLayoutComponent.prototype.reloadAd = function () {
this.ad = {};
this._adController.resumeAd();
};
XzAdGLayoutComponent.prototype.loadAd = function () {
var _this = this;
var adItem = {
type: "native",
locationId: this.locationId,
width: this.adWidth || DEFAULT_AD_WIDTH,
height: this.adHeight || DEFAULT_AD_HEIGHT
};
if (adItem.type === "native") {
setTimeout(function () {
_this._adController = new xz_ad_controller_1.XzAdController(adItem);
_this._adController.initNativeAd(_this.getContainerView());
_this._adController.on("receiveNativeAd", _this.onReceiveNativeAd, _this);
_this._adController.on("fail", _this.onFailReceiveAd, _this);
}, page_1.isIOS ? 1 : 1000);
}
};
XzAdGLayoutComponent.prototype.onReceiveNativeAd = function (data) {
var _this = this;
this.ngZone.run(function () {
_this.ad = data;
if (_this.hideOnFail) {
var view = _this.adContainerRef.nativeElement;
view.height = 'auto';
}
if (_this.adHeight && _this.ad.isHTML) {
var view = _this.adContainerRef.nativeElement;
var firstView = view.getChildAt(0);
if (page_1.isIOS) {
var paddingBottom = isNaN(+firstView.style.paddingBottom) ? 0 : +firstView.style.paddingBottom;
firstView.height = _this.adHeight + paddingBottom;
}
else {
firstView.height = _this.adHeight;
}
}
_this.updateAd.emit({
id: _this.id,
adAvailable: true,
eventName: "updateAd",
object: null
});
});
};
XzAdGLayoutComponent.prototype.onFailReceiveAd = function () {
var _this = this;
this.ngZone.run(function () {
_this.ad = {
loadFailed: true
};
if (_this.hideOnFail) {
var view = _this.adContainerRef.nativeElement;
view.height = 0;
}
_this.updateAd.emit({
id: _this.id,
adAvailable: false,
eventName: "updateAd",
object: null
});
});
};
XzAdGLayoutComponent.prototype.getContainerView = function () {
var container = this.adContainerRef.nativeElement;
var view = container.getChildAt(0);
if (view) {
if (page_1.isIOS) {
var nativeView = view.ios;
container.height = nativeView.frame.size.height;
}
return view;
}
else {
throw new Error("no view found for ad");
}
};
__decorate([
core_1.ViewChild("adContainer", { static: false }),
__metadata("design:type", core_1.ElementRef)
], XzAdGLayoutComponent.prototype, "adContainerRef", void 0);
__decorate([
core_1.Input(),
__metadata("design:type", Number)
], XzAdGLayoutComponent.prototype, "locationId", void 0);
__decorate([
core_1.Input(),
__metadata("design:type", Number)
], XzAdGLayoutComponent.prototype, "adWidth", void 0);
__decorate([
core_1.Input(),
__metadata("design:type", Number)
], XzAdGLayoutComponent.prototype, "adHeight", void 0);
__decorate([
core_1.Input(),
__metadata("design:type", Number)
], XzAdGLayoutComponent.prototype, "id", void 0);
__decorate([
core_1.Input(),
__metadata("design:type", Boolean)
], XzAdGLayoutComponent.prototype, "hideOnFail", void 0);
__decorate([
core_1.Output(),
__metadata("design:type", core_1.EventEmitter)
], XzAdGLayoutComponent.prototype, "updateAd", void 0);
__decorate([
core_1.ContentChild(core_1.TemplateRef, { static: false }),
__metadata("design:type", Object)
], XzAdGLayoutComponent.prototype, "templateRef", void 0);
XzAdGLayoutComponent = __decorate([
core_1.Component({
selector: "XzAdGLayout",
template: "\n\t\t<GridLayout #adContainer>\n\t\t\t<ng-template [ngTemplateOutlet]=\"templateRef\"\n\t [ngTemplateOutletContext]=\"{$implicit: ad}\">\n\t\t\t</ng-template>\n\t\t</GridLayout>\n\t"
}),
__metadata("design:paramtypes", [page_1.Page, core_1.NgZone])
], XzAdGLayoutComponent);
return XzAdGLayoutComponent;
}());
exports.XzAdGLayoutComponent = XzAdGLayoutComponent;
;