2019-08-12 19:34:23 +03:00
|
|
|
///
|
2023-01-31 10:43:56 +02:00
|
|
|
/// Copyright © 2016-2023 The Thingsboard Authors
|
2019-08-12 19:34:23 +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 {
|
2021-05-14 18:21:46 +03:00
|
|
|
ChangeDetectorRef,
|
2021-02-02 15:20:43 +02:00
|
|
|
Component,
|
2023-03-10 12:55:53 +02:00
|
|
|
ElementRef,
|
2021-02-02 15:20:43 +02:00
|
|
|
forwardRef,
|
2023-02-09 12:19:40 +02:00
|
|
|
Injector,
|
2021-02-02 15:20:43 +02:00
|
|
|
Input,
|
|
|
|
|
OnDestroy,
|
|
|
|
|
OnInit,
|
2023-02-09 12:19:40 +02:00
|
|
|
StaticProvider,
|
2021-02-02 15:20:43 +02:00
|
|
|
ViewContainerRef
|
|
|
|
|
} from '@angular/core';
|
2019-08-12 19:34:23 +03:00
|
|
|
import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms';
|
|
|
|
|
import { TranslateService } from '@ngx-translate/core';
|
|
|
|
|
import { MillisecondsToTimeStringPipe } from '@shared/pipe/milliseconds-to-time-string.pipe';
|
|
|
|
|
import {
|
2020-04-14 11:38:30 +03:00
|
|
|
cloneSelectedTimewindow,
|
2021-03-17 19:41:52 +02:00
|
|
|
getTimezoneInfo,
|
2019-08-12 19:34:23 +03:00
|
|
|
HistoryWindowType,
|
2020-04-14 11:38:30 +03:00
|
|
|
initModelFromDefaultTimewindow,
|
2021-03-15 16:29:42 +02:00
|
|
|
QuickTimeIntervalTranslationMap,
|
2021-03-16 12:33:45 +02:00
|
|
|
RealtimeWindowType,
|
2019-08-12 19:34:23 +03:00
|
|
|
Timewindow,
|
2020-04-14 11:38:30 +03:00
|
|
|
TimewindowType
|
2019-08-12 19:34:23 +03:00
|
|
|
} from '@shared/models/time/time.models';
|
2023-02-07 17:45:03 +02:00
|
|
|
import { DatePipe } from '@angular/common';
|
2023-04-21 18:00:56 +03:00
|
|
|
import {
|
|
|
|
|
TIMEWINDOW_PANEL_DATA,
|
|
|
|
|
TimewindowPanelComponent,
|
|
|
|
|
TimewindowPanelData
|
|
|
|
|
} from '@shared/components/time/timewindow-panel.component';
|
2019-08-12 19:34:23 +03:00
|
|
|
import { MediaBreakpoints } from '@shared/models/constants';
|
|
|
|
|
import { BreakpointObserver } from '@angular/cdk/layout';
|
|
|
|
|
import { TimeService } from '@core/services/time.service';
|
2020-02-10 19:06:15 +02:00
|
|
|
import { TooltipPosition } from '@angular/material/tooltip';
|
2021-03-17 19:41:52 +02:00
|
|
|
import { deepClone, isDefinedAndNotNull } from '@core/utils';
|
2020-02-25 19:11:25 +02:00
|
|
|
import { coerceBooleanProperty } from '@angular/cdk/coercion';
|
2023-02-09 12:19:40 +02:00
|
|
|
import { ConnectedPosition, Overlay, OverlayConfig, OverlayRef } from '@angular/cdk/overlay';
|
|
|
|
|
import { ComponentPortal } from '@angular/cdk/portal';
|
2023-05-04 17:46:43 +03:00
|
|
|
import { coerceBoolean } from '@shared/decorators/coercion';
|
2019-08-12 19:34:23 +03:00
|
|
|
|
2019-12-23 14:36:44 +02:00
|
|
|
// @dynamic
|
2019-08-12 19:34:23 +03:00
|
|
|
@Component({
|
|
|
|
|
selector: 'tb-timewindow',
|
|
|
|
|
templateUrl: './timewindow.component.html',
|
|
|
|
|
styleUrls: ['./timewindow.component.scss'],
|
|
|
|
|
providers: [
|
|
|
|
|
{
|
|
|
|
|
provide: NG_VALUE_ACCESSOR,
|
|
|
|
|
useExisting: forwardRef(() => TimewindowComponent),
|
|
|
|
|
multi: true
|
|
|
|
|
}
|
|
|
|
|
]
|
|
|
|
|
})
|
|
|
|
|
export class TimewindowComponent implements OnInit, OnDestroy, ControlValueAccessor {
|
|
|
|
|
|
|
|
|
|
historyOnlyValue = false;
|
|
|
|
|
|
|
|
|
|
@Input()
|
|
|
|
|
set historyOnly(val) {
|
2022-09-15 14:18:16 +03:00
|
|
|
const newHistoryOnlyValue = coerceBooleanProperty(val);
|
|
|
|
|
if (this.historyOnlyValue !== newHistoryOnlyValue) {
|
|
|
|
|
this.historyOnlyValue = newHistoryOnlyValue;
|
|
|
|
|
if (this.onHistoryOnlyChanged()) {
|
|
|
|
|
this.notifyChanged();
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-08-12 19:34:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
get historyOnly() {
|
|
|
|
|
return this.historyOnlyValue;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-21 18:00:56 +03:00
|
|
|
@Input()
|
|
|
|
|
@coerceBoolean()
|
|
|
|
|
forAllTimeEnabled = false;
|
|
|
|
|
|
2022-09-15 14:18:16 +03:00
|
|
|
alwaysDisplayTypePrefixValue = false;
|
|
|
|
|
|
|
|
|
|
@Input()
|
|
|
|
|
set alwaysDisplayTypePrefix(val) {
|
|
|
|
|
this.alwaysDisplayTypePrefixValue = coerceBooleanProperty(val);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
get alwaysDisplayTypePrefix() {
|
|
|
|
|
return this.alwaysDisplayTypePrefixValue;
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-13 14:07:56 +03:00
|
|
|
quickIntervalOnlyValue = false;
|
|
|
|
|
|
|
|
|
|
@Input()
|
|
|
|
|
set quickIntervalOnly(val) {
|
|
|
|
|
this.quickIntervalOnlyValue = coerceBooleanProperty(val);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
get quickIntervalOnly() {
|
|
|
|
|
return this.quickIntervalOnlyValue;
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-12 19:34:23 +03:00
|
|
|
aggregationValue = false;
|
|
|
|
|
|
|
|
|
|
@Input()
|
|
|
|
|
set aggregation(val) {
|
2020-02-25 19:11:25 +02:00
|
|
|
this.aggregationValue = coerceBooleanProperty(val);
|
2019-08-12 19:34:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
get aggregation() {
|
|
|
|
|
return this.aggregationValue;
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-17 18:38:57 +02:00
|
|
|
timezoneValue = false;
|
|
|
|
|
|
|
|
|
|
@Input()
|
|
|
|
|
set timezone(val) {
|
|
|
|
|
this.timezoneValue = coerceBooleanProperty(val);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
get timezone() {
|
|
|
|
|
return this.timezoneValue;
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-12 19:34:23 +03:00
|
|
|
isToolbarValue = false;
|
|
|
|
|
|
|
|
|
|
@Input()
|
|
|
|
|
set isToolbar(val) {
|
2020-02-25 19:11:25 +02:00
|
|
|
this.isToolbarValue = coerceBooleanProperty(val);
|
2019-08-12 19:34:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
get isToolbar() {
|
|
|
|
|
return this.isToolbarValue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
asButtonValue = false;
|
|
|
|
|
|
|
|
|
|
@Input()
|
|
|
|
|
set asButton(val) {
|
2020-02-25 19:11:25 +02:00
|
|
|
this.asButtonValue = coerceBooleanProperty(val);
|
2019-08-12 19:34:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
get asButton() {
|
|
|
|
|
return this.asButtonValue;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-21 18:00:56 +03:00
|
|
|
@Input()
|
|
|
|
|
@coerceBoolean()
|
|
|
|
|
strokedButton = false;
|
|
|
|
|
|
2020-02-25 19:11:25 +02:00
|
|
|
isEditValue = false;
|
|
|
|
|
|
|
|
|
|
@Input()
|
|
|
|
|
set isEdit(val) {
|
|
|
|
|
this.isEditValue = coerceBooleanProperty(val);
|
|
|
|
|
this.timewindowDisabled = this.isTimewindowDisabled();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
get isEdit() {
|
|
|
|
|
return this.isEditValue;
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-12 19:34:23 +03:00
|
|
|
@Input()
|
|
|
|
|
direction: 'left' | 'right' = 'left';
|
|
|
|
|
|
|
|
|
|
@Input()
|
|
|
|
|
tooltipPosition: TooltipPosition = 'above';
|
|
|
|
|
|
|
|
|
|
@Input() disabled: boolean;
|
|
|
|
|
|
|
|
|
|
innerValue: Timewindow;
|
|
|
|
|
|
2020-02-25 19:11:25 +02:00
|
|
|
timewindowDisabled: boolean;
|
|
|
|
|
|
2019-08-12 19:34:23 +03:00
|
|
|
private propagateChange = (_: any) => {};
|
|
|
|
|
|
2023-02-09 12:19:40 +02:00
|
|
|
constructor(private overlay: Overlay,
|
|
|
|
|
private translate: TranslateService,
|
2019-08-12 19:34:23 +03:00
|
|
|
private timeService: TimeService,
|
|
|
|
|
private millisecondsToTimeStringPipe: MillisecondsToTimeStringPipe,
|
|
|
|
|
private datePipe: DatePipe,
|
2021-05-14 18:21:46 +03:00
|
|
|
private cd: ChangeDetectorRef,
|
2023-03-10 12:55:53 +02:00
|
|
|
private nativeElement: ElementRef,
|
2019-08-12 19:34:23 +03:00
|
|
|
public viewContainerRef: ViewContainerRef,
|
2023-02-07 17:45:03 +02:00
|
|
|
public breakpointObserver: BreakpointObserver) {
|
2019-08-12 19:34:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ngOnInit(): void {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ngOnDestroy(): void {
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-07 17:45:03 +02:00
|
|
|
toggleTimewindow($event: Event) {
|
|
|
|
|
if ($event) {
|
|
|
|
|
$event.stopPropagation();
|
2019-08-12 19:34:23 +03:00
|
|
|
}
|
2023-03-10 12:55:53 +02:00
|
|
|
const config = new OverlayConfig({
|
|
|
|
|
panelClass: 'tb-timewindow-panel',
|
|
|
|
|
backdropClass: 'cdk-overlay-transparent-backdrop',
|
|
|
|
|
hasBackdrop: true,
|
|
|
|
|
maxHeight: '80vh',
|
|
|
|
|
height: 'min-content'
|
|
|
|
|
});
|
2023-02-09 12:19:40 +02:00
|
|
|
config.hasBackdrop = true;
|
|
|
|
|
const connectedPosition: ConnectedPosition = {
|
2023-03-10 12:55:53 +02:00
|
|
|
originX: 'start',
|
2023-02-09 12:19:40 +02:00
|
|
|
originY: 'bottom',
|
2023-03-10 12:55:53 +02:00
|
|
|
overlayX: 'start',
|
2023-02-09 12:19:40 +02:00
|
|
|
overlayY: 'top'
|
|
|
|
|
};
|
2023-03-10 12:55:53 +02:00
|
|
|
config.positionStrategy = this.overlay.position().flexibleConnectedTo(this.nativeElement)
|
2023-02-09 12:19:40 +02:00
|
|
|
.withPositions([connectedPosition]);
|
|
|
|
|
|
|
|
|
|
const overlayRef = this.overlay.create(config);
|
|
|
|
|
overlayRef.backdropClick().subscribe(() => {
|
|
|
|
|
overlayRef.dispose();
|
|
|
|
|
});
|
|
|
|
|
const providers: StaticProvider[] = [
|
|
|
|
|
{
|
|
|
|
|
provide: TIMEWINDOW_PANEL_DATA,
|
|
|
|
|
useValue: {
|
|
|
|
|
timewindow: deepClone(this.innerValue),
|
|
|
|
|
historyOnly: this.historyOnly,
|
2023-04-21 18:00:56 +03:00
|
|
|
forAllTimeEnabled: this.forAllTimeEnabled,
|
2023-02-09 12:19:40 +02:00
|
|
|
quickIntervalOnly: this.quickIntervalOnly,
|
|
|
|
|
aggregation: this.aggregation,
|
|
|
|
|
timezone: this.timezone,
|
|
|
|
|
isEdit: this.isEdit
|
2023-04-21 18:00:56 +03:00
|
|
|
} as TimewindowPanelData
|
2023-02-09 12:19:40 +02:00
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
provide: OverlayRef,
|
|
|
|
|
useValue: overlayRef
|
|
|
|
|
}
|
|
|
|
|
];
|
|
|
|
|
const injector = Injector.create({parent: this.viewContainerRef.injector, providers});
|
|
|
|
|
const componentRef = overlayRef.attach(new ComponentPortal(TimewindowPanelComponent,
|
|
|
|
|
this.viewContainerRef, injector));
|
|
|
|
|
componentRef.onDestroy(() => {
|
|
|
|
|
if (componentRef.instance.result) {
|
|
|
|
|
this.innerValue = componentRef.instance.result;
|
|
|
|
|
this.timewindowDisabled = this.isTimewindowDisabled();
|
|
|
|
|
this.updateDisplayValue();
|
|
|
|
|
this.notifyChanged();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
this.cd.detectChanges();
|
2019-08-12 19:34:23 +03:00
|
|
|
}
|
|
|
|
|
|
2022-09-15 14:18:16 +03:00
|
|
|
private onHistoryOnlyChanged(): boolean {
|
|
|
|
|
if (this.historyOnlyValue && this.innerValue) {
|
|
|
|
|
if (this.innerValue.selectedTab !== TimewindowType.HISTORY) {
|
|
|
|
|
this.innerValue.selectedTab = TimewindowType.HISTORY;
|
|
|
|
|
this.updateDisplayValue();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-12 19:34:23 +03:00
|
|
|
registerOnChange(fn: any): void {
|
|
|
|
|
this.propagateChange = fn;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
registerOnTouched(fn: any): void {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setDisabledState(isDisabled: boolean): void {
|
|
|
|
|
this.disabled = isDisabled;
|
2020-02-25 19:11:25 +02:00
|
|
|
this.timewindowDisabled = this.isTimewindowDisabled();
|
2019-08-12 19:34:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
writeValue(obj: Timewindow): void {
|
2022-09-13 14:07:56 +03:00
|
|
|
this.innerValue = initModelFromDefaultTimewindow(obj, this.quickIntervalOnly, this.timeService);
|
2020-02-25 19:11:25 +02:00
|
|
|
this.timewindowDisabled = this.isTimewindowDisabled();
|
2022-09-15 14:18:16 +03:00
|
|
|
if (this.onHistoryOnlyChanged()) {
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
this.notifyChanged();
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
this.updateDisplayValue();
|
|
|
|
|
}
|
2019-08-12 19:34:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
notifyChanged() {
|
2019-09-10 15:12:10 +03:00
|
|
|
this.propagateChange(cloneSelectedTimewindow(this.innerValue));
|
2019-08-12 19:34:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
updateDisplayValue() {
|
|
|
|
|
if (this.innerValue.selectedTab === TimewindowType.REALTIME && !this.historyOnly) {
|
2021-03-16 12:33:45 +02:00
|
|
|
this.innerValue.displayValue = this.translate.instant('timewindow.realtime') + ' - ';
|
|
|
|
|
if (this.innerValue.realtime.realtimeType === RealtimeWindowType.INTERVAL) {
|
|
|
|
|
this.innerValue.displayValue += this.translate.instant(QuickTimeIntervalTranslationMap.get(this.innerValue.realtime.quickInterval));
|
|
|
|
|
} else {
|
|
|
|
|
this.innerValue.displayValue += this.translate.instant('timewindow.last-prefix') + ' ' +
|
|
|
|
|
this.millisecondsToTimeStringPipe.transform(this.innerValue.realtime.timewindowMs);
|
|
|
|
|
}
|
2019-08-12 19:34:23 +03:00
|
|
|
} else {
|
2023-04-21 18:00:56 +03:00
|
|
|
this.innerValue.displayValue = (!this.historyOnly || this.alwaysDisplayTypePrefix) ?
|
|
|
|
|
(this.translate.instant('timewindow.history') + ' - ') : '';
|
2019-08-12 19:34:23 +03:00
|
|
|
if (this.innerValue.history.historyType === HistoryWindowType.LAST_INTERVAL) {
|
|
|
|
|
this.innerValue.displayValue += this.translate.instant('timewindow.last-prefix') + ' ' +
|
|
|
|
|
this.millisecondsToTimeStringPipe.transform(this.innerValue.history.timewindowMs);
|
2021-03-15 16:29:42 +02:00
|
|
|
} else if (this.innerValue.history.historyType === HistoryWindowType.INTERVAL) {
|
|
|
|
|
this.innerValue.displayValue += this.translate.instant(QuickTimeIntervalTranslationMap.get(this.innerValue.history.quickInterval));
|
2023-04-21 18:00:56 +03:00
|
|
|
} else if (this.innerValue.history.historyType === HistoryWindowType.FOR_ALL_TIME) {
|
|
|
|
|
this.innerValue.displayValue += this.translate.instant('timewindow.for-all-time');
|
2019-08-12 19:34:23 +03:00
|
|
|
} else {
|
|
|
|
|
const startString = this.datePipe.transform(this.innerValue.history.fixedTimewindow.startTimeMs, 'yyyy-MM-dd HH:mm:ss');
|
|
|
|
|
const endString = this.datePipe.transform(this.innerValue.history.fixedTimewindow.endTimeMs, 'yyyy-MM-dd HH:mm:ss');
|
|
|
|
|
this.innerValue.displayValue += this.translate.instant('timewindow.period', {startTime: startString, endTime: endString});
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-03-17 19:41:52 +02:00
|
|
|
if (isDefinedAndNotNull(this.innerValue.timezone) && this.innerValue.timezone !== '') {
|
|
|
|
|
this.innerValue.displayValue += ' ';
|
|
|
|
|
this.innerValue.displayTimezoneAbbr = getTimezoneInfo(this.innerValue.timezone).abbr;
|
|
|
|
|
} else {
|
|
|
|
|
this.innerValue.displayTimezoneAbbr = '';
|
|
|
|
|
}
|
2021-05-14 18:21:46 +03:00
|
|
|
this.cd.detectChanges();
|
2019-08-12 19:34:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
hideLabel() {
|
|
|
|
|
return this.isToolbar && !this.breakpointObserver.isMatched(MediaBreakpoints['gt-md']);
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-25 19:11:25 +02:00
|
|
|
private isTimewindowDisabled(): boolean {
|
|
|
|
|
return this.disabled ||
|
|
|
|
|
(!this.isEdit && (!this.innerValue || this.innerValue.hideInterval &&
|
|
|
|
|
(!this.aggregation || this.innerValue.hideAggregation && this.innerValue.hideAggInterval)));
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-12 19:34:23 +03:00
|
|
|
}
|