]>
Commit | Line | Data |
---|---|---|
ff249f49 C |
1 | import { Component, Input, OnInit } from '@angular/core' |
2 | import { FormBuilder, FormControl, FormGroup } from '@angular/forms' | |
3 | import { ActivatedRoute, Router } from '@angular/router' | |
07fa4c97 | 4 | import { VIDEO_IMAGE, VIDEO_SUPPORT } from '@app/shared' |
ff249f49 | 5 | import { NotificationsService } from 'angular2-notifications' |
63c4db6d | 6 | import { ServerService } from '../../../core/server' |
15a7387d | 7 | import { VIDEO_CHANNEL } from '../../../shared/forms/form-validators' |
63c4db6d | 8 | import { ValidatorMessage } from '../../../shared/forms/form-validators/validator-message' |
ff249f49 | 9 | import { |
ff249f49 C |
10 | VIDEO_CATEGORY, |
11 | VIDEO_DESCRIPTION, | |
12 | VIDEO_LANGUAGE, | |
13 | VIDEO_LICENCE, | |
14 | VIDEO_NAME, | |
15 | VIDEO_PRIVACY, | |
16 | VIDEO_TAGS | |
63c4db6d C |
17 | } from '../../../shared/forms/form-validators/video' |
18 | import { VideoEdit } from '../../../shared/video/video-edit.model' | |
74af5145 | 19 | import { map } from 'rxjs/operators' |
ff249f49 C |
20 | |
21 | @Component({ | |
22 | selector: 'my-video-edit', | |
23 | styleUrls: [ './video-edit.component.scss' ], | |
24 | templateUrl: './video-edit.component.html' | |
25 | }) | |
26 | ||
27 | export class VideoEditComponent implements OnInit { | |
28 | @Input() form: FormGroup | |
29 | @Input() formErrors: { [ id: string ]: string } = {} | |
30 | @Input() validationMessages: ValidatorMessage = {} | |
31 | @Input() videoPrivacies = [] | |
74af5145 | 32 | @Input() userVideoChannels: { id: number, label: string, support: string }[] = [] |
ff249f49 | 33 | |
ff249f49 C |
34 | videoCategories = [] |
35 | videoLicences = [] | |
36 | videoLanguages = [] | |
37 | video: VideoEdit | |
38 | ||
39 | tagValidators = VIDEO_TAGS.VALIDATORS | |
40 | tagValidatorsMessages = VIDEO_TAGS.MESSAGES | |
41 | ||
42 | error: string = null | |
43 | ||
44 | constructor ( | |
45 | private formBuilder: FormBuilder, | |
46 | private route: ActivatedRoute, | |
47 | private router: Router, | |
48 | private notificationsService: NotificationsService, | |
49 | private serverService: ServerService | |
50 | ) { } | |
51 | ||
52 | updateForm () { | |
53 | this.formErrors['name'] = '' | |
54 | this.formErrors['privacy'] = '' | |
15a7387d | 55 | this.formErrors['channelId'] = '' |
ff249f49 C |
56 | this.formErrors['category'] = '' |
57 | this.formErrors['licence'] = '' | |
58 | this.formErrors['language'] = '' | |
59 | this.formErrors['description'] = '' | |
6de36768 C |
60 | this.formErrors['thumbnailfile'] = '' |
61 | this.formErrors['previewfile'] = '' | |
07fa4c97 | 62 | this.formErrors['support'] = '' |
ff249f49 C |
63 | |
64 | this.validationMessages['name'] = VIDEO_NAME.MESSAGES | |
65 | this.validationMessages['privacy'] = VIDEO_PRIVACY.MESSAGES | |
15a7387d | 66 | this.validationMessages['channelId'] = VIDEO_CHANNEL.MESSAGES |
ff249f49 C |
67 | this.validationMessages['category'] = VIDEO_CATEGORY.MESSAGES |
68 | this.validationMessages['licence'] = VIDEO_LICENCE.MESSAGES | |
69 | this.validationMessages['language'] = VIDEO_LANGUAGE.MESSAGES | |
70 | this.validationMessages['description'] = VIDEO_DESCRIPTION.MESSAGES | |
6de36768 C |
71 | this.validationMessages['thumbnailfile'] = VIDEO_IMAGE.MESSAGES |
72 | this.validationMessages['previewfile'] = VIDEO_IMAGE.MESSAGES | |
07fa4c97 | 73 | this.validationMessages['support'] = VIDEO_SUPPORT.MESSAGES |
ff249f49 C |
74 | |
75 | this.form.addControl('name', new FormControl('', VIDEO_NAME.VALIDATORS)) | |
76 | this.form.addControl('privacy', new FormControl('', VIDEO_PRIVACY.VALIDATORS)) | |
0f320037 | 77 | this.form.addControl('channelId', new FormControl('', VIDEO_CHANNEL.VALIDATORS)) |
ff249f49 | 78 | this.form.addControl('nsfw', new FormControl(false)) |
47564bbe | 79 | this.form.addControl('commentsEnabled', new FormControl(true)) |
ff249f49 C |
80 | this.form.addControl('category', new FormControl('', VIDEO_CATEGORY.VALIDATORS)) |
81 | this.form.addControl('licence', new FormControl('', VIDEO_LICENCE.VALIDATORS)) | |
82 | this.form.addControl('language', new FormControl('', VIDEO_LANGUAGE.VALIDATORS)) | |
83 | this.form.addControl('description', new FormControl('', VIDEO_DESCRIPTION.VALIDATORS)) | |
2efd32f6 | 84 | this.form.addControl('tags', new FormControl([])) |
6de36768 C |
85 | this.form.addControl('thumbnailfile', new FormControl('')) |
86 | this.form.addControl('previewfile', new FormControl('')) | |
9f4183c9 | 87 | this.form.addControl('support', new FormControl('', VIDEO_SUPPORT.VALIDATORS)) |
74af5145 C |
88 | |
89 | // We will update the "support" field depending on the channel | |
90 | this.form.controls['channelId'] | |
91 | .valueChanges | |
92 | .pipe(map(res => parseInt(res.toString(), 10))) | |
93 | .subscribe( | |
94 | newChannelId => { | |
95 | const oldChannelId = parseInt(this.form.value['channelId'], 10) | |
96 | const currentSupport = this.form.value['support'] | |
97 | ||
98 | // Not initialized yet | |
99 | if (isNaN(newChannelId)) return | |
100 | const newChannel = this.userVideoChannels.find(c => c.id === newChannelId) | |
101 | ||
102 | // First time we set the channel? | |
103 | if (isNaN(oldChannelId)) return this.updateSupportField(newChannel.support) | |
104 | const oldChannel = this.userVideoChannels.find(c => c.id === oldChannelId) | |
105 | ||
106 | if (!newChannel || !oldChannel) { | |
107 | console.error('Cannot find new or old channel.') | |
108 | return | |
109 | } | |
110 | ||
111 | // If the current support text is not the same than the old channel, the user updated it. | |
112 | // We don't want the user to lose his text, so stop here | |
113 | if (currentSupport && currentSupport !== oldChannel.support) return | |
114 | ||
115 | // Update the support text with our new channel | |
116 | this.updateSupportField(newChannel.support) | |
117 | } | |
118 | ) | |
ff249f49 C |
119 | } |
120 | ||
121 | ngOnInit () { | |
122 | this.updateForm() | |
123 | ||
124 | this.videoCategories = this.serverService.getVideoCategories() | |
125 | this.videoLicences = this.serverService.getVideoLicences() | |
126 | this.videoLanguages = this.serverService.getVideoLanguages() | |
127 | } | |
74af5145 C |
128 | |
129 | private updateSupportField (support: string) { | |
130 | return this.form.patchValue({ support: support || '' }) | |
131 | } | |
ff249f49 | 132 | } |