thingsboard/ui-ngx/src/app/modules/home/components/widget/action/widget-action-dialog.component.ts

496 lines
20 KiB
TypeScript
Raw Normal View History

2019-10-24 19:52:19 +03:00
///
2023-01-31 10:43:56 +02:00
/// Copyright © 2016-2023 The Thingsboard Authors
2019-10-24 19:52:19 +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.
///
import { Component, ElementRef, Inject, OnInit, SkipSelf, ViewChild } from '@angular/core';
2020-02-10 13:15:29 +02:00
import { ErrorStateMatcher } from '@angular/material/core';
2023-02-17 19:24:01 +02:00
import { MAT_DIALOG_DATA, MatDialogRef } from '@angular/material/dialog';
2019-10-24 19:52:19 +03:00
import { Store } from '@ngrx/store';
import { AppState } from '@core/core.state';
import {
2023-02-02 15:55:06 +02:00
UntypedFormBuilder,
UntypedFormControl,
UntypedFormGroup,
2019-10-24 19:52:19 +03:00
FormGroupDirective,
NgForm,
ValidatorFn,
Validators
} from '@angular/forms';
import { Observable, of, Subscription } from 'rxjs';
2019-10-24 19:52:19 +03:00
import { Router } from '@angular/router';
import { DialogComponent } from '@app/shared/components/dialog.component';
import {
toCustomAction,
WidgetActionCallbacks,
WidgetActionDescriptorInfo,
WidgetActionsData
} from '@home/components/widget/action/manage-widget-actions.component.models';
import { UtilsService } from '@core/services/utils.service';
import {
WidgetActionSource,
WidgetActionType,
widgetActionTypeTranslationMap
} from '@shared/models/widget.models';
2019-10-24 19:52:19 +03:00
import { map, mergeMap, startWith, tap } from 'rxjs/operators';
import { DashboardService } from '@core/http/dashboard.service';
import { Dashboard } from '@shared/models/dashboard.models';
import { DashboardUtilsService } from '@core/services/dashboard-utils.service';
import { CustomActionEditorCompleter } from '@home/components/widget/action/custom-action.models';
import { isDefinedAndNotNull } from '@core/utils';
import { MobileActionEditorComponent } from '@home/components/widget/action/mobile-action-editor.component';
import { widgetType } from '@shared/models/widget.models';
import { WidgetService } from '@core/http/widget.service';
import { TranslateService } from '@ngx-translate/core';
import { PopoverPlacement, PopoverPlacements } from '@shared/components/popover.models';
2019-10-24 19:52:19 +03:00
export interface WidgetActionDialogData {
isAdd: boolean;
callbacks: WidgetActionCallbacks;
actionsData: WidgetActionsData;
action?: WidgetActionDescriptorInfo;
2021-05-20 13:25:34 +03:00
widgetType: widgetType;
2019-10-24 19:52:19 +03:00
}
const stateDisplayTypes = ['normal', 'separateDialog', 'popover'] as const;
type stateDisplayTypeTuple = typeof stateDisplayTypes;
export type stateDisplayType = stateDisplayTypeTuple[number];
const stateDisplayTypesTranslations = new Map<stateDisplayType, string>(
[
['normal', 'widget-action.open-normal'],
['separateDialog', 'widget-action.open-in-separate-dialog'],
['popover', 'widget-action.open-in-popover'],
]
);
2019-10-24 19:52:19 +03:00
@Component({
selector: 'tb-widget-action-dialog',
templateUrl: './widget-action-dialog.component.html',
providers: [{provide: ErrorStateMatcher, useExisting: WidgetActionDialogComponent}],
2023-02-27 16:24:50 +02:00
styleUrls: []
2019-10-24 19:52:19 +03:00
})
export class WidgetActionDialogComponent extends DialogComponent<WidgetActionDialogComponent,
WidgetActionDescriptorInfo> implements OnInit, ErrorStateMatcher {
2020-02-10 13:10:14 +02:00
@ViewChild('dashboardStateInput') dashboardStateInput: ElementRef;
2019-10-24 19:52:19 +03:00
@ViewChild('mobileActionEditor', {static: false}) mobileActionEditor: MobileActionEditorComponent;
2023-02-02 15:55:06 +02:00
widgetActionFormGroup: UntypedFormGroup;
actionTypeFormGroup: UntypedFormGroup;
actionTypeFormGroupSubscriptions: Subscription[] = [];
2023-02-02 15:55:06 +02:00
stateDisplayTypeFormGroup: UntypedFormGroup;
2019-10-24 19:52:19 +03:00
isAdd: boolean;
action: WidgetActionDescriptorInfo;
widgetActionTypes = Object.keys(WidgetActionType);
widgetActionTypeTranslations = widgetActionTypeTranslationMap;
widgetActionType = WidgetActionType;
filteredDashboardStates: Observable<Array<string>>;
targetDashboardStateSearchText = '';
selectedDashboardStateIds: Observable<Array<string>>;
customActionEditorCompleter = CustomActionEditorCompleter;
2019-10-24 19:52:19 +03:00
submitted = false;
widgetType = widgetType;
2019-10-24 19:52:19 +03:00
functionScopeVariables: string[];
allStateDisplayTypes = stateDisplayTypes;
allPopoverPlacements = PopoverPlacements;
2019-10-24 19:52:19 +03:00
constructor(protected store: Store<AppState>,
protected router: Router,
private utils: UtilsService,
private dashboardService: DashboardService,
private dashboardUtils: DashboardUtilsService,
private widgetService: WidgetService,
private translate: TranslateService,
2019-10-24 19:52:19 +03:00
@Inject(MAT_DIALOG_DATA) public data: WidgetActionDialogData,
@SkipSelf() private errorStateMatcher: ErrorStateMatcher,
public dialogRef: MatDialogRef<WidgetActionDialogComponent, WidgetActionDescriptorInfo>,
2023-02-02 15:55:06 +02:00
public fb: UntypedFormBuilder) {
2019-10-24 19:52:19 +03:00
super(store, router, dialogRef);
this.isAdd = data.isAdd;
if (this.isAdd) {
this.action = {
id: this.utils.guid(),
name: '',
icon: 'more_horiz',
type: null
};
} else {
this.action = this.data.action;
}
this.functionScopeVariables = this.widgetService.getWidgetScopeVariables();
2019-10-24 19:52:19 +03:00
}
ngOnInit(): void {
this.widgetActionFormGroup = this.fb.group({});
this.widgetActionFormGroup.addControl('actionSourceId',
this.fb.control(this.action.actionSourceId, [Validators.required]));
this.widgetActionFormGroup.addControl('name',
this.fb.control(this.action.name, [this.validateActionName(), Validators.required]));
this.widgetActionFormGroup.addControl('icon',
this.fb.control(this.action.icon, [Validators.required]));
this.widgetActionFormGroup.addControl('useShowWidgetActionFunction',
this.fb.control(this.action.useShowWidgetActionFunction, []));
this.widgetActionFormGroup.addControl('showWidgetActionFunction',
this.fb.control(this.action.showWidgetActionFunction || 'return true;', []));
2019-10-24 19:52:19 +03:00
this.widgetActionFormGroup.addControl('type',
this.fb.control(this.action.type, [Validators.required]));
this.updateShowWidgetActionForm();
2019-10-24 19:52:19 +03:00
this.updateActionTypeFormGroup(this.action.type, this.action);
this.widgetActionFormGroup.get('type').valueChanges.subscribe((type: WidgetActionType) => {
this.updateActionTypeFormGroup(type);
});
this.widgetActionFormGroup.get('actionSourceId').valueChanges.subscribe(() => {
this.widgetActionFormGroup.get('name').updateValueAndValidity();
this.updateShowWidgetActionForm();
});
this.widgetActionFormGroup.get('useShowWidgetActionFunction').valueChanges.subscribe(() => {
this.updateShowWidgetActionForm();
2019-10-24 19:52:19 +03:00
});
}
displayShowWidgetActionForm(): boolean {
return !!this.data.actionsData.actionSources[this.widgetActionFormGroup.get('actionSourceId').value]?.hasShowCondition;
}
getWidgetActionFunctionHelpId(): string | undefined {
const actionSourceId = this.widgetActionFormGroup.get('actionSourceId').value;
if (actionSourceId === 'headerButton') {
return 'widget/action/show_widget_action_header_fn';
} else if (actionSourceId === 'actionCellButton') {
return 'widget/action/show_widget_action_cell_fn';
}
return undefined;
}
private updateShowWidgetActionForm() {
const actionSourceId = this.widgetActionFormGroup.get('actionSourceId').value;
const useShowWidgetActionFunction = this.widgetActionFormGroup.get('useShowWidgetActionFunction').value;
if (!!this.data.actionsData.actionSources[actionSourceId]?.hasShowCondition && useShowWidgetActionFunction) {
this.widgetActionFormGroup.get('showWidgetActionFunction').setValidators([Validators.required]);
} else {
this.widgetActionFormGroup.get('showWidgetActionFunction').clearValidators();
}
this.widgetActionFormGroup.get('showWidgetActionFunction').updateValueAndValidity();
}
2019-10-24 19:52:19 +03:00
private updateActionTypeFormGroup(type?: WidgetActionType, action?: WidgetActionDescriptorInfo) {
this.actionTypeFormGroupSubscriptions.forEach(s => s.unsubscribe());
this.actionTypeFormGroupSubscriptions.length = 0;
2019-10-24 19:52:19 +03:00
this.actionTypeFormGroup = this.fb.group({});
if (type) {
switch (type) {
case WidgetActionType.openDashboard:
case WidgetActionType.openDashboardState:
case WidgetActionType.updateDashboardState:
this.actionTypeFormGroup.addControl(
'targetDashboardStateId',
this.fb.control(action ? action.targetDashboardStateId : null,
type === WidgetActionType.openDashboardState ? [Validators.required] : [])
);
this.actionTypeFormGroup.addControl(
'setEntityId',
2021-05-20 13:25:34 +03:00
this.fb.control(this.data.widgetType === widgetType.static ? false : action ? action.setEntityId : true, [])
2019-10-24 19:52:19 +03:00
);
this.actionTypeFormGroup.addControl(
'stateEntityParamName',
this.fb.control(action ? action.stateEntityParamName : null, [])
);
if (type === WidgetActionType.openDashboard) {
this.actionTypeFormGroup.addControl(
'openNewBrowserTab',
this.fb.control(action ? action.openNewBrowserTab : false, [])
);
2019-10-24 19:52:19 +03:00
this.actionTypeFormGroup.addControl(
'targetDashboardId',
this.fb.control(action ? action.targetDashboardId : null,
[Validators.required])
);
this.setupSelectedDashboardStateIds(action ? action.targetDashboardId : null);
} else {
if (type === WidgetActionType.openDashboardState) {
const displayType = this.getStateDisplayType(action);
this.actionTypeFormGroup.addControl(
'stateDisplayType',
this.fb.control(this.getStateDisplayType(action), [Validators.required])
);
this.updateStateDisplayTypeFormGroup(displayType, action);
this.actionTypeFormGroupSubscriptions.push(
this.actionTypeFormGroup.get('stateDisplayType').valueChanges.subscribe((displayTypeValue: stateDisplayType) => {
this.updateStateDisplayTypeFormGroup(displayTypeValue);
})
);
}
2019-10-24 19:52:19 +03:00
this.actionTypeFormGroup.addControl(
'openRightLayout',
this.fb.control(action ? action.openRightLayout : false, [])
);
}
this.setupFilteredDashboardStates();
break;
case WidgetActionType.custom:
this.actionTypeFormGroup.addControl(
'customFunction',
this.fb.control(action ? action.customFunction : null, [])
);
break;
case WidgetActionType.customPretty:
this.actionTypeFormGroup.addControl(
'customAction',
this.fb.control(toCustomAction(action), [Validators.required])
);
break;
case WidgetActionType.mobileAction:
this.actionTypeFormGroup.addControl(
'mobileAction',
this.fb.control(action ? action.mobileAction : null, [Validators.required])
);
break;
2019-10-24 19:52:19 +03:00
}
}
}
private updateStateDisplayTypeFormGroup(displayType?: stateDisplayType, action?: WidgetActionDescriptorInfo) {
this.stateDisplayTypeFormGroup = this.fb.group({});
if (displayType) {
switch (displayType) {
case 'normal':
break;
case 'separateDialog':
this.stateDisplayTypeFormGroup.addControl(
'dialogTitle',
this.fb.control(action ? action.dialogTitle : '', [])
);
this.stateDisplayTypeFormGroup.addControl(
'dialogHideDashboardToolbar',
this.fb.control(action && isDefinedAndNotNull(action.dialogHideDashboardToolbar)
? action.dialogHideDashboardToolbar : true, [])
);
this.stateDisplayTypeFormGroup.addControl(
'dialogWidth',
this.fb.control(action ? action.dialogWidth : null, [Validators.min(1), Validators.max(100)])
);
this.stateDisplayTypeFormGroup.addControl(
'dialogHeight',
this.fb.control(action ? action.dialogHeight : null, [Validators.min(1), Validators.max(100)])
);
break;
case 'popover':
this.stateDisplayTypeFormGroup.addControl(
'popoverPreferredPlacement',
this.fb.control(action && isDefinedAndNotNull(action.popoverPreferredPlacement)
? action.popoverPreferredPlacement : 'top', [])
);
this.stateDisplayTypeFormGroup.addControl(
'popoverHideOnClickOutside',
this.fb.control(action && isDefinedAndNotNull(action.popoverHideOnClickOutside)
? action.popoverHideOnClickOutside : true, [])
);
this.stateDisplayTypeFormGroup.addControl(
'popoverHideDashboardToolbar',
this.fb.control(action && isDefinedAndNotNull(action.popoverHideDashboardToolbar)
? action.popoverHideDashboardToolbar : true, [])
);
this.stateDisplayTypeFormGroup.addControl(
'popoverWidth',
this.fb.control(action && isDefinedAndNotNull(action.popoverWidth) ? action.popoverWidth : '25vw', [])
);
this.stateDisplayTypeFormGroup.addControl(
'popoverHeight',
this.fb.control(action && isDefinedAndNotNull(action.popoverHeight) ? action.popoverHeight : '25vh', [])
);
this.stateDisplayTypeFormGroup.addControl(
'popoverStyle',
this.fb.control(action && isDefinedAndNotNull(action.popoverStyle) ? action.popoverStyle : {}, [])
);
break;
}
}
}
private getStateDisplayType(action?: WidgetActionDescriptorInfo): stateDisplayType {
let res: stateDisplayType = 'normal';
if (action) {
if (action.openInSeparateDialog) {
res = 'separateDialog';
} else if (action.openInPopover) {
res = 'popover';
}
}
return res;
}
2019-10-24 19:52:19 +03:00
private setupSelectedDashboardStateIds(targetDashboardId?: string) {
this.selectedDashboardStateIds =
this.actionTypeFormGroup.get('targetDashboardId').valueChanges.pipe(
// startWith<string>(targetDashboardId),
tap(() => {
this.targetDashboardStateSearchText = '';
}),
mergeMap((dashboardId) => {
if (dashboardId) {
return this.dashboardService.getDashboard(dashboardId);
} else {
return of(null);
}
}),
map((dashboard: Dashboard) => {
if (dashboard) {
dashboard = this.dashboardUtils.validateAndUpdateDashboard(dashboard);
const states = dashboard.configuration.states;
return Object.keys(states);
} else {
return [];
}
})
);
}
private setupFilteredDashboardStates() {
this.targetDashboardStateSearchText = '';
this.filteredDashboardStates = this.actionTypeFormGroup.get('targetDashboardStateId').valueChanges
.pipe(
startWith(''),
map(value => value ? value : ''),
mergeMap(name => this.fetchDashboardStates(name) )
);
}
private fetchDashboardStates(searchText?: string): Observable<Array<string>> {
this.targetDashboardStateSearchText = searchText;
if (this.widgetActionFormGroup.get('type').value === WidgetActionType.openDashboard) {
return this.selectedDashboardStateIds.pipe(
map(stateIds => {
const result = searchText ? stateIds.filter(this.createFilterForDashboardState(searchText)) : stateIds;
if (result && result.length) {
return result;
} else {
return [searchText];
}
})
);
} else {
return of(this.data.callbacks.fetchDashboardStates(searchText));
}
}
private createFilterForDashboardState(query: string): (stateId: string) => boolean {
const lowercaseQuery = query.toLowerCase();
return stateId => stateId.toLowerCase().indexOf(lowercaseQuery) === 0;
}
public clearTargetDashboardState(value: string = '') {
this.dashboardStateInput.nativeElement.value = value;
this.actionTypeFormGroup.get('targetDashboardStateId').patchValue(value, {emitEvent: true});
setTimeout(() => {
this.dashboardStateInput.nativeElement.blur();
this.dashboardStateInput.nativeElement.focus();
}, 0);
}
private validateActionName(): ValidatorFn {
2023-02-02 15:55:06 +02:00
return (c: UntypedFormControl) => {
2019-10-24 19:52:19 +03:00
const newName = c.value;
const valid = this.checkActionName(newName, this.widgetActionFormGroup.get('actionSourceId').value);
return !valid ? {
actionNameNotUnique: true
} : null;
};
}
private checkActionName(name: string, actionSourceId: string): boolean {
let actionNameIsUnique = true;
if (name && actionSourceId) {
const sourceActions = this.data.actionsData.actionsMap[actionSourceId];
if (sourceActions) {
const result = sourceActions.filter((sourceAction) => sourceAction.name === name);
if (result && result.length && result[0].id !== this.action.id) {
actionNameIsUnique = false;
}
}
}
return actionNameIsUnique;
}
2023-02-02 15:55:06 +02:00
isErrorState(control: UntypedFormControl | null, form: FormGroupDirective | NgForm | null): boolean {
2019-10-24 19:52:19 +03:00
const originalErrorState = this.errorStateMatcher.isErrorState(control, form);
const customErrorState = !!(control && control.invalid && this.submitted);
return originalErrorState || customErrorState;
}
public actionSourceName(actionSource: WidgetActionSource): string {
if (actionSource) {
return this.utils.customTranslation(actionSource.name, actionSource.name);
} else {
return '';
}
}
public stateDisplayTypeName(displayType: stateDisplayType): string {
if (displayType) {
return this.translate.instant(stateDisplayTypesTranslations.get(displayType)) + '';
} else {
return '';
}
}
public popoverPlacementName(placement: PopoverPlacement): string {
if (placement) {
return this.translate.instant(`widget-action.popover-placement-${placement}`) + '';
} else {
return '';
}
}
2019-10-24 19:52:19 +03:00
cancel(): void {
this.dialogRef.close(null);
}
save(): void {
this.submitted = true;
if (this.mobileActionEditor != null) {
this.mobileActionEditor.validateOnSubmit();
}
if (this.widgetActionFormGroup.valid && this.actionTypeFormGroup.valid) {
const type: WidgetActionType = this.widgetActionFormGroup.get('type').value;
let result: WidgetActionDescriptorInfo;
if (type === WidgetActionType.customPretty) {
result = {...this.widgetActionFormGroup.value, ...this.actionTypeFormGroup.get('customAction').value};
} else {
result = {...this.widgetActionFormGroup.value, ...this.actionTypeFormGroup.value};
}
if (this.actionTypeFormGroup.get('stateDisplayType') &&
this.actionTypeFormGroup.get('stateDisplayType').value !== 'normal') {
result = {...result, ...this.stateDisplayTypeFormGroup.value};
result.openInSeparateDialog = this.actionTypeFormGroup.get('stateDisplayType').value === 'separateDialog';
result.openInPopover = this.actionTypeFormGroup.get('stateDisplayType').value === 'popover';
} else {
result.openInSeparateDialog = false;
result.openInPopover = false;
}
delete (result as any).stateDisplayType;
result.id = this.action.id;
this.dialogRef.close(result);
2019-10-24 19:52:19 +03:00
}
}
}