Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
311ae55
commit 92c9901
Showing
5 changed files
with
85 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { ChangeDetectorRef, Directive, inject, OnChanges, TemplateRef, ViewContainerRef } from '@angular/core'; | ||
import { LoadingService, OnDestroy$ } from '@cognizone/ng-core'; | ||
import { distinctUntilChanged, map } from 'rxjs'; | ||
|
||
@Directive({ | ||
standalone: true, | ||
}) | ||
export abstract class AbstractLoadDirective extends OnDestroy$ implements OnChanges { | ||
abstract loadingKey?: string; | ||
abstract else?: TemplateRef<unknown>; | ||
protected abstract type: 'loaded' | 'loading'; | ||
|
||
private templateRef: TemplateRef<unknown> = inject(TemplateRef); | ||
private cdr: ChangeDetectorRef = inject(ChangeDetectorRef); | ||
private loadingService: LoadingService = inject(LoadingService); | ||
private vcr: ViewContainerRef = inject(ViewContainerRef); | ||
|
||
ngOnChanges(): void { | ||
this.emptySink(); | ||
|
||
this.subSink = this.loadingService | ||
.getLoading(this.loadingKey) | ||
.pipe( | ||
distinctUntilChanged(), | ||
map(loading => (this.type === 'loading' ? loading : !loading)) | ||
) | ||
.subscribe(loadFlag => { | ||
this.vcr.clear(); | ||
if (!loadFlag && this.else) this.vcr.createEmbeddedView(this.else); | ||
else if (loadFlag) this.vcr.createEmbeddedView(this.templateRef); | ||
this.cdr.markForCheck(); | ||
}); | ||
} | ||
} |
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,22 @@ | ||
import { Directive, Input, TemplateRef } from '@angular/core'; | ||
|
||
import { AbstractLoadDirective } from './abstract-load.directive'; | ||
|
||
/** | ||
* @example ```html | ||
* <div *czIfLoaded="'my-key'">I'm visible if my-key is not loading in LoadingService</div> | ||
* <div *czIfLoaded>I'm visible if the key used by the provided LoadingService is not loading</div> | ||
* ``` | ||
*/ | ||
@Directive({ | ||
selector: '[czIfLoaded]', | ||
standalone: true, | ||
}) | ||
export class IfLoadedDirective extends AbstractLoadDirective { | ||
@Input('czIfLoaded') | ||
loadingKey?: string; | ||
@Input('czIfLoadedElse') | ||
else?: TemplateRef<unknown>; | ||
|
||
protected type: 'loaded' | 'loading' = 'loaded'; | ||
} |
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,22 @@ | ||
import { Directive, Input, TemplateRef } from '@angular/core'; | ||
|
||
import { AbstractLoadDirective } from './abstract-load.directive'; | ||
|
||
/** | ||
* @example ```html | ||
* <div *czIfLoading="'my-key'">I'm visible if my-key is loading in LoadingService</div> | ||
* <div *czIfLoading>I'm visible if the key used by the provided LoadingService is loading</div> | ||
* ``` | ||
*/ | ||
@Directive({ | ||
selector: '[czIfLoading]', | ||
standalone: true, | ||
}) | ||
export class IfLoadingDirective extends AbstractLoadDirective { | ||
@Input('czIfLoading') | ||
loadingKey?: string; | ||
@Input('czIfLoadingElse') | ||
else?: TemplateRef<unknown>; | ||
|
||
protected type: 'loaded' | 'loading' = 'loading'; | ||
} |
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,2 @@ | ||
export * from './if-loaded.directive'; | ||
export * from './if-loading.directive'; |
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,5 @@ | ||
{ | ||
"lib": { | ||
"entryFile": "index.ts" | ||
} | ||
} |