ngx-context-store
Version:
An alternative yet advanced mechanism for Angular components to share data.
151 lines (144 loc) • 8.18 kB
JavaScript
import * as i0 from '@angular/core';
import { forwardRef, Directive, Optional, SkipSelf, Inject, Input, NgModule } from '@angular/core';
import { BehaviorSubject } from 'rxjs';
class NgxContextStoreSetDirective {
constructor(templateRef, viewContainer, parentContextHolder) {
var _a;
this.templateRef = templateRef;
this.viewContainer = viewContainer;
this.parentContextHolder = parentContextHolder;
this.context$ = new BehaviorSubject({});
this._parentSubsciption = (_a = this.parentContextHolder) === null || _a === void 0 ? void 0 : _a.context$.subscribe(ctx => this.updateContext());
}
ngOnInit() {
// structural and attribute directive compatibility
if (this.templateRef && this.viewContainer) {
this._view = this.viewContainer.createEmbeddedView(this.templateRef);
}
}
ngOnChanges(changes) {
var _a, _b, _c, _d, _e;
if ((changes === null || changes === void 0 ? void 0 : changes.setContext) || (changes === null || changes === void 0 ? void 0 : changes.setContextUnder)) {
const setContextUnder = (_c = (_b = (_a = changes === null || changes === void 0 ? void 0 : changes.setContextUnder) === null || _a === void 0 ? void 0 : _a.currentValue) !== null && _b !== void 0 ? _b : this.setContextUnder) !== null && _c !== void 0 ? _c : '$implicit';
const setContextValue = (_e = (_d = changes === null || changes === void 0 ? void 0 : changes.setContext) === null || _d === void 0 ? void 0 : _d.currentValue) !== null && _e !== void 0 ? _e : this.setContext;
this._localContext = Object.assign(Object.assign({}, this._localContext), { [setContextUnder]: setContextValue });
}
else if (changes === null || changes === void 0 ? void 0 : changes.setContextO) {
this._localContext = Object.assign({}, changes === null || changes === void 0 ? void 0 : changes.setContextO.currentValue);
}
this.updateContext();
}
ngOnDestroy() {
if (this._view) {
this.viewContainer.clear();
this._view = undefined;
}
if (this._parentSubsciption) {
this._parentSubsciption.unsubscribe();
}
this.context$.complete();
}
updateContext() {
var _a, _b;
const parentContext = (_b = (_a = this.parentContextHolder) === null || _a === void 0 ? void 0 : _a.context$.value) !== null && _b !== void 0 ? _b : undefined;
this.context$.next(Object.assign(Object.assign({}, parentContext), this._localContext));
}
}
NgxContextStoreSetDirective.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "12.2.10", ngImport: i0, type: NgxContextStoreSetDirective, deps: [{ token: i0.TemplateRef, optional: true }, { token: i0.ViewContainerRef, optional: true }, { token: forwardRef(() => NgxContextStoreSetDirective), optional: true, skipSelf: true }], target: i0.ɵɵFactoryTarget.Directive });
NgxContextStoreSetDirective.ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "12.0.0", version: "12.2.10", type: NgxContextStoreSetDirective, selector: "[setContext], [setContextO]", inputs: { setContext: "setContext", setContextUnder: "setContextUnder", setContextO: "setContextO" }, usesOnChanges: true, ngImport: i0 });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "12.2.10", ngImport: i0, type: NgxContextStoreSetDirective, decorators: [{
type: Directive,
args: [{
selector: '[setContext], [setContextO]',
}]
}], ctorParameters: function () { return [{ type: i0.TemplateRef, decorators: [{
type: Optional
}] }, { type: i0.ViewContainerRef, decorators: [{
type: Optional
}] }, { type: NgxContextStoreSetDirective, decorators: [{
type: Optional
}, {
type: SkipSelf
}, {
type: Inject,
args: [forwardRef(() => NgxContextStoreSetDirective)]
}] }]; }, propDecorators: { setContext: [{
type: Input
}], setContextUnder: [{
type: Input
}], setContextO: [{
type: Input
}] } });
class NgxContextStoreGetDirective {
constructor(templateRef, viewContainer, parentContextHolder) {
this.templateRef = templateRef;
this.viewContainer = viewContainer;
this.parentContextHolder = parentContextHolder;
}
ngOnInit() {
var _a, _b, _c;
const context = (_b = (_a = this.parentContextHolder) === null || _a === void 0 ? void 0 : _a.context$.value) !== null && _b !== void 0 ? _b : undefined;
this._parentSubsciption = (_c = this.parentContextHolder) === null || _c === void 0 ? void 0 : _c.context$.subscribe(ctx => this.updateContext(ctx));
if (this.templateRef && this.viewContainer) {
this._view = this.viewContainer.createEmbeddedView(this.templateRef, context);
}
}
ngOnDestroy() {
if (this._view) {
this.viewContainer.clear();
this._view = undefined;
}
if (this._parentSubsciption) {
this._parentSubsciption.unsubscribe();
}
}
updateContext(context) {
var _a;
if (((_a = this._view) === null || _a === void 0 ? void 0 : _a.context) != null) {
for (const key in context) {
if (Object.prototype.hasOwnProperty.call(context, key)) {
this._view.context[key] = context[key];
}
}
}
}
}
NgxContextStoreGetDirective.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "12.2.10", ngImport: i0, type: NgxContextStoreGetDirective, deps: [{ token: i0.TemplateRef }, { token: i0.ViewContainerRef }, { token: NgxContextStoreSetDirective, optional: true }], target: i0.ɵɵFactoryTarget.Directive });
NgxContextStoreGetDirective.ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "12.0.0", version: "12.2.10", type: NgxContextStoreGetDirective, selector: "[getContext]", ngImport: i0 });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "12.2.10", ngImport: i0, type: NgxContextStoreGetDirective, decorators: [{
type: Directive,
args: [{
selector: '[getContext]',
}]
}], ctorParameters: function () { return [{ type: i0.TemplateRef }, { type: i0.ViewContainerRef }, { type: NgxContextStoreSetDirective, decorators: [{
type: Optional
}] }]; } });
class NgxContextStoreModule {
}
NgxContextStoreModule.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "12.2.10", ngImport: i0, type: NgxContextStoreModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule });
NgxContextStoreModule.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "12.0.0", version: "12.2.10", ngImport: i0, type: NgxContextStoreModule, declarations: [NgxContextStoreSetDirective,
NgxContextStoreGetDirective], exports: [NgxContextStoreSetDirective,
NgxContextStoreGetDirective] });
NgxContextStoreModule.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "12.2.10", ngImport: i0, type: NgxContextStoreModule, imports: [[]] });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "12.2.10", ngImport: i0, type: NgxContextStoreModule, decorators: [{
type: NgModule,
args: [{
declarations: [
NgxContextStoreSetDirective,
NgxContextStoreGetDirective,
],
imports: [],
exports: [
NgxContextStoreSetDirective,
NgxContextStoreGetDirective,
]
}]
}] });
/*
* Public API Surface of ngx-context-store
*/
/**
* Generated bundle index. Do not edit.
*/
export { NgxContextStoreGetDirective, NgxContextStoreModule, NgxContextStoreSetDirective };
//# sourceMappingURL=ngx-context-store.js.map