@asoftwareworld/google-adsense
Version:
Google Adsense for Angular
219 lines (212 loc) • 8.24 kB
JavaScript
import * as i0 from '@angular/core';
import { InjectionToken, PLATFORM_ID, ElementRef, Component, ChangeDetectionStrategy, Inject, Input, ViewChild, NgModule } from '@angular/core';
import { isPlatformBrowser, CommonModule } from '@angular/common';
/**
* @license
* Copyright ASW (A Software World) All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file
*/
const ASW_GOOGLE_ADSENSE_TOKEN = new InjectionToken('AswGoogleAdsenseConfig');
/**
* @license
* Copyright ASW (A Software World) All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file
*/
class AswGoogleAdsense {
config;
platform;
/** adsense account ca-pub-XXXXXXXXXXXXXXXX */
adClient;
/** ad slot/number */
adSlot;
adFormat;
/** can be manually set if you need all ads on a page to have same id page-xxx */
adRegion = 'page-' + Math.floor(Math.random() * 10000) + 1;
/** ins element display style */
display;
/** ins element height in px */
width;
/** ins element width in px */
height;
/** used for in-feed ads */
layout;
/** used for in-feed ads */
layoutKey;
/** enable page-level ads */
pageLevelAds;
/** sets up some sort of google test ad */
adtest;
/* used for flexible ads */
fullWidthResponsive;
/**
* classes applied to the ins element
*/
className = '';
ins;
constructor(config, platform) {
this.config = config;
this.platform = platform;
}
ngOnInit() {
const config = this.config;
this.adClient = this.adClient ?? config.adClient;
this.adSlot = this.adSlot ?? config.adSlot;
this.adFormat = this.adFormat ?? config.adFormat ?? 'auto';
this.display = this.display ?? config.display ?? 'block';
this.width = this.width ?? config.width;
this.height = this.height ?? config.height;
this.layout = this.layout ?? config.layout;
this.layoutKey = this.layoutKey ?? config.layoutKey;
this.pageLevelAds = this.pageLevelAds ?? config.pageLevelAds;
this.adtest = this.adtest ?? config.adtest;
this.fullWidthResponsive = this.fullWidthResponsive ?? config.fullWidthResponsive;
}
ngOnDestroy() {
const iframe = this.ins.nativeElement.querySelector('iframe');
if (iframe && iframe.contentWindow) {
iframe.src = 'about:blank';
iframe.remove();
}
}
ngAfterViewInit() {
if (isPlatformBrowser(this.platform)) {
this.push();
}
}
push() {
const p = {};
if (this.pageLevelAds) {
p.google_ad_client = this.adClient;
p.enable_page_level_ads = true;
}
if (window) {
try {
(window.adsbygoogle = window.adsbygoogle || []).push(p);
}
catch {
// pass
}
}
}
static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "16.1.6", ngImport: i0, type: AswGoogleAdsense, deps: [{ token: ASW_GOOGLE_ADSENSE_TOKEN }, { token: PLATFORM_ID }], target: i0.ɵɵFactoryTarget.Component });
static ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "14.0.0", version: "16.1.6", type: AswGoogleAdsense, selector: "asw-google-adsense", inputs: { adClient: "adClient", adSlot: "adSlot", adFormat: "adFormat", adRegion: "adRegion", display: "display", width: "width", height: "height", layout: "layout", layoutKey: "layoutKey", pageLevelAds: "pageLevelAds", adtest: "adtest", fullWidthResponsive: "fullWidthResponsive", className: "className" }, viewQueries: [{ propertyName: "ins", first: true, predicate: ["ins"], descendants: true, read: ElementRef, static: true }], ngImport: i0, template: `
<ins
#ins
class="adsbygoogle {{ className }}"
[style.display]="display"
[style.width.px]="width"
[style.height.px]="height"
[attr.data-ad-client]="adClient"
[attr.data-ad-slot]="adSlot"
[attr.data-ad-format]="adFormat"
[attr.data-ad-region]="adRegion"
[attr.data-layout]="layout"
[attr.data-adtest]="adtest"
[attr.data-layout-key]="layoutKey"
[attr.data-full-width-responsive]="fullWidthResponsive"
>
</ins>
`, isInline: true, changeDetection: i0.ChangeDetectionStrategy.OnPush });
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "16.1.6", ngImport: i0, type: AswGoogleAdsense, decorators: [{
type: Component,
args: [{
selector: 'asw-google-adsense',
template: `
<ins
#ins
class="adsbygoogle {{ className }}"
[style.display]="display"
[style.width.px]="width"
[style.height.px]="height"
[attr.data-ad-client]="adClient"
[attr.data-ad-slot]="adSlot"
[attr.data-ad-format]="adFormat"
[attr.data-ad-region]="adRegion"
[attr.data-layout]="layout"
[attr.data-adtest]="adtest"
[attr.data-layout-key]="layoutKey"
[attr.data-full-width-responsive]="fullWidthResponsive"
>
</ins>
`,
changeDetection: ChangeDetectionStrategy.OnPush,
}]
}], ctorParameters: function () { return [{ type: undefined, decorators: [{
type: Inject,
args: [ASW_GOOGLE_ADSENSE_TOKEN]
}] }, { type: undefined, decorators: [{
type: Inject,
args: [PLATFORM_ID]
}] }]; }, propDecorators: { adClient: [{
type: Input
}], adSlot: [{
type: Input
}], adFormat: [{
type: Input
}], adRegion: [{
type: Input
}], display: [{
type: Input
}], width: [{
type: Input
}], height: [{
type: Input
}], layout: [{
type: Input
}], layoutKey: [{
type: Input
}], pageLevelAds: [{
type: Input
}], adtest: [{
type: Input
}], fullWidthResponsive: [{
type: Input
}], className: [{
type: Input
}], ins: [{
type: ViewChild,
args: ['ins', { read: ElementRef, static: true }]
}] } });
/**
* @license
* Copyright ASW (A Software World) All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file
*/
class AswGoogleAdsenseModule {
static forRoot(config = {}) {
return {
ngModule: AswGoogleAdsenseModule,
providers: [{ provide: ASW_GOOGLE_ADSENSE_TOKEN, useValue: config }],
};
}
static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "16.1.6", ngImport: i0, type: AswGoogleAdsenseModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule });
static ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "16.1.6", ngImport: i0, type: AswGoogleAdsenseModule, declarations: [AswGoogleAdsense], imports: [CommonModule], exports: [AswGoogleAdsense] });
static ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "16.1.6", ngImport: i0, type: AswGoogleAdsenseModule, imports: [CommonModule] });
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "16.1.6", ngImport: i0, type: AswGoogleAdsenseModule, decorators: [{
type: NgModule,
args: [{
imports: [CommonModule],
exports: [AswGoogleAdsense],
declarations: [AswGoogleAdsense],
}]
}] });
/**
* @license
* Copyright ASW (A Software World) All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file
*/
/**
* Generated bundle index. Do not edit.
*/
export { ASW_GOOGLE_ADSENSE_TOKEN, AswGoogleAdsense, AswGoogleAdsenseModule };
//# sourceMappingURL=asoftwareworld-google-adsense.mjs.map