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

120 lines
4.0 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.
///
import { Component, Input, OnChanges, OnInit, SimpleChanges, ViewChild } 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 { MatDialog } from '@angular/material/dialog';
import { Dashboard, WidgetLayout } from '@shared/models/dashboard.models';
import { IAliasController } from '@core/api/widget-api.models';
import { Widget } from '@shared/models/widget.models';
2019-09-25 19:37:29 +03:00
import { WidgetComponentService } from '@home/components/widget/widget-component.service';
import { WidgetConfigComponentData } from '../../models/widget-component.models';
import { isDefined, isString } from '@core/utils';
import { FormBuilder, FormGroup, NgForm } from '@angular/forms';
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;
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});
}
}