diff --git a/src/app/components/fileupload/fileupload.interface.ts b/src/app/components/fileupload/fileupload.interface.ts index ee803b6b84c..aaa175fdceb 100644 --- a/src/app/components/fileupload/fileupload.interface.ts +++ b/src/app/components/fileupload/fileupload.interface.ts @@ -12,6 +12,20 @@ export interface UploadEvent { */ originalEvent: HttpEvent; } +/** + * Remove uploaded file event. + * @group Events + */ +export interface RemoveUploadedFileEvent { + /** + * Removed file. + */ + file: any; + /** + * Uploaded files. + */ + files: any[]; +} /** * Form data event. * @group Events @@ -128,6 +142,31 @@ export interface FileUploadTemplates { * Custom template of file. */ file(): TemplateRef; + /** + * Custom template of file. + */ + header(context: { + /** + * File list. + */ + $implicit: any; + /** + * Uploaded files list. + */ + uploadedFiles: any; + /** + * Callback to invoke on choose button click. + */ + chooseCallback: VoidFunction; + /** + * Callback to invoke on clear button click. + */ + clearCallback: VoidFunction; + /** + * Callback to invoke on upload. + */ + uploadCallback: VoidFunction; + }): TemplateRef; /** * Custom template of content. */ @@ -136,7 +175,40 @@ export interface FileUploadTemplates { * File list. */ $implicit: any; - }): TemplateRef<{ $implicit: any }>; + /** + * Uploaded files list. + */ + uploadedFiles: any; + /** + * Upload progress. + */ + progress: any; + /** + * Status messages about upload process. + */ + messages: any; + /** + * Callback to invoke on choose button click. + */ + chooseCallback: VoidFunction; + /** + * Callback to invoke on clear button click. + */ + removeFileCallback: VoidFunction; + /** + * Callback to invoke on clear button click. + */ + clearCallback: VoidFunction; + /** + * Callback to invoke on upload. + */ + uploadCallback: VoidFunction; + /** + * Callback to invoke on remove uploaded file, accepts index as a parameter. + * @param index Index of the file to remove. + */ + removeUploadedFileCallback: VoidFunction; + }): TemplateRef; /** * Custom template of toolbar. */ diff --git a/src/app/components/fileupload/fileupload.ts b/src/app/components/fileupload/fileupload.ts index 22e5d03f4e9..e192cb0b591 100755 --- a/src/app/components/fileupload/fileupload.ts +++ b/src/app/components/fileupload/fileupload.ts @@ -23,7 +23,8 @@ import { ViewChild, ViewEncapsulation, booleanAttribute, - numberAttribute + numberAttribute, + signal } from '@angular/core'; import { DomSanitizer } from '@angular/platform-browser'; import { BlockableUI, Message, PrimeNGConfig, PrimeTemplate, SharedModule, TranslationKeys } from 'primeng/api'; @@ -37,7 +38,7 @@ import { ProgressBarModule } from 'primeng/progressbar'; import { RippleModule } from 'primeng/ripple'; import { VoidListener } from 'primeng/ts-helpers'; import { Subscription } from 'rxjs'; -import { FileBeforeUploadEvent, FileProgressEvent, FileRemoveEvent, FileSelectEvent, FileSendEvent, FileUploadErrorEvent, FileUploadEvent, FileUploadHandlerEvent } from './fileupload.interface'; +import { FileBeforeUploadEvent, FileProgressEvent, FileRemoveEvent, FileSelectEvent, FileSendEvent, FileUploadErrorEvent, FileUploadEvent, FileUploadHandlerEvent, RemoveUploadedFileEvent } from './fileupload.interface'; /** * FileUpload is an advanced uploader with dragdrop support, multi file uploads, auto uploading, progress tracking and validations. * @group Components @@ -46,6 +47,18 @@ import { FileBeforeUploadEvent, FileProgressEvent, FileRemoveEvent, FileSelectEv selector: 'p-fileUpload', template: `
+
- +
@@ -126,7 +139,7 @@ import { FileBeforeUploadEvent, FileProgressEvent, FileRemoveEvent, FileSelectEv
- +
@@ -417,6 +430,12 @@ export class FileUpload implements AfterViewInit, AfterContentInit, OnInit, OnDe * @group Emits */ @Output() onImageError: EventEmitter = new EventEmitter(); + /** + * This event is triggered if an error occurs while loading an image file. + * @param {RemoveUploadedFileEvent} event - Remove event. + * @group Emits + */ + @Output() onRemoveUploadedFile: EventEmitter = new EventEmitter(); @ContentChildren(PrimeTemplate) templates: QueryList | undefined; @@ -490,6 +509,8 @@ export class FileUpload implements AfterViewInit, AfterContentInit, OnInit, OnDe dragOverListener: VoidListener; + public uploadedFiles = []; + constructor( @Inject(DOCUMENT) private document: Document, @Inject(PLATFORM_ID) private platformId: any, @@ -741,7 +762,7 @@ export class FileUpload implements AfterViewInit, AfterContentInit, OnInit, OnDe } else { this.onError.emit({ files: this.files }); } - + this.uploadedFiles.push(...this.files); this.clear(); break; case HttpEventType.UploadProgress: { @@ -774,13 +795,28 @@ export class FileUpload implements AfterViewInit, AfterContentInit, OnInit, OnDe this.clearInputElement(); this.cd.markForCheck(); } - + /** + * Removes a single file. + * @param {Event} event - Browser event. + * @param {Number} index - Index of the file. + * @group Method + */ remove(event: Event, index: number) { this.clearInputElement(); this.onRemove.emit({ originalEvent: event, file: this.files[index] }); this.files.splice(index, 1); this.checkFileLimit(this.files); } + /** + * Removes uploaded file. + * @param {Number} index - Index of the file to be removed. + * @group Method + */ + removeUploadedFile(index) { + let removedFile = this.uploadedFiles.splice(index, 1)[0]; + this.uploadedFiles = [...this.uploadedFiles]; + this.onRemoveUploadedFile.emit({ file: removedFile, files: this.uploadedFiles }); + } isFileLimitExceeded() { const isAutoMode = this.auto; diff --git a/src/app/showcase/doc/fileupload/templatedoc.ts b/src/app/showcase/doc/fileupload/templatedoc.ts index 45d9aa7f12c..97e918e1670 100644 --- a/src/app/showcase/doc/fileupload/templatedoc.ts +++ b/src/app/showcase/doc/fileupload/templatedoc.ts @@ -1,5 +1,6 @@ -import { Component } from '@angular/core'; +import { Component, inject } from '@angular/core'; import { Code } from '@domain/code'; +import { PrimeNGConfig } from 'primeng/api'; @Component({ selector: 'file-upload-template-demo', @@ -13,20 +14,20 @@ import { Code } from '@domain/code'; >
- +
- - - + + +
{{ totalSize }}B / 1Mb
- -
+ +
Pending
@@ -36,7 +37,6 @@ import { Code } from '@domain/code'; {{ file.name }}
{{ formatSize(file.size) }}
-
@@ -50,7 +50,7 @@ import { Code } from '@domain/code'; {{ file.name }}
{{ formatSize(file.size) }}
- +
@@ -67,6 +67,34 @@ import { Code } from '@domain/code'; ` }) export class TemplateDoc { + config = inject(PrimeNGConfig); + + choose(event, callback) { + callback(); + } + + onRemoveTemplatingFile(event, file, removeFileCallback, index) { + removeFileCallback(event, index); + } + + uploadEvent(callback) { + callback(); + } + + formatSize(bytes) { + const k = 1024; + const dm = 3; + const sizes = this.config.translation.fileSizeTypes; + if (bytes === 0) { + return `0 \${sizes[0]}`; + } + + const i = Math.floor(Math.log(bytes) / Math.log(k)); + const formattedSize = parseFloat((bytes / Math.pow(k, i)).toFixed(dm)); + + return `${formattedSize} ${sizes[i]}`; + } + code: Code = { basic: `