Add video image components

pull/298/head
Chocobozzz 2018-02-16 19:30:58 +01:00
parent c8a5b03785
commit 702785a54c
No known key found for this signature in database
GPG Key ID: 583A612D890159BE
3 changed files with 116 additions and 0 deletions

View File

@ -0,0 +1,16 @@
<div class="root">
<div>
<div class="button-file">
<span>{{ inputLabel }}</span>
<input
type="file"
[name]="inputName" [id]="inputName" [accept]="videoImageExtensions"
(change)="fileChange($event)"
/>
</div>
<div class="image-constraints">(extensions: {{ videoImageExtensions }}, max size: {{ maxVideoImageSize | bytes }})</div>
</div>
<img *ngIf="imageSrc" [ngStyle]="{ width: previewWidth, height: previewHeight }" [src]="imageSrc" class="preview" />
<div *ngIf="!imageSrc" [ngStyle]="{ width: previewWidth, height: previewHeight }" class="preview no-image"></div>
</div>

View File

@ -0,0 +1,26 @@
@import '_variables';
@import '_mixins';
.root {
height: 150px;
display: flex;
align-items: center;
.button-file {
@include peertube-button-file(190px);
}
.image-constraints {
font-size: 13px;
}
.preview {
border: 2px solid grey;
border-radius: 4px;
margin-left: 50px;
&.no-image {
background-color: #ececec;
}
}
}

View File

@ -0,0 +1,74 @@
import { Component, forwardRef, Input } from '@angular/core'
import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms'
import { DomSanitizer, SafeResourceUrl } from '@angular/platform-browser'
import { ServerService } from '@app/core'
import 'rxjs/add/observable/forkJoin'
@Component({
selector: 'my-video-image',
styleUrls: [ './video-image.component.scss' ],
templateUrl: './video-image.component.html',
providers: [
{
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => VideoImageComponent),
multi: true
}
]
})
export class VideoImageComponent implements ControlValueAccessor {
@Input() inputLabel: string
@Input() inputName: string
@Input() previewWidth: string
@Input() previewHeight: string
imageSrc: SafeResourceUrl
private file: Blob
constructor (
private sanitizer: DomSanitizer,
private serverService: ServerService
) {}
get videoImageExtensions () {
return this.serverService.getConfig().video.image.extensions.join(',')
}
get maxVideoImageSize () {
return this.serverService.getConfig().video.image.size.max
}
fileChange (event: any) {
if (event.target.files && event.target.files.length) {
const [ file ] = event.target.files
this.file = file
this.propagateChange(this.file)
this.updatePreview()
}
}
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) {
const url = URL.createObjectURL(this.file)
this.imageSrc = this.sanitizer.bypassSecurityTrustResourceUrl(url)
}
}
}