2019-05-17 08:45:53 +00:00
|
|
|
import { Component, forwardRef, Input, OnInit } from '@angular/core'
|
2018-02-16 18:30:58 +00:00
|
|
|
import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms'
|
|
|
|
import { ServerService } from '@app/core'
|
2021-09-08 08:10:51 +00:00
|
|
|
import { imageToDataURL } from '@root-helpers/images'
|
2021-06-04 11:31:41 +00:00
|
|
|
import { HTMLServerConfig } from '@shared/models'
|
2020-08-11 14:50:00 +00:00
|
|
|
import { BytesPipe } from '../shared-main'
|
2018-02-16 18:30:58 +00:00
|
|
|
|
|
|
|
@Component({
|
2019-05-17 08:45:53 +00:00
|
|
|
selector: 'my-preview-upload',
|
|
|
|
styleUrls: [ './preview-upload.component.scss' ],
|
|
|
|
templateUrl: './preview-upload.component.html',
|
2018-02-16 18:30:58 +00:00
|
|
|
providers: [
|
|
|
|
{
|
|
|
|
provide: NG_VALUE_ACCESSOR,
|
2019-05-17 08:45:53 +00:00
|
|
|
useExisting: forwardRef(() => PreviewUploadComponent),
|
2018-02-16 18:30:58 +00:00
|
|
|
multi: true
|
|
|
|
}
|
|
|
|
]
|
|
|
|
})
|
2019-05-17 08:45:53 +00:00
|
|
|
export class PreviewUploadComponent implements OnInit, ControlValueAccessor {
|
2018-02-16 18:30:58 +00:00
|
|
|
@Input() inputLabel: string
|
|
|
|
@Input() inputName: string
|
|
|
|
@Input() previewWidth: string
|
|
|
|
@Input() previewHeight: string
|
|
|
|
|
2021-09-08 08:10:51 +00:00
|
|
|
imageSrc: string
|
2019-05-17 08:45:53 +00:00
|
|
|
allowedExtensionsMessage = ''
|
2020-04-02 15:55:03 +00:00
|
|
|
maxSizeText: string
|
2018-02-16 18:30:58 +00:00
|
|
|
|
2021-06-04 11:31:41 +00:00
|
|
|
private serverConfig: HTMLServerConfig
|
2020-04-02 15:55:03 +00:00
|
|
|
private bytesPipe: BytesPipe
|
2020-02-10 13:25:38 +00:00
|
|
|
private file: Blob
|
2018-02-16 18:30:58 +00:00
|
|
|
|
|
|
|
constructor (
|
2020-08-12 08:40:04 +00:00
|
|
|
private serverService: ServerService
|
2020-04-02 15:55:03 +00:00
|
|
|
) {
|
|
|
|
this.bytesPipe = new BytesPipe()
|
2020-08-12 08:40:04 +00:00
|
|
|
this.maxSizeText = $localize`max size`
|
2020-04-02 15:55:03 +00:00
|
|
|
}
|
2018-02-16 18:30:58 +00:00
|
|
|
|
|
|
|
get videoImageExtensions () {
|
2019-12-18 14:31:54 +00:00
|
|
|
return this.serverConfig.video.image.extensions
|
2018-02-16 18:30:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
get maxVideoImageSize () {
|
2019-12-18 14:31:54 +00:00
|
|
|
return this.serverConfig.video.image.size.max
|
2018-02-16 18:30:58 +00:00
|
|
|
}
|
|
|
|
|
2020-04-02 15:55:03 +00:00
|
|
|
get maxVideoImageSizeInBytes () {
|
|
|
|
return this.bytesPipe.transform(this.maxVideoImageSize)
|
|
|
|
}
|
|
|
|
|
2022-03-22 16:41:39 +00:00
|
|
|
getReactiveFileButtonTooltip () {
|
|
|
|
return $localize`(extensions: ${this.videoImageExtensions}, ${this.maxSizeText}: ${this.maxVideoImageSizeInBytes})`
|
|
|
|
}
|
|
|
|
|
2019-05-17 08:45:53 +00:00
|
|
|
ngOnInit () {
|
2021-06-04 11:31:41 +00:00
|
|
|
this.serverConfig = this.serverService.getHTMLConfig()
|
2019-12-18 14:31:54 +00:00
|
|
|
|
2019-05-17 08:45:53 +00:00
|
|
|
this.allowedExtensionsMessage = this.videoImageExtensions.join(', ')
|
|
|
|
}
|
|
|
|
|
2020-02-10 13:25:38 +00:00
|
|
|
onFileChanged (file: Blob) {
|
2018-07-12 17:02:00 +00:00
|
|
|
this.file = file
|
2018-06-22 13:42:55 +00:00
|
|
|
|
2018-07-12 17:02:00 +00:00
|
|
|
this.propagateChange(this.file)
|
|
|
|
this.updatePreview()
|
2018-02-16 18:30:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
propagateChange = (_: any) => { /* empty */ }
|
|
|
|
|
|
|
|
writeValue (file: any) {
|
|
|
|
this.file = file
|
|
|
|
this.updatePreview()
|
|
|
|
}
|
|
|
|
|
|
|
|
registerOnChange (fn: (_: any) => void) {
|
|
|
|
this.propagateChange = fn
|
|
|
|
}
|
|
|
|
|
|
|
|
registerOnTouched () {
|
|
|
|
// Unused
|
|
|
|
}
|
|
|
|
|
|
|
|
private updatePreview () {
|
|
|
|
if (this.file) {
|
2021-09-08 08:10:51 +00:00
|
|
|
imageToDataURL(this.file).then(result => this.imageSrc = result)
|
2018-02-16 18:30:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|