1 import { Control, ControlGroup, Validators } from '@angular/common';
2 import { Component, ElementRef, OnInit } from '@angular/core';
3 import { Router } from '@angular/router';
5 import { BytesPipe } from 'angular-pipes/src/math/bytes.pipe';
6 import { PROGRESSBAR_DIRECTIVES } from 'ng2-bootstrap/components/progressbar';
7 import { FileSelectDirective, FileUploader } from 'ng2-file-upload/ng2-file-upload';
9 import { AuthService } from '../../shared';
12 selector: 'my-videos-add',
13 styles: [ require('./video-add.component.scss') ],
14 template: require('./video-add.component.html'),
15 directives: [ FileSelectDirective, PROGRESSBAR_DIRECTIVES ],
19 export class VideoAddComponent implements OnInit {
20 currentTag: string; // Tag the user is writing in the input
22 videoForm: ControlGroup;
23 uploader: FileUploader;
31 private authService: AuthService,
32 private elementRef: ElementRef,
33 private router: Router
37 if (this.uploader.queue.length === 0) {
41 return this.uploader.queue[0].file.name;
44 get isTagsInputDisabled () {
45 return this.video.tags.length >= 3;
48 getInvalidFieldsTitle() {
50 const nameControl = this.videoForm.controls['name'];
51 const descriptionControl = this.videoForm.controls['description'];
53 if (!nameControl.valid) {
54 title += 'A name is required\n';
57 if (this.video.tags.length === 0) {
58 title += 'At least one tag is required\n';
61 if (this.filename === null) {
62 title += 'A file is required\n';
65 if (!descriptionControl.valid) {
66 title += 'A description is required\n';
73 this.videoForm = new ControlGroup({
74 name: new Control('', Validators.compose([ Validators.required, Validators.minLength(3), Validators.maxLength(50) ])),
75 description: new Control('', Validators.compose([ Validators.required, Validators.minLength(3), Validators.maxLength(250) ])),
76 tags: new Control('', Validators.pattern('^[a-zA-Z0-9]{2,10}$'))
80 this.uploader = new FileUploader({
81 authToken: this.authService.getRequestHeaderValue(),
83 url: '/api/v1/videos',
84 removeAfterUpload: true
87 this.uploader.onBuildItemForm = (item, form) => {
88 form.append('name', this.video.name);
89 form.append('description', this.video.description);
91 for (let i = 0; i < this.video.tags.length; i++) {
92 form.append(`tags[${i}]`, this.video.tags[i]);
97 onTagKeyPress(event: KeyboardEvent) {
99 if (event.keyCode === 13) {
100 // Check if the tag is valid and does not already exist
102 this.currentTag !== '' &&
103 this.videoForm.controls['tags'].valid &&
104 this.video.tags.indexOf(this.currentTag) === -1
106 this.video.tags.push(this.currentTag);
107 this.currentTag = '';
113 this.uploader.clearQueue();
116 removeTag(tag: string) {
117 this.video.tags.splice(this.video.tags.indexOf(tag), 1);
121 const item = this.uploader.queue[0];
122 // TODO: wait for https://github.com/valor-software/ng2-file-upload/pull/242
123 item.alias = 'videofile';
125 item.onSuccess = () => {
126 console.log('Video uploaded.');
128 // Print all the videos once it's finished
129 this.router.navigate(['VideosList']);
132 item.onError = (response: string, status: number) => {
133 this.error = (status === 400) ? response : 'Unknow error';
134 console.error(this.error);
138 this.uploader.uploadAll();