diff options
author | Chocobozzz <florian.bigard@gmail.com> | 2016-10-02 15:39:09 +0200 |
---|---|---|
committer | Chocobozzz <florian.bigard@gmail.com> | 2016-10-02 15:39:09 +0200 |
commit | a6375e69668ea42e19531c6bc68dcd37f3f7cbd7 (patch) | |
tree | 03204a408d56311692c3528bedcf95d2455e94f2 /client/src/app/videos/video-add | |
parent | 052937db8a8d282eccdbdf38d487ed8d85d3c0a7 (diff) | |
parent | c4403b29ad4db097af528a7f04eea07e0ed320d0 (diff) | |
download | PeerTube-a6375e69668ea42e19531c6bc68dcd37f3f7cbd7.tar.gz PeerTube-a6375e69668ea42e19531c6bc68dcd37f3f7cbd7.tar.zst PeerTube-a6375e69668ea42e19531c6bc68dcd37f3f7cbd7.zip |
Merge branch 'master' into webseed-merged
Diffstat (limited to 'client/src/app/videos/video-add')
-rw-r--r-- | client/src/app/videos/video-add/video-add.component.html | 30 | ||||
-rw-r--r-- | client/src/app/videos/video-add/video-add.component.ts | 93 |
2 files changed, 69 insertions, 54 deletions
diff --git a/client/src/app/videos/video-add/video-add.component.html b/client/src/app/videos/video-add/video-add.component.html index bcd78c7cb..64320cae7 100644 --- a/client/src/app/videos/video-add/video-add.component.html +++ b/client/src/app/videos/video-add/video-add.component.html | |||
@@ -2,31 +2,31 @@ | |||
2 | 2 | ||
3 | <div *ngIf="error" class="alert alert-danger">{{ error }}</div> | 3 | <div *ngIf="error" class="alert alert-danger">{{ error }}</div> |
4 | 4 | ||
5 | <form novalidate (ngSubmit)="upload()" [ngFormModel]="videoForm"> | 5 | <form novalidate (ngSubmit)="upload()" [formGroup]="form"> |
6 | <div class="form-group"> | 6 | <div class="form-group"> |
7 | <label for="name">Name</label> | 7 | <label for="name">Name</label> |
8 | <input | 8 | <input |
9 | type="text" class="form-control" name="name" id="name" | 9 | type="text" class="form-control" id="name" |
10 | ngControl="name" #name="ngForm" [(ngModel)]="video.name" | 10 | formControlName="name" |
11 | > | 11 | > |
12 | <div [hidden]="name.valid || name.pristine" class="alert alert-warning"> | 12 | <div *ngIf="formErrors.name" class="alert alert-danger"> |
13 | A name is required and should be between 3 and 50 characters long | 13 | {{ formErrors.name }} |
14 | </div> | 14 | </div> |
15 | </div> | 15 | </div> |
16 | 16 | ||
17 | <div class="form-group"> | 17 | <div class="form-group"> |
18 | <label for="tags">Tags</label> | 18 | <label for="tags">Tags</label> |
19 | <input | 19 | <input |
20 | type="text" class="form-control" name="tags" id="tags" | 20 | type="text" class="form-control" id="currentTag" |
21 | ngControl="tags" #tags="ngForm" [disabled]="isTagsInputDisabled" (keyup)="onTagKeyPress($event)" [(ngModel)]="currentTag" | 21 | formControlName="currentTag" (keyup)="onTagKeyPress($event)" |
22 | > | 22 | > |
23 | <div [hidden]="tags.valid || tags.pristine" class="alert alert-warning"> | 23 | <div *ngIf="formErrors.currentTag" class="alert alert-danger"> |
24 | A tag should be between 2 and 10 characters (alphanumeric) long | 24 | {{ formErrors.currentTag }} |
25 | </div> | 25 | </div> |
26 | </div> | 26 | </div> |
27 | 27 | ||
28 | <div class="tags"> | 28 | <div class="tags"> |
29 | <div class="label label-primary tag" *ngFor="let tag of video.tags"> | 29 | <div class="label label-primary tag" *ngFor="let tag of tags"> |
30 | {{ tag }} | 30 | {{ tag }} |
31 | <span class="remove" (click)="removeTag(tag)">x</span> | 31 | <span class="remove" (click)="removeTag(tag)">x</span> |
32 | </div> | 32 | </div> |
@@ -53,12 +53,12 @@ | |||
53 | <div class="form-group"> | 53 | <div class="form-group"> |
54 | <label for="description">Description</label> | 54 | <label for="description">Description</label> |
55 | <textarea | 55 | <textarea |
56 | name="description" id="description" class="form-control" placeholder="Description..." | 56 | id="description" class="form-control" placeholder="Description..." |
57 | ngControl="description" #description="ngForm" [(ngModel)]="video.description" | 57 | formControlName="description" |
58 | > | 58 | > |
59 | </textarea> | 59 | </textarea> |
60 | <div [hidden]="description.valid || description.pristine" class="alert alert-warning"> | 60 | <div *ngIf="formErrors.description" class="alert alert-danger"> |
61 | A description is required and should be between 3 and 250 characters long | 61 | {{ formErrors.description }} |
62 | </div> | 62 | </div> |
63 | </div> | 63 | </div> |
64 | 64 | ||
@@ -69,7 +69,7 @@ | |||
69 | <div class="form-group"> | 69 | <div class="form-group"> |
70 | <input | 70 | <input |
71 | type="submit" value="Upload" class="btn btn-default form-control" [title]="getInvalidFieldsTitle()" | 71 | type="submit" value="Upload" class="btn btn-default form-control" [title]="getInvalidFieldsTitle()" |
72 | [disabled]="!videoForm.valid || video.tags.length === 0 || filename === null" | 72 | [disabled]="!form.valid || tags.length === 0 || filename === null" |
73 | > | 73 | > |
74 | </div> | 74 | </div> |
75 | </form> | 75 | </form> |
diff --git a/client/src/app/videos/video-add/video-add.component.ts b/client/src/app/videos/video-add/video-add.component.ts index c0f8cb9c4..d12a7d572 100644 --- a/client/src/app/videos/video-add/video-add.component.ts +++ b/client/src/app/videos/video-add/video-add.component.ts | |||
@@ -1,37 +1,42 @@ | |||
1 | import { Control, ControlGroup, Validators } from '@angular/common'; | ||
2 | import { Component, ElementRef, OnInit } from '@angular/core'; | 1 | import { Component, ElementRef, OnInit } from '@angular/core'; |
2 | import { FormBuilder, FormGroup } from '@angular/forms'; | ||
3 | import { Router } from '@angular/router'; | 3 | import { Router } from '@angular/router'; |
4 | 4 | ||
5 | import { BytesPipe } from 'angular-pipes/src/math/bytes.pipe'; | 5 | import { FileUploader } from 'ng2-file-upload/ng2-file-upload'; |
6 | import { PROGRESSBAR_DIRECTIVES } from 'ng2-bootstrap/components/progressbar'; | ||
7 | import { FileSelectDirective, FileUploader } from 'ng2-file-upload/ng2-file-upload'; | ||
8 | 6 | ||
9 | import { AuthService } from '../../shared'; | 7 | import { AuthService, FormReactive, VIDEO_NAME, VIDEO_DESCRIPTION, VIDEO_TAGS } from '../../shared'; |
10 | 8 | ||
11 | @Component({ | 9 | @Component({ |
12 | selector: 'my-videos-add', | 10 | selector: 'my-videos-add', |
13 | styles: [ require('./video-add.component.scss') ], | 11 | styleUrls: [ './video-add.component.scss' ], |
14 | template: require('./video-add.component.html'), | 12 | templateUrl: './video-add.component.html' |
15 | directives: [ FileSelectDirective, PROGRESSBAR_DIRECTIVES ], | ||
16 | pipes: [ BytesPipe ] | ||
17 | }) | 13 | }) |
18 | 14 | ||
19 | export class VideoAddComponent implements OnInit { | 15 | export class VideoAddComponent extends FormReactive implements OnInit { |
20 | currentTag: string; // Tag the user is writing in the input | 16 | tags: string[] = []; |
21 | error: string = null; | ||
22 | videoForm: ControlGroup; | ||
23 | uploader: FileUploader; | 17 | uploader: FileUploader; |
24 | video = { | 18 | |
19 | error: string = null; | ||
20 | form: FormGroup; | ||
21 | formErrors = { | ||
25 | name: '', | 22 | name: '', |
26 | tags: [], | 23 | description: '', |
27 | description: '' | 24 | currentTag: '' |
25 | }; | ||
26 | validationMessages = { | ||
27 | name: VIDEO_NAME.MESSAGES, | ||
28 | description: VIDEO_DESCRIPTION.MESSAGES, | ||
29 | currentTag: VIDEO_TAGS.MESSAGES | ||
28 | }; | 30 | }; |
29 | 31 | ||
30 | constructor( | 32 | constructor( |
31 | private authService: AuthService, | 33 | private authService: AuthService, |
32 | private elementRef: ElementRef, | 34 | private elementRef: ElementRef, |
35 | private formBuilder: FormBuilder, | ||
33 | private router: Router | 36 | private router: Router |
34 | ) {} | 37 | ) { |
38 | super(); | ||
39 | } | ||
35 | 40 | ||
36 | get filename() { | 41 | get filename() { |
37 | if (this.uploader.queue.length === 0) { | 42 | if (this.uploader.queue.length === 0) { |
@@ -41,20 +46,26 @@ export class VideoAddComponent implements OnInit { | |||
41 | return this.uploader.queue[0].file.name; | 46 | return this.uploader.queue[0].file.name; |
42 | } | 47 | } |
43 | 48 | ||
44 | get isTagsInputDisabled () { | 49 | buildForm() { |
45 | return this.video.tags.length >= 3; | 50 | this.form = this.formBuilder.group({ |
51 | name: [ '', VIDEO_NAME.VALIDATORS ], | ||
52 | description: [ '', VIDEO_DESCRIPTION.VALIDATORS ], | ||
53 | currentTag: [ '', VIDEO_TAGS.VALIDATORS ] | ||
54 | }); | ||
55 | |||
56 | this.form.valueChanges.subscribe(data => this.onValueChanged(data)); | ||
46 | } | 57 | } |
47 | 58 | ||
48 | getInvalidFieldsTitle() { | 59 | getInvalidFieldsTitle() { |
49 | let title = ''; | 60 | let title = ''; |
50 | const nameControl = this.videoForm.controls['name']; | 61 | const nameControl = this.form.controls['name']; |
51 | const descriptionControl = this.videoForm.controls['description']; | 62 | const descriptionControl = this.form.controls['description']; |
52 | 63 | ||
53 | if (!nameControl.valid) { | 64 | if (!nameControl.valid) { |
54 | title += 'A name is required\n'; | 65 | title += 'A name is required\n'; |
55 | } | 66 | } |
56 | 67 | ||
57 | if (this.video.tags.length === 0) { | 68 | if (this.tags.length === 0) { |
58 | title += 'At least one tag is required\n'; | 69 | title += 'At least one tag is required\n'; |
59 | } | 70 | } |
60 | 71 | ||
@@ -70,13 +81,6 @@ export class VideoAddComponent implements OnInit { | |||
70 | } | 81 | } |
71 | 82 | ||
72 | ngOnInit() { | 83 | ngOnInit() { |
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}$')) | ||
77 | }); | ||
78 | |||
79 | |||
80 | this.uploader = new FileUploader({ | 84 | this.uploader = new FileUploader({ |
81 | authToken: this.authService.getRequestHeaderValue(), | 85 | authToken: this.authService.getRequestHeaderValue(), |
82 | queueLimit: 1, | 86 | queueLimit: 1, |
@@ -85,26 +89,37 @@ export class VideoAddComponent implements OnInit { | |||
85 | }); | 89 | }); |
86 | 90 | ||
87 | this.uploader.onBuildItemForm = (item, form) => { | 91 | this.uploader.onBuildItemForm = (item, form) => { |
88 | form.append('name', this.video.name); | 92 | const name = this.form.value['name']; |
89 | form.append('description', this.video.description); | 93 | const description = this.form.value['description']; |
94 | |||
95 | form.append('name', name); | ||
96 | form.append('description', description); | ||
90 | 97 | ||
91 | for (let i = 0; i < this.video.tags.length; i++) { | 98 | for (let i = 0; i < this.tags.length; i++) { |
92 | form.append(`tags[${i}]`, this.video.tags[i]); | 99 | form.append(`tags[${i}]`, this.tags[i]); |
93 | } | 100 | } |
94 | }; | 101 | }; |
102 | |||
103 | this.buildForm(); | ||
95 | } | 104 | } |
96 | 105 | ||
97 | onTagKeyPress(event: KeyboardEvent) { | 106 | onTagKeyPress(event: KeyboardEvent) { |
107 | const currentTag = this.form.value['currentTag']; | ||
108 | |||
98 | // Enter press | 109 | // Enter press |
99 | if (event.keyCode === 13) { | 110 | if (event.keyCode === 13) { |
100 | // Check if the tag is valid and does not already exist | 111 | // Check if the tag is valid and does not already exist |
101 | if ( | 112 | if ( |
102 | this.currentTag !== '' && | 113 | currentTag !== '' && |
103 | this.videoForm.controls['tags'].valid && | 114 | this.form.controls['currentTag'].valid && |
104 | this.video.tags.indexOf(this.currentTag) === -1 | 115 | this.tags.indexOf(currentTag) === -1 |
105 | ) { | 116 | ) { |
106 | this.video.tags.push(this.currentTag); | 117 | this.tags.push(currentTag); |
107 | this.currentTag = ''; | 118 | this.form.patchValue({ currentTag: '' }); |
119 | |||
120 | if (this.tags.length >= 3) { | ||
121 | this.form.get('currentTag').disable(); | ||
122 | } | ||
108 | } | 123 | } |
109 | } | 124 | } |
110 | } | 125 | } |
@@ -114,7 +129,7 @@ export class VideoAddComponent implements OnInit { | |||
114 | } | 129 | } |
115 | 130 | ||
116 | removeTag(tag: string) { | 131 | removeTag(tag: string) { |
117 | this.video.tags.splice(this.video.tags.indexOf(tag), 1); | 132 | this.tags.splice(this.tags.indexOf(tag), 1); |
118 | } | 133 | } |
119 | 134 | ||
120 | upload() { | 135 | upload() { |