From 668cfb49da128d278bc20ae8b45feaa65cdb4d8b Mon Sep 17 00:00:00 2001 From: vparomskiy Date: Thu, 13 May 2021 16:56:44 +0300 Subject: [PATCH] add HttpCleint and DrugDrop accessible for widget extensions avoid stackoverflow when importing external module --- .../app/core/services/resources.service.ts | 35 +++++++++++++++---- ui-ngx/src/app/modules/common/modules-map.ts | 4 +++ 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/ui-ngx/src/app/core/services/resources.service.ts b/ui-ngx/src/app/core/services/resources.service.ts index 3babd82a28..e87f16d885 100644 --- a/ui-ngx/src/app/core/services/resources.service.ts +++ b/ui-ngx/src/app/core/services/resources.service.ts @@ -134,6 +134,7 @@ export class ResourcesService { try { modules = this.extractNgModules(module); } catch (e) { + console.error(e); } if (modules && modules.length) { import('@angular/compiler').then( @@ -172,22 +173,44 @@ export class ResourcesService { (e) => { this.loadedModules[url].error(new Error(`Unable to load module from url: ${url}`)); delete this.loadedModules[url]; + console.error(`Unable to load module from url: ${url}`, e); } ); return subject.asObservable(); } - private extractNgModules(module: any, modules: Type[] = [] ): Type[] { - if (module && 'ɵmod' in module) { - modules.push(module); - } else { - for (const k of Object.keys(module)) { - this.extractNgModules(module[k], modules); + private extractNgModules(module: any, modules: Type[] = []): Type[] { + try { + let potentialModules = [module]; + let currentScanDepth = 0; + + while(potentialModules.length && currentScanDepth < 10) { + let newPotentialModules = []; + for (const module of potentialModules) { + if (module && 'ɵmod' in module) { + modules.push(module); + } else { + for (const k of Object.keys(module)) { + if(!this.isPrimitive(module[k])) { + newPotentialModules.push(module[k]); + } + } + } + } + + potentialModules = newPotentialModules; + currentScanDepth++; } + } catch(e) { + console.log('Could not load NgModule', e); } return modules; } + private isPrimitive(test) { + return test !== Object(test); + } + private loadResourceByType(type: 'css' | 'js', url: string): Observable { const subject = new ReplaySubject(); this.loadedResources[url] = subject; diff --git a/ui-ngx/src/app/modules/common/modules-map.ts b/ui-ngx/src/app/modules/common/modules-map.ts index c608f7dbcc..dcb4289934 100644 --- a/ui-ngx/src/app/modules/common/modules-map.ts +++ b/ui-ngx/src/app/modules/common/modules-map.ts @@ -70,6 +70,8 @@ import * as TbCore from '@core/public-api'; import * as TbShared from '@shared/public-api'; import * as TbHomeComponents from '@home/components/public-api'; import * as _moment from 'moment'; +import * as DragDropModule from "@angular/cdk/drag-drop"; +import * as HttpClientModule from "@angular/common/http"; declare const SystemJS; @@ -77,6 +79,7 @@ export const modulesMap: {[key: string]: any} = { '@angular/animations': SystemJS.newModule(AngularAnimations), '@angular/core': SystemJS.newModule(AngularCore), '@angular/common': SystemJS.newModule(AngularCommon), + '@angular/common/http': SystemJS.newModule(HttpClientModule), '@angular/forms': SystemJS.newModule(AngularForms), '@angular/flex-layout': SystemJS.newModule(AngularFlexLayout), '@angular/platform-browser': SystemJS.newModule(AngularPlatformBrowser), @@ -87,6 +90,7 @@ export const modulesMap: {[key: string]: any} = { '@angular/cdk/layout': SystemJS.newModule(AngularCdkLayout), '@angular/cdk/overlay': SystemJS.newModule(AngularCdkOverlay), '@angular/cdk/portal': SystemJS.newModule(AngularCdkPortal), + '@angular/cdk/drag-drop': SystemJS.newModule(DragDropModule), '@angular/material/autocomplete': SystemJS.newModule(AngularMaterialAutocomplete), '@angular/material/badge': SystemJS.newModule(AngularMaterialBadge), '@angular/material/bottom-sheet': SystemJS.newModule(AngularMaterialBottomSheet),