@hapiness/ng-elements-loader
Version:
Service to load Angular Custom Elements inside Angular's applications
2 lines • 4.81 kB
JavaScript
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("@angular/core"),require("@angular/elements"),require("rxjs"),require("rxjs/operators")):"function"==typeof define&&define.amd?define("@hapiness/ng-elements-loader",["exports","@angular/core","@angular/elements","rxjs","rxjs/operators"],t):t((e.hapiness=e.hapiness||{},e.hapiness["ng-elements-loader"]={}),e.ng.core,e.ng.elements,e.rxjs,e.rxjs.operators)}(this,function(e,t,r,o,u){"use strict";var n=function(){function e(e,t,n){this._injector=e,this._compiler=t,this._componentFactoryResolver=n,this._elementsToLoad=new Map}return e.prototype.loadContainingCustomElements=function(e){var t=this;return o.of(e).pipe(u.flatMap(function(e){return e?o.of([].concat(e)):o.throwError(new Error("Parameter must be a `CustomElementModuleSelector` or an array of it"))}),u.flatMap(function(e){return e.length?o.of(e):o.throwError(new Error("`CustomElementModuleSelector` array must have at least one element"))}),u.flatMap(function(e){return o.from(e)}),u.filter(function(e){return!!e.selector&&!!e.module}),u.toArray(),u.tap(function(e){return e.forEach(function(e){return t._elementsToLoad.set(e.selector,e.module)})}),u.flatMap(function(){return t._customElements(!0)}))},e.prototype.registerContainingCustomElements=function(e){var t=this;return o.of(e).pipe(u.flatMap(function(e){return e?o.of([].concat(e)):o.throwError(new Error("Parameter must be a `CustomElementComponentSelector` or an array of it"))}),u.flatMap(function(e){return e.length?o.of(e):o.throwError(new Error("`CustomElementComponentSelector` array must have at least one element"))}),u.flatMap(function(e){return o.from(e)}),u.filter(function(e){return!!e.selector&&!!e.component}),u.toArray(),u.tap(function(e){return e.forEach(function(e){return t._elementsToLoad.set(e.selector,e.component)})}),u.flatMap(function(){return t._customElements(!1)}))},e.prototype._customElements=function(t){var n=this;return o.of(o.of(Array.from(this._elementsToLoad.keys()))).pipe(u.flatMap(function(e){return o.merge(e.pipe(u.filter(function(e){return!!e&&!!e.length}),u.flatMap(function(e){return o.forkJoin(e.map(function(e){return n._register(e,t)}))})),e.pipe(u.filter(function(e){return!e||!e.length}),u.map(function(e){return undefined})))}))},e.prototype._register=function(t,n){var r=this;return o.of(o.of(this._elementsToLoad.has(t))).pipe(u.flatMap(function(e){return o.merge(e.pipe(u.filter(function(e){return!!e}),u.flatMap(function(e){return o.of(o.of(customElements.get(t))).pipe(u.flatMap(function(e){return o.merge(e.pipe(u.filter(function(e){return!!e}),u.tap(function(e){return r._elementsToLoad["delete"](t)})),e.pipe(u.filter(function(e){return!e}),u.flatMap(function(e){return r._createCustomElement(t,n)})))}))})),e.pipe(u.filter(function(e){return!e}),u.map(function(e){return undefined})))}))},e.prototype._createCustomElement=function(t,e){var n=this;return o.of(o.of(e)).pipe(u.flatMap(function(e){return o.merge(e.pipe(u.filter(function(e){return!!e}),u.flatMap(function(e){return n._componentAndInjectorFromCompiledModule(n._elementsToLoad.get(t))})),e.pipe(u.filter(function(e){return!e}),u.flatMap(function(e){return n._componentAndInjectorFromComponentFactory(n._elementsToLoad.get(t))})))}),u.map(function(e){return r.createCustomElement(e.component,{injector:e.injector})}),u.tap(function(e){return customElements.define(t,e)}),u.tap(function(e){return n._elementsToLoad["delete"](t)}),u.flatMap(function(e){return o.from(customElements.whenDefined(t))}))},e.prototype._componentAndInjectorFromCompiledModule=function(e){var t=this;return o.from(this._compiler.compileModuleAndAllComponentsAsync(e)).pipe(u.map(function(e){return e.ngModuleFactory}),u.map(function(e){return e.create(t._injector)}),u.flatMap(function(e){return e.instance.customElementComponent?o.of({component:e.instance.customElementComponent,injector:e.injector}):o.throwError(new Error("WithCustomElementComponent interface must be implemented by all modules that declare a component that can be used as a custom element."))}))},e.prototype._componentAndInjectorFromComponentFactory=function(t){return o.of(this._componentFactoryResolver.resolveComponentFactory(t)).pipe(u.map(function(e){return e.ngModule}),u.map(function(e){return{component:t,injector:e.injector}}))},e.decorators=[{type:t.Injectable,args:[{providedIn:"root"}]}],e.ctorParameters=function(){return[{type:t.Injector},{type:t.Compiler},{type:t.ComponentFactoryResolver}]},e.ngInjectableDef=t.defineInjectable({factory:function(){return new e(t.inject(t.INJECTOR),t.inject(t.Compiler),t.inject(t.ComponentFactoryResolver))},token:e,providedIn:"root"}),e}();e.ElementsLoaderService=n,Object.defineProperty(e,"__esModule",{value:!0})});
//# sourceMappingURL=hapiness-ng-elements-loader.umd.min.js.map