FarmMapsLib/projects/common/src/fm/components/app/app.component.ts

228 lines
8.6 KiB
TypeScript
Raw Normal View History

2022-02-03 11:00:09 +00:00
import { Component, OnInit, OnDestroy, Inject, Optional,ViewEncapsulation, RendererFactory2, PLATFORM_ID, ChangeDetectionStrategy, HostListener, Input } from '@angular/core';
2020-01-27 16:28:17 +00:00
import { Router, NavigationStart, NavigationEnd, RouteConfigLoadStart, RouteConfigLoadEnd, ActivatedRoute, PRIMARY_OUTLET } from '@angular/router';
2020-10-29 18:14:06 +00:00
import { Meta, Title, MetaDefinition } from '@angular/platform-browser'; import { DOCUMENT } from "@angular/common";
import { Subscription, Observable } from 'rxjs';
import { distinctUntilChanged } from 'rxjs/operators';
2019-11-25 13:34:51 +00:00
import { Store, Action } from '@ngrx/store';
2020-06-12 10:24:08 +00:00
import { IUser } from '../../models/user';
2020-10-29 18:14:06 +00:00
import { OAuthService, OAuthErrorEvent } from 'angular-oauth2-oidc';
2022-02-03 11:00:09 +00:00
import { FM_COMMON_STARTPAGE} from '../../common.module';
2019-11-25 13:34:51 +00:00
//AppCommon
import { IEventMessage } from '../../models/event.message';
2020-10-29 18:14:06 +00:00
import { IListItem } from '../../models/list.item';
2019-11-25 13:34:51 +00:00
import { EventService } from '../../services/event.service';
2020-10-29 18:14:06 +00:00
import { ItemTypeService } from '../../services/itemtype.service';
import * as commonActions from '../../actions/app-common.actions';
2020-07-22 18:31:12 +00:00
import { HealthCheckService } from '../../services/healthcheck.service';
2020-10-29 18:14:06 +00:00
import { AppConfig } from '../../shared/app.config';
2019-11-25 13:34:51 +00:00
import * as appReducers from '../../reducers/app-common.reducer';
@Component({
selector: 'fm-app',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss'],
encapsulation: ViewEncapsulation.None,
changeDetection: ChangeDetectionStrategy.OnPush
})
export class AppComponent implements OnInit, OnDestroy {
// This will go at the END of your title for example "Home - Angular Universal..." <-- after the dash (-)
2023-03-06 13:04:14 +00:00
private endPageTitle = 'Farmmaps';
2019-11-25 13:34:51 +00:00
// If no Title is provided, we'll use a default one before the dash(-)
2023-03-06 13:04:14 +00:00
private defaultPageTitle = 'Farmmaps';
2019-11-25 13:34:51 +00:00
private routerSub$: Subscription;
private eventSub$: Subscription;
public currentFolder: Observable<IListItem>;
public folderParents: Observable<IListItem[]>;
2020-10-29 18:14:06 +00:00
public fullScreen: Observable<boolean> = this.store$.select(appReducers.selectGetFullScreen);
public isOnline: Observable<boolean> = this.store$.select(appReducers.SelectGetIsOnline);
public routeLoading: Observable<boolean> = this.store$.select(appReducers.selectGetRouteLoading);
public menuVisible: Observable<boolean> = this.store$.select(appReducers.SelectGetMenuVisible);
public accountMenuVisible: Observable<boolean> = this.store$.select(appReducers.SelectGetAccountMenuVisible);
2020-12-09 20:45:38 +00:00
public appMenuVisible: Observable<boolean> = this.store$.select(appReducers.SelectGetAppMenuVisible);
2021-02-11 10:44:34 +00:00
public notificationMenuVisible: Observable<boolean> = this.store$.select(appReducers.SelectGetNotificationMenuVisible);
2021-02-16 15:49:23 +00:00
public helpMenuVisible: Observable<boolean> = this.store$.select(appReducers.SelectGetHelpMenuVisible);
2021-02-11 10:44:34 +00:00
public unreadNotifications: Observable<number> = this.store$.select(appReducers.SelectgetUnreadNotifications);
2020-10-29 18:14:06 +00:00
public user: Observable<IUser> = this.store$.select(appReducers.SelectGetUser);
public isPageMode: Observable<boolean> = this.store$.select(appReducers.SelectGetIsPageMode);
2023-03-06 13:04:14 +00:00
@Input() showUploadProgress = true;
2019-11-25 13:34:51 +00:00
constructor(
2022-02-03 11:00:09 +00:00
@Optional() @Inject(FM_COMMON_STARTPAGE) public startPage: string,
2020-10-29 18:14:06 +00:00
public router: Router,
private activatedRoute$: ActivatedRoute,
private title$: Title,
private meta$: Meta,
private store$: Store<appReducers.State>,
private eventService$: EventService,
private healthCheckService$: HealthCheckService,
private itemTypeService$: ItemTypeService,
private oauthService$: OAuthService,
private appConfig$: AppConfig
) {
2019-11-25 13:34:51 +00:00
}
2020-10-29 18:14:06 +00:00
2019-11-25 13:34:51 +00:00
getActionFromEvent(event: IEventMessage): Action {
2023-03-06 13:04:14 +00:00
let action: Action = null;
2019-11-25 13:34:51 +00:00
console.debug(`${event.eventType} Event received`);
switch (event.eventType) {
case "ItemChanged": {
action = new commonActions.ItemChangedEvent(event.itemCode, event.attributes);
break;
}
case "ItemAdded": {
action = new commonActions.ItemAddedEvent(event.itemCode, event.attributes);
break;
}
case "ItemDeleted": {
action = new commonActions.ItemDeletedEvent(event.itemCode, event.attributes);
break;
}
case "taskStart": {
action = new commonActions.TaskStartEvent(event.itemCode, event.attributes);
break;
}
case "taskEnd": {
action = new commonActions.TaskEndEvent(event.itemCode, event.attributes);
break;
}
case "taskError": {
action = new commonActions.TaskErrorEvent(event.itemCode, event.attributes);
break;
}
2020-08-05 18:05:44 +00:00
case "taskProgress": {
action = new commonActions.TaskProgressEvent(event.itemCode, event.attributes);
break;
}
2019-12-10 17:14:48 +00:00
case "deviceUpdate": {
action = new commonActions.DeviceUpdateEvent(event.itemCode, event.attributes);
break;
}
2021-02-11 10:44:34 +00:00
case "notification": {
action = new commonActions.NotificationEvent(event.attributes);
break;
2021-05-05 16:56:53 +00:00
}
case "DeleteUser": {
action = new commonActions.Logout();
break;
}
2019-11-25 13:34:51 +00:00
}
return action;
}
2020-12-08 15:14:58 +00:00
async loadItemTypes() {
await this.itemTypeService$.load(this.appConfig$)
}
2019-11-25 13:34:51 +00:00
ngOnInit() {
this.InstallRouteEventHandler();
this.InstallEventServiceEventHandler();
2020-10-29 18:14:06 +00:00
this.InstallAuthenticationEventHandler();
this.InstallHealthCheck();
//load item types
2020-12-08 15:14:58 +00:00
this.loadItemTypes();
2019-11-25 13:34:51 +00:00
}
@HostListener('document:keyup', ['$event'])
onKeyUp(event: KeyboardEvent) {
2023-03-06 13:04:14 +00:00
const x = event.keyCode;
2019-11-25 13:34:51 +00:00
if (x === 27) {
2020-10-29 18:14:06 +00:00
this.store$.dispatch(new commonActions.Escape(true, false));
}
}
2019-11-25 13:34:51 +00:00
ngOnDestroy() {
// Subscription clean-up
2020-10-29 18:14:06 +00:00
if (this.routerSub$) this.routerSub$.unsubscribe();
if (this.eventSub$) this.eventSub$.unsubscribe();
}
private InstallAuthenticationEventHandler() {
// auth event handler
this.oauthService$.events.subscribe((event) => {
console.debug(event.type);
if (event.type == 'token_error' || event.type == 'silent_refresh_timeout' || event.type == 'logout') {
2023-03-06 13:04:14 +00:00
const e = event as OAuthErrorEvent;
const p = e.params as any;
2020-10-29 18:14:06 +00:00
if (event.type == 'silent_refresh_timeout' || event.type == 'logout' || (p.error && p.error == 'login_required')) {
console.debug("Session expired");
this.router.navigate(['loggedout'], { queryParams: { redirectTo: this.router.url } });
}
}
2020-10-30 07:22:58 +00:00
if(event.type == 'token_received') {
this.store$.dispatch(new commonActions.InitUser());
}
2020-10-29 18:14:06 +00:00
});
2020-10-30 11:37:02 +00:00
if(this.oauthService$.hasValidAccessToken()) {
2020-10-30 07:22:58 +00:00
this.store$.dispatch(new commonActions.InitUser());
} else {
if(this.oauthService$.getRefreshToken() !=null) {
this.oauthService$.refreshToken();
}
}
2019-11-25 13:34:51 +00:00
}
private InstallRouteEventHandler() {
2023-03-06 13:04:14 +00:00
const other = this;
2019-11-25 13:34:51 +00:00
this.routerSub$ = this.router.events.subscribe(event => {
if (event instanceof RouteConfigLoadStart) {
2020-10-29 18:14:06 +00:00
other.store$.dispatch(new commonActions.StartRouteLoading());
}
if (event instanceof NavigationEnd && (event.url == "/" || event.url.startsWith("/content") )) {
other.store$.dispatch(new commonActions.SetPageMode(true));
} else if (event instanceof NavigationEnd) {
other.store$.dispatch(new commonActions.SetPageMode(false));
2019-11-25 13:34:51 +00:00
}
if (event instanceof RouteConfigLoadEnd) {
2020-10-29 18:14:06 +00:00
other.store$.dispatch(new commonActions.EndRouteLoading());
}
if (event instanceof NavigationStart) {
other.store$.dispatch(new commonActions.SetMenuVisible(false));
2020-01-27 16:28:17 +00:00
}
2020-10-29 18:14:06 +00:00
});
2019-11-25 13:34:51 +00:00
}
private InstallEventServiceEventHandler() {
2023-03-06 13:04:14 +00:00
const other = this;
2020-10-29 18:14:06 +00:00
this.eventSub$ = this.eventService$.event.subscribe(event => {
2023-03-06 13:04:14 +00:00
const action = other.getActionFromEvent(event);
2020-10-29 18:14:06 +00:00
if (action) other.store$.dispatch(action);
});
}
private InstallHealthCheck() {
//check health every 30 seconds
this.healthCheckService$.check(30000).pipe(distinctUntilChanged()).subscribe((online) => {
if (online) {
this.store$.dispatch(new commonActions.Online());
} else {
this.store$.dispatch(new commonActions.Offline());
}
2019-11-25 13:34:51 +00:00
});
}
handleClick(event: MouseEvent) {
2020-10-29 18:14:06 +00:00
this.store$.dispatch(new commonActions.Escape(false, true));
}
2020-01-07 15:33:56 +00:00
2020-01-07 20:43:48 +00:00
handleStopBubble(event: MouseEvent) {
event.stopPropagation();
2020-10-29 18:14:06 +00:00
}
2020-01-07 20:43:48 +00:00
2020-10-29 18:14:06 +00:00
handleToggleMenu(event: MouseEvent) {
2020-01-08 13:23:24 +00:00
event.stopPropagation();
2020-10-29 18:14:06 +00:00
this.store$.dispatch(new commonActions.ToggleMenu());
}
handleHome(event: MouseEvent) {
this.router.navigate(['/']);
2020-01-07 15:33:56 +00:00
}
2019-11-25 13:34:51 +00:00
}