thingsboard/ui-ngx/src/app/modules/home/pages/dashboard/edit-widget.component.ts

139 lines
4.9 KiB
TypeScript
Raw Normal View History

2019-09-25 19:37:29 +03:00
///
2020-02-20 10:26:43 +02:00
/// Copyright © 2016-2020 The Thingsboard Authors
2019-09-25 19:37:29 +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.
///
2019-10-24 19:52:19 +03:00
import { Component, OnInit, Input, OnChanges, SimpleChanges, ViewChild, ChangeDetectionStrategy } from '@angular/core';
2019-09-25 19:37:29 +03:00
import { PageComponent } from '@shared/components/page.component';
import { Store } from '@ngrx/store';
import { AppState } from '@core/core.state';
import { ActivatedRoute, Router } from '@angular/router';
import { WidgetService } from '@core/http/widget.service';
import { DialogService } from '@core/services/dialog.service';
import { MatDialog } from '@angular/material/dialog';
import { TranslateService } from '@ngx-translate/core';
import { Dashboard, WidgetLayout } from '@shared/models/dashboard.models';
import { IAliasController } from '@core/api/widget-api.models';
import { Widget, WidgetActionSource, WidgetTypeParameters } from '@shared/models/widget.models';
import { WidgetComponentService } from '@home/components/widget/widget-component.service';
import { WidgetConfigComponentData } from '../../models/widget-component.models';
import { deepClone, isDefined, isString } from '@core/utils';
2019-10-21 19:57:18 +03:00
import { FormBuilder, FormGroup, NgForm, Validators } from '@angular/forms';
import { EntityType } from '@shared/models/entity-type.models';
import { Observable, of } from 'rxjs';
import { EntityAlias, EntityAliases } from '@shared/models/alias.models';
import { WidgetConfigCallbacks } from '@home/components/widget/widget-config.component.models';
import {
EntityAliasesDialogComponent,
EntityAliasesDialogData
} from '@home/components/alias/entity-aliases-dialog.component';
import {
EntityAliasDialogComponent,
EntityAliasDialogData
} from '@home/components/alias/entity-alias-dialog.component';
import { tap } from 'rxjs/operators';
2019-09-25 19:37:29 +03:00
@Component({
selector: 'tb-edit-widget',
templateUrl: './edit-widget.component.html',
styleUrls: []
})
export class EditWidgetComponent extends PageComponent implements OnInit, OnChanges {
@Input()
dashboard: Dashboard;
@Input()
aliasController: IAliasController;
@Input()
widgetEditMode: boolean;
@Input()
widget: Widget;
@Input()
widgetLayout: WidgetLayout;
2019-10-21 19:57:18 +03:00
@ViewChild('widgetForm', {static: true}) widgetForm: NgForm;
2019-09-25 19:37:29 +03:00
widgetFormGroup: FormGroup;
widgetConfig: WidgetConfigComponentData;
constructor(protected store: Store<AppState>,
private dialog: MatDialog,
2019-10-21 19:57:18 +03:00
private fb: FormBuilder,
2019-09-25 19:37:29 +03:00
private widgetComponentService: WidgetComponentService) {
super(store);
2019-10-21 19:57:18 +03:00
this.widgetFormGroup = this.fb.group({
widgetConfig: [null]
});
2019-09-25 19:37:29 +03:00
}
ngOnInit(): void {
this.loadWidgetConfig();
}
ngOnChanges(changes: SimpleChanges): void {
let reloadConfig = false;
for (const propName of Object.keys(changes)) {
const change = changes[propName];
if (!change.firstChange && change.currentValue !== change.previousValue) {
if (['widget', 'widgetLayout'].includes(propName)) {
reloadConfig = true;
}
}
}
if (reloadConfig) {
this.loadWidgetConfig();
}
}
private loadWidgetConfig() {
2019-10-31 18:33:51 +02:00
if (!this.widget) {
return;
}
2019-09-25 19:37:29 +03:00
const widgetInfo = this.widgetComponentService.getInstantWidgetInfo(this.widget);
2019-10-21 19:57:18 +03:00
const rawSettingsSchema = widgetInfo.typeSettingsSchema || widgetInfo.settingsSchema;
const rawDataKeySettingsSchema = widgetInfo.typeDataKeySettingsSchema || widgetInfo.dataKeySettingsSchema;
const typeParameters = widgetInfo.typeParameters;
const actionSources = widgetInfo.actionSources;
const isDataEnabled = isDefined(widgetInfo.typeParameters) ? !widgetInfo.typeParameters.useCustomDatasources : true;
let settingsSchema;
if (!rawSettingsSchema || rawSettingsSchema === '') {
settingsSchema = {};
2019-09-25 19:37:29 +03:00
} else {
2019-10-21 19:57:18 +03:00
settingsSchema = isString(rawSettingsSchema) ? JSON.parse(rawSettingsSchema) : rawSettingsSchema;
2019-09-25 19:37:29 +03:00
}
2019-10-21 19:57:18 +03:00
let dataKeySettingsSchema;
if (!rawDataKeySettingsSchema || rawDataKeySettingsSchema === '') {
dataKeySettingsSchema = {};
2019-09-25 19:37:29 +03:00
} else {
2019-10-21 19:57:18 +03:00
dataKeySettingsSchema = isString(rawDataKeySettingsSchema) ? JSON.parse(rawDataKeySettingsSchema) : rawDataKeySettingsSchema;
2019-09-25 19:37:29 +03:00
}
2019-10-21 19:57:18 +03:00
this.widgetConfig = {
config: this.widget.config,
layout: this.widgetLayout,
widgetType: this.widget.type,
typeParameters,
actionSources,
isDataEnabled,
settingsSchema,
dataKeySettingsSchema
};
2019-09-25 19:37:29 +03:00
this.widgetFormGroup.reset({widgetConfig: this.widgetConfig});
}
}