2019-08-09 19:13:18 +03:00
|
|
|
///
|
|
|
|
|
/// Copyright © 2016-2019 The Thingsboard Authors
|
|
|
|
|
///
|
|
|
|
|
/// 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.
|
|
|
|
|
///
|
|
|
|
|
|
2019-12-12 19:55:17 +02:00
|
|
|
import { AfterViewInit, Component, ElementRef, Inject, OnInit, ViewChild } from '@angular/core';
|
|
|
|
|
import { fromEvent, Observable } from 'rxjs';
|
2019-08-09 19:13:18 +03:00
|
|
|
import { select, Store } from '@ngrx/store';
|
2019-12-12 19:55:17 +02:00
|
|
|
import { debounceTime, distinctUntilChanged, map, mergeMap, take, tap } from 'rxjs/operators';
|
2019-08-09 19:13:18 +03:00
|
|
|
|
|
|
|
|
import { BreakpointObserver, BreakpointState } from '@angular/cdk/layout';
|
|
|
|
|
import { User } from '@shared/models/user.model';
|
|
|
|
|
import { PageComponent } from '@shared/components/page.component';
|
|
|
|
|
import { AppState } from '@core/core.state';
|
|
|
|
|
import { AuthService } from '@core/auth/auth.service';
|
|
|
|
|
import { UserService } from '@core/http/user.service';
|
|
|
|
|
import { MenuService } from '@core/services/menu.service';
|
2019-11-15 12:22:14 +02:00
|
|
|
import { getCurrentAuthState, selectAuthUser, selectUserDetails } from '@core/auth/auth.selectors';
|
2019-08-09 19:13:18 +03:00
|
|
|
import { MediaBreakpoints } from '@shared/models/constants';
|
|
|
|
|
import { ActionNotificationShow } from '@core/notification/notification.actions';
|
|
|
|
|
import { Router } from '@angular/router';
|
|
|
|
|
import * as screenfull from 'screenfull';
|
|
|
|
|
import { MatSidenav } from '@angular/material';
|
2019-11-15 12:22:14 +02:00
|
|
|
import { AuthState } from '@core/auth/auth.models';
|
|
|
|
|
import { WINDOW } from '@core/services/window.service';
|
2019-12-12 19:55:17 +02:00
|
|
|
import { ISearchableComponent, instanceOfSearchableComponent } from '@home/models/searchable-component.models';
|
2019-08-09 19:13:18 +03:00
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
|
selector: 'tb-home',
|
|
|
|
|
templateUrl: './home.component.html',
|
|
|
|
|
styleUrls: ['./home.component.scss']
|
|
|
|
|
})
|
2019-12-12 19:55:17 +02:00
|
|
|
export class HomeComponent extends PageComponent implements AfterViewInit, OnInit {
|
2019-08-09 19:13:18 +03:00
|
|
|
|
2019-11-15 12:22:14 +02:00
|
|
|
authState: AuthState = getCurrentAuthState(this.store);
|
|
|
|
|
|
|
|
|
|
forceFullscreen = this.authState.forceFullscreen;
|
|
|
|
|
|
2019-09-19 20:10:52 +03:00
|
|
|
activeComponent: any;
|
2019-12-12 19:55:17 +02:00
|
|
|
searchableComponent: ISearchableComponent;
|
2019-09-19 20:10:52 +03:00
|
|
|
|
2020-02-04 15:14:17 +02:00
|
|
|
sidenavMode: 'over' | 'push' | 'side' = 'side';
|
2019-08-09 19:13:18 +03:00
|
|
|
sidenavOpened = true;
|
|
|
|
|
|
|
|
|
|
logo = require('../../../assets/logo_title_white.svg');
|
|
|
|
|
|
2020-02-10 13:10:14 +02:00
|
|
|
@ViewChild('sidenav')
|
2019-08-09 19:13:18 +03:00
|
|
|
sidenav: MatSidenav;
|
|
|
|
|
|
2020-02-10 13:10:14 +02:00
|
|
|
@ViewChild('searchInput') searchInputField: ElementRef;
|
2019-12-12 19:55:17 +02:00
|
|
|
|
2019-08-09 19:13:18 +03:00
|
|
|
// @ts-ignore
|
|
|
|
|
fullscreenEnabled = screenfull.enabled;
|
|
|
|
|
|
|
|
|
|
authUser$: Observable<any>;
|
|
|
|
|
userDetails$: Observable<User>;
|
|
|
|
|
userDetailsString: Observable<string>;
|
|
|
|
|
|
2019-12-12 19:55:17 +02:00
|
|
|
searchEnabled = false;
|
|
|
|
|
showSearch = false;
|
|
|
|
|
searchText = '';
|
|
|
|
|
|
2019-08-09 19:13:18 +03:00
|
|
|
constructor(protected store: Store<AppState>,
|
2019-11-15 12:22:14 +02:00
|
|
|
@Inject(WINDOW) private window: Window,
|
2019-08-09 19:13:18 +03:00
|
|
|
private authService: AuthService,
|
|
|
|
|
private router: Router,
|
|
|
|
|
private userService: UserService, private menuService: MenuService,
|
|
|
|
|
public breakpointObserver: BreakpointObserver) {
|
|
|
|
|
super(store);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ngOnInit() {
|
|
|
|
|
|
|
|
|
|
this.authUser$ = this.store.pipe(select(selectAuthUser));
|
|
|
|
|
this.userDetails$ = this.store.pipe(select(selectUserDetails));
|
|
|
|
|
this.userDetailsString = this.userDetails$.pipe(map((user: User) => {
|
|
|
|
|
return JSON.stringify(user);
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
const isGtSm = this.breakpointObserver.isMatched(MediaBreakpoints['gt-sm']);
|
|
|
|
|
this.sidenavMode = isGtSm ? 'side' : 'over';
|
|
|
|
|
this.sidenavOpened = isGtSm;
|
|
|
|
|
|
|
|
|
|
this.breakpointObserver
|
|
|
|
|
.observe(MediaBreakpoints['gt-sm'])
|
|
|
|
|
.subscribe((state: BreakpointState) => {
|
|
|
|
|
if (state.matches) {
|
|
|
|
|
this.sidenavMode = 'side';
|
|
|
|
|
this.sidenavOpened = true;
|
|
|
|
|
} else {
|
|
|
|
|
this.sidenavMode = 'over';
|
|
|
|
|
this.sidenavOpened = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-12 19:55:17 +02:00
|
|
|
ngAfterViewInit() {
|
|
|
|
|
fromEvent(this.searchInputField.nativeElement, 'keyup')
|
|
|
|
|
.pipe(
|
|
|
|
|
debounceTime(150),
|
|
|
|
|
distinctUntilChanged(),
|
|
|
|
|
tap(() => {
|
|
|
|
|
this.searchTextUpdated();
|
|
|
|
|
})
|
|
|
|
|
)
|
|
|
|
|
.subscribe();
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-09 19:13:18 +03:00
|
|
|
sidenavClicked() {
|
|
|
|
|
if (this.sidenavMode === 'over') {
|
|
|
|
|
this.sidenav.toggle();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
toggleFullscreen() {
|
|
|
|
|
// @ts-ignore
|
|
|
|
|
if (screenfull.enabled) {
|
|
|
|
|
// @ts-ignore
|
|
|
|
|
screenfull.toggle();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
isFullscreen() {
|
|
|
|
|
// @ts-ignore
|
|
|
|
|
return screenfull.isFullscreen;
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-15 12:22:14 +02:00
|
|
|
goBack() {
|
|
|
|
|
this.window.history.back();
|
|
|
|
|
}
|
2019-12-12 19:55:17 +02:00
|
|
|
|
|
|
|
|
activeComponentChanged(activeComponent: any) {
|
|
|
|
|
this.showSearch = false;
|
|
|
|
|
this.searchText = '';
|
|
|
|
|
this.activeComponent = activeComponent;
|
|
|
|
|
if (this.activeComponent && instanceOfSearchableComponent(this.activeComponent)) {
|
|
|
|
|
this.searchEnabled = true;
|
|
|
|
|
this.searchableComponent = this.activeComponent;
|
|
|
|
|
} else {
|
|
|
|
|
this.searchEnabled = false;
|
|
|
|
|
this.searchableComponent = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
displaySearchMode(): boolean {
|
|
|
|
|
return this.searchEnabled && this.showSearch;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
openSearch() {
|
|
|
|
|
if (this.searchEnabled) {
|
|
|
|
|
this.showSearch = true;
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
this.searchInputField.nativeElement.focus();
|
|
|
|
|
this.searchInputField.nativeElement.setSelectionRange(0, 0);
|
|
|
|
|
}, 10);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
closeSearch() {
|
|
|
|
|
if (this.searchEnabled) {
|
|
|
|
|
this.showSearch = false;
|
|
|
|
|
if (this.searchText.length) {
|
|
|
|
|
this.searchText = '';
|
|
|
|
|
this.searchTextUpdated();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private searchTextUpdated() {
|
|
|
|
|
if (this.searchableComponent) {
|
|
|
|
|
this.searchableComponent.onSearchTextUpdated(this.searchText);
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-08-09 19:13:18 +03:00
|
|
|
}
|