This repository has been archived by the owner on Nov 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
app.module.ts
152 lines (148 loc) · 6.58 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
/*
* Copyright (c) 2019 Software AG, Darmstadt, Germany and/or its licensors
*
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { Injector, NgModule } from '@angular/core';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { NavigationError, Router, RouterModule as NgRouterModule } from '@angular/router';
import { UpgradeModule as NgUpgradeModule } from '@angular/upgrade/static';
import { AppStateService, CoreModule, RouterModule} from '@c8y/ngx-components';
import { DashboardUpgradeModule, UpgradeModule, HybridAppModule } from '@c8y/ngx-components/upgrade';
import { BuilderModule } from "./builder/builder.module";
import { filter, first, map, startWith, tap, withLatestFrom } from "rxjs/operators";
import { IUser } from '@c8y/client';
import { SimulationStrategiesModule } from "./simulation-strategies/simulation-strategies.module";
import { CustomWidgetsModule } from "./custom-widgets/custom-widgets.module";
import { interval } from 'rxjs';
import { SettingsService } from './builder/settings/settings.service';
import { cockpitWidgets } from '@c8y/ngx-components/widgets/cockpit';
@NgModule({
imports: [
// Upgrade module must be the first
UpgradeModule,
BrowserAnimationsModule,
RouterModule.forRoot(),
NgRouterModule.forRoot([], { enableTracing: false, useHash: true }),
CoreModule.forRoot(),
NgUpgradeModule,
DashboardUpgradeModule,
BuilderModule,
SimulationStrategiesModule,
cockpitWidgets(),
CustomWidgetsModule
]
})
export class AppModule extends HybridAppModule {
constructor(protected upgrade: NgUpgradeModule, appStateService: AppStateService, private router: Router,
private injector: Injector,
private settingsService: SettingsService) {
super();
// Fixes a bug where the router removes the hash when the user tries to navigate to an app and is not logged in
appStateService.currentUser.pipe(filter(user => user != null)).pipe(
withLatestFrom(
router.events.pipe(
filter(event => event instanceof NavigationError),
tap((event: NavigationError) => {
if ((location as any).replaceState) {
// Change the location without navigating anywhere
(location as any).replaceState(event.url)
}
}),
startWith(null)
)
),
first(),
filter(([, event]: [IUser, NavigationError | null]) => event != null),
map(([, event]: [IUser, NavigationError]) => event)
).subscribe(event => {
router.navigateByUrl(event.url);
});
}
ngDoBootstrap(): void {
super.ngDoBootstrap();
// Only do this after bootstrapping so that angularJs is loaded
// A hack to get href hash changes to always trigger an Angular Router update... There seems to be an AngularUpgrade/AngularJS/Cumulocity bug somewhere that stops the hashchange event firing.
// This bug is apparent when trying to use the AppSwitcher to change to another AppBuilder App, sometimes it works, sometimes it doesn't
const $injector = this.injector.get('$injector');
$injector.invoke(['$rootScope', ($rootScope) => {
$rootScope.$on('$locationChangeStart', (event, next, current) => {
const nextSplit = next.split('#');
const currentSplit = current.split('#');
// For gainsight tracking
if (nextSplit.length > 1) { this.trackRouteEvents(nextSplit[1]); }
if (nextSplit[0] != currentSplit[0]) {
return;
}
this.router.navigateByUrl(nextSplit.length > 1 ? nextSplit[1] : '');
});
}]);
// Hiding (+) icon sine its not relevant for app builder
const actionOutletInt = interval(1000);
const actionOutletSub = actionOutletInt.subscribe(async val => {
let actionOutlet = document.querySelector('c8y-action-outlet button') as any;
if (actionOutlet) {
actionOutlet.disabled = true;
actionOutlet.style.color = '#B0B9BF';
actionOutlet.style.display = 'none';
actionOutletSub.unsubscribe();
}
});
}
// Gainsight Events Tracking
private trackRouteEvents(url: string) {
const urlParams = url.split('/');
if (urlParams.length > 0) {
let appId = '';
let dashboardId = '';
let other = "";
if (urlParams.length > 2) {
urlParams.forEach((param, index) => {
switch (param) {
case 'application':
appId = urlParams[index + 1];
break;
case 'dashboard':
dashboardId = urlParams[index + 1];
break;
case 'config':
case 'branding':
case 'simulator-config':
other = param;
break;
default:
break;
}
});
if (window && window['aptrinsic']) {
window['aptrinsic']('track', 'gp_appbuilder_apppage_viewed', {
"appId": appId,
"tenantId": this.settingsService.getTenantName(),
"dashboardId": dashboardId,
"pageId": other
});
}
} else if (urlParams.length > 1) {
other = urlParams[1];
if (window && window['aptrinsic']) {
window['aptrinsic']('track', 'gp_appbuilder_page_viewed', {
"tenantId": this.settingsService.getTenantName(),
"pageId": other
});
}
}
}
}
}