@worktile/planet 12.1.0 → 12.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.all-contributorsrc +84 -0
- package/.circleci/config.yml +17 -0
- package/.coveralls.yml +1 -0
- package/.docgeni/public/assets/favicon.ico +0 -0
- package/.docgeni/public/index.html +13 -0
- package/.docgenirc.js +35 -0
- package/.dockerignore +1 -0
- package/.editorconfig +22 -0
- package/.github/FUNDING.yml +12 -0
- package/.github/ISSUE_TEMPLATE/bug_report.md +38 -0
- package/.github/ISSUE_TEMPLATE/feature_request.md +20 -0
- package/.prettierignore +7 -0
- package/.prettierrc +28 -0
- package/.travis.yml +26 -0
- package/.wpmrc.js +11 -0
- package/CHANGELOG.md +391 -0
- package/CODE_OF_CONDUCT.md +76 -0
- package/Dockerfile +11 -0
- package/LICENSE +21 -0
- package/README.md +348 -0
- package/README.zh-CN.md +307 -0
- package/SECURITY.md +21 -0
- package/angular.json +373 -0
- package/commitlint.config.js +6 -0
- package/docs/api/defineApplication.md +46 -0
- package/docs/api/globalEventDispatcher.md +51 -0
- package/docs/api/index.md +5 -0
- package/docs/api/planet.md +117 -0
- package/docs/guides/getting-started.md +174 -0
- package/docs/guides/index.md +5 -0
- package/docs/guides/intro.md +216 -0
- package/examples/app1/browserslist +11 -0
- package/examples/app1/extra-webpack.config.js +33 -0
- package/examples/app1/karma.conf.js +31 -0
- package/examples/app1/postcss.config.js +1 -0
- package/examples/app1/src/app/app.module.ts +44 -0
- package/examples/app1/src/app/app.routing.ts +49 -0
- package/examples/app1/src/app/counter.service.ts +16 -0
- package/examples/app1/src/app/dashboard/dashboard.component.html +51 -0
- package/examples/app1/src/app/dashboard/dashboard.component.ts +70 -0
- package/examples/app1/src/app/detail/detail.component.html +8 -0
- package/examples/app1/src/app/detail/detail.component.ts +18 -0
- package/examples/app1/src/app/overlay.ts +26 -0
- package/examples/app1/src/app/projects/dialog/projects-dialog.component.html +10 -0
- package/examples/app1/src/app/projects/dialog/projects-dialog.component.ts +41 -0
- package/examples/app1/src/app/projects/projects.component.ts +33 -0
- package/examples/app1/src/app/root/root.component.css +3 -0
- package/examples/app1/src/app/root/root.component.html +16 -0
- package/examples/app1/src/app/root/root.component.ts +35 -0
- package/examples/app1/src/app/services/initialized-data.resolver.ts +15 -0
- package/examples/app1/src/app/shared.module.ts +57 -0
- package/examples/app1/src/app/user/detail/user-detail.component.html +5 -0
- package/examples/app1/src/app/user/detail/user-detail.component.ts +20 -0
- package/examples/app1/src/app/user/user-list.component.html +7 -0
- package/examples/app1/src/app/user/user-list.component.ts +20 -0
- package/examples/app1/src/app/user/user.module.ts +31 -0
- package/examples/app1/src/assets/.gitkeep +0 -0
- package/examples/app1/src/assets/github.png +0 -0
- package/examples/app1/src/assets/main.css +3 -0
- package/examples/app1/src/environments/environment.prod.ts +3 -0
- package/examples/app1/src/environments/environment.ts +16 -0
- package/examples/app1/src/favicon.ico +0 -0
- package/examples/app1/src/index.html +14 -0
- package/examples/app1/src/main.ts +35 -0
- package/examples/app1/src/polyfills.ts +84 -0
- package/examples/app1/src/styles.scss +35 -0
- package/examples/app1/src/test.ts +14 -0
- package/examples/app1/temp.js +221 -0
- package/examples/app1/tsconfig.app.json +10 -0
- package/examples/app1/tsconfig.spec.json +18 -0
- package/examples/app1/tslint.json +17 -0
- package/examples/app1/webpack.config.js +127 -0
- package/examples/app2/browserslist +11 -0
- package/examples/app2/extra-webpack.config.js +32 -0
- package/examples/app2/karma.conf.js +31 -0
- package/examples/app2/src/app/app.module.ts +90 -0
- package/examples/app2/src/app/dashboard/dashboard.component.html +21 -0
- package/examples/app2/src/app/dashboard/dashboard.component.ts +27 -0
- package/examples/app2/src/app/overlay.ts +26 -0
- package/examples/app2/src/app/projects/detail/detail.component.html +16 -0
- package/examples/app2/src/app/projects/detail/detail.component.ts +21 -0
- package/examples/app2/src/app/projects/project-list.component.html +29 -0
- package/examples/app2/src/app/projects/project-list.component.ts +58 -0
- package/examples/app2/src/app/projects/project.resolver.ts +15 -0
- package/examples/app2/src/app/projects/projects.service.ts +28 -0
- package/examples/app2/src/app/projects/tasks/tasks.component.html +10 -0
- package/examples/app2/src/app/projects/tasks/tasks.component.ts +16 -0
- package/examples/app2/src/app/projects/view/view.component.html +1 -0
- package/examples/app2/src/app/projects/view/view.component.ts +20 -0
- package/examples/app2/src/app/root/root.component.html +14 -0
- package/examples/app2/src/app/root/root.component.scss +12 -0
- package/examples/app2/src/app/root/root.component.ts +28 -0
- package/examples/app2/src/app/shared.module.ts +59 -0
- package/examples/app2/src/assets/.gitkeep +0 -0
- package/examples/app2/src/environments/environment.prod.ts +3 -0
- package/examples/app2/src/environments/environment.ts +16 -0
- package/examples/app2/src/favicon.ico +0 -0
- package/examples/app2/src/index.html +14 -0
- package/examples/app2/src/main.ts +36 -0
- package/examples/app2/src/polyfills.ts +84 -0
- package/examples/app2/src/styles.scss +28 -0
- package/examples/app2/src/test.ts +14 -0
- package/examples/app2/tsconfig.app.json +10 -0
- package/examples/app2/tsconfig.spec.json +18 -0
- package/examples/app2/tslint.json +7 -0
- package/examples/app2/webpack.config.js +92 -0
- package/examples/common/app-root-context.ts +10 -0
- package/examples/common/cache.ts +82 -0
- package/examples/common/index.ts +13 -0
- package/examples/common/module.ts +10 -0
- package/examples/common/overlay-container.service.ts +28 -0
- package/examples/common/section-card/section-card.component.html +5 -0
- package/examples/common/section-card/section-card.component.scss +15 -0
- package/examples/common/section-card/section-card.component.ts +11 -0
- package/examples/common/utils.ts +0 -0
- package/examples/portal/src/app/a-detail/a-detail.component.html +8 -0
- package/examples/portal/src/app/a-detail/a-detail.component.ts +18 -0
- package/examples/portal/src/app/about/about.component.html +27 -0
- package/examples/portal/src/app/about/about.component.scss +0 -0
- package/examples/portal/src/app/about/about.component.spec.ts +26 -0
- package/examples/portal/src/app/about/about.component.ts +26 -0
- package/examples/portal/src/app/app-routing.module.ts +57 -0
- package/examples/portal/src/app/app.component.html +73 -0
- package/examples/portal/src/app/app.component.scss +58 -0
- package/examples/portal/src/app/app.component.spec.ts +33 -0
- package/examples/portal/src/app/app.component.ts +111 -0
- package/examples/portal/src/app/app.module.ts +64 -0
- package/examples/portal/src/app/custom-settings.service.ts +44 -0
- package/examples/portal/src/app/overlay.ts +26 -0
- package/examples/portal/src/app/settings/settings.component.html +31 -0
- package/examples/portal/src/app/settings/settings.component.scss +3 -0
- package/examples/portal/src/app/settings/settings.component.ts +43 -0
- package/examples/portal/src/assets/.gitkeep +0 -0
- package/examples/portal/src/assets/apps.json +29 -0
- package/examples/portal/src/assets/icons/sprite.defs.svg +1 -0
- package/examples/portal/src/assets/images/logo.svg +16 -0
- package/examples/portal/src/assets/ngx-planet-micro-front-end.gif +0 -0
- package/examples/portal/src/browserslist +11 -0
- package/examples/portal/src/environments/environment.prod.ts +3 -0
- package/examples/portal/src/environments/environment.ts +16 -0
- package/examples/portal/src/extra-webpack.config.js +32 -0
- package/examples/portal/src/favicon.ico +0 -0
- package/examples/portal/src/index.html +14 -0
- package/examples/portal/src/karma.conf.js +31 -0
- package/examples/portal/src/main.ts +18 -0
- package/examples/portal/src/polyfills.ts +84 -0
- package/examples/portal/src/reboot.scss +14 -0
- package/examples/portal/src/styles.scss +14 -0
- package/examples/portal/src/test.ts +14 -0
- package/examples/portal/src/tsconfig.app.json +10 -0
- package/examples/portal/src/tsconfig.spec.json +9 -0
- package/examples/portal/src/tslint.json +7 -0
- package/extra-webpack.config.js +5 -0
- package/nginx.conf +17 -0
- package/package.json +121 -16
- package/packages/planet/karma.conf.js +37 -0
- package/packages/planet/ng-package.json +7 -0
- package/packages/planet/package.json +10 -0
- package/packages/planet/src/application/planet-application-loader.spec.ts +1102 -0
- package/packages/planet/src/application/planet-application-loader.ts +549 -0
- package/packages/planet/src/application/planet-application-ref.spec.ts +233 -0
- package/packages/planet/src/application/planet-application-ref.ts +131 -0
- package/packages/planet/src/application/planet-application.service.spec.ts +145 -0
- package/packages/planet/src/application/planet-application.service.ts +88 -0
- package/packages/planet/src/application/portal-application.spec.ts +53 -0
- package/packages/planet/src/application/portal-application.ts +28 -0
- package/packages/planet/src/assets-loader.spec.ts +689 -0
- package/packages/planet/src/assets-loader.ts +247 -0
- package/packages/planet/src/component/planet-component-loader.spec.ts +187 -0
- package/packages/planet/src/component/planet-component-loader.ts +173 -0
- package/packages/planet/src/component/planet-component-ref.ts +8 -0
- package/{component/plant-component.config.d.ts → packages/planet/src/component/plant-component.config.ts} +4 -4
- package/packages/planet/src/debug.spec.ts +58 -0
- package/packages/planet/src/debug.ts +66 -0
- package/packages/planet/src/empty/empty.component.spec.ts +34 -0
- package/packages/planet/src/empty/empty.component.ts +7 -0
- package/packages/planet/src/global-event-dispatcher.spec.ts +81 -0
- package/packages/planet/src/global-event-dispatcher.ts +82 -0
- package/packages/planet/src/global-planet.spec.ts +39 -0
- package/packages/planet/src/global-planet.ts +73 -0
- package/packages/planet/src/helpers.spec.ts +242 -0
- package/packages/planet/src/helpers.ts +125 -0
- package/packages/planet/src/module.spec.ts +79 -0
- package/packages/planet/src/module.ts +25 -0
- package/{planet.class.d.ts → packages/planet/src/planet.class.ts} +26 -5
- package/packages/planet/src/planet.spec.ts +226 -0
- package/packages/planet/src/planet.ts +113 -0
- package/{public-api.d.ts → packages/planet/src/public-api.ts} +4 -1
- package/packages/planet/src/sandbox/constants.ts +6 -0
- package/packages/planet/src/sandbox/exec.ts +22 -0
- package/packages/planet/src/sandbox/index.ts +24 -0
- package/packages/planet/src/sandbox/proxy/patches/document.ts +13 -0
- package/packages/planet/src/sandbox/proxy/patches/eventListener.ts +49 -0
- package/packages/planet/src/sandbox/proxy/patches/index.ts +13 -0
- package/packages/planet/src/sandbox/proxy/patches/storage.ts +53 -0
- package/packages/planet/src/sandbox/proxy/patches/timer.ts +50 -0
- package/packages/planet/src/sandbox/proxy/proxies/common.ts +137 -0
- package/packages/planet/src/sandbox/proxy/proxies/document.ts +140 -0
- package/packages/planet/src/sandbox/proxy/proxies/window.ts +176 -0
- package/packages/planet/src/sandbox/proxy/proxy-sandbox.ts +59 -0
- package/{sandbox/proxy/types.d.ts → packages/planet/src/sandbox/proxy/types.ts} +8 -2
- package/packages/planet/src/sandbox/sandbox.ts +20 -0
- package/packages/planet/src/sandbox/snapshot/snapshot-sandbox.ts +12 -0
- package/packages/planet/src/sandbox/types.ts +1 -0
- package/packages/planet/src/test.ts +16 -0
- package/packages/planet/src/testing/app1.module.ts +34 -0
- package/packages/planet/src/testing/app2.module.ts +24 -0
- package/packages/planet/src/testing/applications.ts +58 -0
- package/packages/planet/src/testing/index.ts +2 -0
- package/packages/planet/src/testing/utils.ts +31 -0
- package/packages/planet/tsconfig.lib.json +27 -0
- package/packages/planet/tsconfig.lib.prod.json +9 -0
- package/packages/planet/tsconfig.spec.json +17 -0
- package/packages/planet/tslint.json +7 -0
- package/postcss.config.js +3 -0
- package/proxy.conf.js +15 -0
- package/tsconfig.json +23 -0
- package/tslint.json +80 -0
- package/application/planet-application-loader.d.ts +0 -70
- package/application/planet-application-loader.d.ts.map +0 -1
- package/application/planet-application-loader.ngfactory.d.ts.map +0 -1
- package/application/planet-application-ref.d.ts +0 -36
- package/application/planet-application-ref.d.ts.map +0 -1
- package/application/planet-application.service.d.ts +0 -19
- package/application/planet-application.service.d.ts.map +0 -1
- package/application/planet-application.service.ngfactory.d.ts.map +0 -1
- package/application/portal-application.d.ts +0 -15
- package/application/portal-application.d.ts.map +0 -1
- package/assets-loader.d.ts +0 -33
- package/assets-loader.d.ts.map +0 -1
- package/assets-loader.ngfactory.d.ts.map +0 -1
- package/bundles/worktile-planet.umd.js +0 -2552
- package/bundles/worktile-planet.umd.js.map +0 -1
- package/component/planet-component-loader.d.ts +0 -28
- package/component/planet-component-loader.d.ts.map +0 -1
- package/component/planet-component-loader.ngfactory.d.ts.map +0 -1
- package/component/planet-component-ref.d.ts +0 -8
- package/component/planet-component-ref.d.ts.map +0 -1
- package/component/plant-component.config.d.ts.map +0 -1
- package/debug.d.ts +0 -22
- package/debug.d.ts.map +0 -1
- package/empty/empty.component.d.ts +0 -3
- package/empty/empty.component.d.ts.map +0 -1
- package/empty/empty.component.ngfactory.d.ts.map +0 -1
- package/esm2015/application/planet-application-loader.js +0 -452
- package/esm2015/application/planet-application-ref.js +0 -94
- package/esm2015/application/planet-application.service.js +0 -87
- package/esm2015/application/portal-application.js +0 -16
- package/esm2015/assets-loader.js +0 -217
- package/esm2015/component/planet-component-loader.js +0 -155
- package/esm2015/component/planet-component-ref.js +0 -3
- package/esm2015/component/plant-component.config.js +0 -7
- package/esm2015/debug.js +0 -33
- package/esm2015/empty/empty.component.js +0 -10
- package/esm2015/global-event-dispatcher.js +0 -71
- package/esm2015/global-planet.js +0 -51
- package/esm2015/helpers.js +0 -119
- package/esm2015/module.js +0 -27
- package/esm2015/planet.class.js +0 -9
- package/esm2015/planet.js +0 -90
- package/esm2015/public-api.js +0 -18
- package/esm2015/sandbox/constants.js +0 -7
- package/esm2015/sandbox/exec.js +0 -15
- package/esm2015/sandbox/index.js +0 -20
- package/esm2015/sandbox/proxy/patches/document.js +0 -12
- package/esm2015/sandbox/proxy/patches/eventListener.js +0 -41
- package/esm2015/sandbox/proxy/patches/index.js +0 -11
- package/esm2015/sandbox/proxy/patches/storage.js +0 -40
- package/esm2015/sandbox/proxy/patches/timer.js +0 -43
- package/esm2015/sandbox/proxy/proxies/common.js +0 -132
- package/esm2015/sandbox/proxy/proxies/document.js +0 -126
- package/esm2015/sandbox/proxy/proxies/window.js +0 -155
- package/esm2015/sandbox/proxy/proxy-sandbox.js +0 -49
- package/esm2015/sandbox/proxy/types.js +0 -2
- package/esm2015/sandbox/sandbox.js +0 -7
- package/esm2015/sandbox/snapshot/snapshot-sandbox.js +0 -12
- package/esm2015/sandbox/types.js +0 -2
- package/esm2015/worktile-planet.js +0 -6
- package/fesm2015/worktile-planet.js +0 -2036
- package/fesm2015/worktile-planet.js.map +0 -1
- package/global-event-dispatcher.d.ts +0 -20
- package/global-event-dispatcher.d.ts.map +0 -1
- package/global-event-dispatcher.ngfactory.d.ts.map +0 -1
- package/global-planet.d.ts +0 -23
- package/global-planet.d.ts.map +0 -1
- package/helpers.d.ts +0 -39
- package/helpers.d.ts.map +0 -1
- package/module.d.ts +0 -6
- package/module.d.ts.map +0 -1
- package/module.ngfactory.d.ts.map +0 -1
- package/planet.class.d.ts.map +0 -1
- package/planet.d.ts +0 -25
- package/planet.d.ts.map +0 -1
- package/planet.ngfactory.d.ts.map +0 -1
- package/public-api.d.ts.map +0 -1
- package/sandbox/constants.d.ts +0 -7
- package/sandbox/constants.d.ts.map +0 -1
- package/sandbox/exec.d.ts +0 -3
- package/sandbox/exec.d.ts.map +0 -1
- package/sandbox/index.d.ts +0 -7
- package/sandbox/index.d.ts.map +0 -1
- package/sandbox/proxy/patches/document.d.ts +0 -3
- package/sandbox/proxy/patches/document.d.ts.map +0 -1
- package/sandbox/proxy/patches/eventListener.d.ts +0 -3
- package/sandbox/proxy/patches/eventListener.d.ts.map +0 -1
- package/sandbox/proxy/patches/index.d.ts +0 -3
- package/sandbox/proxy/patches/index.d.ts.map +0 -1
- package/sandbox/proxy/patches/storage.d.ts +0 -15
- package/sandbox/proxy/patches/storage.d.ts.map +0 -1
- package/sandbox/proxy/patches/timer.d.ts +0 -3
- package/sandbox/proxy/patches/timer.d.ts.map +0 -1
- package/sandbox/proxy/proxies/common.d.ts +0 -13
- package/sandbox/proxy/proxies/common.d.ts.map +0 -1
- package/sandbox/proxy/proxies/document.d.ts +0 -14
- package/sandbox/proxy/proxies/document.d.ts.map +0 -1
- package/sandbox/proxy/proxies/window.d.ts +0 -13
- package/sandbox/proxy/proxies/window.d.ts.map +0 -1
- package/sandbox/proxy/proxy-sandbox.d.ts +0 -16
- package/sandbox/proxy/proxy-sandbox.d.ts.map +0 -1
- package/sandbox/proxy/types.d.ts.map +0 -1
- package/sandbox/sandbox.d.ts +0 -12
- package/sandbox/sandbox.d.ts.map +0 -1
- package/sandbox/snapshot/snapshot-sandbox.d.ts +0 -9
- package/sandbox/snapshot/snapshot-sandbox.d.ts.map +0 -1
- package/sandbox/types.d.ts +0 -2
- package/sandbox/types.d.ts.map +0 -1
- package/worktile-planet.d.ts +0 -6
- package/worktile-planet.d.ts.map +0 -1
- package/worktile-planet.metadata.json +0 -1
|
@@ -0,0 +1,247 @@
|
|
|
1
|
+
import { Injectable } from '@angular/core';
|
|
2
|
+
import { hashCode, isEmpty, getScriptsAndStylesFullPaths } from './helpers';
|
|
3
|
+
import { of, Observable, Observer, forkJoin, concat } from 'rxjs';
|
|
4
|
+
import { map, switchMap, concatAll } from 'rxjs/operators';
|
|
5
|
+
import { HttpClient } from '@angular/common/http';
|
|
6
|
+
import { PlanetApplication } from './planet.class';
|
|
7
|
+
import { createSandbox } from './sandbox';
|
|
8
|
+
|
|
9
|
+
export interface AssetsLoadResult {
|
|
10
|
+
src: string;
|
|
11
|
+
hashCode: number;
|
|
12
|
+
loaded: boolean;
|
|
13
|
+
status: string;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
@Injectable({
|
|
17
|
+
providedIn: 'root'
|
|
18
|
+
})
|
|
19
|
+
export class AssetsLoader {
|
|
20
|
+
private loadedSources: number[] = [];
|
|
21
|
+
|
|
22
|
+
constructor(private http: HttpClient) {}
|
|
23
|
+
|
|
24
|
+
loadScript(src: string): Observable<AssetsLoadResult> {
|
|
25
|
+
const id = hashCode(src);
|
|
26
|
+
if (this.loadedSources.includes(id)) {
|
|
27
|
+
return of({
|
|
28
|
+
src: src,
|
|
29
|
+
hashCode: id,
|
|
30
|
+
loaded: true,
|
|
31
|
+
status: 'Loaded'
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
return new Observable((observer: Observer<AssetsLoadResult>) => {
|
|
35
|
+
const script: HTMLScriptElement = document.createElement('script');
|
|
36
|
+
script.type = 'text/javascript';
|
|
37
|
+
script.src = src;
|
|
38
|
+
script.async = true;
|
|
39
|
+
if (script['readyState']) {
|
|
40
|
+
// IE
|
|
41
|
+
script['onreadystatechange'] = () => {
|
|
42
|
+
if (script['readyState'] === 'loaded' || script['readyState'] === 'complete') {
|
|
43
|
+
script['onreadystatechange'] = null;
|
|
44
|
+
observer.next({
|
|
45
|
+
src: src,
|
|
46
|
+
hashCode: id,
|
|
47
|
+
loaded: true,
|
|
48
|
+
status: 'Loaded'
|
|
49
|
+
});
|
|
50
|
+
observer.complete();
|
|
51
|
+
this.loadedSources.push(id);
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
} else {
|
|
55
|
+
// Others
|
|
56
|
+
script.onload = () => {
|
|
57
|
+
observer.next({
|
|
58
|
+
src: src,
|
|
59
|
+
hashCode: id,
|
|
60
|
+
loaded: true,
|
|
61
|
+
status: 'Loaded'
|
|
62
|
+
});
|
|
63
|
+
observer.complete();
|
|
64
|
+
this.loadedSources.push(id);
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
script.onerror = error => {
|
|
68
|
+
observer.error({
|
|
69
|
+
src: src,
|
|
70
|
+
hashCode: id,
|
|
71
|
+
loaded: false,
|
|
72
|
+
status: 'Error',
|
|
73
|
+
error: error
|
|
74
|
+
});
|
|
75
|
+
observer.complete();
|
|
76
|
+
};
|
|
77
|
+
document.body.appendChild(script);
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
loadScriptWithSandbox(app: string, src: string): Observable<AssetsLoadResult> {
|
|
82
|
+
const id = hashCode(src);
|
|
83
|
+
if (this.loadedSources.includes(id)) {
|
|
84
|
+
return of({
|
|
85
|
+
src: src,
|
|
86
|
+
hashCode: id,
|
|
87
|
+
loaded: true,
|
|
88
|
+
status: 'Loaded'
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
return new Observable((observer: Observer<AssetsLoadResult>) => {
|
|
92
|
+
this.http.get(src, { responseType: 'text' }).subscribe(
|
|
93
|
+
(code: string) => {
|
|
94
|
+
this.loadedSources.push(id);
|
|
95
|
+
const sandbox = createSandbox(app);
|
|
96
|
+
sandbox.execScript(code, src);
|
|
97
|
+
observer.next({
|
|
98
|
+
src: src,
|
|
99
|
+
hashCode: id,
|
|
100
|
+
loaded: true,
|
|
101
|
+
status: 'Loaded'
|
|
102
|
+
});
|
|
103
|
+
observer.complete();
|
|
104
|
+
},
|
|
105
|
+
error => {
|
|
106
|
+
observer.error({
|
|
107
|
+
src: src,
|
|
108
|
+
hashCode: id,
|
|
109
|
+
loaded: false,
|
|
110
|
+
status: 'Error',
|
|
111
|
+
error: error
|
|
112
|
+
});
|
|
113
|
+
observer.complete();
|
|
114
|
+
}
|
|
115
|
+
);
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
loadStyle(src: string): Observable<AssetsLoadResult> {
|
|
120
|
+
const id = hashCode(src);
|
|
121
|
+
if (this.loadedSources.includes(id)) {
|
|
122
|
+
return of({
|
|
123
|
+
src: src,
|
|
124
|
+
hashCode: id,
|
|
125
|
+
loaded: true,
|
|
126
|
+
status: 'Loaded'
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
return new Observable((observer: Observer<AssetsLoadResult>) => {
|
|
130
|
+
const head = document.getElementsByTagName('head')[0];
|
|
131
|
+
const link = document.createElement('link');
|
|
132
|
+
link.rel = 'stylesheet';
|
|
133
|
+
link.type = 'text/css';
|
|
134
|
+
link.href = src;
|
|
135
|
+
link.media = 'all';
|
|
136
|
+
link.onload = () => {
|
|
137
|
+
observer.next({
|
|
138
|
+
src: src,
|
|
139
|
+
hashCode: id,
|
|
140
|
+
loaded: true,
|
|
141
|
+
status: 'Loaded'
|
|
142
|
+
});
|
|
143
|
+
observer.complete();
|
|
144
|
+
this.loadedSources.push(id);
|
|
145
|
+
};
|
|
146
|
+
link.onerror = error => {
|
|
147
|
+
observer.error({
|
|
148
|
+
src: src,
|
|
149
|
+
hashCode: id,
|
|
150
|
+
loaded: true,
|
|
151
|
+
status: 'Loaded',
|
|
152
|
+
error: error
|
|
153
|
+
});
|
|
154
|
+
observer.complete();
|
|
155
|
+
};
|
|
156
|
+
head.appendChild(link);
|
|
157
|
+
});
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
loadScripts(
|
|
161
|
+
sources: string[],
|
|
162
|
+
options: {
|
|
163
|
+
app?: string;
|
|
164
|
+
sandbox?: boolean;
|
|
165
|
+
serial?: boolean;
|
|
166
|
+
} = {
|
|
167
|
+
serial: false,
|
|
168
|
+
sandbox: false
|
|
169
|
+
}
|
|
170
|
+
): Observable<AssetsLoadResult[]> {
|
|
171
|
+
if (isEmpty(sources)) {
|
|
172
|
+
return of(null);
|
|
173
|
+
}
|
|
174
|
+
const observables = sources.map(src => {
|
|
175
|
+
// TODO: 暂时只支持Proxy沙箱
|
|
176
|
+
if (options.sandbox && window.Proxy) {
|
|
177
|
+
return this.loadScriptWithSandbox(options.app, src);
|
|
178
|
+
} else {
|
|
179
|
+
return this.loadScript(src);
|
|
180
|
+
}
|
|
181
|
+
});
|
|
182
|
+
if (options.serial) {
|
|
183
|
+
const a = concat(...observables).pipe(
|
|
184
|
+
map(item => {
|
|
185
|
+
return of([item]);
|
|
186
|
+
}),
|
|
187
|
+
concatAll()
|
|
188
|
+
);
|
|
189
|
+
return a;
|
|
190
|
+
} else {
|
|
191
|
+
return forkJoin(observables).pipe();
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
loadStyles(sources: string[]): Observable<AssetsLoadResult[]> {
|
|
196
|
+
if (isEmpty(sources)) {
|
|
197
|
+
return of(null);
|
|
198
|
+
}
|
|
199
|
+
return forkJoin(
|
|
200
|
+
sources.map(src => {
|
|
201
|
+
return this.loadStyle(src);
|
|
202
|
+
})
|
|
203
|
+
);
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
loadScriptsAndStyles(
|
|
207
|
+
scripts: string[] = [],
|
|
208
|
+
styles: string[] = [],
|
|
209
|
+
options?: {
|
|
210
|
+
app?: string;
|
|
211
|
+
sandbox?: boolean;
|
|
212
|
+
serial?: boolean;
|
|
213
|
+
}
|
|
214
|
+
) {
|
|
215
|
+
return forkJoin([this.loadScripts(scripts, options), this.loadStyles(styles)]);
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
loadAppAssets(app: PlanetApplication) {
|
|
219
|
+
if (app.manifest) {
|
|
220
|
+
return this.loadManifest(`${app.manifest}?t=${new Date().getTime()}`).pipe(
|
|
221
|
+
switchMap(manifestResult => {
|
|
222
|
+
const { scripts, styles } = getScriptsAndStylesFullPaths(app, manifestResult);
|
|
223
|
+
return this.loadScriptsAndStyles(scripts, styles, {
|
|
224
|
+
app: app.name,
|
|
225
|
+
sandbox: app.sandbox,
|
|
226
|
+
serial: app.loadSerial
|
|
227
|
+
});
|
|
228
|
+
})
|
|
229
|
+
);
|
|
230
|
+
} else {
|
|
231
|
+
const { scripts, styles } = getScriptsAndStylesFullPaths(app);
|
|
232
|
+
return this.loadScriptsAndStyles(scripts, styles, {
|
|
233
|
+
app: app.name,
|
|
234
|
+
sandbox: app.sandbox,
|
|
235
|
+
serial: app.loadSerial
|
|
236
|
+
});
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
loadManifest(url: string): Observable<{ [key: string]: string }> {
|
|
241
|
+
return this.http.get(url).pipe(
|
|
242
|
+
map((response: any) => {
|
|
243
|
+
return response;
|
|
244
|
+
})
|
|
245
|
+
);
|
|
246
|
+
}
|
|
247
|
+
}
|
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
import { TestBed, inject, tick, fakeAsync } from '@angular/core/testing';
|
|
2
|
+
import { Compiler, Injector, Type, NgModuleRef } from '@angular/core';
|
|
3
|
+
import { app1Name, App1Module, App1ProjectsComponent } from '../testing/app1.module';
|
|
4
|
+
import { app2Name, App2Module } from '../testing/app2.module';
|
|
5
|
+
import { PlanetPortalApplication } from '../application/portal-application';
|
|
6
|
+
import { PlanetComponentLoader } from './planet-component-loader';
|
|
7
|
+
import { HttpClientTestingModule } from '@angular/common/http/testing';
|
|
8
|
+
import { of } from 'rxjs';
|
|
9
|
+
import { tap } from 'rxjs/operators';
|
|
10
|
+
import { PlantComponentConfig } from './plant-component.config';
|
|
11
|
+
import {
|
|
12
|
+
defineApplication,
|
|
13
|
+
getPlanetApplicationRef,
|
|
14
|
+
getApplicationLoader,
|
|
15
|
+
clearGlobalPlanet,
|
|
16
|
+
getApplicationService
|
|
17
|
+
} from '../global-planet';
|
|
18
|
+
import { Planet } from 'ngx-planet/planet';
|
|
19
|
+
import { RouterTestingModule } from '@angular/router/testing';
|
|
20
|
+
|
|
21
|
+
describe('PlanetComponentLoader', () => {
|
|
22
|
+
let compiler: Compiler;
|
|
23
|
+
let injector: Injector;
|
|
24
|
+
let planet: Planet;
|
|
25
|
+
|
|
26
|
+
function defineAndBootstrapApplication(name: string, appModule: Type<any>) {
|
|
27
|
+
const ngModuleFactory = compiler.compileModuleSync(appModule);
|
|
28
|
+
const ngModuleRef = ngModuleFactory.create(injector);
|
|
29
|
+
defineApplication(name, {
|
|
30
|
+
template: '<app1-root></app1-root>',
|
|
31
|
+
bootstrap: (portalApp?: PlanetPortalApplication) => {
|
|
32
|
+
return new Promise(resolve => {
|
|
33
|
+
resolve(ngModuleRef);
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
});
|
|
37
|
+
const appRef = getPlanetApplicationRef(name);
|
|
38
|
+
const portalApplication = new PlanetPortalApplication();
|
|
39
|
+
appRef.bootstrap(portalApplication);
|
|
40
|
+
return ngModuleRef;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
beforeEach(() => {
|
|
44
|
+
TestBed.configureTestingModule({
|
|
45
|
+
imports: [HttpClientTestingModule, RouterTestingModule.withRoutes([])]
|
|
46
|
+
});
|
|
47
|
+
compiler = TestBed.inject(Compiler);
|
|
48
|
+
planet = TestBed.inject(Planet);
|
|
49
|
+
injector = TestBed.inject(Injector);
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
afterEach(() => {
|
|
53
|
+
clearGlobalPlanet();
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
it('should register component success', fakeAsync(() => {
|
|
57
|
+
// mock app1 and app2 bootstrap
|
|
58
|
+
const app1ModuleRef = defineAndBootstrapApplication(app1Name, App1Module);
|
|
59
|
+
registerAppComponents(app1ModuleRef);
|
|
60
|
+
tick();
|
|
61
|
+
const app1Ref = getPlanetApplicationRef(app1Name);
|
|
62
|
+
expect(app1Ref.getComponentFactory()).toBeTruthy();
|
|
63
|
+
}));
|
|
64
|
+
|
|
65
|
+
it('should app2 load app1 component when app1 component has completed the registration', fakeAsync(() => {
|
|
66
|
+
// mock app1 and app2 bootstrap
|
|
67
|
+
const app1ModuleRef = defineAndBootstrapApplication(app1Name, App1Module);
|
|
68
|
+
const app2ModuleRef = defineAndBootstrapApplication(app2Name, App2Module);
|
|
69
|
+
tick();
|
|
70
|
+
|
|
71
|
+
expect(() => {
|
|
72
|
+
loadApp1Component(app2ModuleRef);
|
|
73
|
+
}).toThrowError(`${app1Name}'s component(app1-projects) is not registered`);
|
|
74
|
+
|
|
75
|
+
registerAppComponents(app1ModuleRef);
|
|
76
|
+
tick();
|
|
77
|
+
|
|
78
|
+
expect(() => {
|
|
79
|
+
loadApp1Component(app2ModuleRef, { container: null });
|
|
80
|
+
}).toThrowError(`config 'container' cannot be null`);
|
|
81
|
+
|
|
82
|
+
loadApp1ComponentAndExpectHtml(app2ModuleRef);
|
|
83
|
+
}));
|
|
84
|
+
|
|
85
|
+
it('should app2 load app1 component with wrapperClass', fakeAsync(() => {
|
|
86
|
+
// mock app1 and app2 bootstrap
|
|
87
|
+
const app1ModuleRef = defineAndBootstrapApplication(app1Name, App1Module);
|
|
88
|
+
const app2ModuleRef = defineAndBootstrapApplication(app2Name, App2Module);
|
|
89
|
+
tick();
|
|
90
|
+
registerAppComponents(app1ModuleRef);
|
|
91
|
+
loadApp1Component(app2ModuleRef, { wrapperClass: 'custom-wrapper' }).subscribe(componentRef => {
|
|
92
|
+
expect(componentRef.wrapperElement.classList.contains('planet-component-wrapper')).toBeTruthy();
|
|
93
|
+
expect(componentRef.wrapperElement.classList.contains('custom-wrapper')).toBeTruthy();
|
|
94
|
+
});
|
|
95
|
+
}));
|
|
96
|
+
|
|
97
|
+
it('should app2 load app1 component with stylePrefix of app1', fakeAsync(() => {
|
|
98
|
+
// mock app1 and app2 bootstrap
|
|
99
|
+
const app1ModuleRef = defineAndBootstrapApplication(app1Name, App1Module);
|
|
100
|
+
const app2ModuleRef = defineAndBootstrapApplication(app2Name, App2Module);
|
|
101
|
+
const applicationService = getApplicationService();
|
|
102
|
+
const applicationServiceSpy = spyOn(applicationService, 'getAppByName');
|
|
103
|
+
applicationServiceSpy.and.returnValue({
|
|
104
|
+
stylePrefix: 'app1-prefix',
|
|
105
|
+
name: 'app1',
|
|
106
|
+
hostParent: '',
|
|
107
|
+
routerPathPrefix: ''
|
|
108
|
+
});
|
|
109
|
+
tick();
|
|
110
|
+
registerAppComponents(app1ModuleRef);
|
|
111
|
+
loadApp1Component(app2ModuleRef, { wrapperClass: 'custom-wrapper' }).subscribe(componentRef => {
|
|
112
|
+
expect(componentRef.wrapperElement.classList.contains('planet-component-wrapper')).toBeTruthy();
|
|
113
|
+
expect(componentRef.wrapperElement.classList.contains('custom-wrapper')).toBeTruthy();
|
|
114
|
+
expect(componentRef.wrapperElement.classList.contains('app1-prefix')).toBeTruthy();
|
|
115
|
+
});
|
|
116
|
+
}));
|
|
117
|
+
|
|
118
|
+
it('should app2 load app1 component and preload app1', fakeAsync(() => {
|
|
119
|
+
// mock app2 bootstrap
|
|
120
|
+
const app2ModuleRef = defineAndBootstrapApplication(app2Name, App1Module);
|
|
121
|
+
// mock app1 preload
|
|
122
|
+
const applicationLoader = getApplicationLoader();
|
|
123
|
+
const applicationLoaderSpy = spyOn(applicationLoader, 'preload');
|
|
124
|
+
const preload$ = of(getPlanetApplicationRef(app1Name)).pipe(
|
|
125
|
+
tap(() => {
|
|
126
|
+
const app1ModuleRef = defineAndBootstrapApplication(app1Name, App1Module);
|
|
127
|
+
registerAppComponents(app1ModuleRef);
|
|
128
|
+
})
|
|
129
|
+
);
|
|
130
|
+
applicationLoaderSpy.and.returnValue(preload$);
|
|
131
|
+
expect(applicationLoaderSpy).not.toHaveBeenCalled();
|
|
132
|
+
loadApp1ComponentAndExpectHtml(app2ModuleRef);
|
|
133
|
+
expect(applicationLoaderSpy).toHaveBeenCalled();
|
|
134
|
+
}));
|
|
135
|
+
|
|
136
|
+
it('should app2 dispose app1 component', fakeAsync(() => {
|
|
137
|
+
// mock app1 and app2 bootstrap
|
|
138
|
+
const app1ModuleRef = defineAndBootstrapApplication(app1Name, App1Module);
|
|
139
|
+
const app2ModuleRef = defineAndBootstrapApplication(app2Name, App2Module);
|
|
140
|
+
tick();
|
|
141
|
+
registerAppComponents(app1ModuleRef);
|
|
142
|
+
loadApp1Component(app2ModuleRef).subscribe(componentRef => {
|
|
143
|
+
const parent = componentRef.wrapperElement.parentElement;
|
|
144
|
+
componentRef.dispose();
|
|
145
|
+
expect(parent.innerHTML).toEqual('');
|
|
146
|
+
});
|
|
147
|
+
}));
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
function registerAppComponents(appModuleRef: NgModuleRef<any>) {
|
|
151
|
+
const componentLoader = appModuleRef.injector.get(PlanetComponentLoader);
|
|
152
|
+
componentLoader.register([{ name: 'app1-projects', component: App1ProjectsComponent }]);
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
function loadApp1Component(appModuleRef: NgModuleRef<any>, config?: Partial<PlantComponentConfig>) {
|
|
156
|
+
const componentLoader = appModuleRef.injector.get(PlanetComponentLoader);
|
|
157
|
+
const hostElement = createComponentHostElement();
|
|
158
|
+
const result = componentLoader.load(
|
|
159
|
+
app1Name,
|
|
160
|
+
'app1-projects',
|
|
161
|
+
Object.assign({}, { container: hostElement }, config)
|
|
162
|
+
);
|
|
163
|
+
tick(20);
|
|
164
|
+
return result;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
function loadApp1ComponentAndExpectHtml(app2ModuleRef: NgModuleRef<any>) {
|
|
168
|
+
loadApp1Component(app2ModuleRef).subscribe(componentRef => {
|
|
169
|
+
expect(componentRef.wrapperElement.outerHTML).toEqual(
|
|
170
|
+
`<div class="planet-component-wrapper" planet-inline=""><app1-projects> projects is work </app1-projects></div>`
|
|
171
|
+
);
|
|
172
|
+
});
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
function createComponentHostElement(): HTMLElement {
|
|
176
|
+
const componentHostClass = 'component-host';
|
|
177
|
+
let element = document.body.getElementsByClassName(componentHostClass)[0];
|
|
178
|
+
if (element) {
|
|
179
|
+
element.innerHTML = '';
|
|
180
|
+
return element as HTMLElement;
|
|
181
|
+
} else {
|
|
182
|
+
element = document.createElement('DIV');
|
|
183
|
+
element.classList.add('component-host');
|
|
184
|
+
document.body.appendChild(element);
|
|
185
|
+
return element as HTMLElement;
|
|
186
|
+
}
|
|
187
|
+
}
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
import { Injectable, ApplicationRef, NgModuleRef, NgZone, ElementRef, Inject } from '@angular/core';
|
|
2
|
+
import { ComponentType, DomPortalOutlet, ComponentPortal, PortalInjector } from '@angular/cdk/portal';
|
|
3
|
+
import { PlanetApplicationRef } from '../application/planet-application-ref';
|
|
4
|
+
import { PlanetComponentRef } from './planet-component-ref';
|
|
5
|
+
import { PlantComponentConfig } from './plant-component.config';
|
|
6
|
+
import { coerceArray } from '../helpers';
|
|
7
|
+
import { map, shareReplay, finalize, debounce, filter, take, delay, delayWhen } from 'rxjs/operators';
|
|
8
|
+
import { of, Observable, interval, timer } from 'rxjs';
|
|
9
|
+
import { DOCUMENT } from '@angular/common';
|
|
10
|
+
import { globalPlanet, getApplicationLoader, getApplicationService } from '../global-planet';
|
|
11
|
+
|
|
12
|
+
const componentWrapperClass = 'planet-component-wrapper';
|
|
13
|
+
|
|
14
|
+
export interface PlanetComponent<T = any> {
|
|
15
|
+
name: string;
|
|
16
|
+
component: ComponentType<T>;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
@Injectable({
|
|
20
|
+
providedIn: 'root'
|
|
21
|
+
})
|
|
22
|
+
export class PlanetComponentLoader {
|
|
23
|
+
private domPortalOutletCache = new WeakMap<any, DomPortalOutlet>();
|
|
24
|
+
|
|
25
|
+
private get applicationLoader() {
|
|
26
|
+
return getApplicationLoader();
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
private get applicationService() {
|
|
30
|
+
return getApplicationService();
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
constructor(
|
|
34
|
+
private applicationRef: ApplicationRef,
|
|
35
|
+
private ngModuleRef: NgModuleRef<any>,
|
|
36
|
+
private ngZone: NgZone,
|
|
37
|
+
@Inject(DOCUMENT) private document: any
|
|
38
|
+
) {}
|
|
39
|
+
|
|
40
|
+
private getPlantAppRef(name: string): Observable<PlanetApplicationRef> {
|
|
41
|
+
if (globalPlanet.apps[name] && globalPlanet.apps[name].appModuleRef) {
|
|
42
|
+
return of(globalPlanet.apps[name]);
|
|
43
|
+
} else {
|
|
44
|
+
const app = this.applicationService.getAppByName(name);
|
|
45
|
+
return this.applicationLoader.preload(app, true).pipe(
|
|
46
|
+
map(() => {
|
|
47
|
+
return globalPlanet.apps[name];
|
|
48
|
+
})
|
|
49
|
+
);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
private createInjector<TData>(
|
|
54
|
+
appModuleRef: NgModuleRef<any>,
|
|
55
|
+
componentRef: PlanetComponentRef<TData>
|
|
56
|
+
): PortalInjector {
|
|
57
|
+
const injectionTokens = new WeakMap<any, any>([[PlanetComponentRef, componentRef]]);
|
|
58
|
+
const defaultInjector = appModuleRef.injector;
|
|
59
|
+
return new PortalInjector(defaultInjector, injectionTokens);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
private getContainerElement(config: PlantComponentConfig): HTMLElement {
|
|
63
|
+
if (!config.container) {
|
|
64
|
+
throw new Error(`config 'container' cannot be null`);
|
|
65
|
+
} else {
|
|
66
|
+
if ((config.container as ElementRef).nativeElement) {
|
|
67
|
+
return (config.container as ElementRef).nativeElement;
|
|
68
|
+
} else {
|
|
69
|
+
return config.container as HTMLElement;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
private createWrapperElement(config: PlantComponentConfig) {
|
|
75
|
+
const container = this.getContainerElement(config);
|
|
76
|
+
const element = this.document.createElement('div');
|
|
77
|
+
const subApp = this.applicationService.getAppByName(this.ngModuleRef.instance.appName);
|
|
78
|
+
element.classList.add(componentWrapperClass);
|
|
79
|
+
element.setAttribute('planet-inline', '');
|
|
80
|
+
if (config.wrapperClass) {
|
|
81
|
+
element.classList.add(config.wrapperClass);
|
|
82
|
+
}
|
|
83
|
+
if (subApp && subApp.stylePrefix) {
|
|
84
|
+
element.classList.add(subApp.stylePrefix);
|
|
85
|
+
}
|
|
86
|
+
container.appendChild(element);
|
|
87
|
+
return element;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
private attachComponent<TData>(
|
|
91
|
+
plantComponent: PlanetComponent,
|
|
92
|
+
appModuleRef: NgModuleRef<any>,
|
|
93
|
+
config: PlantComponentConfig
|
|
94
|
+
): PlanetComponentRef<TData> {
|
|
95
|
+
const plantComponentRef = new PlanetComponentRef();
|
|
96
|
+
const componentFactoryResolver = appModuleRef.componentFactoryResolver;
|
|
97
|
+
const appRef = this.applicationRef;
|
|
98
|
+
const injector = this.createInjector<TData>(appModuleRef, plantComponentRef);
|
|
99
|
+
const wrapper = this.createWrapperElement(config);
|
|
100
|
+
let portalOutlet = this.domPortalOutletCache.get(wrapper);
|
|
101
|
+
if (portalOutlet) {
|
|
102
|
+
portalOutlet.detach();
|
|
103
|
+
} else {
|
|
104
|
+
portalOutlet = new DomPortalOutlet(wrapper, componentFactoryResolver, appRef, injector);
|
|
105
|
+
this.domPortalOutletCache.set(wrapper, portalOutlet);
|
|
106
|
+
}
|
|
107
|
+
const componentPortal = new ComponentPortal(plantComponent.component, null);
|
|
108
|
+
const componentRef = portalOutlet.attachComponentPortal<TData>(componentPortal);
|
|
109
|
+
if (config.initialState) {
|
|
110
|
+
Object.assign(componentRef.instance, config.initialState);
|
|
111
|
+
}
|
|
112
|
+
plantComponentRef.componentInstance = componentRef.instance;
|
|
113
|
+
plantComponentRef.componentRef = componentRef;
|
|
114
|
+
plantComponentRef.wrapperElement = wrapper;
|
|
115
|
+
plantComponentRef.dispose = () => {
|
|
116
|
+
this.domPortalOutletCache.delete(wrapper);
|
|
117
|
+
portalOutlet.dispose();
|
|
118
|
+
};
|
|
119
|
+
return plantComponentRef;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
private registerComponentFactory(componentOrComponents: PlanetComponent | PlanetComponent[]) {
|
|
123
|
+
const app = this.ngModuleRef.instance.appName;
|
|
124
|
+
this.getPlantAppRef(app).subscribe(appRef => {
|
|
125
|
+
appRef.registerComponentFactory((componentName: string, config: PlantComponentConfig<any>) => {
|
|
126
|
+
const components = coerceArray(componentOrComponents);
|
|
127
|
+
const component = components.find(item => item.name === componentName);
|
|
128
|
+
if (component) {
|
|
129
|
+
return this.ngZone.run(() => {
|
|
130
|
+
const componentRef = this.attachComponent<any>(component, appRef.appModuleRef, config);
|
|
131
|
+
return componentRef;
|
|
132
|
+
});
|
|
133
|
+
} else {
|
|
134
|
+
throw Error(`unregistered component ${componentName} in app ${app}`);
|
|
135
|
+
}
|
|
136
|
+
});
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
register(components: PlanetComponent | PlanetComponent[]) {
|
|
141
|
+
setTimeout(() => {
|
|
142
|
+
this.registerComponentFactory(components);
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
load<TComp = unknown, TData = unknown>(
|
|
147
|
+
app: string,
|
|
148
|
+
componentName: string,
|
|
149
|
+
config: PlantComponentConfig<TData>
|
|
150
|
+
): Observable<PlanetComponentRef<TComp>> {
|
|
151
|
+
const result = this.getPlantAppRef(app).pipe(
|
|
152
|
+
delayWhen(appRef => {
|
|
153
|
+
if (appRef.getComponentFactory()) {
|
|
154
|
+
return of('');
|
|
155
|
+
} else {
|
|
156
|
+
// Because register use 'setTimeout',so timer 20
|
|
157
|
+
return timer(20);
|
|
158
|
+
}
|
|
159
|
+
}),
|
|
160
|
+
map(appRef => {
|
|
161
|
+
const componentFactory = appRef.getComponentFactory();
|
|
162
|
+
if (componentFactory) {
|
|
163
|
+
return componentFactory<TData, TComp>(componentName, config);
|
|
164
|
+
} else {
|
|
165
|
+
throw new Error(`${app}'s component(${componentName}) is not registered`);
|
|
166
|
+
}
|
|
167
|
+
}),
|
|
168
|
+
shareReplay()
|
|
169
|
+
);
|
|
170
|
+
result.subscribe();
|
|
171
|
+
return result;
|
|
172
|
+
}
|
|
173
|
+
}
|
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import { ElementRef } from '@angular/core';
|
|
2
|
-
|
|
1
|
+
import { ViewContainerRef, ElementRef } from '@angular/core';
|
|
2
|
+
|
|
3
|
+
export class PlantComponentConfig<TData = any> {
|
|
3
4
|
/** Load target container */
|
|
4
5
|
container: HTMLElement | ElementRef<HTMLElement | any>;
|
|
5
6
|
/** Component wrapper class. */
|
|
6
7
|
wrapperClass?: string;
|
|
7
8
|
/** Data being injected into the child component. */
|
|
8
|
-
initialState?: TData | null;
|
|
9
|
+
initialState?: TData | null = null;
|
|
9
10
|
}
|
|
10
|
-
//# sourceMappingURL=plant-component.config.d.ts.map
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { createDebug, clearDebugFactory, setDebugFactory, getDebugFactory, Debug, Debugger } from './debug';
|
|
2
|
+
|
|
3
|
+
describe('debug', () => {
|
|
4
|
+
afterEach(() => {
|
|
5
|
+
clearDebugFactory();
|
|
6
|
+
});
|
|
7
|
+
|
|
8
|
+
it('should bypass create debug and log message', () => {
|
|
9
|
+
const debug = createDebug('app-loader');
|
|
10
|
+
debug('this is debug message');
|
|
11
|
+
expect(true).toBe(true);
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
it('should set debug factory success', () => {
|
|
15
|
+
const mockDebugFactory = function(namespace: string): Debugger {
|
|
16
|
+
return undefined;
|
|
17
|
+
} as Debug;
|
|
18
|
+
setDebugFactory(mockDebugFactory);
|
|
19
|
+
const debugFactory = getDebugFactory();
|
|
20
|
+
expect(debugFactory).toBe(mockDebugFactory);
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
it('should set debug factory fail', () => {
|
|
24
|
+
const mockDebugFactory = {} as Debug;
|
|
25
|
+
expect(() => {
|
|
26
|
+
setDebugFactory(mockDebugFactory);
|
|
27
|
+
}).toThrowError('debug factory type is invalid, must be function');
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
it('should use custom debug factory to debug', () => {
|
|
31
|
+
const mockDebug = jasmine.createSpy('mock debug');
|
|
32
|
+
const mockDebugFactory = jasmine.createSpy('mock debug factory').and.callFake(namespace => {
|
|
33
|
+
expect(namespace).toEqual('planet:app-loader');
|
|
34
|
+
return mockDebug;
|
|
35
|
+
});
|
|
36
|
+
setDebugFactory(mockDebugFactory as any);
|
|
37
|
+
const debug = createDebug('app-loader');
|
|
38
|
+
expect(mockDebug).not.toHaveBeenCalled();
|
|
39
|
+
debug('this is debug message');
|
|
40
|
+
expect(mockDebug).toHaveBeenCalled();
|
|
41
|
+
|
|
42
|
+
expect(mockDebug).toHaveBeenCalledWith('this is debug message', []);
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
it('should use cache debug for same namespace', () => {
|
|
46
|
+
const mockDebugFactory = jasmine.createSpy('mock debug factory').and.callFake(namespace => {
|
|
47
|
+
expect(namespace).toEqual('planet:app-loader');
|
|
48
|
+
return jasmine.createSpy('mock debug');
|
|
49
|
+
});
|
|
50
|
+
setDebugFactory(mockDebugFactory as any);
|
|
51
|
+
expect(mockDebugFactory).toHaveBeenCalledTimes(0);
|
|
52
|
+
const debug1 = createDebug('app-loader');
|
|
53
|
+
const debug2 = createDebug('app-loader');
|
|
54
|
+
debug1('message1');
|
|
55
|
+
debug2('message2');
|
|
56
|
+
expect(mockDebugFactory).toHaveBeenCalledTimes(1);
|
|
57
|
+
});
|
|
58
|
+
});
|