UNPKG

nativescript-xz-ad-generation

Version:
166 lines (165 loc) 6.69 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var xz_ad_controller_base_1 = require("./xz-ad-controller-base"); var XzAdController = (function (_super) { __extends(XzAdController, _super); function XzAdController() { return _super !== null && _super.apply(this, arguments) || this; } XzAdController.prototype.initNativeAd = function (parentView) { if (this.adItem.type !== "native") { console.log("Unsupported ad type ==> ", this.adItem.type); return null; } if (parentView == null) { throw new Error("no parent view!"); } var targetView = parentView.ios; var parentWidth = targetView.frame.size.width; var parentHeight = targetView.frame.size.height; var pl = isNaN(+parentView.style.paddingLeft) ? 0 : +parentView.style.paddingLeft; var pr = isNaN(+parentView.style.paddingRight) ? 0 : +parentView.style.paddingRight; var pt = isNaN(+parentView.style.paddingTop) ? 0 : +parentView.style.paddingTop; var adWidth = parentWidth - (pl + pr); var adHeight = parentHeight / parentWidth * adWidth; this._tapTargetView = new WeakRef(targetView); this._adg = ADGManagerViewController.alloc().initWithLocationIDAdTypeRootViewController("" + this.adItem.locationId, 4, null); this._adg.addAdContainerView(targetView); this._delegate = ADGManagerViewControllerDelegateImpl.initWithOwner(new WeakRef(this)); this._adg.delegate = this._delegate; this._adg.usePartsResponse = true; this._adg.informationIconViewDefault = false; this._adg.adSize = CGSizeMake(adWidth, adHeight); this._adg.adOrigin = CGPointMake(pl, pt); this._adg.loadRequest(); return this; }; XzAdController.prototype.initWithView = function (view, viewHeight, viewWidth, scale) { if (this.adItem.type !== "banner") { return null; } this._adg = ADGManagerViewController.alloc().initWithLocationIDAdTypeRootViewController("" + this.adItem.locationId, 4, null); this._adg.addAdContainerView(view); this._adg.adScale = scale; this._adg.adSize = CGSizeMake(viewWidth, viewHeight); this._adg.delegate = ADGManagerViewControllerDelegateImpl.initWithOwner(new WeakRef(this)); this._adg.loadRequest(); return this; }; XzAdController.prototype.dispose = function () { if (this._adg) { this._adg.delegate = null; this._adg = null; } }; XzAdController.prototype.resumeAd = function () { if (this._adg) { this._adg.resumeRefresh(); } }; XzAdController.prototype.loadRequest = function () { this._adg.loadRequest(); }; XzAdController.prototype.onReceiveNativeAd = function (ad) { var adData = { eventName: "receiveNativeAd", locationId: this.adItem.locationId, object: null }; if (ad) { if (ad.mainImage) { adData.mainImageUrl = ad.mainImage.url; adData.mainImageHeight = ad.mainImage.height; adData.mainImageWidth = ad.mainImage.width; } if (ad.iconImage) { adData.iconImageUrl = ad.iconImage.url; adData.iconImageHeight = ad.iconImage.height; adData.iconImageWidth = ad.iconImage.width; } if (ad.desc) { adData.description = ad.desc.value; } if (ad.title) { adData.title = ad.title.text; } if (ad.sponsored) { adData.sponsor = ad.sponsored.value; } adData.nativeAd = ad; if (this._tapTargetView.get()) { ad.setTapEvent(this._tapTargetView.get()); } } else { adData.isHTML = true; } this.notify(adData); }; XzAdController.prototype.onFailed = function () { this.notify({ eventName: "fail", object: null }); }; XzAdController.prototype.getAdItem = function () { return this.adItem; }; return XzAdController; }(xz_ad_controller_base_1.XzAdControllerBase)); exports.XzAdController = XzAdController; var ADGManagerViewControllerDelegateImpl = (function (_super) { __extends(ADGManagerViewControllerDelegateImpl, _super); function ADGManagerViewControllerDelegateImpl() { return _super !== null && _super.apply(this, arguments) || this; } ADGManagerViewControllerDelegateImpl.initWithOwner = function (owner) { var ins = ADGManagerViewControllerDelegateImpl.new(); ins._owner = owner; return ins; }; ADGManagerViewControllerDelegateImpl.prototype.ADGManagerViewControllerReceiveAd = function (adgManagerViewController) { if (this._owner && this._owner.get()) { var controller = this._owner.get(); if (controller.getAdItem().type == 'native') { this._owner.get().onReceiveNativeAd(null); } } }; ADGManagerViewControllerDelegateImpl.prototype.ADGManagerViewControllerFailedToReceiveAdCode = function (adgManagerViewController, code) { var failed = false; switch (code) { case 3: console.log("no ad"); failed = true; break; case 4: console.log("need connection"); failed = true; break; case 5: console.log("exceed limit"); failed = true; break; default: console.log("retry..."); if (this._owner && this._owner.get()) { this._owner.get().loadRequest(); } break; } if (failed && this._owner && this._owner.get()) { this._owner.get().onFailed(); } }; ADGManagerViewControllerDelegateImpl.prototype.ADGManagerViewControllerReceiveAdMediationNativeAd = function (adgManagerViewController, mediationNativeAd) { if (mediationNativeAd.isKindOfClass(ADGNativeAd.class())) { var nativeAd = mediationNativeAd; if (this._owner && this._owner.get()) { this._owner.get().onReceiveNativeAd(nativeAd); } } }; ADGManagerViewControllerDelegateImpl.ObjCProtocols = [ADGManagerViewControllerDelegate]; return ADGManagerViewControllerDelegateImpl; }(NSObject));