You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
RTL/src/app/shared/components/navigation/side-navigation/side-navigation.component.ts

162 lines
6.5 KiB
TypeScript

import { Component, OnInit, OnDestroy, Output, EventEmitter } from '@angular/core';
import { Router, ActivatedRoute } from '@angular/router';
import { Subject, Observable, of } from 'rxjs';
import { takeUntil, filter } from 'rxjs/operators';
import { Store } from '@ngrx/store';
import { Actions } from '@ngrx/effects';
import { environment } from '../../../../../environments/environment';
import { FlatTreeControl } from '@angular/cdk/tree';
import { MatTreeFlatDataSource, MatTreeFlattener } from '@angular/material/tree';
import { Node, Settings } from '../../../models/RTLconfig';
import { LoggerService } from '../../../services/logger.service';
import { GetInfo, GetInfoChain } from '../../../models/lndModels';
import { MenuChildNode, FlatMenuNode, MENU_DATA } from '../../../models/navMenu';
import { RTLEffects } from '../../../store/rtl.effects';
import * as RTLActions from '../../../store/rtl.actions';
import * as fromRTLReducer from '../../../store/rtl.reducers';
@Component({
selector: 'rtl-side-navigation',
templateUrl: './side-navigation.component.html',
styleUrls: ['./side-navigation.component.scss']
})
export class SideNavigationComponent implements OnInit, OnDestroy {
@Output() ChildNavClicked = new EventEmitter<any>();
public selNode: Node;
public settings: Settings;
public version = '';
public information: GetInfo = {};
public informationChain: GetInfoChain = {};
public flgLoading = true;
public logoutNode = [{id: 200, parentId: 0, name: 'Logout', icon: 'eject'}];
public showLogout = false;
public numPendingChannels = 0;
public smallScreen = false;
public childRootRoute = '';
private unSubs = [new Subject(), new Subject(), new Subject()];
treeControl: FlatTreeControl<FlatMenuNode>;
treeControlLogout: FlatTreeControl<FlatMenuNode>;
treeFlattener: MatTreeFlattener<MenuChildNode, FlatMenuNode>;
treeFlattenerLogout: MatTreeFlattener<MenuChildNode, FlatMenuNode>;
navMenus: MatTreeFlatDataSource<MenuChildNode, FlatMenuNode>;
navMenusLogout: MatTreeFlatDataSource<MenuChildNode, FlatMenuNode>;
constructor(private logger: LoggerService, private store: Store<fromRTLReducer.State>, private actions$: Actions, private rtlEffects: RTLEffects, private router: Router, private activatedRoute: ActivatedRoute) {
this.version = environment.VERSION;
if (MENU_DATA.LNDChildren[MENU_DATA.LNDChildren.length - 1].id === 200) {
MENU_DATA.LNDChildren.pop();
}
this.treeFlattener = new MatTreeFlattener(this.transformer, this.getLevel, this.isExpandable, this.getChildren);
this.treeControl = new FlatTreeControl<FlatMenuNode>(this.getLevel, this.isExpandable);
this.navMenus = new MatTreeFlatDataSource(this.treeControl, this.treeFlattener);
this.navMenus.data = MENU_DATA.LNDChildren;
this.treeFlattenerLogout = new MatTreeFlattener(this.transformer, this.getLevel, this.isExpandable, this.getChildren);
this.treeControlLogout = new FlatTreeControl<FlatMenuNode>(this.getLevel, this.isExpandable);
this.navMenusLogout = new MatTreeFlatDataSource(this.treeControlLogout, this.treeFlattenerLogout);
this.navMenusLogout.data = this.logoutNode;
}
ngOnInit() {
this.store.select('rtlRoot')
.pipe(takeUntil(this.unSubs[0]))
.subscribe((rtlStore: fromRTLReducer.State) => {
this.selNode = rtlStore.selNode;
this.settings = this.selNode.settings;
this.information = rtlStore.information;
this.numPendingChannels = rtlStore.numberOfPendingChannels;
if (undefined !== this.information.identity_pubkey) {
if (undefined !== this.information.chains && typeof this.information.chains[0] === 'string') {
this.informationChain.chain = this.information.chains[0].toString();
this.informationChain.network = (this.information.testnet) ? 'Testnet' : 'Mainnet';
} else if (typeof this.information.chains[0] === 'object' && this.information.chains[0].hasOwnProperty('chain')) {
const getInfoChain = <GetInfoChain>this.information.chains[0];
this.informationChain.chain = getInfoChain.chain;
this.informationChain.network = getInfoChain.network;
}
} else {
this.informationChain.chain = '';
this.informationChain.network = '';
}
this.flgLoading = (undefined !== this.information.identity_pubkey) ? false : true;
this.showLogout = (sessionStorage.getItem('token')) ? true : false;
if (!sessionStorage.getItem('token')) {
this.flgLoading = false;
}
if (window.innerWidth <= 414) {
this.smallScreen = true;
}
if(+this.selNode.index === 1) {
this.navMenus.data = MENU_DATA.LNDChildren;
} else {
this.navMenus.data = MENU_DATA.CLChildren;
}
this.logger.info(rtlStore);
});
this.actions$
.pipe(
takeUntil(this.unSubs[2]),
filter((action) => action.type === RTLActions.SIGNOUT)
).subscribe(() => {
this.showLogout = false;
});
}
private transformer(node: MenuChildNode, level: number) { return new FlatMenuNode(!!node.children, level, node.id, node.parentId, node.name, node.icon, node.link); }
private getLevel(node: FlatMenuNode) { return node.level; }
private isExpandable(node: FlatMenuNode) { return node.expandable; }
private getChildren(node: MenuChildNode): Observable<MenuChildNode[]> { return of(node.children); }
hasChild(_: number, _nodeData: FlatMenuNode) { return _nodeData.expandable; }
toggleTree(node: FlatMenuNode) {
this.treeControl.collapseAll();
if (node.parentId === 0) {
this.treeControl.expandDescendants(node);
this.router.navigate([node.link], {relativeTo: this.activatedRoute.firstChild});
} else {
const parentNode = this.treeControl.dataNodes.filter(dataNode => {
return dataNode.id === node.parentId;
})[0];
this.treeControl.expandDescendants(parentNode);
}
}
onClick(node: MenuChildNode) {
if (node.name === 'Logout') {
this.store.dispatch(new RTLActions.OpenConfirmation({
width: '70%', data: { type: 'CONFIRM', titleMessage: 'Logout from this device?', noBtnText: 'Cancel', yesBtnText: 'Logout'
}}));
this.rtlEffects.closeConfirm
.pipe(takeUntil(this.unSubs[1]))
.subscribe(confirmRes => {
if (confirmRes) {
this.showLogout = false;
this.store.dispatch(new RTLActions.Signout());
}
});
}
this.ChildNavClicked.emit(node);
}
onChildNavClicked(node) {
this.ChildNavClicked.emit(node);
}
ngOnDestroy() {
this.unSubs.forEach(completeSub => {
completeSub.next();
completeSub.complete();
});
}
}