2022-05-20 17:50:35 +03:00
|
|
|
///
|
2025-02-25 09:39:16 +02:00
|
|
|
/// Copyright © 2016-2025 The Thingsboard Authors
|
2022-05-20 17:50:35 +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.
|
|
|
|
|
///
|
|
|
|
|
|
2022-05-25 20:05:59 +03:00
|
|
|
import {
|
|
|
|
|
AfterViewInit,
|
|
|
|
|
Component,
|
|
|
|
|
ElementRef,
|
|
|
|
|
forwardRef,
|
|
|
|
|
Input,
|
|
|
|
|
NgZone,
|
|
|
|
|
OnInit,
|
|
|
|
|
ViewChild,
|
|
|
|
|
ViewEncapsulation
|
|
|
|
|
} from '@angular/core';
|
2023-02-02 15:55:06 +02:00
|
|
|
import { ControlValueAccessor, UntypedFormBuilder, UntypedFormGroup, NG_VALUE_ACCESSOR } from '@angular/forms';
|
2022-05-25 20:05:59 +03:00
|
|
|
import { Observable } from 'rxjs';
|
2022-05-25 16:26:39 +03:00
|
|
|
import { debounceTime, distinctUntilChanged, map, share, switchMap, tap } from 'rxjs/operators';
|
2022-05-20 17:50:35 +03:00
|
|
|
import { Store } from '@ngrx/store';
|
|
|
|
|
import { AppState } from '@app/core/core.state';
|
|
|
|
|
import { coerceBooleanProperty } from '@angular/cdk/coercion';
|
|
|
|
|
import { BranchInfo } from '@shared/models/vc.models';
|
|
|
|
|
import { EntitiesVersionControlService } from '@core/http/entities-version-control.service';
|
2022-05-25 12:26:23 +03:00
|
|
|
import { isNotEmptyStr } from '@core/utils';
|
2023-02-17 19:24:01 +02:00
|
|
|
import { MatAutocomplete, MatAutocompleteTrigger } from '@angular/material/autocomplete';
|
2025-09-15 15:11:43 +03:00
|
|
|
import { SubscriptSizing, MatFormFieldAppearance } from '@angular/material/form-field';
|
2022-05-20 17:50:35 +03:00
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
|
selector: 'tb-branch-autocomplete',
|
|
|
|
|
templateUrl: './branch-autocomplete.component.html',
|
2022-05-25 20:05:59 +03:00
|
|
|
styleUrls: ['./branch-autocomplete.component.scss'],
|
2022-05-20 17:50:35 +03:00
|
|
|
providers: [{
|
|
|
|
|
provide: NG_VALUE_ACCESSOR,
|
|
|
|
|
useExisting: forwardRef(() => BranchAutocompleteComponent),
|
|
|
|
|
multi: true
|
2022-05-25 20:05:59 +03:00
|
|
|
}],
|
|
|
|
|
encapsulation: ViewEncapsulation.None
|
2022-05-20 17:50:35 +03:00
|
|
|
})
|
|
|
|
|
export class BranchAutocompleteComponent implements ControlValueAccessor, OnInit, AfterViewInit {
|
|
|
|
|
|
2023-02-02 15:55:06 +02:00
|
|
|
branchFormGroup: UntypedFormGroup;
|
2022-05-20 17:50:35 +03:00
|
|
|
|
|
|
|
|
modelValue: string | null;
|
|
|
|
|
|
2023-02-23 19:01:53 +02:00
|
|
|
@Input()
|
|
|
|
|
subscriptSizing: SubscriptSizing = 'fixed';
|
|
|
|
|
|
2025-09-15 15:11:43 +03:00
|
|
|
@Input()
|
|
|
|
|
appearance: MatFormFieldAppearance = 'fill';
|
|
|
|
|
|
2022-05-20 17:50:35 +03:00
|
|
|
private requiredValue: boolean;
|
|
|
|
|
|
|
|
|
|
get required(): boolean {
|
|
|
|
|
return this.requiredValue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Input()
|
|
|
|
|
set required(value: boolean) {
|
|
|
|
|
this.requiredValue = coerceBooleanProperty(value);
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-25 12:26:23 +03:00
|
|
|
private disabledValue: boolean;
|
|
|
|
|
|
|
|
|
|
get disabled(): boolean {
|
|
|
|
|
return this.disabledValue;
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-20 17:50:35 +03:00
|
|
|
@Input()
|
2022-05-25 12:26:23 +03:00
|
|
|
set disabled(value: boolean) {
|
|
|
|
|
this.disabledValue = coerceBooleanProperty(value);
|
|
|
|
|
if (this.disabledValue) {
|
|
|
|
|
this.branchFormGroup.disable({emitEvent: false});
|
|
|
|
|
} else {
|
|
|
|
|
this.branchFormGroup.enable({emitEvent: false});
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-05-20 17:50:35 +03:00
|
|
|
|
|
|
|
|
@Input()
|
|
|
|
|
selectDefaultBranch = true;
|
|
|
|
|
|
2022-05-25 12:26:23 +03:00
|
|
|
@Input()
|
|
|
|
|
selectionMode = false;
|
|
|
|
|
|
2022-05-31 18:42:33 +03:00
|
|
|
@Input()
|
|
|
|
|
emptyPlaceholder: string;
|
|
|
|
|
|
2022-05-25 16:26:39 +03:00
|
|
|
@ViewChild('branchAutocomplete') matAutocomplete: MatAutocomplete;
|
|
|
|
|
@ViewChild('branchInput', { read: MatAutocompleteTrigger, static: true }) autoCompleteTrigger: MatAutocompleteTrigger;
|
2023-02-21 19:18:04 +02:00
|
|
|
@ViewChild('branchInput', {static: true}) branchInput: ElementRef<HTMLInputElement>;
|
2022-05-20 17:50:35 +03:00
|
|
|
|
2022-05-25 12:26:23 +03:00
|
|
|
filteredBranches: Observable<Array<BranchInfo>>;
|
2022-05-20 17:50:35 +03:00
|
|
|
|
2022-05-25 12:26:23 +03:00
|
|
|
defaultBranch: BranchInfo = null;
|
2022-05-20 17:50:35 +03:00
|
|
|
|
|
|
|
|
searchText = '';
|
|
|
|
|
|
2022-05-25 16:26:39 +03:00
|
|
|
loading = false;
|
|
|
|
|
|
2022-05-20 17:50:35 +03:00
|
|
|
private dirty = false;
|
|
|
|
|
|
2022-05-25 16:26:39 +03:00
|
|
|
private clearButtonClicked = false;
|
2022-05-25 12:26:23 +03:00
|
|
|
|
2022-05-20 17:50:35 +03:00
|
|
|
private propagateChange = (v: any) => { };
|
|
|
|
|
|
|
|
|
|
constructor(private store: Store<AppState>,
|
|
|
|
|
private entitiesVersionControlService: EntitiesVersionControlService,
|
2023-02-02 15:55:06 +02:00
|
|
|
private fb: UntypedFormBuilder,
|
2022-05-25 12:26:23 +03:00
|
|
|
private zone: NgZone) {
|
2022-05-20 17:50:35 +03:00
|
|
|
this.branchFormGroup = this.fb.group({
|
|
|
|
|
branch: [null, []]
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
registerOnChange(fn: any): void {
|
|
|
|
|
this.propagateChange = fn;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
registerOnTouched(fn: any): void {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ngOnInit() {
|
|
|
|
|
this.filteredBranches = this.branchFormGroup.get('branch').valueChanges
|
|
|
|
|
.pipe(
|
2022-05-25 12:26:23 +03:00
|
|
|
tap((value: BranchInfo | string) => {
|
|
|
|
|
let modelValue: BranchInfo | null;
|
|
|
|
|
if (typeof value === 'string' || !value) {
|
|
|
|
|
if (!this.selectionMode && typeof value === 'string' && isNotEmptyStr(value)) {
|
|
|
|
|
modelValue = {name: value, default: false};
|
|
|
|
|
} else {
|
|
|
|
|
modelValue = null;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
modelValue = value;
|
|
|
|
|
}
|
2022-05-25 16:26:39 +03:00
|
|
|
if (!this.selectionMode || modelValue) {
|
|
|
|
|
this.updateView(modelValue);
|
|
|
|
|
}
|
2022-05-25 12:26:23 +03:00
|
|
|
}),
|
|
|
|
|
map(value => {
|
|
|
|
|
if (value) {
|
|
|
|
|
if (typeof value === 'string') {
|
|
|
|
|
return value;
|
|
|
|
|
} else {
|
|
|
|
|
return value.name;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
return '';
|
|
|
|
|
}
|
|
|
|
|
}),
|
2022-05-20 17:50:35 +03:00
|
|
|
debounceTime(150),
|
|
|
|
|
distinctUntilChanged(),
|
2022-05-25 12:26:23 +03:00
|
|
|
switchMap(name => this.fetchBranches(name)),
|
|
|
|
|
share()
|
2022-05-20 17:50:35 +03:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ngAfterViewInit(): void {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setDisabledState(isDisabled: boolean): void {
|
|
|
|
|
this.disabled = isDisabled;
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-25 16:26:39 +03:00
|
|
|
isDefaultBranchSelected(): boolean {
|
|
|
|
|
return this.defaultBranch && this.defaultBranch.name === this.modelValue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
selectDefaultBranchIfNeeded(force = false): void {
|
2022-05-25 12:26:23 +03:00
|
|
|
if ((this.selectDefaultBranch && !this.modelValue) || force) {
|
2022-05-25 16:26:39 +03:00
|
|
|
setTimeout(() => {
|
|
|
|
|
if (this.defaultBranch) {
|
|
|
|
|
this.branchFormGroup.get('branch').patchValue(this.defaultBranch, {emitEvent: false});
|
|
|
|
|
this.modelValue = this.defaultBranch?.name;
|
|
|
|
|
this.propagateChange(this.modelValue);
|
|
|
|
|
} else {
|
|
|
|
|
this.loading = true;
|
|
|
|
|
this.getBranches().subscribe(
|
|
|
|
|
() => {
|
|
|
|
|
if (this.defaultBranch || force) {
|
|
|
|
|
this.branchFormGroup.get('branch').patchValue(this.defaultBranch, {emitEvent: false});
|
|
|
|
|
this.modelValue = this.defaultBranch?.name;
|
|
|
|
|
this.propagateChange(this.modelValue);
|
|
|
|
|
this.loading = false;
|
|
|
|
|
} else {
|
|
|
|
|
this.loading = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
);
|
2022-05-20 17:50:35 +03:00
|
|
|
}
|
2022-05-25 16:26:39 +03:00
|
|
|
});
|
2022-05-20 17:50:35 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
writeValue(value: string | null): void {
|
|
|
|
|
this.searchText = '';
|
|
|
|
|
this.modelValue = value;
|
|
|
|
|
if (value != null) {
|
2022-05-25 12:26:23 +03:00
|
|
|
this.branchFormGroup.get('branch').patchValue({name: value}, {emitEvent: false});
|
2022-05-20 17:50:35 +03:00
|
|
|
} else {
|
2022-05-25 12:26:23 +03:00
|
|
|
this.branchFormGroup.get('branch').patchValue(null, {emitEvent: false});
|
2022-05-20 17:50:35 +03:00
|
|
|
this.selectDefaultBranchIfNeeded();
|
|
|
|
|
}
|
|
|
|
|
this.dirty = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
onFocus() {
|
|
|
|
|
if (this.dirty) {
|
|
|
|
|
this.branchFormGroup.get('branch').updateValueAndValidity({onlySelf: true, emitEvent: true});
|
|
|
|
|
this.dirty = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-25 16:26:39 +03:00
|
|
|
onBlur() {
|
|
|
|
|
if (this.clearButtonClicked) {
|
|
|
|
|
this.clearButtonClicked = false;
|
|
|
|
|
} else if (!this.matAutocomplete.isOpen) {
|
|
|
|
|
this.selectAvailableValue();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-25 12:26:23 +03:00
|
|
|
onPanelClosed() {
|
2022-05-25 16:26:39 +03:00
|
|
|
this.selectAvailableValue();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
selectAvailableValue() {
|
|
|
|
|
if (this.selectionMode) {
|
|
|
|
|
const branch = this.branchFormGroup.get('branch').value;
|
2022-05-25 20:05:59 +03:00
|
|
|
this.getBranches().pipe(
|
|
|
|
|
map(branches => {
|
|
|
|
|
let foundBranch = branches.find(b => b.name === branch);
|
|
|
|
|
if (!foundBranch && isNotEmptyStr(this.modelValue)) {
|
|
|
|
|
foundBranch = branches.find(b => b.name === this.modelValue);
|
2022-05-25 16:26:39 +03:00
|
|
|
}
|
2022-05-25 20:05:59 +03:00
|
|
|
return foundBranch;
|
|
|
|
|
})
|
|
|
|
|
).subscribe((val) => {
|
|
|
|
|
if (!val && this.defaultBranch) {
|
|
|
|
|
val = this.defaultBranch;
|
|
|
|
|
}
|
|
|
|
|
this.zone.run(() => {
|
|
|
|
|
this.branchFormGroup.get('branch').patchValue(val, {emitEvent: true});
|
|
|
|
|
}, 0);
|
|
|
|
|
});
|
2022-05-25 12:26:23 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
updateView(value: BranchInfo | null) {
|
|
|
|
|
if (this.modelValue !== value?.name) {
|
|
|
|
|
this.modelValue = value?.name;
|
2022-05-20 17:50:35 +03:00
|
|
|
this.propagateChange(this.modelValue);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-25 12:26:23 +03:00
|
|
|
displayBranchFn(branch?: BranchInfo): string | undefined {
|
|
|
|
|
return branch ? branch.name : undefined;
|
2022-05-20 17:50:35 +03:00
|
|
|
}
|
|
|
|
|
|
2022-05-25 16:26:39 +03:00
|
|
|
private fetchBranches(searchText?: string): Observable<Array<BranchInfo>> {
|
2022-05-20 17:50:35 +03:00
|
|
|
this.searchText = searchText;
|
|
|
|
|
return this.getBranches().pipe(
|
2022-05-25 12:26:23 +03:00
|
|
|
map(branches => {
|
2023-02-21 19:18:04 +02:00
|
|
|
let res = branches.filter(branch => searchText ? branch.name.toUpperCase().startsWith(searchText.toUpperCase()) : true);
|
2022-05-25 12:26:23 +03:00
|
|
|
if (!this.selectionMode && isNotEmptyStr(searchText) && !res.find(b => b.name === searchText)) {
|
|
|
|
|
res = [{name: searchText, default: false}, ...res];
|
|
|
|
|
}
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
)
|
2022-05-20 17:50:35 +03:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-25 16:26:39 +03:00
|
|
|
private getBranches(): Observable<Array<BranchInfo>> {
|
|
|
|
|
return this.entitiesVersionControlService.listBranches().pipe(
|
|
|
|
|
tap((data) => {
|
|
|
|
|
this.defaultBranch = data.find(branch => branch.default);
|
|
|
|
|
})
|
|
|
|
|
);
|
2022-05-20 17:50:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
clear() {
|
2022-05-25 16:26:39 +03:00
|
|
|
this.clearButtonClicked = true;
|
2022-05-20 17:50:35 +03:00
|
|
|
setTimeout(() => {
|
2022-05-25 16:26:39 +03:00
|
|
|
this.branchFormGroup.get('branch').patchValue(null, {emitEvent: true});
|
2022-05-20 17:50:35 +03:00
|
|
|
this.branchInput.nativeElement.blur();
|
|
|
|
|
this.branchInput.nativeElement.focus();
|
|
|
|
|
}, 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|