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

168 lines
5.9 KiB
TypeScript
Raw Normal View History

2020-01-29 09:23:07 +00:00
import { Component, OnInit, OnDestroy, Inject, 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';
2019-11-25 13:34:51 +00:00
import { Meta, Title, MetaDefinition } from '@angular/platform-browser';import { DOCUMENT } from "@angular/common";
import { Subscription , Observable } from 'rxjs';
2020-07-22 19:16:38 +00:00
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';
2019-11-25 13:34:51 +00:00
//AppCommon
import { IEventMessage } from '../../models/event.message';
import { IListItem} from '../../models/list.item';
import { EventService } from '../../services/event.service';
import * as commonActions from '../../actions/app-common.actions';
2020-07-22 18:31:12 +00:00
import { HealthCheckService } from '../../services/healthcheck.service';
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 (-)
private endPageTitle: string = 'Farmmaps';
// If no Title is provided, we'll use a default one before the dash(-)
private defaultPageTitle: string = 'Farmmaps';
private routerSub$: Subscription;
private eventSub$: Subscription;
public currentFolder: Observable<IListItem>;
public folderParents: Observable<IListItem[]>;
public fullScreen: Observable<boolean>;
2020-07-23 09:28:07 +00:00
public isOnline: Observable<boolean>;
2019-11-25 13:34:51 +00:00
public routeLoading: Observable<boolean>;
2020-01-29 09:23:07 +00:00
public menuVisible: Observable<boolean>;
2020-06-24 13:07:11 +00:00
public accountMenuVisible: Observable<boolean>;
2020-06-12 10:24:08 +00:00
public user:Observable<IUser>;
2020-01-29 09:23:07 +00:00
@Input() showUploadProgress: boolean =true;
2019-11-25 13:34:51 +00:00
constructor(
private router: Router,
private activatedRoute: ActivatedRoute,
private title: Title,
private meta: Meta,
private store: Store<appReducers.State>,
private eventService: EventService,
2020-07-22 18:31:12 +00:00
private healthCheckService: HealthCheckService
) {
2020-07-22 18:31:12 +00:00
//check health every 30 seconds
2020-07-22 19:16:38 +00:00
this.healthCheckService.check(30000).pipe(distinctUntilChanged()).subscribe((online) => {
2020-07-22 18:31:12 +00:00
if(online) {
2020-07-22 19:16:38 +00:00
this.store.dispatch(new commonActions.Online());
2020-07-22 18:31:12 +00:00
} else {
2020-07-22 19:16:38 +00:00
this.store.dispatch(new commonActions.Offline());
2020-07-22 18:31:12 +00:00
}
});
2019-11-25 13:34:51 +00:00
}
getActionFromEvent(event: IEventMessage): Action {
var action: Action = null;
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;
}
2019-11-25 13:34:51 +00:00
}
return action;
}
ngOnInit() {
this.fullScreen = this.store.select(appReducers.selectGetFullScreen);
2020-07-23 09:28:07 +00:00
this.isOnline = this.store.select(appReducers.SelectGetIsOnline);
2019-11-25 13:34:51 +00:00
this.routeLoading = this.store.select(appReducers.selectGetRouteLoading);
2020-01-07 15:33:56 +00:00
this.menuVisible = this.store.select(appReducers.SelectGetMenuVisible);
2020-06-24 13:07:11 +00:00
this.accountMenuVisible = this.store.select(appReducers.SelectGetAccountMenuVisible);
2020-06-12 10:24:08 +00:00
this.user = this.store.select(appReducers.SelectGetUser);
2019-11-25 13:34:51 +00:00
this.InstallRouteEventHandler();
this.InstallEventServiceEventHandler();
}
@HostListener('document:keyup', ['$event'])
onKeyUp(event: KeyboardEvent) {
2019-11-25 13:34:51 +00:00
let x = event.keyCode;
if (x === 27) {
this.store.dispatch(new commonActions.Escape(true,false));
}
}
2019-11-25 13:34:51 +00:00
ngOnDestroy() {
// Subscription clean-up
if(this.routerSub$) this.routerSub$.unsubscribe();
if(this.eventSub$) this.eventSub$.unsubscribe();
}
private InstallRouteEventHandler() {
var other = this;
this.routerSub$ = this.router.events.subscribe(event => {
if (event instanceof RouteConfigLoadStart) {
other.store.dispatch(new commonActions.StartRouteLoading());
}
if (event instanceof RouteConfigLoadEnd) {
other.store.dispatch(new commonActions.EndRouteLoading());
}
2020-01-27 16:28:17 +00:00
if(event instanceof NavigationStart) {
other.store.dispatch(new commonActions.SetMenuVisible(false));
}
2019-11-25 13:34:51 +00:00
});
}
private InstallEventServiceEventHandler() {
var other = this;
this.eventSub$ = this.eventService.event.subscribe(event => {
var action = other.getActionFromEvent(event);
if (action) other.store.dispatch(action);
});
}
handleClick(event: MouseEvent) {
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-01-08 13:23:24 +00:00
handleToggleMenu(event:MouseEvent) {
event.stopPropagation();
2020-01-07 15:33:56 +00:00
this.store.dispatch(new commonActions.ToggleMenu());
}
2019-11-25 13:34:51 +00:00
}