-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathapp.module.ts
265 lines (240 loc) · 8.12 KB
/
app.module.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
// PURPOSE
// - This is where we register every component that will be used or imported
// - add an import to define where it is located, e.g. import { BaseComponent } from './base/base.component'
// - add to declarations and entryComponents
import {forEach, extend, isNumber, uniqueId} from 'lodash'
import {Stratus} from '@stratusjs/runtime/stratus'
// Angular Core
import {HttpClientModule} from '@angular/common/http'
import {
ApplicationRef,
NgModule
} from '@angular/core'
import {
FormsModule,
ReactiveFormsModule
} from '@angular/forms'
import {MatNativeDateModule} from '@angular/material/core'
import {BrowserModule} from '@angular/platform-browser'
import {BrowserAnimationsModule} from '@angular/platform-browser/animations'
// Angular Locales
// import localeFr from '@angular/common/locales/fr'
// import localeEs from '@angular/common/locales/es'
// Register Locales
// registerLocaleData(localeFr, 'fr-FR')
// registerLocaleData(localeEs, 'es-ES')
// Material Modules
import {MaterialModules} from './material'
// Angular Packages
import {FlexLayoutModule} from '@angular/flex-layout'
// Base Components
import {BaseComponent} from './base/base.component'
// Stratus Custom Directives/Components
import {ConfirmDialogComponent} from './confirm-dialog/confirm-dialog.component'
import {EditorComponent} from './editor/editor.component'
import {JsonComponent} from './json/json.component'
import {MediaSelectorComponent} from './media-selector/media-selector.component'
import {SelectorComponent} from './selector/selector.component'
import {TreeComponent} from './tree/tree.component'
import {TreeDialogComponent} from './tree/tree-dialog.component'
import {TreeNodeComponent} from './tree/tree-node.component'
// Custom Angular StratusPackages
// import {FormPackage} from '../../form/src/form.module'
import {MapPackage} from '../../map/src/map.module'
import {StripePackage} from '../../stripe/src/stripe.module'
// Froala Modules (Required by Editor)
import {
FroalaEditorModule,
FroalaViewModule
} from 'angular-froala-wysiwyg'
// ngx-json-viewer Module (Required by JSON)
import {
NgxJsonViewerModule
} from 'ngx-json-viewer'
// Editor Dialogs
import {CitationDialogComponent} from './editor/citation-dialog.component'
import {
CodeViewDialogComponent
} from './editor/code-view-dialog.component'
import {
MediaDialogComponent
} from './editor/media-dialog.component'
import {
LinkDialogComponent
} from './editor/link-dialog.component'
export type StratusPackage = {
stratusModule: any
stratusComponents?: {[key:string]: any}
}
// Dynamic Loader Prototype
// import {
// AngularModules
// } from './angular.modules'
// let roster: {};
// roster = {
// // 'sa-base': BaseComponent,
// 'sa-selector': SelectorComponent,
// 'sa-tree': TreeComponent
// };
//
// const bootstrap = keys(roster)
// .map(component => {
// const elements = document.getElementsByTagName(component);
// if (!elements || !elements.length) {
// return null;
// }
// return component;
// })
// .filter((item) => !!item)
// .map((element) => get(roster, element) || null)
// .filter((item) => !!item);
// These are for external libraries (or Angular)
const ngModuleImports: any[] = [
// AngularModules,
BrowserModule,
BrowserAnimationsModule,
// CodeEditorModule.forRoot(),
FlexLayoutModule,
FormsModule,
FroalaEditorModule.forRoot(),
FroalaViewModule.forRoot(),
HttpClientModule,
MaterialModules,
MatNativeDateModule,
NgxJsonViewerModule,
ReactiveFormsModule,
// SelectorComponent.forRoot()
]
// These determine what exists as a component within Angular system.
const ngDeclarations: any[] = [
BaseComponent,
CitationDialogComponent,
CodeViewDialogComponent,
ConfirmDialogComponent,
EditorComponent,
JsonComponent,
LinkDialogComponent,
MediaDialogComponent,
MediaSelectorComponent,
SelectorComponent,
TreeComponent,
TreeDialogComponent,
TreeNodeComponent,
]
// This determines what is accessible via DOM as a component. These must be listed in `ngDeclarations`.
const ngEntryComponents: any[] = [
BaseComponent, // FIXME shouldn't be needed as doesn't load on DOM
CitationDialogComponent,
CodeViewDialogComponent,
ConfirmDialogComponent,
EditorComponent,
JsonComponent,
LinkDialogComponent,
MediaDialogComponent,
MediaSelectorComponent,
SelectorComponent,
TreeComponent,
TreeDialogComponent,
TreeNodeComponent,
]
const appModuleComponents = {
'sa-base': BaseComponent,
'sa-editor': EditorComponent,
'sa-json': JsonComponent,
'sa-media-selector': MediaSelectorComponent,
'sa-selector': SelectorComponent,
'sa-tree': TreeComponent
}
// This determines what custom Stratus Packages we want loaded in and will handle it's own declarations
const stratusPackages: StratusPackage[] = [
MapPackage, // @stratusjs/map
StripePackage // @stratusjs/stripe
]
stratusPackages.forEach((stratusPackage) => {
ngModuleImports.push(stratusPackage.stratusModule)
if (stratusPackage.hasOwnProperty('stratusComponents')) {
extend(appModuleComponents, stratusPackage.stratusComponents)
}
})
@NgModule({
// These are for external libraries (or Angular)
imports: ngModuleImports,
// This determines what is accessible via DOM as a component. These must be listed in `declarations`.
entryComponents: ngEntryComponents,
// These determine what exists as a component within Angular system.
declarations: ngDeclarations,
// bootstrap,
providers: [
{provide: Window, useValue: window}
]
})
export class AppModule {
// node: true || false
initialTimeout = 1000
instances = {}
// These modules will be hydrated directly in the HTML, and *cannot* load in a component template/dialog
modules = appModuleComponents
constructor() {
Stratus.Instances[uniqueId('sa_app_module_')] = this
}
ngDoBootstrap(appRef: ApplicationRef) {
this.detectBoot(appRef)
}
// Fade out detection cycles
exponentialTimeout(limit?: number) {
if (isNumber(limit) && limit < this.initialTimeout) {
return limit
}
// store current
const currentTimeout = this.initialTimeout
// increase amount
this.initialTimeout = this.initialTimeout * 1.01
// return
return currentTimeout
}
detectBoot(appRef: ApplicationRef) {
forEach(this.modules, (module, selector) => {
// if (!(module instanceof ComponentFactory)) {
// return;
// }
const elements = document.getElementsByTagName(selector)
if (!elements || !elements.length) {
return
}
forEach(elements, (node) => {
if (node.hasAttribute('ng-version')) {
return
}
// console.log('ngDoBootstrap detected:', node);
// FIXME: The Modules aren't explicitly the correct type
// @ts-ignore
appRef.bootstrap(module, node)
})
})
// FIXME this logic is broken
/*
forEach(this.directives, (directive, selector) => {
// if (!(module instanceof ComponentFactory)) {
// return;
// }
// const elements = document.getElementsByTagName(selector)
const elements = document.querySelectorAll(`[${selector}]`)
if (!elements || !elements.length) {
return
}
forEach(elements, (node) => {
if (node.hasAttribute('ng-version')) {
return
}
console.warn('detected ', selector)
// console.log('ngDoBootstrap detected:', node);
// FIXME: Directives cannot be added to ngModule.entryComponents, so this will error
// @ts-ignore
appRef.bootstrap(directive, node)
})
})*/
setTimeout(() => {
this.detectBoot(appRef)
}, this.exponentialTimeout(4000))
}
}