2019-10-24 19:52:19 +03:00
|
|
|
///
|
2024-01-09 10:46:16 +02:00
|
|
|
/// Copyright © 2016-2024 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.
|
|
|
|
|
///
|
|
|
|
|
|
2024-04-19 16:22:39 +03:00
|
|
|
import { Component, Inject, OnDestroy, 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 {
|
2024-03-08 12:37:23 +02:00
|
|
|
FormBuilder,
|
|
|
|
|
FormControl,
|
|
|
|
|
FormGroup,
|
2024-01-26 17:54:24 +02:00
|
|
|
FormGroupDirective,
|
|
|
|
|
NgForm,
|
2019-10-24 19:52:19 +03:00
|
|
|
ValidatorFn,
|
|
|
|
|
Validators
|
|
|
|
|
} from '@angular/forms';
|
2024-01-26 17:54:24 +02:00
|
|
|
import { Subject } from 'rxjs';
|
2019-10-24 19:52:19 +03:00
|
|
|
import { Router } from '@angular/router';
|
|
|
|
|
import { DialogComponent } from '@app/shared/components/dialog.component';
|
|
|
|
|
import {
|
2024-03-08 12:37:23 +02:00
|
|
|
toWidgetActionDescriptor,
|
2019-10-24 19:52:19 +03:00
|
|
|
WidgetActionCallbacks,
|
|
|
|
|
WidgetActionDescriptorInfo,
|
|
|
|
|
WidgetActionsData
|
|
|
|
|
} from '@home/components/widget/action/manage-widget-actions.component.models';
|
|
|
|
|
import { UtilsService } from '@core/services/utils.service';
|
2021-09-28 12:34:34 +03:00
|
|
|
import {
|
2024-03-08 12:37:23 +02:00
|
|
|
actionDescriptorToAction,
|
2024-04-19 16:22:39 +03:00
|
|
|
CellClickColumnInfo,
|
2024-03-08 12:37:23 +02:00
|
|
|
defaultWidgetAction,
|
2021-09-28 12:34:34 +03:00
|
|
|
WidgetActionSource,
|
2024-01-26 17:54:24 +02:00
|
|
|
widgetType
|
2021-09-28 12:34:34 +03:00
|
|
|
} from '@shared/models/widget.models';
|
2024-01-26 17:54:24 +02:00
|
|
|
import { takeUntil } from 'rxjs/operators';
|
2024-02-01 19:16:37 +02:00
|
|
|
import { CustomActionEditorCompleter } from '@home/components/widget/lib/settings/common/action/custom-action.models';
|
2021-09-28 10:33:31 +03:00
|
|
|
import { WidgetService } from '@core/http/widget.service';
|
2024-04-19 16:22:39 +03:00
|
|
|
import { isDefinedAndNotNull, isNotEmptyStr } from '@core/utils';
|
|
|
|
|
import { MatSelect } from '@angular/material/select';
|
|
|
|
|
import { TranslateService } from '@ngx-translate/core';
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@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,
|
2024-01-26 17:54:24 +02:00
|
|
|
WidgetActionDescriptorInfo> implements OnInit, OnDestroy, ErrorStateMatcher {
|
2021-05-04 19:44:15 +03:00
|
|
|
|
2023-12-21 17:54:32 +02:00
|
|
|
private destroy$ = new Subject<void>();
|
|
|
|
|
|
2024-03-08 12:37:23 +02:00
|
|
|
widgetActionFormGroup: FormGroup;
|
2019-10-24 19:52:19 +03:00
|
|
|
|
|
|
|
|
isAdd: boolean;
|
|
|
|
|
action: WidgetActionDescriptorInfo;
|
|
|
|
|
|
2020-05-20 19:42:58 +03:00
|
|
|
customActionEditorCompleter = CustomActionEditorCompleter;
|
|
|
|
|
|
2019-10-24 19:52:19 +03:00
|
|
|
submitted = false;
|
|
|
|
|
|
2021-09-28 08:41:39 +03:00
|
|
|
functionScopeVariables: string[];
|
|
|
|
|
|
2024-04-19 16:22:39 +03:00
|
|
|
configuredColumns: Array<CellClickColumnInfo> = [];
|
|
|
|
|
usedCellClickColumns: Array<number> = [];
|
|
|
|
|
|
|
|
|
|
@ViewChild('columnIndexSelect') columnIndexSelect: MatSelect;
|
|
|
|
|
columnIndexPlaceholderText = this.translate.instant('widget-config.select-column-index');
|
|
|
|
|
|
2019-10-24 19:52:19 +03:00
|
|
|
constructor(protected store: Store<AppState>,
|
|
|
|
|
protected router: Router,
|
|
|
|
|
private utils: UtilsService,
|
2021-09-28 08:41:39 +03:00
|
|
|
private widgetService: WidgetService,
|
2019-10-24 19:52:19 +03:00
|
|
|
@Inject(MAT_DIALOG_DATA) public data: WidgetActionDialogData,
|
|
|
|
|
@SkipSelf() private errorStateMatcher: ErrorStateMatcher,
|
|
|
|
|
public dialogRef: MatDialogRef<WidgetActionDialogComponent, WidgetActionDescriptorInfo>,
|
2024-04-19 16:22:39 +03:00
|
|
|
public fb: FormBuilder,
|
|
|
|
|
private translate: TranslateService) {
|
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',
|
2024-02-01 19:16:37 +02:00
|
|
|
...defaultWidgetAction(data.widgetType !== widgetType.static)
|
2019-10-24 19:52:19 +03:00
|
|
|
};
|
|
|
|
|
} else {
|
|
|
|
|
this.action = this.data.action;
|
|
|
|
|
}
|
2021-09-28 08:41:39 +03:00
|
|
|
this.functionScopeVariables = this.widgetService.getWidgetScopeVariables();
|
2024-04-19 16:22:39 +03:00
|
|
|
if (this.action.actionSourceId === 'cellClick') {
|
|
|
|
|
this.getCellClickColumnsInfo();
|
|
|
|
|
}
|
2019-10-24 19:52:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ngOnInit(): void {
|
2024-03-08 12:37:23 +02:00
|
|
|
this.widgetActionFormGroup = this.fb.group({
|
|
|
|
|
actionSourceId: [this.action.actionSourceId, Validators.required],
|
2024-05-17 19:04:39 +03:00
|
|
|
columnIndex: [this.checkColumnIndex(this.action.columnIndex)],
|
2024-03-08 12:37:23 +02:00
|
|
|
name: [this.action.name, [this.validateActionName(), Validators.required]],
|
|
|
|
|
icon: [this.action.icon, Validators.required],
|
|
|
|
|
useShowWidgetActionFunction: [this.action.useShowWidgetActionFunction],
|
|
|
|
|
showWidgetActionFunction: [this.action.showWidgetActionFunction || 'return true;'],
|
|
|
|
|
widgetAction: [actionDescriptorToAction(toWidgetActionDescriptor(this.action)), Validators.required]
|
|
|
|
|
});
|
2021-09-28 12:34:34 +03:00
|
|
|
this.updateShowWidgetActionForm();
|
2023-12-21 17:54:32 +02:00
|
|
|
this.widgetActionFormGroup.get('actionSourceId').valueChanges.pipe(
|
|
|
|
|
takeUntil(this.destroy$)
|
2024-04-19 16:22:39 +03:00
|
|
|
).subscribe((value) => {
|
2019-10-24 19:52:19 +03:00
|
|
|
this.widgetActionFormGroup.get('name').updateValueAndValidity();
|
2021-09-28 12:34:34 +03:00
|
|
|
this.updateShowWidgetActionForm();
|
2024-04-19 16:22:39 +03:00
|
|
|
if (value === 'cellClick') {
|
|
|
|
|
this.widgetActionFormGroup.get('columnIndex').setValidators([Validators.required]);
|
|
|
|
|
this.getCellClickColumnsInfo();
|
|
|
|
|
} else {
|
|
|
|
|
this.widgetActionFormGroup.get('columnIndex').clearValidators();
|
|
|
|
|
}
|
|
|
|
|
this.widgetActionFormGroup.get('columnIndex').updateValueAndValidity();
|
2021-09-28 12:34:34 +03:00
|
|
|
});
|
2023-12-21 17:54:32 +02:00
|
|
|
this.widgetActionFormGroup.get('useShowWidgetActionFunction').valueChanges.pipe(
|
|
|
|
|
takeUntil(this.destroy$)
|
|
|
|
|
).subscribe(() => {
|
2021-09-28 12:34:34 +03:00
|
|
|
this.updateShowWidgetActionForm();
|
2019-10-24 19:52:19 +03:00
|
|
|
});
|
2024-04-19 16:22:39 +03:00
|
|
|
setTimeout(() => {
|
|
|
|
|
if (this.action?.actionSourceId === 'cellClick' && isDefinedAndNotNull(this.action.columnIndex) &&
|
|
|
|
|
this.widgetActionFormGroup.get('columnIndex').value === null) {
|
2024-05-17 19:04:39 +03:00
|
|
|
this.widgetActionFormGroup.get('columnIndex').setValidators([Validators.required]);
|
|
|
|
|
this.widgetActionFormGroup.get('columnIndex').updateValueAndValidity();
|
2024-04-19 16:22:39 +03:00
|
|
|
this.columnIndexPlaceholderText = `${this.action.columnIndex} (${this.translate.instant('widget-config.not-set')})`;
|
|
|
|
|
this.columnIndexSelect.focus();
|
|
|
|
|
}
|
|
|
|
|
});
|
2019-10-24 19:52:19 +03:00
|
|
|
}
|
|
|
|
|
|
2023-12-21 17:54:32 +02:00
|
|
|
ngOnDestroy() {
|
|
|
|
|
this.destroy$.next();
|
|
|
|
|
this.destroy$.complete();
|
|
|
|
|
super.ngOnDestroy();
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-28 12:34:34 +03:00
|
|
|
displayShowWidgetActionForm(): boolean {
|
2021-09-28 17:58:32 +03:00
|
|
|
return !!this.data.actionsData.actionSources[this.widgetActionFormGroup.get('actionSourceId').value]?.hasShowCondition;
|
2021-09-28 12:34:34 +03:00
|
|
|
}
|
|
|
|
|
|
2021-10-06 21:03:29 +03:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-28 12:34:34 +03:00
|
|
|
private updateShowWidgetActionForm() {
|
|
|
|
|
const actionSourceId = this.widgetActionFormGroup.get('actionSourceId').value;
|
|
|
|
|
const useShowWidgetActionFunction = this.widgetActionFormGroup.get('useShowWidgetActionFunction').value;
|
2021-09-28 17:58:32 +03:00
|
|
|
if (!!this.data.actionsData.actionSources[actionSourceId]?.hasShowCondition && useShowWidgetActionFunction) {
|
2021-09-28 12:34:34 +03:00
|
|
|
this.widgetActionFormGroup.get('showWidgetActionFunction').setValidators([Validators.required]);
|
|
|
|
|
} else {
|
|
|
|
|
this.widgetActionFormGroup.get('showWidgetActionFunction').clearValidators();
|
|
|
|
|
}
|
|
|
|
|
this.widgetActionFormGroup.get('showWidgetActionFunction').updateValueAndValidity();
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-19 16:22:39 +03:00
|
|
|
private checkColumnIndex(columnIndex: number): number | null {
|
|
|
|
|
return isDefinedAndNotNull(columnIndex) && this.configuredColumns.length - 1 < columnIndex ? null : columnIndex;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private getCellClickColumnsInfo(): void {
|
|
|
|
|
if (!this.configuredColumns.length) {
|
|
|
|
|
this.configuredColumns = this.data.callbacks.fetchCellClickColumns();
|
|
|
|
|
this.data.actionsData.actionsMap['cellClick']?.forEach(action => {
|
|
|
|
|
const actionColumn = this.configuredColumns[action.columnIndex];
|
|
|
|
|
if (actionColumn && action.columnIndex !== this.action.columnIndex) {
|
|
|
|
|
this.usedCellClickColumns.push(action.columnIndex);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-24 19:52:19 +03:00
|
|
|
private validateActionName(): ValidatorFn {
|
2024-03-08 12:37:23 +02:00
|
|
|
return (c: FormControl) => {
|
2019-10-24 19:52:19 +03:00
|
|
|
const newName = c.value;
|
2024-03-08 12:37:23 +02:00
|
|
|
const valid = this.checkActionName(newName, c.parent?.get('actionSourceId').value);
|
2019-10-24 19:52:19 +03:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-08 12:37:23 +02:00
|
|
|
isErrorState(control: FormControl | 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 '';
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-19 16:22:39 +03:00
|
|
|
public getCellClickColumnInfo(index: number, columnInfo: CellClickColumnInfo): string {
|
|
|
|
|
return `${index} (${isNotEmptyStr(columnInfo.label) ? columnInfo.label : columnInfo.name})`;
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-24 19:52:19 +03:00
|
|
|
cancel(): void {
|
|
|
|
|
this.dialogRef.close(null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
save(): void {
|
|
|
|
|
this.submitted = true;
|
2024-01-26 17:54:24 +02:00
|
|
|
if (this.widgetActionFormGroup.valid) {
|
|
|
|
|
const result: WidgetActionDescriptorInfo =
|
|
|
|
|
{...this.widgetActionFormGroup.value, ...this.widgetActionFormGroup.get('widgetAction').value};
|
|
|
|
|
delete (result as any).widgetAction;
|
2021-05-04 19:44:15 +03:00
|
|
|
result.id = this.action.id;
|
2024-04-19 16:22:39 +03:00
|
|
|
if (!isDefinedAndNotNull(result.columnIndex)) {
|
|
|
|
|
delete result.columnIndex;
|
|
|
|
|
}
|
2021-05-04 19:44:15 +03:00
|
|
|
this.dialogRef.close(result);
|
2019-10-24 19:52:19 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|