-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ad-block): introduce structural directives (#223)
- Loading branch information
1 parent
daa8d8a
commit 455e91f
Showing
6 changed files
with
179 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
projects/flosportsinc/ng-ad-block/common/ad-block.directive.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import { Component, NgModule } from '@angular/core' | ||
import { TestBed } from '@angular/core/testing' | ||
import { By } from '@angular/platform-browser' | ||
import { of } from 'rxjs' | ||
import { FloAdBlockModule } from './ad-block.module' | ||
import { FloIfAdBlockedDirective } from './ad-block.directive' | ||
import { AD_BLOCK_LOADER } from './ad-block.tokens' | ||
import { AdBlockService } from './ad-block.service' | ||
|
||
@Component({ | ||
selector: 'flo-test-component', | ||
template: ` | ||
<div id="floIfAdBlocked" *floIfAdBlocked>floIfAdBlocked</div> | ||
<div id="floIfNotAdBlocked" *floIfNotAdBlocked>floIfNotAdBlocked</div> | ||
`}) | ||
export class FloTestComponent { } | ||
|
||
export const trueLoader = () => of(true) | ||
export const falseLoader = () => of(false) | ||
|
||
@NgModule({ | ||
declarations: [FloTestComponent], | ||
imports: [FloAdBlockModule], | ||
exports: [FloAdBlockModule, FloTestComponent], | ||
providers: [ | ||
AdBlockService, | ||
{ provide: AD_BLOCK_LOADER, useFactory: falseLoader } | ||
] | ||
}) | ||
export class FloFullscreenTestModule { } | ||
|
||
const createSut = () => { | ||
const sut = TestBed.createComponent(FloTestComponent) | ||
sut.detectChanges() | ||
return sut | ||
} | ||
|
||
describe(FloIfAdBlockedDirective.name, () => { | ||
beforeEach(() => { | ||
TestBed.configureTestingModule({ | ||
imports: [FloFullscreenTestModule] | ||
}).compileComponents() | ||
}) | ||
|
||
it('should compile', () => { | ||
expect(createSut()).toBeTruthy() | ||
}) | ||
|
||
it('should show floIfAdBlocked element', () => { | ||
TestBed.resetTestingModule() | ||
TestBed.configureTestingModule({ | ||
imports: [FloFullscreenTestModule], | ||
providers: [ | ||
{ provide: AD_BLOCK_LOADER, useFactory: trueLoader } | ||
] | ||
}) | ||
const sut = createSut() | ||
const element1 = sut.debugElement.query(By.css('#floIfAdBlocked')) | ||
const element2 = sut.debugElement.query(By.css('#floIfNotAdBlocked')) | ||
sut.detectChanges() | ||
expect(element1.nativeElement.innerText).toEqual('floIfAdBlocked') | ||
expect(element2).toBeNull() | ||
}) | ||
|
||
it('should show floIfNotAdBlocked element', () => { | ||
const sut = createSut() | ||
const element1 = sut.debugElement.query(By.css('#floIfAdBlocked')) | ||
const element2 = sut.debugElement.query(By.css('#floIfNotAdBlocked')) | ||
sut.detectChanges() | ||
expect(element1).toBeNull() | ||
expect(element2.nativeElement.innerText).toEqual('floIfNotAdBlocked') | ||
}) | ||
}) |
70 changes: 70 additions & 0 deletions
70
projects/flosportsinc/ng-ad-block/common/ad-block.directive.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import { Directive, TemplateRef, ViewContainerRef, ChangeDetectorRef, OnInit, OnDestroy } from '@angular/core' | ||
import { AdBlockService } from './ad-block.service' | ||
import { Subject } from 'rxjs' | ||
import { takeUntil } from 'rxjs/operators' | ||
|
||
export abstract class FloAdBlockDirective implements OnInit, OnDestroy { | ||
constructor(protected tr: TemplateRef<any>, protected vc: ViewContainerRef, | ||
protected ads: AdBlockService, protected cd: ChangeDetectorRef) { } | ||
|
||
protected readonly ngOnDestroy$ = new Subject() | ||
// tslint:disable-next-line: readonly-keyword | ||
protected showWhenAdBlocked: boolean | ||
|
||
ngOnInit() { | ||
this.ads.isAnAdBlockerActive() | ||
.pipe(takeUntil(this.ngOnDestroy$)) | ||
.subscribe(isActive => { | ||
if (isActive) { | ||
if (this.showWhenAdBlocked) { | ||
this.vc.createEmbeddedView(this.tr) | ||
} else { | ||
this.vc.clear() | ||
} | ||
} else { | ||
if (this.showWhenAdBlocked) { | ||
this.vc.clear() | ||
} else { | ||
this.vc.createEmbeddedView(this.tr) | ||
} | ||
} | ||
this.cd.detectChanges() | ||
}) | ||
} | ||
|
||
ngOnDestroy() { | ||
this.ngOnDestroy$.next() | ||
this.ngOnDestroy$.complete() | ||
} | ||
} | ||
|
||
const IF_BLOCKED_SELECTOR = 'floIfAdBlocked' | ||
const IF_NOT_BLOCKED_SELECTOR = 'floIfNotAdBlocked' | ||
|
||
@Directive({ | ||
selector: `[${IF_BLOCKED_SELECTOR}]`, | ||
inputs: [IF_BLOCKED_SELECTOR] | ||
}) | ||
export class FloIfAdBlockedDirective extends FloAdBlockDirective { | ||
constructor(protected tr: TemplateRef<any>, protected vc: ViewContainerRef, protected ads: AdBlockService, | ||
protected cd: ChangeDetectorRef) { | ||
super(tr, vc, ads, cd) | ||
this.showWhenAdBlocked = true | ||
} | ||
|
||
readonly elmInputKey = IF_BLOCKED_SELECTOR | ||
} | ||
|
||
@Directive({ | ||
selector: `[${IF_NOT_BLOCKED_SELECTOR}]`, | ||
inputs: [IF_NOT_BLOCKED_SELECTOR] | ||
}) | ||
export class FloIfNotAdBlockedDirective extends FloAdBlockDirective { | ||
constructor(protected tr: TemplateRef<any>, protected vc: ViewContainerRef, protected ads: AdBlockService, | ||
protected cd: ChangeDetectorRef) { | ||
super(tr, vc, ads, cd) | ||
this.showWhenAdBlocked = false | ||
} | ||
|
||
readonly elmInputKey = IF_NOT_BLOCKED_SELECTOR | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,10 @@ | ||
import { NgModule } from '@angular/core' | ||
import { AdBlockService } from './ad-block.service' | ||
import { FloIfAdBlockedDirective, FloIfNotAdBlockedDirective } from './ad-block.directive' | ||
|
||
@NgModule({ | ||
providers: [AdBlockService] | ||
providers: [AdBlockService], | ||
declarations: [FloIfAdBlockedDirective, FloIfNotAdBlockedDirective], | ||
exports: [FloIfAdBlockedDirective, FloIfNotAdBlockedDirective] | ||
}) | ||
export class FloAdBlockModule { } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters