|
|
@ -14,14 +14,14 @@ |
|
|
/// limitations under the License.
|
|
|
/// limitations under the License.
|
|
|
///
|
|
|
///
|
|
|
|
|
|
|
|
|
import { Component, forwardRef, Input, OnDestroy, OnInit } from '@angular/core'; |
|
|
import { Component, forwardRef, Input, OnChanges, OnDestroy, OnInit, SimpleChanges } from '@angular/core'; |
|
|
import { ControlValueAccessor, FormBuilder, FormGroup, NG_VALUE_ACCESSOR } from '@angular/forms'; |
|
|
import { ControlValueAccessor, FormBuilder, FormGroup, NG_VALUE_ACCESSOR } from '@angular/forms'; |
|
|
import { TimeService } from '@core/services/time.service'; |
|
|
import { TimeService } from '@core/services/time.service'; |
|
|
import { coerceNumberProperty } from '@angular/cdk/coercion'; |
|
|
import { coerceNumberProperty } from '@angular/cdk/coercion'; |
|
|
import { MatFormFieldAppearance, SubscriptSizing } from '@angular/material/form-field'; |
|
|
import { MatFormFieldAppearance, SubscriptSizing } from '@angular/material/form-field'; |
|
|
import { coerceBoolean } from '@shared/decorators/coercion'; |
|
|
import { coerceBoolean } from '@shared/decorators/coercion'; |
|
|
import { Interval, IntervalMath, TimeInterval } from '@shared/models/time/time.models'; |
|
|
import { Interval, IntervalMath, TimeInterval } from '@shared/models/time/time.models'; |
|
|
import { isDefined } from '@core/utils'; |
|
|
import { isDefined, isEqual } from '@core/utils'; |
|
|
import { IntervalType } from '@shared/models/telemetry/telemetry.models'; |
|
|
import { IntervalType } from '@shared/models/telemetry/telemetry.models'; |
|
|
import { takeUntil } from 'rxjs/operators'; |
|
|
import { takeUntil } from 'rxjs/operators'; |
|
|
import { Subject } from 'rxjs'; |
|
|
import { Subject } from 'rxjs'; |
|
|
@ -38,12 +38,13 @@ import { Subject } from 'rxjs'; |
|
|
} |
|
|
} |
|
|
] |
|
|
] |
|
|
}) |
|
|
}) |
|
|
export class TimeintervalComponent implements OnInit, ControlValueAccessor, OnDestroy { |
|
|
export class TimeintervalComponent implements OnInit, ControlValueAccessor, OnChanges, OnDestroy { |
|
|
|
|
|
|
|
|
minValue: number; |
|
|
minValue: number; |
|
|
maxValue: number; |
|
|
maxValue: number; |
|
|
|
|
|
|
|
|
disabledAdvancedState = false; |
|
|
disabledAdvancedState = false; |
|
|
|
|
|
allowedIntervalsList: Array<Interval>; |
|
|
|
|
|
|
|
|
@Input() |
|
|
@Input() |
|
|
set min(min: number) { |
|
|
set min(min: number) { |
|
|
@ -80,6 +81,15 @@ export class TimeintervalComponent implements OnInit, ControlValueAccessor, OnDe |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Input() |
|
|
|
|
|
set allowedIntervals(allowedIntervals: Array<Interval>) { |
|
|
|
|
|
console.log('set', allowedIntervals); |
|
|
|
|
|
if (!this.allowedIntervalsList || !isEqual(allowedIntervals, this.allowedIntervalsList)) { |
|
|
|
|
|
this.allowedIntervalsList = allowedIntervals; |
|
|
|
|
|
this.updateIntervalValue(true); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
@Input() |
|
|
@Input() |
|
|
@coerceBoolean() |
|
|
@coerceBoolean() |
|
|
useCalendarIntervals = false; |
|
|
useCalendarIntervals = false; |
|
|
@ -155,6 +165,10 @@ export class TimeintervalComponent implements OnInit, ControlValueAccessor, OnDe |
|
|
this.boundInterval(); |
|
|
this.boundInterval(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
ngOnChanges({allowedIntervals}: SimpleChanges): void { |
|
|
|
|
|
console.log('changes', allowedIntervals); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
registerOnChange(fn: any): void { |
|
|
registerOnChange(fn: any): void { |
|
|
this.propagateChange = fn; |
|
|
this.propagateChange = fn; |
|
|
if (isDefined(this.propagateChangeValue)) { |
|
|
if (isDefined(this.propagateChangeValue)) { |
|
|
@ -184,8 +198,11 @@ export class TimeintervalComponent implements OnInit, ControlValueAccessor, OnDe |
|
|
if (typeof this.modelValue !== 'undefined') { |
|
|
if (typeof this.modelValue !== 'undefined') { |
|
|
const min = this.timeService.boundMinInterval(this.minValue); |
|
|
const min = this.timeService.boundMinInterval(this.minValue); |
|
|
const max = this.timeService.boundMaxInterval(this.maxValue); |
|
|
const max = this.timeService.boundMaxInterval(this.maxValue); |
|
|
if (IntervalMath.numberValue(this.modelValue) >= min && IntervalMath.numberValue(this.modelValue) <= max) { |
|
|
if (this.allowedIntervalsList?.length || |
|
|
const advanced = !this.timeService.matchesExistingInterval(this.minValue, this.maxValue, this.modelValue, |
|
|
IntervalMath.numberValue(this.modelValue) >= min && IntervalMath.numberValue(this.modelValue) <= max) { |
|
|
|
|
|
const advanced = this.allowedIntervalsList?.length |
|
|
|
|
|
? !this.allowedIntervalsList.includes(this.modelValue) |
|
|
|
|
|
: !this.timeService.matchesExistingInterval(this.minValue, this.maxValue, this.modelValue, |
|
|
this.useCalendarIntervals); |
|
|
this.useCalendarIntervals); |
|
|
if (advanced && this.disabledAdvancedState) { |
|
|
if (advanced && this.disabledAdvancedState) { |
|
|
this.advanced = false; |
|
|
this.advanced = false; |
|
|
@ -225,20 +242,28 @@ export class TimeintervalComponent implements OnInit, ControlValueAccessor, OnDe |
|
|
private boundInterval(updateToPreferred = false) { |
|
|
private boundInterval(updateToPreferred = false) { |
|
|
const min = this.timeService.boundMinInterval(this.minValue); |
|
|
const min = this.timeService.boundMinInterval(this.minValue); |
|
|
const max = this.timeService.boundMaxInterval(this.maxValue); |
|
|
const max = this.timeService.boundMaxInterval(this.maxValue); |
|
|
this.intervals = this.timeService.getIntervals(this.minValue, this.maxValue, this.useCalendarIntervals); |
|
|
this.intervals = this.allowedIntervalsList?.length |
|
|
|
|
|
? this.timeService.intervalValuesToTimeIntervals(this.allowedIntervalsList) |
|
|
|
|
|
: this.timeService.getIntervals(this.minValue, this.maxValue, this.useCalendarIntervals); |
|
|
if (!this.disabledAdvancedState) { |
|
|
if (!this.disabledAdvancedState) { |
|
|
this.intervals.push(this.customTimeInterval); |
|
|
this.intervals.push(this.customTimeInterval); |
|
|
} |
|
|
} |
|
|
if (this.rendered) { |
|
|
if (this.rendered) { |
|
|
let newInterval = this.modelValue; |
|
|
let newInterval = this.modelValue; |
|
|
const newIntervalMs = IntervalMath.numberValue(newInterval); |
|
|
if (this.allowedIntervalsList?.length) { |
|
|
if (newIntervalMs < min) { |
|
|
if (!this.allowedIntervalsList.includes(newInterval) && !this.advanced) { |
|
|
newInterval = min; |
|
|
newInterval = this.allowedIntervalsList[0]; |
|
|
} else if (newIntervalMs >= max && updateToPreferred) { |
|
|
} |
|
|
newInterval = this.timeService.boundMaxInterval(max / 7); |
|
|
} else { |
|
|
} |
|
|
const newIntervalMs = IntervalMath.numberValue(newInterval); |
|
|
if (!this.advanced) { |
|
|
if (newIntervalMs < min) { |
|
|
newInterval = this.timeService.boundToPredefinedInterval(min, max, newInterval, this.useCalendarIntervals); |
|
|
newInterval = min; |
|
|
|
|
|
} else if (newIntervalMs >= max && updateToPreferred) { |
|
|
|
|
|
newInterval = this.timeService.boundMaxInterval(max / 7); |
|
|
|
|
|
} |
|
|
|
|
|
if (!this.advanced) { |
|
|
|
|
|
newInterval = this.timeService.boundToPredefinedInterval(min, max, newInterval, this.useCalendarIntervals); |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
if (newInterval !== this.modelValue) { |
|
|
if (newInterval !== this.modelValue) { |
|
|
this.setInterval(newInterval); |
|
|
this.setInterval(newInterval); |
|
|
|