import { Component, OnInit, Input, Output, EventEmitter, OnChanges } from '@angular/core'; import { FileUploader, FileItem } from 'ng2-file-upload'; import { FileManagementService } from '../file-management.service'; @Component({ selector: 'lib-file-upload', templateUrl: './file-upload.component.html', styleUrls: ['./file-upload.component.css'] }) export class FileUploadComponent implements OnInit, OnChanges { @Input() dataSource; @Input() breadCrumbs; @Input() selectedFile; @Output() addFile = new EventEmitter(); @Output() editFile = new EventEmitter(); @Output() changeView = new EventEmitter(); oneAtATime = true; urlName = ''; request = {}; sidebar3Opened = true; uploader: FileUploader = new FileUploader({ url: '/api/' }); hasBaseDropZoneOver = false; searchedProduct = ''; url = ''; uploadedFiles: any; constructor(private service: FileManagementService) { } ngOnInit() { } ngOnChanges(changes) { if (changes && changes.dataSource && changes.dataSource.currentValue !== changes.dataSource.previousValue) { this.dataSource = changes.dataSource.currentValue; this.dataSource.forEach(ele => { if (ele && ele.values && ele.values.length === 0) { this.service.getAPIs(ele.metaDataLink).subscribe((res: any) => { ele.values = res; }, error => { console.log(error); }); } }); } if (changes && changes.breadCrumbs && changes.breadCrumbs.currentValue !== changes.breadCrumbs.previousValue) { this.breadCrumbs = changes.breadCrumbs.currentValue; } if (changes && changes.selectedFile && changes.selectedFile.currentValue !== changes.selectedFile.previousValue) { this.selectedFile = changes.selectedFile.currentValue; this.setTags(this.selectedFile, this.dataSource); } } fileOverBase(e: any): void { this.hasBaseDropZoneOver = e; } addRemoveFile(type) { if (type === 'url') { if (this.url && this.uploader.queue.length > 0) { this.uploader.removeFromQueue(this.uploader.queue[0]); this.uploadedFiles = null; } } if (type === 'file') { if (this.uploader.queue.length > 0) { this.uploader.removeFromQueue(this.uploader.queue[0]); this.uploadedFiles = null; } } } selectionChanged(id, item, e?) { if (item) { if (this.request[item.requestKey]) { if (item.multiSelect) { if (e) { this.request[item.requestKey].push(id); } else if (!e) { this.request[item.requestKey] = this.request[item.requestKey].filter(x => x !== id); } } else if (!item.multiSelect) { if (e) { this.request[item.requestKey] = id; } else if (!e) { delete this.request[item.requestKey]; } } } else { if (item.multiSelect) { this.request[item.requestKey] = [id]; } else { this.request[item.requestKey] = id; } } } } isChecked(id, item) { if (item) { if (item.multiSelect) { return this.request[item.requestKey] && this.request[item.requestKey].length > 0 && this.request[item.requestKey].findIndex(x => x === id) >= 0; } else if (!item.multiSelect) { return this.request[item.requestKey] && (this.request[item.requestKey] === id); } } } setTags(fileObj, data) { if (fileObj && fileObj.columns && fileObj.columns.tags && data) { Object.entries(fileObj.columns.tags).forEach((ele: any) => { if (ele[1].length > 0) { const reqKey = data.find(x => x.tagName === ele[0]); if (reqKey) { this.request[reqKey.requestKey] = ele[1].map(x => x.id); } } }); } } getSearchedData(val, canSearch, bindName, searchedKey?) { if (val && canSearch) { return val.filter(x => x[bindName].toLowerCase().includes(searchedKey.toLowerCase())); } else { return val; } } changeViewMode(view) { this.changeView.emit(view); } saveData(selectedFile) { if (this.uploader.queue.length > 0) { this.request['file'] = this.uploader.queue[0].file.rawFile; } if (this.url) { this.request['url'] = this.url; } let formData = new FormData(); const obj = {}; for (let key in this.request) { if (key === 'file') { formData.append(key, this.request[key]); } else { obj[key] = this.request[key]; } } formData.append('dataToBeSaved', JSON.stringify(obj)); if (!selectedFile) { this.addFile.emit({ 'data': formData }); } else { this.editFile.emit({ 'data': { 'dataToBeSaved': obj }, 'fileId': selectedFile.fileId }); } } }