2019-10-31 10:06:57 +02:00
|
|
|
///
|
2023-01-31 10:43:56 +02:00
|
|
|
/// Copyright © 2016-2023 The Thingsboard Authors
|
2019-10-31 10:06:57 +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.
|
|
|
|
|
///
|
|
|
|
|
|
2022-05-11 15:58:15 +03:00
|
|
|
import { AfterViewInit, ChangeDetectorRef, Component, forwardRef, Input, OnDestroy, ViewChild } from '@angular/core';
|
2019-10-31 10:06:57 +02:00
|
|
|
import { PageComponent } from '@shared/components/page.component';
|
|
|
|
|
import { Store } from '@ngrx/store';
|
|
|
|
|
import { AppState } from '@core/core.state';
|
2020-03-20 16:23:49 +02:00
|
|
|
import { ControlValueAccessor, NG_VALUE_ACCESSOR, } from '@angular/forms';
|
|
|
|
|
import { Subscription } from 'rxjs';
|
2019-10-31 10:06:57 +02:00
|
|
|
import { coerceBooleanProperty } from '@angular/cdk/coercion';
|
|
|
|
|
import { FlowDirective } from '@flowjs/ngx-flow';
|
|
|
|
|
import { DomSanitizer, SafeUrl } from '@angular/platform-browser';
|
2020-03-20 16:23:49 +02:00
|
|
|
import { UtilsService } from '@core/services/utils.service';
|
2021-02-04 19:00:28 +02:00
|
|
|
import { DialogService } from '@core/services/dialog.service';
|
|
|
|
|
import { TranslateService } from '@ngx-translate/core';
|
|
|
|
|
import { FileSizePipe } from '@shared/pipe/file-size.pipe';
|
2019-10-31 10:06:57 +02:00
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
|
selector: 'tb-image-input',
|
|
|
|
|
templateUrl: './image-input.component.html',
|
|
|
|
|
styleUrls: ['./image-input.component.scss'],
|
|
|
|
|
providers: [
|
|
|
|
|
{
|
|
|
|
|
provide: NG_VALUE_ACCESSOR,
|
|
|
|
|
useExisting: forwardRef(() => ImageInputComponent),
|
|
|
|
|
multi: true
|
|
|
|
|
}
|
|
|
|
|
]
|
|
|
|
|
})
|
|
|
|
|
export class ImageInputComponent extends PageComponent implements AfterViewInit, OnDestroy, ControlValueAccessor {
|
|
|
|
|
|
|
|
|
|
@Input()
|
|
|
|
|
label: string;
|
|
|
|
|
|
2021-02-04 19:00:28 +02:00
|
|
|
@Input()
|
|
|
|
|
maxSizeByte: number;
|
|
|
|
|
|
2019-10-31 10:06:57 +02:00
|
|
|
private requiredValue: boolean;
|
2020-03-20 16:23:49 +02:00
|
|
|
|
2019-10-31 10:06:57 +02:00
|
|
|
get required(): boolean {
|
|
|
|
|
return this.requiredValue;
|
|
|
|
|
}
|
2020-03-20 16:23:49 +02:00
|
|
|
|
2019-10-31 10:06:57 +02:00
|
|
|
@Input()
|
|
|
|
|
set required(value: boolean) {
|
|
|
|
|
const newVal = coerceBooleanProperty(value);
|
|
|
|
|
if (this.requiredValue !== newVal) {
|
|
|
|
|
this.requiredValue = newVal;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Input()
|
|
|
|
|
disabled: boolean;
|
|
|
|
|
|
2020-03-20 16:23:49 +02:00
|
|
|
@Input()
|
|
|
|
|
showPreview = true;
|
|
|
|
|
|
|
|
|
|
@Input()
|
|
|
|
|
inputId = this.utils.guid();
|
|
|
|
|
|
2019-10-31 10:06:57 +02:00
|
|
|
imageUrl: string;
|
|
|
|
|
safeImageUrl: SafeUrl;
|
|
|
|
|
|
|
|
|
|
@ViewChild('flow', {static: true})
|
|
|
|
|
flow: FlowDirective;
|
|
|
|
|
|
|
|
|
|
autoUploadSubscription: Subscription;
|
|
|
|
|
|
|
|
|
|
private propagateChange = null;
|
|
|
|
|
|
|
|
|
|
constructor(protected store: Store<AppState>,
|
2020-03-20 16:23:49 +02:00
|
|
|
private utils: UtilsService,
|
2021-02-04 19:00:28 +02:00
|
|
|
private sanitizer: DomSanitizer,
|
|
|
|
|
private dialog: DialogService,
|
|
|
|
|
private translate: TranslateService,
|
2022-05-11 15:58:15 +03:00
|
|
|
private fileSize: FileSizePipe,
|
|
|
|
|
private cd: ChangeDetectorRef) {
|
2019-10-31 10:06:57 +02:00
|
|
|
super(store);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ngAfterViewInit() {
|
|
|
|
|
this.autoUploadSubscription = this.flow.events$.subscribe(event => {
|
|
|
|
|
if (event.type === 'fileAdded') {
|
|
|
|
|
const file = (event.event[0] as flowjs.FlowFile).file;
|
2021-02-04 19:00:28 +02:00
|
|
|
if (this.maxSizeByte && this.maxSizeByte < file.size) {
|
|
|
|
|
this.dialog.alert(
|
|
|
|
|
this.translate.instant('dashboard.cannot-upload-file'),
|
|
|
|
|
this.translate.instant('dashboard.maximum-upload-file-size', {size: this.fileSize.transform(this.maxSizeByte)})
|
|
|
|
|
).subscribe(
|
|
|
|
|
() => { }
|
|
|
|
|
);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2019-10-31 10:06:57 +02:00
|
|
|
const reader = new FileReader();
|
|
|
|
|
reader.onload = (loadEvent) => {
|
|
|
|
|
if (typeof reader.result === 'string' && reader.result.startsWith('data:image/')) {
|
|
|
|
|
this.imageUrl = reader.result;
|
|
|
|
|
this.safeImageUrl = this.sanitizer.bypassSecurityTrustUrl(this.imageUrl);
|
|
|
|
|
this.updateModel();
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
reader.readAsDataURL(file);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ngOnDestroy() {
|
|
|
|
|
this.autoUploadSubscription.unsubscribe();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
registerOnChange(fn: any): void {
|
|
|
|
|
this.propagateChange = fn;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
registerOnTouched(fn: any): void {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setDisabledState(isDisabled: boolean): void {
|
|
|
|
|
this.disabled = isDisabled;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
writeValue(value: string): void {
|
|
|
|
|
this.imageUrl = value;
|
|
|
|
|
if (this.imageUrl) {
|
|
|
|
|
this.safeImageUrl = this.sanitizer.bypassSecurityTrustUrl(this.imageUrl);
|
|
|
|
|
} else {
|
|
|
|
|
this.safeImageUrl = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private updateModel() {
|
2022-05-11 15:58:15 +03:00
|
|
|
this.cd.markForCheck();
|
2019-10-31 10:06:57 +02:00
|
|
|
this.propagateChange(this.imageUrl);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
clearImage() {
|
|
|
|
|
this.imageUrl = null;
|
|
|
|
|
this.safeImageUrl = null;
|
|
|
|
|
this.updateModel();
|
|
|
|
|
}
|
|
|
|
|
}
|