thingsboard/ui-ngx/src/app/modules/home/components/details-panel.component.ts

122 lines
3.2 KiB
TypeScript
Raw Normal View History

2019-08-12 19:34:23 +03:00
///
2023-01-31 10:43:56 +02:00
/// Copyright © 2016-2023 The Thingsboard Authors
2019-08-12 19:34:23 +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 { ChangeDetectorRef, Component, EventEmitter, Input, OnDestroy, Output } from '@angular/core';
2019-08-12 19:34:23 +03:00
import { PageComponent } from '@shared/components/page.component';
import { Store } from '@ngrx/store';
import { AppState } from '@core/core.state';
import { FormGroup } from '@angular/forms';
import { Subscription } from 'rxjs';
2019-08-12 19:34:23 +03:00
@Component({
selector: 'tb-details-panel',
templateUrl: './details-panel.component.html',
styleUrls: ['./details-panel.component.scss']
})
export class DetailsPanelComponent extends PageComponent implements OnDestroy {
2019-08-12 19:34:23 +03:00
@Input() headerHeightPx = 100;
@Input() headerTitle = '';
@Input() headerSubtitle = '';
@Input() isReadOnly = false;
@Input() isAlwaysEdit = false;
@Input() isShowSearch = false;
@Input() backgroundColor = '#FFF';
2019-09-25 19:37:29 +03:00
private theFormValue: FormGroup;
private formSubscription: Subscription = null;
2019-09-25 19:37:29 +03:00
@Input()
set theForm(value: FormGroup) {
if (this.theFormValue !== value) {
if (this.formSubscription !== null) {
this.formSubscription.unsubscribe();
this.formSubscription = null;
}
this.theFormValue = value;
if (this.theFormValue !== null) {
this.formSubscription = this.theFormValue.valueChanges.subscribe(() => this.cd.detectChanges());
}
}
2019-09-25 19:37:29 +03:00
}
get theForm(): FormGroup {
2019-09-25 19:37:29 +03:00
return this.theFormValue;
}
2019-08-12 19:34:23 +03:00
@Output()
closeDetails = new EventEmitter<void>();
@Output()
toggleDetailsEditMode = new EventEmitter<boolean>();
@Output()
applyDetails = new EventEmitter<void>();
@Output()
closeSearch = new EventEmitter<void>();
2019-08-12 19:34:23 +03:00
isEditValue = false;
showSearchPane = false;
2019-08-12 19:34:23 +03:00
@Output()
isEditChange = new EventEmitter<boolean>();
@Input()
get isEdit() {
2019-09-25 19:37:29 +03:00
return this.isAlwaysEdit || this.isEditValue;
2019-08-12 19:34:23 +03:00
}
set isEdit(val: boolean) {
this.isEditValue = val;
this.isEditChange.emit(this.isEditValue);
}
constructor(protected store: Store<AppState>,
private cd: ChangeDetectorRef) {
2019-08-12 19:34:23 +03:00
super(store);
}
ngOnDestroy() {
if (this.formSubscription !== null) {
this.formSubscription.unsubscribe();
}
super.ngOnDestroy();
}
2019-08-12 19:34:23 +03:00
onCloseDetails() {
this.closeDetails.emit();
}
onToggleDetailsEditMode() {
if (!this.isAlwaysEdit) {
this.isEdit = !this.isEdit;
}
this.toggleDetailsEditMode.emit(this.isEditValue);
}
onApplyDetails() {
2019-09-25 19:37:29 +03:00
if (this.theForm && this.theForm.valid) {
2019-08-12 19:34:23 +03:00
this.applyDetails.emit();
}
}
onToggleSearch() {
this.showSearchPane = !this.showSearchPane;
if (!this.showSearchPane) {
this.closeSearch.emit();
}
}
2019-08-12 19:34:23 +03:00
}