12 changed files with 330 additions and 4 deletions
@ -0,0 +1,66 @@ |
|||
<!-- |
|||
|
|||
Copyright © 2016-2021 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. |
|||
|
|||
--> |
|||
<form (ngSubmit)="save()"> |
|||
<mat-toolbar color="primary"> |
|||
<h2 translate>dashboard.update-image</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 style="position: relative;"> |
|||
<fieldset [disabled]="(isLoading$ | async) || (takingScreenshot$ | async)" fxLayout="column" fxLayoutAlign="center center" fxLayoutGap="16px"> |
|||
<div class="tb-image-preview-container"> |
|||
<div *ngIf="!safeImageUrl; else elseBlock">{{ 'dashboard.no-image' | translate }}</div> |
|||
<ng-template #elseBlock><img class="tb-image-preview" [src]="safeImageUrl" /></ng-template> |
|||
</div> |
|||
<button mat-raised-button color="accent" |
|||
type="button" |
|||
[disabled]="(isLoading$ | async) || (takingScreenshot$ | async)" |
|||
(click)="takeScreenShot()"> |
|||
{{ 'dashboard.take-screenshot' | translate }} |
|||
</button> |
|||
<div [formGroup]="dashboardImageFormGroup"> |
|||
<tb-image-input [showPreview]="false" label="{{'dashboard.image' | translate}}" |
|||
formControlName="dashboardImage"> |
|||
</tb-image-input> |
|||
</div> |
|||
</fieldset> |
|||
<div *ngIf="takingScreenshot$ | async" class="taking-screenshot-progress tb-absolute-fill" fxLayout="column" |
|||
fxLayoutAlign="center center"> |
|||
<mat-progress-spinner color="accent" mode="indeterminate"></mat-progress-spinner> |
|||
</div> |
|||
</div> |
|||
<div mat-dialog-actions fxLayoutAlign="end center"> |
|||
<button mat-button color="primary" |
|||
type="button" |
|||
[disabled]="(isLoading$ | async) || (takingScreenshot$ | async)" |
|||
(click)="cancel()" cdkFocusInitial> |
|||
{{ 'action.cancel' | translate }} |
|||
</button> |
|||
<button mat-raised-button color="primary" |
|||
type="submit" |
|||
[disabled]="(isLoading$ | async) || (takingScreenshot$ | async) || !dashboardImageFormGroup.dirty"> |
|||
{{ 'action.update' | translate }} |
|||
</button> |
|||
</div> |
|||
</form> |
|||
@ -0,0 +1,54 @@ |
|||
/** |
|||
* Copyright © 2016-2021 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. |
|||
*/ |
|||
|
|||
$previewSize: 300px !default; |
|||
|
|||
:host { |
|||
.tb-image-preview { |
|||
width: auto; |
|||
max-width: $previewSize - 2; |
|||
height: auto; |
|||
max-height: $previewSize - 2; |
|||
} |
|||
|
|||
.tb-image-preview-container { |
|||
position: relative; |
|||
float: left; |
|||
width: $previewSize; |
|||
height: $previewSize; |
|||
margin-right: 12px; |
|||
vertical-align: top; |
|||
border: solid 1px; |
|||
|
|||
div { |
|||
width: 100%; |
|||
font-size: 18px; |
|||
text-align: center; |
|||
} |
|||
|
|||
div, |
|||
.tb-image-preview { |
|||
position: absolute; |
|||
top: 50%; |
|||
left: 50%; |
|||
transform: translate(-50%, -50%); |
|||
} |
|||
} |
|||
|
|||
.taking-screenshot-progress { |
|||
background-color: rgba(255, 255, 255, 0.65); |
|||
} |
|||
} |
|||
@ -0,0 +1,132 @@ |
|||
///
|
|||
/// Copyright © 2016-2021 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 } from '@angular/core'; |
|||
import { MAT_DIALOG_DATA, MatDialogRef } from '@angular/material/dialog'; |
|||
import { Store } from '@ngrx/store'; |
|||
import { AppState } from '@core/core.state'; |
|||
import { FormBuilder, FormGroup } from '@angular/forms'; |
|||
import { Router } from '@angular/router'; |
|||
import { DialogComponent } from '@app/shared/components/dialog.component'; |
|||
import { DashboardId } from '@shared/models/id/dashboard-id'; |
|||
import { DashboardService } from '@core/http/dashboard.service'; |
|||
import { DomSanitizer, SafeUrl } from '@angular/platform-browser'; |
|||
import html2canvas from 'html2canvas'; |
|||
import { map, share } from 'rxjs/operators'; |
|||
import { BehaviorSubject, from } from 'rxjs'; |
|||
|
|||
export interface DashboardImageDialogData { |
|||
dashboardId: DashboardId; |
|||
currentImage?: string; |
|||
dashboardElement: HTMLElement; |
|||
} |
|||
|
|||
export interface DashboardImageDialogResult { |
|||
image?: string; |
|||
} |
|||
|
|||
@Component({ |
|||
selector: 'tb-dashboard-image-dialog', |
|||
templateUrl: './dashboard-image-dialog.component.html', |
|||
styleUrls: ['./dashboard-image-dialog.component.scss'] |
|||
}) |
|||
export class DashboardImageDialogComponent extends DialogComponent<DashboardImageDialogComponent, DashboardImageDialogResult> { |
|||
|
|||
takingScreenshotSubject = new BehaviorSubject(false); |
|||
|
|||
takingScreenshot$ = this.takingScreenshotSubject.asObservable().pipe( |
|||
share() |
|||
); |
|||
|
|||
dashboardId: DashboardId; |
|||
safeImageUrl?: SafeUrl; |
|||
dashboardElement: HTMLElement; |
|||
|
|||
dashboardImageFormGroup: FormGroup; |
|||
|
|||
constructor(protected store: Store<AppState>, |
|||
protected router: Router, |
|||
@Inject(MAT_DIALOG_DATA) public data: DashboardImageDialogData, |
|||
public dialogRef: MatDialogRef<DashboardImageDialogComponent, DashboardImageDialogResult>, |
|||
private dashboardService: DashboardService, |
|||
private sanitizer: DomSanitizer, |
|||
private fb: FormBuilder) { |
|||
super(store, router, dialogRef); |
|||
|
|||
this.dashboardId = this.data.dashboardId; |
|||
this.updateImage(this.data.currentImage); |
|||
this.dashboardElement = this.data.dashboardElement; |
|||
|
|||
this.dashboardImageFormGroup = this.fb.group({ |
|||
dashboardImage: [this.data.currentImage] |
|||
}); |
|||
|
|||
this.dashboardImageFormGroup.get('dashboardImage').valueChanges.subscribe( |
|||
(newImage) => { |
|||
this.updateImage(newImage); |
|||
} |
|||
); |
|||
} |
|||
|
|||
takeScreenShot() { |
|||
this.takingScreenshotSubject.next(true); |
|||
from(html2canvas(this.dashboardElement, { |
|||
logging: false, |
|||
useCORS: true, |
|||
foreignObjectRendering: false, |
|||
scale: 512 / this.dashboardElement.clientWidth |
|||
})).pipe( |
|||
map(canvas => canvas.toDataURL())).subscribe( |
|||
(image) => { |
|||
this.updateImage(image); |
|||
this.dashboardImageFormGroup.patchValue({dashboardImage: image}, {emitEvent: false}); |
|||
this.dashboardImageFormGroup.markAsDirty(); |
|||
this.takingScreenshotSubject.next(false); |
|||
}, |
|||
(e) => { |
|||
this.takingScreenshotSubject.next(false); |
|||
} |
|||
); |
|||
} |
|||
|
|||
cancel(): void { |
|||
this.dialogRef.close(null); |
|||
} |
|||
|
|||
save(): void { |
|||
this.dashboardService.getDashboard(this.dashboardId.id).subscribe( |
|||
(dashboard) => { |
|||
const newImage: string = this.dashboardImageFormGroup.get('dashboardImage').value; |
|||
dashboard.image = newImage; |
|||
this.dashboardService.saveDashboard(dashboard).subscribe( |
|||
() => { |
|||
this.dialogRef.close({ |
|||
image: newImage |
|||
}); |
|||
} |
|||
); |
|||
} |
|||
); |
|||
} |
|||
|
|||
private updateImage(imageUrl: string) { |
|||
if (imageUrl) { |
|||
this.safeImageUrl = this.sanitizer.bypassSecurityTrustUrl(imageUrl); |
|||
} else { |
|||
this.safeImageUrl = null; |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue