thingsboard/ui-ngx/src/app/modules/home/components/debug-config/debug-config-button.component.ts

103 lines
3.7 KiB
TypeScript
Raw Normal View History

///
/// Copyright © 2016-2024 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.
///
import {
Component,
Input,
Renderer2,
ViewContainerRef,
DestroyRef,
ChangeDetectionStrategy,
2024-11-12 17:13:58 +02:00
EventEmitter,
Output
} from '@angular/core';
import { CommonModule } from '@angular/common';
import { SharedModule } from '@shared/shared.module';
2024-11-13 14:50:23 +02:00
import { DurationLeftPipe } from '@shared/pipe/duration-left.pipe';
import { TbPopoverService } from '@shared/components/popover.service';
import { MatButton } from '@angular/material/button';
2024-11-13 14:50:23 +02:00
import { DebugConfigPanelComponent } from './debug-config-panel.component';
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
2024-11-13 15:09:29 +02:00
import { shareReplay, timer } from 'rxjs';
2024-11-12 17:13:58 +02:00
import { SECOND } from '@shared/models/time/time.models';
2024-11-13 14:50:23 +02:00
import { HasDebugConfig } from '@shared/models/entity.models';
import { map } from 'rxjs/operators';
2024-11-13 15:09:29 +02:00
import { getCurrentAuthState } from '@core/auth/auth.selectors';
import { AppState } from '@core/core.state';
import { Store } from '@ngrx/store';
@Component({
2024-11-12 17:13:58 +02:00
selector: 'tb-debug-config-button',
templateUrl: './debug-config-button.component.html',
2024-11-13 17:00:44 +02:00
styleUrls: ['./debug-config-button.component.scss'],
standalone: true,
imports: [
CommonModule,
SharedModule,
2024-11-13 14:50:23 +02:00
DurationLeftPipe,
],
changeDetection: ChangeDetectionStrategy.OnPush
})
2024-11-12 17:13:58 +02:00
export class DebugConfigButtonComponent {
2024-11-12 17:13:58 +02:00
@Input() debugFailures = false;
@Input() debugAll = false;
@Input() debugAllUntil = 0;
@Input() disabled = false;
2024-11-13 17:00:44 +02:00
@Input() minifyMode = false;
2024-11-13 15:09:29 +02:00
@Input() debugLimitsConfiguration: string;
2024-11-13 14:50:23 +02:00
@Output() onDebugConfigChanged = new EventEmitter<HasDebugConfig>();
2024-11-13 15:09:29 +02:00
isDebugAllActive$ = timer(0, SECOND).pipe(map(() => this.debugAllUntil > new Date().getTime()), shareReplay(1));
readonly maxDebugModeDurationMinutes = getCurrentAuthState(this.store).maxDebugModeDurationMinutes;
2024-11-13 14:50:23 +02:00
constructor(private popoverService: TbPopoverService,
private renderer: Renderer2,
2024-11-13 15:09:29 +02:00
private store: Store<AppState>,
private viewContainerRef: ViewContainerRef,
private destroyRef: DestroyRef,
2024-11-13 14:50:23 +02:00
) {}
openDebugStrategyPanel($event: Event, matButton: MatButton): void {
if ($event) {
$event.stopPropagation();
}
const trigger = matButton._elementRef.nativeElement;
if (this.popoverService.hasPopover(trigger)) {
this.popoverService.hidePopover(trigger);
} else {
const debugStrategyPopover = this.popoverService.displayPopover(trigger, this.renderer,
2024-11-12 17:13:58 +02:00
this.viewContainerRef, DebugConfigPanelComponent, 'bottom', true, null,
{
debugFailures: this.debugFailures,
debugAll: this.debugAll,
debugAllUntil: this.debugAllUntil,
2024-11-13 15:09:29 +02:00
maxDebugModeDurationMinutes: this.maxDebugModeDurationMinutes,
debugLimitsConfiguration: this.debugLimitsConfiguration
2024-11-12 17:13:58 +02:00
},
{},
{}, {}, true);
debugStrategyPopover.tbComponentRef.instance.popover = debugStrategyPopover;
2024-11-13 14:50:23 +02:00
debugStrategyPopover.tbComponentRef.instance.onConfigApplied.pipe(takeUntilDestroyed(this.destroyRef)).subscribe((config: HasDebugConfig) => {
2024-11-12 17:13:58 +02:00
this.onDebugConfigChanged.emit(config);
debugStrategyPopover.hide();
});
}
}
}