/** @file * @author Edouard DUPIN * @copyright 2018, Edouard DUPIN, all right reserved * @license PROPRIETARY (see license file) */ import { Component, OnInit } from '@angular/core'; import { PopInService, UploadProgress } from '@kangaroo-and-rabbit/kar-cw'; import { Artist, Track } from 'app/back-api'; import { ArtistService, DataService, GenderService, ArianeService, TrackService } from 'app/service'; export class ElementList { constructor( public value: number, public label: string) { } } @Component({ selector: 'app-artist-edit', templateUrl: './artist-edit.html', styleUrls: ['./artist-edit.less'] }) export class ArtistEditScene implements OnInit { idArtist: number = -1; itemIsRemoved: boolean = false; itemIsNotFound: boolean = false; itemIsLoading: boolean = true; error: string = ''; name: string = ''; description: string = ''; coverFile: File; uploadFileValue: string = ''; selectedFiles: FileList; albumsCount: string = null; trackCount: string = null; coversDisplay: Array = []; // section tha define the upload value to display in the pop-in of upload public upload: UploadProgress = new UploadProgress(); // --------------- confirm section ------------------ public confirmDeleteComment: string = null; public confirmDeleteImageUrl: string = null; private deleteCoverId: string = null; private deleteItemId: number = null; deleteConfirmed() { if (this.deleteCoverId !== null) { this.removeCoverAfterConfirm(this.deleteCoverId); this.cleanConfirm(); } if (this.deleteItemId !== null) { this.removeItemAfterConfirm(this.deleteItemId); this.cleanConfirm(); } } cleanConfirm() { this.confirmDeleteComment = null; this.confirmDeleteImageUrl = null; this.deleteCoverId = null; this.deleteItemId = null; } constructor(private dataService: DataService, private genderService: GenderService, private artistService: ArtistService, private trackService: TrackService, private arianeService: ArianeService, private popInService: PopInService) { } ngOnInit() { this.idArtist = this.arianeService.getArtistId(); let self = this; this.artistService.get(this.idArtist) .then((response: Artist) => { // console.log("get response of track : " + JSON.stringify(response, null, 2)); self.name = response.name; self.description = response.description; self.updateCoverList(response.covers); // console.log("covers_list : " + JSON.stringify(self.covers_display, null, 2)); self.itemIsLoading = false; }).catch((response) => { self.error = 'Can not get the data'; self.name = ''; self.description = ''; self.coversDisplay = []; self.itemIsNotFound = true; self.itemIsLoading = false; }); console.log(`get parameter id: ${this.idArtist}`); this.trackService.getAlbumIdsOfAnArtist(this.idArtist) .then((response: number[]) => { self.albumsCount = "" + response.length; }).catch((response) => { self.albumsCount = '---'; }); this.trackService.getTracksOfAnArtist(this.idArtist) .then((response: Track[]) => { self.trackCount = "" + response.length; }).catch((response) => { self.trackCount = '---'; }); } updateCoverList(covers: any) { this.coversDisplay = []; if (covers !== undefined && covers !== null) { for (let iii = 0; iii < covers.length; iii++) { this.coversDisplay.push({ id: covers[iii], url: this.dataService.getThumbnailUrl(covers[iii]) }); } } else { this.coversDisplay = []; } } onName(value: any): void { this.name = value; } onDescription(value: any): void { this.description = value; } sendValues(): void { console.log('send new values....'); let data = { name: this.name, description: this.description }; if (this.description === undefined) { data.description = null; } this.artistService.patch(this.idArtist, data); } // At the drag drop area // (drop)="onDropFile($event)" onDropFile(event: DragEvent) { event.preventDefault(); // this.uploadFile(_event.dataTransfer.files[0]); } // At the drag drop area // (dragover)="onDragOverFile($event)" onDragOverFile(event) { event.stopPropagation(); event.preventDefault(); } // At the file input element // (change)="selectFile($event)" onChangeCover(value: any): void { this.selectedFiles = value.files; this.coverFile = this.selectedFiles[0]; console.log(`select file ${this.coverFile.name}`); this.uploadCover(this.coverFile); } uploadCover(file: File) { if (file === undefined) { console.log('No file selected!'); return; } let self = this; // clean upload labels this.upload.clear(); // display the upload pop-in this.popInService.open('popin-upload-progress'); this.artistService.uploadCover(this.idArtist, file, (count, total) => { self.upload.mediaSendSize = count; self.upload.mediaSize = total; }) .then((response: any) => { self.upload.result = 'Cover added done'; // we retrive the whiole media ==> update data ... self.updateCoverList(response.covers); }).catch((response: any) => { // self.error = "Can not get the data"; console.log('Can not add the cover in the track...'); }); } removeCover(id: string) { this.cleanConfirm(); this.confirmDeleteComment = `Delete the cover ID: ${id}`; this.confirmDeleteImageUrl = this.dataService.getThumbnailUrl(id); this.deleteCoverId = id; this.popInService.open('popin-delete-confirm'); } removeCoverAfterConfirm(id: string) { console.log(`Request remove cover: ${id}`); let self = this; this.artistService.deleteCover(this.idArtist, id) .then((response: any) => { self.upload.result = 'Cover remove done'; // we retrive the whiole media ==> update data ... self.updateCoverList(response.covers); }).catch((response: any) => { // self.error = "Can not get the data"; console.log('Can not remove the cover of the track...'); }); } removeItem() { console.log('Request remove Media...'); this.cleanConfirm(); this.confirmDeleteComment = `Delete the Artist: ${this.idArtist}`; this.deleteItemId = this.idArtist; this.popInService.open('popin-delete-confirm'); } removeItemAfterConfirm(_id: number) { let self = this; this.artistService.delete(_id) .then((response3) => { // self.data_ori = tmpp; // self.updateNeedSend(); self.itemIsRemoved = true; }).catch((response3) => { // self.updateNeedSend(); }); } }