12 changed files with 846 additions and 82 deletions
@ -0,0 +1,86 @@ |
|||||
|
<!-- |
||||
|
|
||||
|
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. |
||||
|
|
||||
|
--> |
||||
|
<ng-container [formGroup]="patternSettingsFormGroup"> |
||||
|
<div class="tb-form-panel stroked tb-slide-toggle"> |
||||
|
<mat-expansion-panel #expansionPanel class="tb-settings" [expanded]="settingsExpanded" |
||||
|
[disabled]="!patternSettingsFormGroup.get('show').value"> |
||||
|
<mat-expansion-panel-header class="flex flex-row flex-wrap"> |
||||
|
<mat-panel-title> |
||||
|
<div class="flex flex-1 flex-row items-center justify-between"> |
||||
|
<mat-slide-toggle class="mat-slide flex items-stretch justify-center" formControlName="show" (click)="$event.stopPropagation()"> |
||||
|
{{ (patternType === 'label' ? 'widgets.maps.data-layer.label' : 'widgets.maps.data-layer.tooltip') | translate }} |
||||
|
</mat-slide-toggle> |
||||
|
<tb-toggle-select [class.!hidden]="!expansionPanel.expanded" formControlName="type" (click)="$event.stopPropagation()"> |
||||
|
<tb-toggle-option [value]="DataLayerPatternType.pattern">{{ 'widgets.maps.data-layer.pattern-type-pattern' | translate }}</tb-toggle-option> |
||||
|
<tb-toggle-option [value]="DataLayerPatternType.function">{{ 'widgets.maps.data-layer.pattern-type-function' | translate }}</tb-toggle-option> |
||||
|
</tb-toggle-select> |
||||
|
</div> |
||||
|
</mat-panel-title> |
||||
|
</mat-expansion-panel-header> |
||||
|
<ng-template matExpansionPanelContent> |
||||
|
<tb-html *ngIf="patternSettingsFormGroup.get('type').value === DataLayerPatternType.pattern" |
||||
|
formControlName="pattern" |
||||
|
required |
||||
|
minHeight="100px" |
||||
|
label="{{ (patternType === 'label' ? 'widgets.maps.data-layer.label-pattern' : 'widgets.maps.data-layer.tooltip-pattern') | translate }}"> |
||||
|
</tb-html> |
||||
|
<tb-js-func *ngIf="patternSettingsFormGroup.get('type').value === DataLayerPatternType.function" |
||||
|
formControlName="patternFunction" |
||||
|
required |
||||
|
withModules |
||||
|
[globalVariables]="functionScopeVariables" |
||||
|
[functionArgs]="['data', 'dsData']" |
||||
|
functionTitle="{{ (patternType === 'label' ? 'widgets.maps.data-layer.label-function' : 'widgets.maps.data-layer.tooltip-function') | translate }}" |
||||
|
helpId="{{ patternType === 'label' ? 'widget/lib/map/label_fn' : 'widget/lib/map/tooltip_fn' }}"> |
||||
|
</tb-js-func> |
||||
|
<ng-container *ngIf="patternType === 'tooltip'"> |
||||
|
<div class="tb-form-row space-between column-xs"> |
||||
|
<div translate>widgets.maps.data-layer.tooltip-trigger</div> |
||||
|
<mat-form-field class="medium-width" appearance="outline" subscriptSizing="dynamic"> |
||||
|
<mat-select formControlName="trigger"> |
||||
|
<mat-option *ngFor="let trigger of dataLayerTooltipTriggers" [value]="trigger"> |
||||
|
{{ dataLayerTooltipTriggerTranslationMap.get(trigger) | translate }} |
||||
|
</mat-option> |
||||
|
</mat-select> |
||||
|
</mat-form-field> |
||||
|
</div> |
||||
|
<div class="tb-form-row"> |
||||
|
<mat-slide-toggle class="mat-slide" formControlName="autoclose"> |
||||
|
{{ 'widgets.maps.data-layer.auto-close-tooltips' | translate }} |
||||
|
</mat-slide-toggle> |
||||
|
</div> |
||||
|
<div *ngIf="hasTooltipOffset" class="tb-form-row space-between column-xs"> |
||||
|
<div translate>widgets.maps.data-layer.tooltip-offset</div> |
||||
|
<div class="flex flex-row items-center justify-start gap-2"> |
||||
|
<div class="tb-small-label" translate>widgets.maps.data-layer.tooltip-offset-horizontal</div> |
||||
|
<mat-form-field appearance="outline" class="number" subscriptSizing="dynamic"> |
||||
|
<input matInput formControlName="offsetX" |
||||
|
type="number" placeholder="{{ 'widget-config.set' | translate }}"> |
||||
|
</mat-form-field> |
||||
|
<div class="tb-small-label" translate>widgets.maps.data-layer.tooltip-offset-vertical</div> |
||||
|
<mat-form-field appearance="outline" class="number" subscriptSizing="dynamic"> |
||||
|
<input matInput formControlName="offsetY" |
||||
|
type="number" placeholder="{{ 'widget-config.set' | translate }}"> |
||||
|
</mat-form-field> |
||||
|
</div> |
||||
|
</div> |
||||
|
</ng-container> |
||||
|
</ng-template> |
||||
|
</mat-expansion-panel> |
||||
|
</div> |
||||
|
</ng-container> |
||||
@ -0,0 +1,180 @@ |
|||||
|
///
|
||||
|
/// 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, DestroyRef, forwardRef, Input, OnInit } from '@angular/core'; |
||||
|
import { |
||||
|
ControlValueAccessor, |
||||
|
NG_VALIDATORS, |
||||
|
NG_VALUE_ACCESSOR, |
||||
|
UntypedFormBuilder, |
||||
|
UntypedFormControl, |
||||
|
UntypedFormGroup, |
||||
|
Validator, |
||||
|
Validators |
||||
|
} from '@angular/forms'; |
||||
|
import { merge } from 'rxjs'; |
||||
|
import { WidgetService } from '@core/http/widget.service'; |
||||
|
import { takeUntilDestroyed } from '@angular/core/rxjs-interop'; |
||||
|
import { |
||||
|
DataLayerPatternSettings, |
||||
|
DataLayerPatternType, |
||||
|
DataLayerTooltipSettings, dataLayerTooltipTriggers, dataLayerTooltipTriggerTranslationMap |
||||
|
} from '@home/components/widget/lib/maps/map.models'; |
||||
|
import { coerceBoolean } from '@shared/decorators/coercion'; |
||||
|
|
||||
|
@Component({ |
||||
|
selector: 'tb-data-layer-pattern-settings', |
||||
|
templateUrl: './data-layer-pattern-settings.component.html', |
||||
|
styleUrls: ['./../../widget-settings.scss'], |
||||
|
providers: [ |
||||
|
{ |
||||
|
provide: NG_VALUE_ACCESSOR, |
||||
|
useExisting: forwardRef(() => DataLayerPatternSettingsComponent), |
||||
|
multi: true |
||||
|
}, |
||||
|
{ |
||||
|
provide: NG_VALIDATORS, |
||||
|
useExisting: forwardRef(() => DataLayerPatternSettingsComponent), |
||||
|
multi: true |
||||
|
} |
||||
|
] |
||||
|
}) |
||||
|
export class DataLayerPatternSettingsComponent implements OnInit, ControlValueAccessor, Validator { |
||||
|
|
||||
|
DataLayerPatternType = DataLayerPatternType; |
||||
|
|
||||
|
dataLayerTooltipTriggers = dataLayerTooltipTriggers; |
||||
|
|
||||
|
dataLayerTooltipTriggerTranslationMap = dataLayerTooltipTriggerTranslationMap; |
||||
|
|
||||
|
settingsExpanded = false; |
||||
|
|
||||
|
functionScopeVariables = this.widgetService.getWidgetScopeVariables(); |
||||
|
|
||||
|
@Input() |
||||
|
disabled: boolean; |
||||
|
|
||||
|
@Input() |
||||
|
patternType: 'label' | 'tooltip' = 'label'; |
||||
|
|
||||
|
@Input() |
||||
|
@coerceBoolean() |
||||
|
hasTooltipOffset = false; |
||||
|
|
||||
|
private modelValue: DataLayerPatternSettings | DataLayerTooltipSettings; |
||||
|
|
||||
|
private propagateChange = null; |
||||
|
|
||||
|
public patternSettingsFormGroup: UntypedFormGroup; |
||||
|
|
||||
|
constructor(private fb: UntypedFormBuilder, |
||||
|
private widgetService: WidgetService, |
||||
|
private destroyRef: DestroyRef) { |
||||
|
} |
||||
|
|
||||
|
ngOnInit(): void { |
||||
|
|
||||
|
this.patternSettingsFormGroup = this.fb.group({ |
||||
|
show: [null, []], |
||||
|
type: [null, []], |
||||
|
pattern: [null, [Validators.required]], |
||||
|
patternFunction: [null, [Validators.required]] |
||||
|
}); |
||||
|
if (this.patternType === 'tooltip') { |
||||
|
this.patternSettingsFormGroup.addControl('trigger', this.fb.control(null, [])); |
||||
|
this.patternSettingsFormGroup.addControl('autoclose', this.fb.control(null, [])); |
||||
|
if (this.hasTooltipOffset) { |
||||
|
this.patternSettingsFormGroup.addControl('offsetX', this.fb.control(null, [])); |
||||
|
this.patternSettingsFormGroup.addControl('offsetY', this.fb.control(null, [])); |
||||
|
} |
||||
|
} |
||||
|
this.patternSettingsFormGroup.valueChanges.pipe( |
||||
|
takeUntilDestroyed(this.destroyRef) |
||||
|
).subscribe(() => { |
||||
|
this.updateModel(); |
||||
|
}); |
||||
|
merge(this.patternSettingsFormGroup.get('show').valueChanges, |
||||
|
this.patternSettingsFormGroup.get('type').valueChanges |
||||
|
).pipe( |
||||
|
takeUntilDestroyed(this.destroyRef) |
||||
|
).subscribe(() => { |
||||
|
this.updateValidators(); |
||||
|
}); |
||||
|
} |
||||
|
|
||||
|
registerOnChange(fn: any): void { |
||||
|
this.propagateChange = fn; |
||||
|
} |
||||
|
|
||||
|
registerOnTouched(_fn: any): void { |
||||
|
} |
||||
|
|
||||
|
setDisabledState(isDisabled: boolean): void { |
||||
|
this.disabled = isDisabled; |
||||
|
if (isDisabled) { |
||||
|
this.patternSettingsFormGroup.disable({emitEvent: false}); |
||||
|
} else { |
||||
|
this.patternSettingsFormGroup.enable({emitEvent: false}); |
||||
|
this.updateValidators(); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
writeValue(value: DataLayerPatternSettings | DataLayerTooltipSettings): void { |
||||
|
this.modelValue = value; |
||||
|
this.patternSettingsFormGroup.patchValue( |
||||
|
value, {emitEvent: false} |
||||
|
); |
||||
|
this.updateValidators(); |
||||
|
this.settingsExpanded = this.patternSettingsFormGroup.get('show').value; |
||||
|
this.patternSettingsFormGroup.get('show').valueChanges.pipe( |
||||
|
takeUntilDestroyed(this.destroyRef) |
||||
|
).subscribe((show) => { |
||||
|
this.settingsExpanded = show; |
||||
|
}); |
||||
|
} |
||||
|
|
||||
|
public validate(c: UntypedFormControl) { |
||||
|
const valid = this.patternSettingsFormGroup.valid; |
||||
|
return valid ? null : { |
||||
|
[this.patternType]: { |
||||
|
valid: false, |
||||
|
}, |
||||
|
}; |
||||
|
} |
||||
|
|
||||
|
private updateValidators() { |
||||
|
const show: boolean = this.patternSettingsFormGroup.get('show').value; |
||||
|
const type: DataLayerPatternType = this.patternSettingsFormGroup.get('type').value; |
||||
|
if (show) { |
||||
|
this.patternSettingsFormGroup.enable({emitEvent: false}); |
||||
|
if (type === DataLayerPatternType.pattern) { |
||||
|
this.patternSettingsFormGroup.get('pattern').enable({emitEvent: false}); |
||||
|
this.patternSettingsFormGroup.get('patternFunction').disable({emitEvent: false}); |
||||
|
} else { |
||||
|
this.patternSettingsFormGroup.get('pattern').disable({emitEvent: false}); |
||||
|
this.patternSettingsFormGroup.get('patternFunction').enable({emitEvent: false}); |
||||
|
} |
||||
|
} else { |
||||
|
this.patternSettingsFormGroup.disable({emitEvent: false}); |
||||
|
this.patternSettingsFormGroup.get('show').enable({emitEvent: false}); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
private updateModel() { |
||||
|
this.modelValue = this.patternSettingsFormGroup.getRawValue(); |
||||
|
this.propagateChange(this.modelValue); |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue