@avine/ng-if-non-nullish
Version:
Nullish coalescing operator as Angular structural directive and more...
108 lines (104 loc) • 3.87 kB
JavaScript
import * as i0 from '@angular/core';
import { Directive, Input } from '@angular/core';
class IfNonNullishDirective {
set ifNonNullish(data) {
this.hasNoData = data === null || data === undefined;
this.updateView(data);
}
set ifNonNullishFallback(fallbackTemplate) {
this.fallbackTemplate = fallbackTemplate ?? null;
if (this.hasNoData) {
this.createFallbackView();
}
}
/**
* Assert the correct type of the expression bound to the `ifNonNullish` input within the template.
*
* @see https://angular.io/guide/structural-directives#improving-template-type-checking-for-custom-directives
*/
static ngTemplateGuard_ifNonNullish(_directive, data) {
return true;
}
/**
* Asserts the correct type of the context for the template that `IfNonNullish` will render.
*
* @see https://angular.io/guide/structural-directives#improving-template-type-checking-for-custom-directives
*/
static ngTemplateContextGuard(_directive, context) {
return true;
}
constructor(viewContainerRef, templateRef) {
this.viewContainerRef = viewContainerRef;
this.templateRef = templateRef;
this.hasNoData = true;
this.fallbackTemplate = null;
this.viewState = 'clear';
}
updateView(data) {
if (!this.hasNoData) {
this.switchToRegularView(data);
}
else {
this.switchToFallbackView();
}
}
switchToRegularView(data) {
this.upsertContext(data);
if (this.viewState === 'regular') {
return;
}
this.createRegularView();
}
switchToFallbackView() {
if (this.viewState === 'fallback') {
return;
}
this.createFallbackView();
}
upsertContext(data) {
if (this.context) {
this.context.$implicit = this.context.ifNonNullish = data;
}
else {
this.context = { $implicit: data, ifNonNullish: data };
}
}
createRegularView() {
this.clearView();
this.viewContainerRef.createEmbeddedView(this.templateRef, this.context);
this.viewState = 'regular';
}
createFallbackView() {
this.clearView();
if (!this.fallbackTemplate) {
return;
}
this.viewContainerRef.createEmbeddedView(this.fallbackTemplate);
this.viewState = 'fallback';
}
clearView() {
if (this.viewState === 'clear') {
return;
}
this.viewContainerRef.clear();
this.viewState = 'clear';
}
static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "16.2.5", ngImport: i0, type: IfNonNullishDirective, deps: [{ token: i0.ViewContainerRef }, { token: i0.TemplateRef }], target: i0.ɵɵFactoryTarget.Directive }); }
static { this.ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "16.2.5", type: IfNonNullishDirective, isStandalone: true, selector: "[ifNonNullish]", inputs: { ifNonNullish: "ifNonNullish", ifNonNullishFallback: "ifNonNullishFallback" }, ngImport: i0 }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "16.2.5", ngImport: i0, type: IfNonNullishDirective, decorators: [{
type: Directive,
args: [{
standalone: true,
selector: '[ifNonNullish]',
}]
}], ctorParameters: function () { return [{ type: i0.ViewContainerRef }, { type: i0.TemplateRef }]; }, propDecorators: { ifNonNullish: [{
type: Input
}], ifNonNullishFallback: [{
type: Input
}] } });
/**
* Generated bundle index. Do not edit.
*/
export { IfNonNullishDirective };
//# sourceMappingURL=avine-ng-if-non-nullish.mjs.map