2023-01-17 18:26:25 +02:00
|
|
|
///
|
2025-02-25 09:39:16 +02:00
|
|
|
/// Copyright © 2016-2025 The Thingsboard Authors
|
2023-01-17 18:26:25 +02: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.
|
|
|
|
|
///
|
|
|
|
|
|
2023-03-07 12:14:49 +02:00
|
|
|
import { Component, ElementRef, forwardRef, Input, OnInit, ViewChild, ViewEncapsulation } from '@angular/core';
|
2023-01-17 18:26:25 +02:00
|
|
|
import { ControlValueAccessor, FormBuilder, FormGroup, NG_VALUE_ACCESSOR, Validators } from '@angular/forms';
|
|
|
|
|
import { Observable, of } from 'rxjs';
|
2023-05-19 18:01:37 +03:00
|
|
|
import { catchError, debounceTime, map, share, switchMap, tap } from 'rxjs/operators';
|
2023-01-17 18:26:25 +02:00
|
|
|
import { Store } from '@ngrx/store';
|
|
|
|
|
import { AppState } from '@core/core.state';
|
|
|
|
|
import { TranslateService } from '@ngx-translate/core';
|
|
|
|
|
import { EntityId } from '@shared/models/id/entity-id';
|
|
|
|
|
import { EntityService } from '@core/http/entity.service';
|
|
|
|
|
import { TruncatePipe } from '@shared/pipe/truncate.pipe';
|
|
|
|
|
import { PageLink } from '@shared/models/page/page-link';
|
|
|
|
|
import { Direction } from '@shared/models/page/sort-order';
|
|
|
|
|
import { emptyPageData } from '@shared/models/page/page-data';
|
2023-01-23 12:47:00 +02:00
|
|
|
import {
|
2024-02-07 17:50:47 +02:00
|
|
|
NotificationDeliveryMethodInfoMap,
|
2023-01-23 12:47:00 +02:00
|
|
|
NotificationTemplate,
|
|
|
|
|
NotificationType
|
|
|
|
|
} from '@shared/models/notification.models';
|
2023-01-17 18:26:25 +02:00
|
|
|
import { NotificationService } from '@core/http/notification.service';
|
|
|
|
|
import { isEqual } from '@core/utils';
|
2023-02-10 18:31:05 +02:00
|
|
|
import {
|
|
|
|
|
TemplateNotificationDialogComponent,
|
|
|
|
|
TemplateNotificationDialogData
|
2023-03-21 17:56:12 +02:00
|
|
|
} from '@home/pages/notification/template/template-notification-dialog.component';
|
2023-02-10 18:31:05 +02:00
|
|
|
import { MatDialog } from '@angular/material/dialog';
|
|
|
|
|
import { MatButton } from '@angular/material/button';
|
2023-05-04 17:46:43 +03:00
|
|
|
import { coerceBoolean } from '@shared/decorators/coercion';
|
2023-01-17 18:26:25 +02:00
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
|
selector: 'tb-template-autocomplete',
|
|
|
|
|
templateUrl: './template-autocomplete.component.html',
|
|
|
|
|
styleUrls: ['./template-autocomplete.component.scss'],
|
2023-03-07 12:14:49 +02:00
|
|
|
encapsulation: ViewEncapsulation.None,
|
2023-01-17 18:26:25 +02:00
|
|
|
providers: [{
|
|
|
|
|
provide: NG_VALUE_ACCESSOR,
|
|
|
|
|
useExisting: forwardRef(() => TemplateAutocompleteComponent),
|
|
|
|
|
multi: true
|
|
|
|
|
}]
|
|
|
|
|
})
|
|
|
|
|
export class TemplateAutocompleteComponent implements ControlValueAccessor, OnInit {
|
|
|
|
|
|
2024-02-07 17:50:47 +02:00
|
|
|
notificationDeliveryMethodInfoMap = NotificationDeliveryMethodInfoMap;
|
2023-01-17 18:26:25 +02:00
|
|
|
selectTemplateFormGroup: FormGroup;
|
|
|
|
|
|
|
|
|
|
@Input()
|
2023-02-24 11:29:36 +02:00
|
|
|
@coerceBoolean()
|
|
|
|
|
required: boolean;
|
2023-01-17 18:26:25 +02:00
|
|
|
|
2023-02-10 18:31:05 +02:00
|
|
|
@Input()
|
2023-02-24 11:29:36 +02:00
|
|
|
@coerceBoolean()
|
2023-03-20 16:51:42 +02:00
|
|
|
allowCreate = false;
|
2023-02-10 18:31:05 +02:00
|
|
|
|
2023-05-19 18:01:37 +03:00
|
|
|
@Input()
|
|
|
|
|
@coerceBoolean()
|
|
|
|
|
allowEdit = false;
|
|
|
|
|
|
2023-02-10 18:31:05 +02:00
|
|
|
|
2023-01-17 18:26:25 +02:00
|
|
|
@Input()
|
|
|
|
|
disabled: boolean;
|
|
|
|
|
|
2023-02-01 15:58:00 +02:00
|
|
|
private notificationTypeValue: NotificationType;
|
|
|
|
|
get notificationTypes(): NotificationType {
|
|
|
|
|
return this.notificationTypeValue;
|
|
|
|
|
}
|
2023-01-23 12:47:00 +02:00
|
|
|
@Input()
|
2023-02-01 15:58:00 +02:00
|
|
|
set notificationTypes(type) {
|
|
|
|
|
if (type !== this.notificationTypeValue) {
|
|
|
|
|
this.notificationTypeValue = type;
|
|
|
|
|
this.reset();
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-01-23 12:47:00 +02:00
|
|
|
|
2023-01-17 18:26:25 +02:00
|
|
|
@ViewChild('templateInput', {static: true}) templateInput: ElementRef;
|
|
|
|
|
|
|
|
|
|
filteredTemplate: Observable<Array<NotificationTemplate>>;
|
|
|
|
|
|
|
|
|
|
searchText = '';
|
|
|
|
|
|
|
|
|
|
private modelValue: EntityId | null;
|
|
|
|
|
private dirty = false;
|
|
|
|
|
|
|
|
|
|
private propagateChange = (v: any) => { };
|
|
|
|
|
|
|
|
|
|
constructor(private store: Store<AppState>,
|
|
|
|
|
public translate: TranslateService,
|
|
|
|
|
public truncate: TruncatePipe,
|
|
|
|
|
private entityService: EntityService,
|
|
|
|
|
private notificationService: NotificationService,
|
2023-02-10 18:31:05 +02:00
|
|
|
private fb: FormBuilder,
|
|
|
|
|
private dialog: MatDialog) {
|
2023-01-17 18:26:25 +02:00
|
|
|
this.selectTemplateFormGroup = this.fb.group({
|
|
|
|
|
templateName: [null]
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
registerOnChange(fn: any): void {
|
|
|
|
|
this.propagateChange = fn;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
registerOnTouched(fn: any): void {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ngOnInit() {
|
|
|
|
|
if (this.required) {
|
|
|
|
|
this.selectTemplateFormGroup.get('templateName').addValidators(Validators.required);
|
|
|
|
|
this.selectTemplateFormGroup.get('templateName').updateValueAndValidity({emitEvent: false});
|
|
|
|
|
}
|
|
|
|
|
this.filteredTemplate = this.selectTemplateFormGroup.get('templateName').valueChanges
|
|
|
|
|
.pipe(
|
|
|
|
|
debounceTime(150),
|
|
|
|
|
tap(value => {
|
|
|
|
|
let modelValue;
|
|
|
|
|
if (typeof value === 'string' || !value) {
|
|
|
|
|
modelValue = null;
|
|
|
|
|
} else {
|
|
|
|
|
modelValue = value.id;
|
|
|
|
|
}
|
|
|
|
|
this.updateView(modelValue);
|
|
|
|
|
if (value === null) {
|
|
|
|
|
this.clear();
|
|
|
|
|
}
|
|
|
|
|
}),
|
|
|
|
|
map(value => value ? (typeof value === 'string' ? value : value.name) : ''),
|
|
|
|
|
switchMap(name => this.fetchTemplate(name)),
|
|
|
|
|
share()
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setDisabledState(isDisabled: boolean): void {
|
|
|
|
|
this.disabled = isDisabled;
|
|
|
|
|
if (this.disabled) {
|
|
|
|
|
this.selectTemplateFormGroup.disable({emitEvent: false});
|
|
|
|
|
} else {
|
|
|
|
|
this.selectTemplateFormGroup.enable({emitEvent: false});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
textIsNotEmpty(text: string): boolean {
|
|
|
|
|
return (text && text.length > 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
writeValue(value: EntityId | null): void {
|
|
|
|
|
this.searchText = '';
|
|
|
|
|
if (value != null) {
|
2023-03-20 16:51:42 +02:00
|
|
|
this.notificationService.getNotificationTemplateById(value.id, {
|
|
|
|
|
ignoreLoading: true,
|
|
|
|
|
ignoreErrors: true
|
|
|
|
|
}).subscribe({
|
|
|
|
|
next: (entity) => {
|
2023-01-17 18:26:25 +02:00
|
|
|
this.modelValue = entity.id;
|
|
|
|
|
this.selectTemplateFormGroup.get('templateName').patchValue(entity, {emitEvent: false});
|
|
|
|
|
},
|
2023-03-20 16:51:42 +02:00
|
|
|
error: () => {
|
2023-01-17 18:26:25 +02:00
|
|
|
this.modelValue = null;
|
|
|
|
|
this.selectTemplateFormGroup.get('templateName').patchValue('', {emitEvent: false});
|
|
|
|
|
if (value !== null) {
|
|
|
|
|
this.propagateChange(this.modelValue);
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-03-20 16:51:42 +02:00
|
|
|
});
|
2023-01-17 18:26:25 +02:00
|
|
|
} else {
|
|
|
|
|
this.modelValue = null;
|
|
|
|
|
this.selectTemplateFormGroup.get('templateName').patchValue('', {emitEvent: false});
|
|
|
|
|
}
|
|
|
|
|
this.dirty = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
onFocus() {
|
|
|
|
|
if (this.dirty) {
|
|
|
|
|
this.selectTemplateFormGroup.get('templateName').updateValueAndValidity({onlySelf: true, emitEvent: true});
|
|
|
|
|
this.dirty = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-01 15:58:00 +02:00
|
|
|
displayTemplateFn(template?: NotificationTemplate): string | undefined {
|
|
|
|
|
return template ? template.name : undefined;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
clear() {
|
|
|
|
|
this.selectTemplateFormGroup.get('templateName').patchValue('', {emitEvent: true});
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
this.templateInput.nativeElement.blur();
|
|
|
|
|
this.templateInput.nativeElement.focus();
|
|
|
|
|
}, 0);
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-19 18:01:37 +03:00
|
|
|
editTemplate($event: Event) {
|
|
|
|
|
if ($event) {
|
|
|
|
|
$event.stopPropagation();
|
|
|
|
|
}
|
|
|
|
|
this.notificationService.getNotificationTemplateById(this.modelValue.id).subscribe(
|
|
|
|
|
(template) => {
|
|
|
|
|
this.openNotificationTemplateDialog({template});
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-10 18:31:05 +02:00
|
|
|
createTemplate($event: Event, button: MatButton) {
|
2025-07-28 17:55:30 +03:00
|
|
|
$event?.stopPropagation();
|
2023-02-10 18:31:05 +02:00
|
|
|
button._elementRef.nativeElement.blur();
|
2025-07-28 17:55:30 +03:00
|
|
|
this.createTemplateByName($event);
|
2023-05-19 18:01:37 +03:00
|
|
|
}
|
2025-07-28 17:57:43 +03:00
|
|
|
|
2025-07-28 17:55:30 +03:00
|
|
|
createTemplateByName($event: Event, name?: string) {
|
|
|
|
|
$event?.stopPropagation();
|
2025-07-28 17:24:37 +03:00
|
|
|
this.openNotificationTemplateDialog({
|
|
|
|
|
isAdd: true,
|
|
|
|
|
predefinedType: this.notificationTypes,
|
2025-07-28 17:55:30 +03:00
|
|
|
name
|
2025-07-28 17:24:37 +03:00
|
|
|
});
|
|
|
|
|
}
|
2023-05-19 18:01:37 +03:00
|
|
|
|
|
|
|
|
private openNotificationTemplateDialog(dialogData?: TemplateNotificationDialogData) {
|
2023-02-10 18:31:05 +02:00
|
|
|
this.dialog.open<TemplateNotificationDialogComponent, TemplateNotificationDialogData,
|
|
|
|
|
NotificationTemplate>(TemplateNotificationDialogComponent, {
|
|
|
|
|
disableClose: true,
|
|
|
|
|
panelClass: ['tb-dialog', 'tb-fullscreen-dialog'],
|
2023-05-19 18:01:37 +03:00
|
|
|
data: dialogData
|
2023-02-10 18:31:05 +02:00
|
|
|
}).afterClosed()
|
|
|
|
|
.subscribe((res) => {
|
|
|
|
|
if (res) {
|
|
|
|
|
this.selectTemplateFormGroup.get('templateName').patchValue(res);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-01 15:58:00 +02:00
|
|
|
private updateView(value: EntityId | null) {
|
2023-01-17 18:26:25 +02:00
|
|
|
if (!isEqual(this.modelValue, value)) {
|
|
|
|
|
this.modelValue = value;
|
|
|
|
|
this.propagateChange(this.modelValue);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-01 15:58:00 +02:00
|
|
|
private fetchTemplate(searchText?: string): Observable<Array<NotificationTemplate>> {
|
2023-01-17 18:26:25 +02:00
|
|
|
this.searchText = searchText;
|
|
|
|
|
const pageLink = new PageLink(10, 0, searchText, {
|
|
|
|
|
property: 'name',
|
|
|
|
|
direction: Direction.ASC
|
|
|
|
|
});
|
2023-01-23 12:47:00 +02:00
|
|
|
return this.notificationService.getNotificationTemplates(pageLink, this.notificationTypes, {ignoreLoading: true}).pipe(
|
2023-01-17 18:26:25 +02:00
|
|
|
catchError(() => of(emptyPageData<NotificationTemplate>())),
|
2023-03-20 16:51:42 +02:00
|
|
|
map(pageData => pageData.data)
|
2023-01-17 18:26:25 +02:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-01 15:58:00 +02:00
|
|
|
private reset() {
|
|
|
|
|
this.selectTemplateFormGroup.get('templateName').patchValue('', {emitEvent: false});
|
2023-02-02 15:25:04 +02:00
|
|
|
this.updateView(null);
|
|
|
|
|
this.dirty = true;
|
2023-01-17 18:26:25 +02:00
|
|
|
}
|
|
|
|
|
}
|