UNPKG

ngx-context-store

Version:

An alternative yet advanced mechanism for Angular components to share data.

191 lines (180 loc) 11.2 kB
(function (global, factory) { typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@angular/core'), require('rxjs')) : typeof define === 'function' && define.amd ? define('ngx-context-store', ['exports', '@angular/core', 'rxjs'], factory) : (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global["ngx-context-store"] = {}, global.ng.core, global.rxjs)); })(this, (function (exports, i0, rxjs) { 'use strict'; function _interopNamespace(e) { if (e && e.__esModule) return e; var n = Object.create(null); if (e) { Object.keys(e).forEach(function (k) { if (k !== 'default') { var d = Object.getOwnPropertyDescriptor(e, k); Object.defineProperty(n, k, d.get ? d : { enumerable: true, get: function () { return e[k]; } }); } }); } n["default"] = e; return Object.freeze(n); } var i0__namespace = /*#__PURE__*/_interopNamespace(i0); var NgxContextStoreSetDirective = /** @class */ (function () { function NgxContextStoreSetDirective(templateRef, viewContainer, parentContextHolder) { var _this = this; var _a; this.templateRef = templateRef; this.viewContainer = viewContainer; this.parentContextHolder = parentContextHolder; this.context$ = new rxjs.BehaviorSubject({}); this._parentSubsciption = (_a = this.parentContextHolder) === null || _a === void 0 ? void 0 : _a.context$.subscribe(function (ctx) { return _this.updateContext(); }); } NgxContextStoreSetDirective.prototype.ngOnInit = function () { // structural and attribute directive compatibility if (this.templateRef && this.viewContainer) { this._view = this.viewContainer.createEmbeddedView(this.templateRef); } }; NgxContextStoreSetDirective.prototype.ngOnChanges = function (changes) { var _f; 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)) { var 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'; var 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), (_f = {}, _f[setContextUnder] = setContextValue, _f)); } 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(); }; NgxContextStoreSetDirective.prototype.ngOnDestroy = function () { if (this._view) { this.viewContainer.clear(); this._view = undefined; } if (this._parentSubsciption) { this._parentSubsciption.unsubscribe(); } this.context$.complete(); }; NgxContextStoreSetDirective.prototype.updateContext = function () { var _a, _b; var 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)); }; return NgxContextStoreSetDirective; }()); NgxContextStoreSetDirective.ɵfac = i0__namespace.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "12.2.10", ngImport: i0__namespace, type: NgxContextStoreSetDirective, deps: [{ token: i0__namespace.TemplateRef, optional: true }, { token: i0__namespace.ViewContainerRef, optional: true }, { token: i0.forwardRef(function () { return NgxContextStoreSetDirective; }), optional: true, skipSelf: true }], target: i0__namespace.ɵɵFactoryTarget.Directive }); NgxContextStoreSetDirective.ɵdir = i0__namespace.ɵɵ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__namespace }); i0__namespace.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "12.2.10", ngImport: i0__namespace, type: NgxContextStoreSetDirective, decorators: [{ type: i0.Directive, args: [{ selector: '[setContext], [setContextO]', }] }], ctorParameters: function () { return [{ type: i0__namespace.TemplateRef, decorators: [{ type: i0.Optional }] }, { type: i0__namespace.ViewContainerRef, decorators: [{ type: i0.Optional }] }, { type: NgxContextStoreSetDirective, decorators: [{ type: i0.Optional }, { type: i0.SkipSelf }, { type: i0.Inject, args: [i0.forwardRef(function () { return NgxContextStoreSetDirective; })] }] }]; }, propDecorators: { setContext: [{ type: i0.Input }], setContextUnder: [{ type: i0.Input }], setContextO: [{ type: i0.Input }] } }); var NgxContextStoreGetDirective = /** @class */ (function () { function NgxContextStoreGetDirective(templateRef, viewContainer, parentContextHolder) { this.templateRef = templateRef; this.viewContainer = viewContainer; this.parentContextHolder = parentContextHolder; } NgxContextStoreGetDirective.prototype.ngOnInit = function () { var _this = this; var _a, _b, _c; var 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(function (ctx) { return _this.updateContext(ctx); }); if (this.templateRef && this.viewContainer) { this._view = this.viewContainer.createEmbeddedView(this.templateRef, context); } }; NgxContextStoreGetDirective.prototype.ngOnDestroy = function () { if (this._view) { this.viewContainer.clear(); this._view = undefined; } if (this._parentSubsciption) { this._parentSubsciption.unsubscribe(); } }; NgxContextStoreGetDirective.prototype.updateContext = function (context) { var _a; if (((_a = this._view) === null || _a === void 0 ? void 0 : _a.context) != null) { for (var key in context) { if (Object.prototype.hasOwnProperty.call(context, key)) { this._view.context[key] = context[key]; } } } }; return NgxContextStoreGetDirective; }()); NgxContextStoreGetDirective.ɵfac = i0__namespace.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "12.2.10", ngImport: i0__namespace, type: NgxContextStoreGetDirective, deps: [{ token: i0__namespace.TemplateRef }, { token: i0__namespace.ViewContainerRef }, { token: NgxContextStoreSetDirective, optional: true }], target: i0__namespace.ɵɵFactoryTarget.Directive }); NgxContextStoreGetDirective.ɵdir = i0__namespace.ɵɵngDeclareDirective({ minVersion: "12.0.0", version: "12.2.10", type: NgxContextStoreGetDirective, selector: "[getContext]", ngImport: i0__namespace }); i0__namespace.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "12.2.10", ngImport: i0__namespace, type: NgxContextStoreGetDirective, decorators: [{ type: i0.Directive, args: [{ selector: '[getContext]', }] }], ctorParameters: function () { return [{ type: i0__namespace.TemplateRef }, { type: i0__namespace.ViewContainerRef }, { type: NgxContextStoreSetDirective, decorators: [{ type: i0.Optional }] }]; } }); var NgxContextStoreModule = /** @class */ (function () { function NgxContextStoreModule() { } return NgxContextStoreModule; }()); NgxContextStoreModule.ɵfac = i0__namespace.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "12.2.10", ngImport: i0__namespace, type: NgxContextStoreModule, deps: [], target: i0__namespace.ɵɵFactoryTarget.NgModule }); NgxContextStoreModule.ɵmod = i0__namespace.ɵɵngDeclareNgModule({ minVersion: "12.0.0", version: "12.2.10", ngImport: i0__namespace, type: NgxContextStoreModule, declarations: [NgxContextStoreSetDirective, NgxContextStoreGetDirective], exports: [NgxContextStoreSetDirective, NgxContextStoreGetDirective] }); NgxContextStoreModule.ɵinj = i0__namespace.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "12.2.10", ngImport: i0__namespace, type: NgxContextStoreModule, imports: [[]] }); i0__namespace.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "12.2.10", ngImport: i0__namespace, type: NgxContextStoreModule, decorators: [{ type: i0.NgModule, args: [{ declarations: [ NgxContextStoreSetDirective, NgxContextStoreGetDirective, ], imports: [], exports: [ NgxContextStoreSetDirective, NgxContextStoreGetDirective, ] }] }] }); /* * Public API Surface of ngx-context-store */ /** * Generated bundle index. Do not edit. */ exports.NgxContextStoreGetDirective = NgxContextStoreGetDirective; exports.NgxContextStoreModule = NgxContextStoreModule; exports.NgxContextStoreSetDirective = NgxContextStoreSetDirective; Object.defineProperty(exports, '__esModule', { value: true }); })); //# sourceMappingURL=ngx-context-store.umd.js.map