From af0d9e724abb51d02581c50b64d3822a3f61645e Mon Sep 17 00:00:00 2001 From: Benjamin Ifland Date: Thu, 28 Mar 2019 14:14:37 +0100 Subject: [PATCH] file detail edit form --- WEB/src/app/data/edit-song.service.ts | 87 +++++++++++++++++---------- 1 file changed, 56 insertions(+), 31 deletions(-) diff --git a/WEB/src/app/data/edit-song.service.ts b/WEB/src/app/data/edit-song.service.ts index 0bea8cc..ad14073 100644 --- a/WEB/src/app/data/edit-song.service.ts +++ b/WEB/src/app/data/edit-song.service.ts @@ -10,8 +10,10 @@ import { Song } from '../models/song.model'; export class EditSongService { constructor(private songsService: SongsService) {} - public initEditForm(attachSync: boolean): FormGroup { - const song = attachSync ? this.songsService.selectedSong.value : this.defaultValues(); + public initSongEditForm(attachSync: boolean): FormGroup { + const song = attachSync + ? this.songsService.selectedSong.value + : this.defaultValues(); const form = new FormGroup({ Number: new FormControl(song.Number, { updateOn: 'blur', @@ -31,42 +33,65 @@ export class EditSongService { validators: Validators.required }), Tempo: new FormControl(song.Tempo, { updateOn: 'blur' }), - Comments: new FormControl(song.Comments, { updateOn: 'blur' }), + Comments: new FormControl(song.Comments, { updateOn: 'blur' }) }); - if (attachSync) { this.attachSync(form, song); } + if (attachSync) { + this.attachSync(form, song); + } return form; } - private attachSync(form: FormGroup, song: Song) { - const controls = Object.keys(form.controls); - controls.forEach(control => { - form.controls[control].valueChanges.pipe(switchMap(value => this.songsService.patch$(song.ID, control, value))).subscribe(); - }); - } + public initFileEditForm(fileId: number): FormGroup { + const file = this.songsService.selectedSong.value.Files.filter( + _ => _.ID === fileId + )[0]; + const form = new FormGroup({ + Name: new FormControl(file.Name, { + updateOn: 'blur', + validators: Validators.required + }), + FileType: new FormControl(file.FileType, { + updateOn: 'blur' + }) + }); - private defaultValues(): Song { - const song: Song = { - ID: null, - Number: this.firstFreeNumber(), - Name: null, - Tempo: null, - Text: null, - SongType: null, - Key: null, - Comments: null, - Final: false, - Files: [] - }; + return form; + } - return song; - } + private attachSync(form: FormGroup, song: Song) { + const controls = Object.keys(form.controls); + controls.forEach(control => { + form.controls[control].valueChanges + .pipe( + switchMap(value => this.songsService.patch$(song.ID, control, value)) + ) + .subscribe(); + }); + } - private firstFreeNumber(): number { - let number = 0; - const numbers = this.songsService.songs.value.map(_ => _.Number); - while (numbers.indexOf(++number) !== -1) { } - return number; - } + private defaultValues(): Song { + const song: Song = { + ID: null, + Number: this.firstFreeNumber(), + Name: null, + Tempo: null, + Text: null, + SongType: null, + Key: null, + Comments: null, + Final: false, + Files: [] + }; + + return song; + } + + private firstFreeNumber(): number { + let number = 0; + const numbers = this.songsService.songs.value.map(_ => _.Number); + while (numbers.indexOf(++number) !== -1) {} + return number; + } }