2019-09-19 20:10:52 +03:00
|
|
|
///
|
2023-01-31 10:43:56 +02:00
|
|
|
/// Copyright © 2016-2023 The Thingsboard Authors
|
2019-09-19 20:10:52 +03:00
|
|
|
///
|
|
|
|
|
/// 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.
|
|
|
|
|
///
|
|
|
|
|
|
2021-02-02 15:20:43 +02:00
|
|
|
import {
|
|
|
|
|
Component,
|
2023-03-28 13:38:29 +03:00
|
|
|
ElementRef,
|
2021-02-02 15:20:43 +02:00
|
|
|
forwardRef,
|
|
|
|
|
Inject,
|
|
|
|
|
Injector,
|
|
|
|
|
Input,
|
|
|
|
|
OnInit,
|
|
|
|
|
StaticProvider,
|
|
|
|
|
ViewChild,
|
|
|
|
|
ViewContainerRef
|
|
|
|
|
} from '@angular/core';
|
2019-09-19 20:10:52 +03:00
|
|
|
import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms';
|
|
|
|
|
import { Observable, of } from 'rxjs';
|
|
|
|
|
import { PageLink } from '@shared/models/page/page-link';
|
|
|
|
|
import { map, share } from 'rxjs/operators';
|
|
|
|
|
import { emptyPageData, PageData } from '@shared/models/page/page-data';
|
|
|
|
|
import { DashboardInfo } from '@app/shared/models/dashboard.models';
|
|
|
|
|
import { DashboardService } from '@core/http/dashboard.service';
|
|
|
|
|
import { Store } from '@ngrx/store';
|
|
|
|
|
import { AppState } from '@app/core/core.state';
|
|
|
|
|
import { getCurrentAuthUser } from '@app/core/auth/auth.selectors';
|
|
|
|
|
import { Authority } from '@shared/models/authority.enum';
|
|
|
|
|
import { coerceBooleanProperty } from '@angular/cdk/coercion';
|
|
|
|
|
import { TooltipPosition } from '@angular/material/tooltip';
|
|
|
|
|
import { CdkOverlayOrigin, ConnectedPosition, Overlay, OverlayConfig, OverlayRef } from '@angular/cdk/overlay';
|
|
|
|
|
import { BreakpointObserver } from '@angular/cdk/layout';
|
|
|
|
|
import { DOCUMENT } from '@angular/common';
|
|
|
|
|
import { WINDOW } from '@core/services/window.service';
|
2021-02-02 15:20:43 +02:00
|
|
|
import { ComponentPortal } from '@angular/cdk/portal';
|
2019-09-19 20:10:52 +03:00
|
|
|
import {
|
|
|
|
|
DASHBOARD_SELECT_PANEL_DATA,
|
2023-03-28 13:38:29 +03:00
|
|
|
DashboardSelectPanelComponent
|
2019-09-19 20:10:52 +03:00
|
|
|
} from './dashboard-select-panel.component';
|
2019-09-23 20:35:31 +03:00
|
|
|
import { NULL_UUID } from '@shared/models/id/has-uuid';
|
2019-09-19 20:10:52 +03:00
|
|
|
|
2019-12-23 14:36:44 +02:00
|
|
|
// @dynamic
|
2019-09-19 20:10:52 +03:00
|
|
|
@Component({
|
|
|
|
|
selector: 'tb-dashboard-select',
|
|
|
|
|
templateUrl: './dashboard-select.component.html',
|
|
|
|
|
styleUrls: ['./dashboard-select.component.scss'],
|
|
|
|
|
providers: [{
|
|
|
|
|
provide: NG_VALUE_ACCESSOR,
|
|
|
|
|
useExisting: forwardRef(() => DashboardSelectComponent),
|
|
|
|
|
multi: true
|
|
|
|
|
}]
|
|
|
|
|
})
|
|
|
|
|
export class DashboardSelectComponent implements ControlValueAccessor, OnInit {
|
|
|
|
|
|
|
|
|
|
@Input()
|
|
|
|
|
dashboardsScope: 'customer' | 'tenant';
|
|
|
|
|
|
|
|
|
|
@Input()
|
|
|
|
|
customerId: string;
|
|
|
|
|
|
|
|
|
|
@Input()
|
|
|
|
|
tooltipPosition: TooltipPosition = 'above';
|
|
|
|
|
|
|
|
|
|
private requiredValue: boolean;
|
|
|
|
|
get required(): boolean {
|
|
|
|
|
return this.requiredValue;
|
|
|
|
|
}
|
|
|
|
|
@Input()
|
|
|
|
|
set required(value: boolean) {
|
|
|
|
|
this.requiredValue = coerceBooleanProperty(value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Input()
|
|
|
|
|
disabled: boolean;
|
|
|
|
|
|
|
|
|
|
dashboards$: Observable<Array<DashboardInfo>>;
|
|
|
|
|
|
|
|
|
|
dashboardId: string | null;
|
|
|
|
|
|
2020-02-10 13:10:14 +02:00
|
|
|
@ViewChild('dashboardSelectPanelOrigin') dashboardSelectPanelOrigin: CdkOverlayOrigin;
|
2019-09-19 20:10:52 +03:00
|
|
|
|
|
|
|
|
private propagateChange = (v: any) => { };
|
|
|
|
|
|
|
|
|
|
constructor(private store: Store<AppState>,
|
|
|
|
|
private dashboardService: DashboardService,
|
|
|
|
|
private overlay: Overlay,
|
|
|
|
|
private breakpointObserver: BreakpointObserver,
|
|
|
|
|
private viewContainerRef: ViewContainerRef,
|
2023-03-28 13:38:29 +03:00
|
|
|
private nativeElement: ElementRef,
|
2019-09-19 20:10:52 +03:00
|
|
|
@Inject(DOCUMENT) private document: Document,
|
|
|
|
|
@Inject(WINDOW) private window: Window) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
registerOnChange(fn: any): void {
|
|
|
|
|
this.propagateChange = fn;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
registerOnTouched(fn: any): void {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ngOnInit() {
|
|
|
|
|
|
|
|
|
|
const pageLink = new PageLink(100);
|
|
|
|
|
|
|
|
|
|
this.dashboards$ = this.getDashboards(pageLink).pipe(
|
|
|
|
|
map((pageData) => pageData.data),
|
|
|
|
|
share()
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setDisabledState(isDisabled: boolean): void {
|
|
|
|
|
this.disabled = isDisabled;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
writeValue(value: string | null): void {
|
|
|
|
|
this.dashboardId = value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
dashboardIdChanged() {
|
|
|
|
|
this.updateView();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
openDashboardSelectPanel() {
|
2023-03-28 13:38:29 +03:00
|
|
|
if (!this.disabled) {
|
|
|
|
|
const config = new OverlayConfig({
|
|
|
|
|
panelClass: 'tb-dashboard-select-panel',
|
|
|
|
|
backdropClass: 'cdk-overlay-transparent-backdrop',
|
|
|
|
|
hasBackdrop: true
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const connectedPosition: ConnectedPosition = {
|
|
|
|
|
originX: 'start',
|
|
|
|
|
originY: 'bottom',
|
|
|
|
|
overlayX: 'start',
|
|
|
|
|
overlayY: 'top'
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
config.positionStrategy = this.overlay.position().flexibleConnectedTo(this.nativeElement)
|
|
|
|
|
.withPositions([connectedPosition]);
|
|
|
|
|
const overlayRef = this.overlay.create(config);
|
|
|
|
|
overlayRef.backdropClick().subscribe(() => {
|
|
|
|
|
overlayRef.dispose();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const providers: StaticProvider[] = [
|
|
|
|
|
{
|
|
|
|
|
provide: DASHBOARD_SELECT_PANEL_DATA,
|
|
|
|
|
useValue: {
|
|
|
|
|
dashboards$: this.dashboards$,
|
|
|
|
|
dashboardId: this.dashboardId,
|
|
|
|
|
onDashboardSelected: (dashboardId) => {
|
|
|
|
|
overlayRef.dispose();
|
|
|
|
|
this.dashboardId = dashboardId;
|
|
|
|
|
this.updateView();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
provide: OverlayRef,
|
|
|
|
|
useValue: overlayRef
|
2019-09-19 20:10:52 +03:00
|
|
|
}
|
2023-03-28 13:38:29 +03:00
|
|
|
];
|
|
|
|
|
const injector = Injector.create({parent: this.viewContainerRef.injector, providers});
|
|
|
|
|
overlayRef.attach(new ComponentPortal(DashboardSelectPanelComponent, this.viewContainerRef, injector));
|
|
|
|
|
}
|
2019-09-19 20:10:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private updateView() {
|
|
|
|
|
this.propagateChange(this.dashboardId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private getDashboards(pageLink: PageLink): Observable<PageData<DashboardInfo>> {
|
|
|
|
|
let dashboardsObservable: Observable<PageData<DashboardInfo>>;
|
|
|
|
|
const authUser = getCurrentAuthUser(this.store);
|
|
|
|
|
if (this.dashboardsScope === 'customer' || authUser.authority === Authority.CUSTOMER_USER) {
|
2019-09-23 20:35:31 +03:00
|
|
|
if (this.customerId && this.customerId !== NULL_UUID) {
|
2019-11-15 12:22:14 +02:00
|
|
|
dashboardsObservable = this.dashboardService.getCustomerDashboards(this.customerId, pageLink,
|
|
|
|
|
{ignoreLoading: true});
|
2019-09-19 20:10:52 +03:00
|
|
|
} else {
|
|
|
|
|
dashboardsObservable = of(emptyPageData());
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2019-11-15 12:22:14 +02:00
|
|
|
dashboardsObservable = this.dashboardService.getTenantDashboards(pageLink, {ignoreLoading: true});
|
2019-09-19 20:10:52 +03:00
|
|
|
}
|
|
|
|
|
return dashboardsObservable;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|