You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

dynamic-loader.component.ts 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /**
  2. * Set existing vendor modules into SystemJS registry.
  3. * This way SystemJS won't make HTTP requests to fetch imported modules
  4. * needed by the dynamicaly loaded Widgets.
  5. */
  6. import { System } from 'systemjs';
  7. declare const SystemJS: System;
  8. import * as angularRouter from '@angular/router';
  9. import * as angularCore from '@angular/core';
  10. import * as angularCommon from '@angular/common';
  11. import * as angularCommonHttp from '@angular/common/http';
  12. import * as angularForms from '@angular/forms';
  13. import * as angularAnimations from '@angular/animations';
  14. import * as angularAnimationsBrowser from '@angular/animations/browser'
  15. import * as angularPlatformBrowser from '@angular/platform-browser';
  16. import * as angularPlatformBrowserDynamic from '@angular/platform-browser-dynamic';
  17. import * as clarityModule from '@clr/angular';
  18. import * as frontblockGenericTypes from 'frontblock-generic/Types.js'
  19. import * as browserAnimationsModule from "@angular/platform-browser/animations";
  20. import * as btcHdkey from "btc-hdkey"
  21. import * as bitcoinjslib from "bitcoinjs-lib"
  22. import * as fetch from "node-fetch"
  23. import * as coinselect from "coinselect/accumulative"
  24. SystemJS.set('@clr/angular', SystemJS.newModule(clarityModule));
  25. SystemJS.set('@angular/router', SystemJS.newModule(angularRouter));
  26. SystemJS.set('@angular/core', SystemJS.newModule(angularCore));
  27. SystemJS.set('@angular/common', SystemJS.newModule(angularCommon));
  28. SystemJS.set('@angular/common/http', SystemJS.newModule(angularCommonHttp));
  29. SystemJS.set('@angular/forms', SystemJS.newModule(angularForms));
  30. SystemJS.set('@angular/animations', SystemJS.newModule(angularAnimations));
  31. SystemJS.set('@angular/animations/browser', SystemJS.newModule(angularAnimationsBrowser));
  32. SystemJS.set('@angular/platform-browser', SystemJS.newModule(angularPlatformBrowser));
  33. SystemJS.set('@angular/platform-browser/animations', SystemJS.newModule(browserAnimationsModule));
  34. SystemJS.set('@angular/platform-browser-dynamic', SystemJS.newModule(angularPlatformBrowserDynamic));
  35. SystemJS.set('frontblock-generic/Types', SystemJS.newModule(frontblockGenericTypes));
  36. SystemJS.set('btc-hdkey', SystemJS.newModule(btcHdkey))
  37. SystemJS.set('bitcoinjs-lib', SystemJS.newModule(bitcoinjslib))
  38. SystemJS.set('node-fetch', SystemJS.newModule(fetch))
  39. SystemJS.set('coinselect/accumulative', SystemJS.newModule(coinselect))
  40. SystemJS.config({ meta: { '*': { authorization: true } } });
  41. /** --------- */
  42. import { AfterViewInit, Component, ViewChild } from '@angular/core';
  43. import { SidebarComponent } from '../sidebar/sidebar.component';
  44. import { Router } from '@angular/router';
  45. import { FrontendPlugin, SidebarEntries, SidebarEntry } from "frontblock-generic/Plugin"
  46. import { environment } from "../../environments/environment"
  47. const fb = environment.production ? window["fb"] : {
  48. PluginManager: {
  49. getLoadedPluginNames: () => [/*"ApiClient", "PluginManager",*/ "Wallet"]
  50. }
  51. }
  52. @Component({
  53. selector: 'dynamic-loader',
  54. templateUrl: './dynamic-loader.component.html',
  55. styleUrls: ['./dynamic-loader.component.scss']
  56. })
  57. export class DynamicLoaderComponent implements AfterViewInit {
  58. @ViewChild(SidebarComponent, {static:false})
  59. private sidebar:SidebarComponent
  60. constructor(
  61. private router: Router,
  62. ) {
  63. }
  64. ngAfterViewInit() {
  65. this.loadWidgets()
  66. }
  67. private async loadWidgets() {
  68. while (!fb.PluginManager) {
  69. await new Promise((resolve) => setTimeout(resolve, 50))
  70. }
  71. const pluginNames = await fb.PluginManager.getLoadedPluginNames()
  72. pluginNames.forEach(element => this.installWidget(element));
  73. }
  74. private async installWidget(pluginName: string) {
  75. let module: FrontendPlugin
  76. if (!angularCore.isDevMode())
  77. module = await SystemJS.import("plugins/" + pluginName + ".js");
  78. else
  79. module = await SystemJS.import("assets/" + pluginName.toLowerCase() + ".js")
  80. const plugin = new module['PluginModule']()
  81. const entry: SidebarEntries | SidebarEntry = plugin.getSidebarEntry()
  82. const rc = this.router.config
  83. switch (typeof (<any>entry).links) {
  84. case "undefined":
  85. const e: SidebarEntry = <SidebarEntry>entry
  86. e.route = "plugin/" + e.route
  87. this.sidebar.entries.push(e)
  88. rc.unshift({
  89. path: "plugin",
  90. loadChildren: async () => module['PluginModule']
  91. })
  92. break;
  93. case "object":
  94. const m: SidebarEntries = <SidebarEntries>entry
  95. m.links = m.links.map(link => {
  96. return { text: link.text, route: m.parentRoute + "/" + link.route }
  97. })
  98. this.sidebar.multientires.push(m)
  99. rc.unshift({
  100. path: m.parentRoute,
  101. loadChildren: async () => module['PluginModule']
  102. })
  103. break;
  104. }
  105. this.router.resetConfig(rc)
  106. }
  107. }