22 changed files with 662 additions and 4 deletions
@ -0,0 +1,40 @@ |
|||
///
|
|||
/// Copyright © 2016-2022 The Thingsboard Authors
|
|||
///
|
|||
/// 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 { Injectable } from '@angular/core'; |
|||
import { HttpClient } from '@angular/common/http'; |
|||
import { defaultHttpOptionsFromConfig, RequestConfig } from '@core/http/http-utils'; |
|||
import { Observable } from 'rxjs'; |
|||
import { BranchInfo, VersionCreateRequest, VersionCreationResult } from '@shared/models/vc.models'; |
|||
|
|||
@Injectable({ |
|||
providedIn: 'root' |
|||
}) |
|||
export class EntitiesVersionControlService { |
|||
|
|||
constructor( |
|||
private http: HttpClient |
|||
) { |
|||
} |
|||
|
|||
public listBranches(config?: RequestConfig): Observable<Array<BranchInfo>> { |
|||
return this.http.get<Array<BranchInfo>>('/api/entities/vc/branches', defaultHttpOptionsFromConfig(config)); |
|||
} |
|||
|
|||
public saveEntitiesVersion(request: VersionCreateRequest, config?: RequestConfig): Observable<VersionCreationResult> { |
|||
return this.http.post<VersionCreationResult>('/api/entities/vc/version', request, defaultHttpOptionsFromConfig(config)); |
|||
} |
|||
} |
|||
@ -0,0 +1,77 @@ |
|||
<!-- |
|||
|
|||
Copyright © 2016-2022 The Thingsboard Authors |
|||
|
|||
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. |
|||
|
|||
--> |
|||
<section style="min-width: 400px;"> |
|||
<mat-toolbar color="primary"> |
|||
<h2>{{ (createResult ? 'version-control.entity-version-exported' : 'version-control.export-entity-version') | translate }}</h2> |
|||
<span fxFlex></span> |
|||
<button mat-button mat-icon-button |
|||
(click)="cancel()" |
|||
type="button"> |
|||
<mat-icon class="material-icons">close</mat-icon> |
|||
</button> |
|||
</mat-toolbar> |
|||
<mat-progress-bar color="warn" mode="indeterminate" *ngIf="isLoading$ | async"> |
|||
</mat-progress-bar> |
|||
<div mat-dialog-content> |
|||
<form *ngIf="!createResult" [formGroup]="exportFormGroup"> |
|||
<fieldset [disabled]="isLoading$ | async"> |
|||
<div fxFlex fxLayout="column"> |
|||
<tb-branch-autocomplete |
|||
required |
|||
formControlName="branch"> |
|||
</tb-branch-autocomplete> |
|||
<mat-form-field class="mat-block" fxFlex> |
|||
<mat-label translate>version-control.version-name</mat-label> |
|||
<input required matInput formControlName="versionName"> |
|||
<mat-error *ngIf="exportFormGroup.get('versionName').hasError('required')"> |
|||
{{ 'version-control.version-name-required' | translate }} |
|||
</mat-error> |
|||
</mat-form-field> |
|||
<mat-checkbox formControlName="saveRelations" style="margin-bottom: 16px;"> |
|||
{{ 'version-control.export-entity-relations' | translate }} |
|||
</mat-checkbox> |
|||
</div> |
|||
</fieldset> |
|||
</form> |
|||
<div *ngIf="createResult" fxFlex fxLayout="column" fxLayoutAlign="center"> |
|||
<div [innerHTML]="createResultMessage"></div> |
|||
</div> |
|||
</div> |
|||
<div *ngIf="!createResult" mat-dialog-actions fxLayoutAlign="end center"> |
|||
<button mat-button color="primary" |
|||
type="button" |
|||
[disabled]="(isLoading$ | async)" |
|||
(click)="cancel()" cdkFocusInitial> |
|||
{{ 'action.cancel' | translate }} |
|||
</button> |
|||
<button mat-raised-button color="primary" |
|||
type="button" |
|||
(click)="export()" |
|||
[disabled]="(isLoading$ | async) || exportFormGroup.invalid || !exportFormGroup.dirty"> |
|||
{{ 'action.export' | translate }} |
|||
</button> |
|||
</div> |
|||
<div *ngIf="createResult" mat-dialog-actions fxLayoutAlign="end center"> |
|||
<button mat-button color="primary" |
|||
type="button" |
|||
[disabled]="(isLoading$ | async)" |
|||
(click)="cancel()" cdkFocusInitial> |
|||
{{ 'action.close' | translate }} |
|||
</button> |
|||
</div> |
|||
</section> |
|||
@ -0,0 +1,105 @@ |
|||
///
|
|||
/// Copyright © 2016-2022 The Thingsboard Authors
|
|||
///
|
|||
/// 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, Inject, OnInit, SkipSelf } from '@angular/core'; |
|||
import { ErrorStateMatcher } from '@angular/material/core'; |
|||
import { MAT_DIALOG_DATA, MatDialogRef } from '@angular/material/dialog'; |
|||
import { Store } from '@ngrx/store'; |
|||
import { AppState } from '@core/core.state'; |
|||
import { FormBuilder, FormControl, FormGroup, FormGroupDirective, NgForm, Validators } from '@angular/forms'; |
|||
import { Router } from '@angular/router'; |
|||
import { DialogComponent } from '@app/shared/components/dialog.component'; |
|||
import { EntityId } from '@shared/models/id/entity-id'; |
|||
import { |
|||
SingleEntityVersionCreateRequest, |
|||
VersionCreateRequestType, |
|||
VersionCreationResult |
|||
} from '@shared/models/vc.models'; |
|||
import { EntitiesVersionControlService } from '@core/http/entities-version-control.service'; |
|||
import { DomSanitizer, SafeHtml } from '@angular/platform-browser'; |
|||
import { TranslateService } from '@ngx-translate/core'; |
|||
|
|||
export interface VcEntityExportDialogData { |
|||
entityId: EntityId; |
|||
} |
|||
|
|||
@Component({ |
|||
selector: 'tb-vc-entity-export-dialog', |
|||
templateUrl: './vc-entity-export-dialog.component.html', |
|||
providers: [{provide: ErrorStateMatcher, useExisting: VcEntityExportDialogComponent}], |
|||
styleUrls: [] |
|||
}) |
|||
export class VcEntityExportDialogComponent extends DialogComponent<VcEntityExportDialogComponent> |
|||
implements OnInit, ErrorStateMatcher { |
|||
|
|||
exportFormGroup: FormGroup; |
|||
|
|||
submitted = false; |
|||
|
|||
createResult: VersionCreationResult; |
|||
|
|||
createResultMessage: SafeHtml; |
|||
|
|||
constructor(protected store: Store<AppState>, |
|||
protected router: Router, |
|||
@Inject(MAT_DIALOG_DATA) public data: VcEntityExportDialogData, |
|||
@SkipSelf() private errorStateMatcher: ErrorStateMatcher, |
|||
public dialogRef: MatDialogRef<VcEntityExportDialogComponent>, |
|||
private entitiesVersionControlService: EntitiesVersionControlService, |
|||
private translate: TranslateService, |
|||
private domSanitizer: DomSanitizer, |
|||
private fb: FormBuilder) { |
|||
super(store, router, dialogRef); |
|||
|
|||
this.exportFormGroup = this.fb.group({ |
|||
branch: [null, [Validators.required]], |
|||
versionName: [null, [Validators.required]], |
|||
saveRelations: [false, []] |
|||
}); |
|||
} |
|||
|
|||
ngOnInit(): void { |
|||
} |
|||
|
|||
isErrorState(control: FormControl | null, form: FormGroupDirective | NgForm | null): boolean { |
|||
const originalErrorState = this.errorStateMatcher.isErrorState(control, form); |
|||
const customErrorState = !!(control && control.invalid && this.submitted); |
|||
return originalErrorState || customErrorState; |
|||
} |
|||
|
|||
cancel(): void { |
|||
this.dialogRef.close(); |
|||
} |
|||
|
|||
export(): void { |
|||
this.submitted = true; |
|||
const request: SingleEntityVersionCreateRequest = { |
|||
entityId: this.data.entityId, |
|||
branch: this.exportFormGroup.get('branch').value, |
|||
versionName: this.exportFormGroup.get('versionName').value, |
|||
config: { |
|||
saveRelations: this.exportFormGroup.get('saveRelations').value |
|||
}, |
|||
type: VersionCreateRequestType.SINGLE_ENTITY |
|||
}; |
|||
this.entitiesVersionControlService.saveEntitiesVersion(request).subscribe((result) => { |
|||
this.createResult = result; |
|||
const message = this.translate.instant('version-control.export-entity-version-result-message', |
|||
{name: result.version.name, commitId: result.version.id}); |
|||
this.createResultMessage = this.domSanitizer.bypassSecurityTrustHtml(message); |
|||
}); |
|||
} |
|||
} |
|||
@ -0,0 +1,43 @@ |
|||
<!-- |
|||
|
|||
Copyright © 2016-2022 The Thingsboard Authors |
|||
|
|||
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. |
|||
|
|||
--> |
|||
<mat-form-field [formGroup]="branchFormGroup" class="mat-block"> |
|||
<mat-label>{{ 'version-control.branch' | translate }}</mat-label> |
|||
<input matInput type="text" placeholder="{{ 'version-control.select-branch' | translate }}" |
|||
#branchInput |
|||
formControlName="branch" |
|||
(focusin)="onFocus()" |
|||
[required]="required" |
|||
[matAutocomplete]="subTypeAutocomplete"> |
|||
<button *ngIf="branchFormGroup.get('branch').value && !disabled" |
|||
type="button" |
|||
matSuffix mat-button mat-icon-button aria-label="Clear" |
|||
(click)="clear()"> |
|||
<mat-icon class="material-icons">close</mat-icon> |
|||
</button> |
|||
<mat-autocomplete |
|||
class="tb-autocomplete" |
|||
#subTypeAutocomplete="matAutocomplete" |
|||
[displayWith]="displayBranchFn"> |
|||
<mat-option *ngFor="let branch of filteredBranches | async" [value]="branch"> |
|||
<span [innerHTML]="branch | highlight:searchText"></span> |
|||
</mat-option> |
|||
</mat-autocomplete> |
|||
<mat-error *ngIf="branchFormGroup.get('branch').hasError('required')"> |
|||
{{ 'version-control.branch-required' | translate }} |
|||
</mat-error> |
|||
</mat-form-field> |
|||
@ -0,0 +1,199 @@ |
|||
///
|
|||
/// Copyright © 2016-2022 The Thingsboard Authors
|
|||
///
|
|||
/// 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 { AfterViewInit, Component, ElementRef, forwardRef, Input, OnInit, ViewChild } from '@angular/core'; |
|||
import { ControlValueAccessor, FormBuilder, FormGroup, NG_VALUE_ACCESSOR } from '@angular/forms'; |
|||
import { Observable, of } from 'rxjs'; |
|||
import { |
|||
catchError, |
|||
debounceTime, |
|||
distinctUntilChanged, |
|||
map, |
|||
publishReplay, |
|||
refCount, |
|||
switchMap, |
|||
tap |
|||
} from 'rxjs/operators'; |
|||
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'; |
|||
|
|||
@Component({ |
|||
selector: 'tb-branch-autocomplete', |
|||
templateUrl: './branch-autocomplete.component.html', |
|||
styleUrls: [], |
|||
providers: [{ |
|||
provide: NG_VALUE_ACCESSOR, |
|||
useExisting: forwardRef(() => BranchAutocompleteComponent), |
|||
multi: true |
|||
}] |
|||
}) |
|||
export class BranchAutocompleteComponent implements ControlValueAccessor, OnInit, AfterViewInit { |
|||
|
|||
branchFormGroup: FormGroup; |
|||
|
|||
modelValue: string | null; |
|||
|
|||
private requiredValue: boolean; |
|||
|
|||
get required(): boolean { |
|||
return this.requiredValue; |
|||
} |
|||
|
|||
@Input() |
|||
set required(value: boolean) { |
|||
this.requiredValue = coerceBooleanProperty(value); |
|||
} |
|||
|
|||
@Input() |
|||
disabled: boolean; |
|||
|
|||
@Input() |
|||
selectDefaultBranch = true; |
|||
|
|||
@ViewChild('branchInput', {static: true}) branchInput: ElementRef; |
|||
|
|||
filteredBranches: Observable<Array<string>>; |
|||
|
|||
branches: Observable<Array<BranchInfo>>; |
|||
|
|||
searchText = ''; |
|||
|
|||
private dirty = false; |
|||
|
|||
private propagateChange = (v: any) => { }; |
|||
|
|||
constructor(private store: Store<AppState>, |
|||
private entitiesVersionControlService: EntitiesVersionControlService, |
|||
private fb: FormBuilder) { |
|||
this.branchFormGroup = this.fb.group({ |
|||
branch: [null, []] |
|||
}); |
|||
} |
|||
|
|||
registerOnChange(fn: any): void { |
|||
this.propagateChange = fn; |
|||
} |
|||
|
|||
registerOnTouched(fn: any): void { |
|||
} |
|||
|
|||
ngOnInit() { |
|||
|
|||
this.branches = null; |
|||
this.filteredBranches = this.branchFormGroup.get('branch').valueChanges |
|||
.pipe( |
|||
debounceTime(150), |
|||
distinctUntilChanged(), |
|||
tap(value => { |
|||
this.updateView(value); |
|||
}), |
|||
map(value => value ? value : ''), |
|||
switchMap(branch => this.fetchBranches(branch)) |
|||
); |
|||
} |
|||
|
|||
ngAfterViewInit(): void { |
|||
} |
|||
|
|||
setDisabledState(isDisabled: boolean): void { |
|||
this.disabled = isDisabled; |
|||
if (this.disabled) { |
|||
this.branchFormGroup.disable({emitEvent: false}); |
|||
} else { |
|||
this.branchFormGroup.enable({emitEvent: false}); |
|||
} |
|||
} |
|||
|
|||
selectDefaultBranchIfNeeded(): void { |
|||
if (this.selectDefaultBranch && !this.modelValue) { |
|||
this.getBranches().subscribe( |
|||
(data) => { |
|||
if (data && data.length) { |
|||
const defaultBranch = data.find(branch => branch.default); |
|||
if (defaultBranch) { |
|||
this.modelValue = defaultBranch.name; |
|||
this.branchFormGroup.get('branch').patchValue(this.modelValue, {emitEvent: false}); |
|||
this.propagateChange(this.modelValue); |
|||
} |
|||
} |
|||
} |
|||
); |
|||
} |
|||
} |
|||
|
|||
writeValue(value: string | null): void { |
|||
this.searchText = ''; |
|||
this.modelValue = value; |
|||
if (value != null) { |
|||
this.branchFormGroup.get('branch').patchValue(value, {emitEvent: false}); |
|||
} else { |
|||
this.branchFormGroup.get('branch').patchValue('', {emitEvent: false}); |
|||
this.selectDefaultBranchIfNeeded(); |
|||
} |
|||
this.dirty = true; |
|||
} |
|||
|
|||
onFocus() { |
|||
if (this.dirty) { |
|||
this.branchFormGroup.get('branch').updateValueAndValidity({onlySelf: true, emitEvent: true}); |
|||
this.dirty = false; |
|||
} |
|||
} |
|||
|
|||
updateView(value: string | null) { |
|||
if (this.modelValue !== value) { |
|||
this.modelValue = value; |
|||
this.propagateChange(this.modelValue); |
|||
} |
|||
} |
|||
|
|||
displayBranchFn(branch?: string): string | undefined { |
|||
return branch ? branch : undefined; |
|||
} |
|||
|
|||
fetchBranches(searchText?: string): Observable<Array<string>> { |
|||
this.searchText = searchText; |
|||
return this.getBranches().pipe( |
|||
map(branches => branches.map(branch => branch.name).filter(branchName => { |
|||
return searchText ? branchName.toUpperCase().startsWith(searchText.toUpperCase()) : true; |
|||
})) |
|||
); |
|||
} |
|||
|
|||
getBranches(): Observable<Array<BranchInfo>> { |
|||
if (!this.branches) { |
|||
const branchesObservable = this.entitiesVersionControlService.listBranches({ignoreLoading: true, ignoreErrors: true}); |
|||
this.branches = branchesObservable.pipe( |
|||
catchError(() => of([] as Array<BranchInfo>)), |
|||
publishReplay(1), |
|||
refCount() |
|||
); |
|||
} |
|||
return this.branches; |
|||
} |
|||
|
|||
clear() { |
|||
this.branchFormGroup.get('branch').patchValue(null, {emitEvent: true}); |
|||
setTimeout(() => { |
|||
this.branchInput.nativeElement.blur(); |
|||
this.branchInput.nativeElement.focus(); |
|||
}, 0); |
|||
} |
|||
|
|||
} |
|||
@ -0,0 +1,55 @@ |
|||
///
|
|||
/// Copyright © 2016-2022 The Thingsboard Authors
|
|||
///
|
|||
/// 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 { EntityId } from '@shared/models/id/entity-id'; |
|||
|
|||
export interface VersionCreateConfig { |
|||
saveRelations: boolean; |
|||
} |
|||
|
|||
export enum VersionCreateRequestType { |
|||
SINGLE_ENTITY = 'SINGLE_ENTITY', |
|||
COMPLEX = 'COMPLEX' |
|||
} |
|||
|
|||
export interface VersionCreateRequest { |
|||
versionName: string; |
|||
branch: string; |
|||
type: VersionCreateRequestType; |
|||
} |
|||
|
|||
export interface SingleEntityVersionCreateRequest extends VersionCreateRequest { |
|||
entityId: EntityId; |
|||
config: VersionCreateConfig; |
|||
type: VersionCreateRequestType.SINGLE_ENTITY; |
|||
} |
|||
|
|||
export interface BranchInfo { |
|||
name: string; |
|||
default: boolean; |
|||
} |
|||
|
|||
export interface EntityVersion { |
|||
id: string; |
|||
name: string; |
|||
} |
|||
|
|||
export interface VersionCreationResult { |
|||
version: EntityVersion; |
|||
added: number; |
|||
modified: number; |
|||
removed: number; |
|||
} |
|||
Loading…
Reference in new issue