2019-10-24 19:52:19 +03:00
|
|
|
///
|
2021-01-11 13:42:16 +02:00
|
|
|
/// Copyright © 2016-2021 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';
|
|
|
|
|
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 {
|
|
|
|
|
FormBuilder,
|
|
|
|
|
FormControl,
|
|
|
|
|
FormGroup,
|
|
|
|
|
FormGroupDirective,
|
|
|
|
|
NgForm,
|
|
|
|
|
ValidatorFn,
|
|
|
|
|
Validators
|
|
|
|
|
} from '@angular/forms';
|
|
|
|
|
import { Observable, of } from 'rxjs';
|
|
|
|
|
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';
|
2021-09-28 12:34:34 +03:00
|
|
|
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';
|
2020-05-20 19:42:58 +03:00
|
|
|
import { CustomActionEditorCompleter } from '@home/components/widget/action/custom-action.models';
|
2021-01-21 18:03:36 +02:00
|
|
|
import { isDefinedAndNotNull } from '@core/utils';
|
2021-05-04 19:44:15 +03:00
|
|
|
import { MobileActionEditorComponent } from '@home/components/widget/action/mobile-action-editor.component';
|
2021-05-18 10:52:38 +03:00
|
|
|
import { widgetType } from '@shared/models/widget.models';
|
2021-09-28 10:33:31 +03:00
|
|
|
import { WidgetService } from '@core/http/widget.service';
|
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}],
|
2020-04-21 11:53:26 +03:00
|
|
|
styleUrls: ['./widget-action-dialog.component.scss']
|
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
|
|
|
|
2021-05-04 19:44:15 +03:00
|
|
|
@ViewChild('mobileActionEditor', {static: false}) mobileActionEditor: MobileActionEditorComponent;
|
|
|
|
|
|
2019-10-24 19:52:19 +03:00
|
|
|
widgetActionFormGroup: FormGroup;
|
|
|
|
|
actionTypeFormGroup: FormGroup;
|
|
|
|
|
|
|
|
|
|
isAdd: boolean;
|
|
|
|
|
action: WidgetActionDescriptorInfo;
|
|
|
|
|
|
|
|
|
|
widgetActionTypes = Object.keys(WidgetActionType);
|
|
|
|
|
widgetActionTypeTranslations = widgetActionTypeTranslationMap;
|
|
|
|
|
widgetActionType = WidgetActionType;
|
|
|
|
|
|
|
|
|
|
filteredDashboardStates: Observable<Array<string>>;
|
|
|
|
|
targetDashboardStateSearchText = '';
|
|
|
|
|
selectedDashboardStateIds: Observable<Array<string>>;
|
|
|
|
|
|
2020-05-20 19:42:58 +03:00
|
|
|
customActionEditorCompleter = CustomActionEditorCompleter;
|
|
|
|
|
|
2019-10-24 19:52:19 +03:00
|
|
|
submitted = false;
|
2021-05-18 10:52:38 +03:00
|
|
|
widgetType = widgetType;
|
2019-10-24 19:52:19 +03:00
|
|
|
|
2021-09-28 08:41:39 +03:00
|
|
|
functionScopeVariables: string[];
|
|
|
|
|
|
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,
|
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>,
|
|
|
|
|
public fb: FormBuilder) {
|
|
|
|
|
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;
|
|
|
|
|
}
|
2021-09-28 08:41:39 +03:00
|
|
|
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]));
|
2021-09-28 12:34:34 +03:00
|
|
|
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]));
|
2021-09-28 12:34:34 +03:00
|
|
|
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();
|
2021-09-28 12:34:34 +03:00
|
|
|
this.updateShowWidgetActionForm();
|
|
|
|
|
});
|
|
|
|
|
this.widgetActionFormGroup.get('useShowWidgetActionFunction').valueChanges.subscribe(() => {
|
|
|
|
|
this.updateShowWidgetActionForm();
|
2019-10-24 19:52:19 +03:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-24 19:52:19 +03:00
|
|
|
private updateActionTypeFormGroup(type?: WidgetActionType, action?: WidgetActionDescriptorInfo) {
|
|
|
|
|
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) {
|
2021-01-14 13:17:23 +02:00
|
|
|
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 {
|
2021-01-21 18:03:36 +02:00
|
|
|
if (type === WidgetActionType.openDashboardState) {
|
|
|
|
|
this.actionTypeFormGroup.addControl(
|
|
|
|
|
'openInSeparateDialog',
|
|
|
|
|
this.fb.control(action ? action.openInSeparateDialog : false, [])
|
|
|
|
|
);
|
|
|
|
|
this.actionTypeFormGroup.addControl(
|
|
|
|
|
'dialogTitle',
|
|
|
|
|
this.fb.control(action ? action.dialogTitle : '', [])
|
|
|
|
|
);
|
|
|
|
|
this.actionTypeFormGroup.addControl(
|
|
|
|
|
'dialogHideDashboardToolbar',
|
|
|
|
|
this.fb.control(action && isDefinedAndNotNull(action.dialogHideDashboardToolbar) ? action.dialogHideDashboardToolbar : true, [])
|
|
|
|
|
);
|
|
|
|
|
this.actionTypeFormGroup.addControl(
|
|
|
|
|
'dialogWidth',
|
|
|
|
|
this.fb.control(action ? action.dialogWidth : null, [Validators.min(1), Validators.max(100)])
|
|
|
|
|
);
|
|
|
|
|
this.actionTypeFormGroup.addControl(
|
|
|
|
|
'dialogHeight',
|
|
|
|
|
this.fb.control(action ? action.dialogHeight : null, [Validators.min(1), Validators.max(100)])
|
|
|
|
|
);
|
|
|
|
|
}
|
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;
|
2021-05-04 19:44:15 +03:00
|
|
|
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 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 {
|
|
|
|
|
return (c: FormControl) => {
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
isErrorState(control: FormControl | null, form: FormGroupDirective | NgForm | null): boolean {
|
|
|
|
|
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 '';
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cancel(): void {
|
|
|
|
|
this.dialogRef.close(null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
save(): void {
|
|
|
|
|
this.submitted = true;
|
2021-05-04 19:44:15 +03:00
|
|
|
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};
|
|
|
|
|
}
|
|
|
|
|
result.id = this.action.id;
|
|
|
|
|
this.dialogRef.close(result);
|
2019-10-24 19:52:19 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|