diff --git a/ng2-components/ng2-alfresco-upload/src/components/file-uploading-dialog.component.html b/ng2-components/ng2-alfresco-upload/src/components/file-uploading-dialog.component.html index 4e42dc8e30..545232110b 100644 --- a/ng2-components/ng2-alfresco-upload/src/components/file-uploading-dialog.component.html +++ b/ng2-components/ng2-alfresco-upload/src/components/file-uploading-dialog.component.html @@ -9,7 +9,7 @@ keyboard_arrow_up -
+
clear
diff --git a/ng2-components/ng2-alfresco-upload/src/components/file-uploading-dialog.component.spec.ts b/ng2-components/ng2-alfresco-upload/src/components/file-uploading-dialog.component.spec.ts index 11dde66c35..5a73b9e76b 100644 --- a/ng2-components/ng2-alfresco-upload/src/components/file-uploading-dialog.component.spec.ts +++ b/ng2-components/ng2-alfresco-upload/src/components/file-uploading-dialog.component.spec.ts @@ -22,8 +22,8 @@ import { CoreModule } from 'ng2-alfresco-core'; import { FileUploadingDialogComponent } from './file-uploading-dialog.component'; import { FileUploadingListComponent } from './file-uploading-list.component'; import { UploadService } from '../services/upload.service'; -import { FileModel } from '../models/file.model'; -import { FileUploadCompleteEvent } from '../events/file.event'; +import { FileModel, FileUploadStatus } from '../models/file.model'; +import { FileUploadCompleteEvent, FileUploadEvent } from '../events/file.event'; describe('FileUploadingDialogComponent', () => { @@ -110,4 +110,38 @@ describe('FileUploadingDialogComponent', () => { expect(element.querySelector('.minimize-button').getAttribute('class')).toEqual('minimize-button active'); }); + + it('should show the close button when the file upload is completed', async(() => { + component.isDialogActive = true; + uploadService.addToQueue(new FileModel( { name: 'file' })); + fixture.detectChanges(); + fixture.whenStable().then(() => { + let closeButton = element.querySelector('#button-close-upload-list'); + expect(closeButton).not.toBeNull(); + }); + + uploadService.fileUpload.next(new FileUploadCompleteEvent(file, 1, { status: FileUploadStatus.Complete }, 0)); + })); + + it('should show the close button when the file upload is in error', async(() => { + component.isDialogActive = true; + fixture.detectChanges(); + fixture.whenStable().then(() => { + let closeButton = element.querySelector('#button-close-upload-list'); + expect(closeButton).not.toBeNull(); + }); + + uploadService.fileUpload.next(new FileUploadEvent(file, FileUploadStatus.Error)); + })); + + it('should show the close button when the file upload is cancelled', async(() => { + component.isDialogActive = true; + fixture.detectChanges(); + fixture.whenStable().then(() => { + let closeButton = element.querySelector('#button-close-upload-list'); + expect(closeButton).not.toBeNull(); + }); + + uploadService.fileUpload.next(new FileUploadEvent(file, FileUploadStatus.Cancelled)); + })); }); diff --git a/ng2-components/ng2-alfresco-upload/src/components/file-uploading-dialog.component.ts b/ng2-components/ng2-alfresco-upload/src/components/file-uploading-dialog.component.ts index 582301c080..e0c3577de3 100644 --- a/ng2-components/ng2-alfresco-upload/src/components/file-uploading-dialog.component.ts +++ b/ng2-components/ng2-alfresco-upload/src/components/file-uploading-dialog.component.ts @@ -16,7 +16,7 @@ */ import { Component, Input, ChangeDetectorRef, OnInit, OnDestroy, ChangeDetectionStrategy } from '@angular/core'; -import { FileModel } from '../models/file.model'; +import { FileModel, FileUploadStatus } from '../models/file.model'; import { AlfrescoTranslationService } from 'ng2-alfresco-core'; import { UploadService } from '../services/upload.service'; import { FileUploadCompleteEvent } from '../events/file.event'; @@ -39,6 +39,7 @@ export class FileUploadingDialogComponent implements OnInit, OnDestroy { private listSubscription: any; private counterSubscription: any; + private showCloseButton: boolean = false; constructor(private cd: ChangeDetectorRef, translateService: AlfrescoTranslationService, @@ -56,17 +57,21 @@ export class FileUploadingDialogComponent implements OnInit, OnDestroy { this.isDialogActive = true; this.cd.detectChanges(); } + this.showCloseButton = false; }); - this.counterSubscription = this.uploadService.fileUploadComplete.subscribe((e: FileUploadCompleteEvent) => { - this.totalCompleted = e.totalComplete; + this.counterSubscription = this.uploadService.fileUploadComplete.subscribe((event: FileUploadCompleteEvent) => { + this.totalCompleted = event.totalComplete; if (this.totalCompleted > 1) { this.totalCompletedMsg = 'FILE_UPLOAD.MESSAGES.COMPLETED'; } this.cd.detectChanges(); }); - this.uploadService.fileUpload.subscribe(e => { + this.uploadService.fileUpload.subscribe((event: FileUploadCompleteEvent) => { + if (event.status !== FileUploadStatus.Progress) { + this.isUploadProcessCompleted(event); + } this.cd.detectChanges(); }); } @@ -76,6 +81,7 @@ export class FileUploadingDialogComponent implements OnInit, OnDestroy { */ toggleVisible(): void { this.isDialogActive = !this.isDialogActive; + this.uploadService.clearQueue(); this.cd.detectChanges(); } @@ -91,4 +97,24 @@ export class FileUploadingDialogComponent implements OnInit, OnDestroy { this.listSubscription.unsubscribe(); this.counterSubscription.unsubscribe(); } + + private isUploadProcessCompleted(event: FileUploadCompleteEvent) { + if (this.isAllFileUploadEnded(event) && this.isUploadStateCompleted(event.status)) { + this.showCloseDialogButton(); + } else if (event.status === FileUploadStatus.Error || event.status === FileUploadStatus.Cancelled) { + this.showCloseDialogButton(); + } + } + + private showCloseDialogButton() { + this.showCloseButton = true; + } + + private isAllFileUploadEnded(event: FileUploadCompleteEvent) { + return event.totalComplete === this.uploadService.getQueue().length - event.totalAborted; + } + + private isUploadStateCompleted(state): boolean { + return FileUploadStatus.Complete === state; + } } diff --git a/ng2-components/ng2-alfresco-upload/src/events/file.event.ts b/ng2-components/ng2-alfresco-upload/src/events/file.event.ts index 690cd5667a..e269b2b943 100644 --- a/ng2-components/ng2-alfresco-upload/src/events/file.event.ts +++ b/ng2-components/ng2-alfresco-upload/src/events/file.event.ts @@ -29,7 +29,7 @@ export class FileUploadEvent { export class FileUploadCompleteEvent extends FileUploadEvent { - constructor(file: FileModel, public totalComplete: number = 0, public data?: any) { + constructor(file: FileModel, public totalComplete: number = 0, public data?: any, public totalAborted: number = 0) { super(file, FileUploadStatus.Complete); } diff --git a/ng2-components/ng2-alfresco-upload/src/services/upload.service.spec.ts b/ng2-components/ng2-alfresco-upload/src/services/upload.service.spec.ts index 72d0edf4db..20159e3f42 100644 --- a/ng2-components/ng2-alfresco-upload/src/services/upload.service.spec.ts +++ b/ng2-components/ng2-alfresco-upload/src/services/upload.service.spec.ts @@ -48,15 +48,15 @@ describe('UploadService', () => { }); it('should add an element in the queue and returns it', () => { - let filesFake = new FileModel({name: 'fake-name', size: 10}); + let filesFake = new FileModel({ name: 'fake-name', size: 10 }); service.addToQueue(filesFake); expect(service.getQueue().length).toEqual(1); }); it('should add two elements in the queue and returns them', () => { let filesFake = [ - new FileModel({name: 'fake-name', size: 10}), - new FileModel({name: 'fake-name2', size: 20}) + new FileModel({ name: 'fake-name', size: 10 }), + new FileModel({ name: 'fake-name2', size: 20 }) ]; service.addToQueue(...filesFake); expect(service.getQueue().length).toEqual(2); @@ -78,7 +78,7 @@ describe('UploadService', () => { done(); }); let fileFake = new FileModel( - {name: 'fake-name', size: 10}, + { name: 'fake-name', size: 10 }, { parentId: '-root-', path: 'fake-dir' } ); service.addToQueue(fileFake); @@ -103,7 +103,7 @@ describe('UploadService', () => { done(); }); let fileFake = new FileModel( - {name: 'fake-name', size: 10}, + { name: 'fake-name', size: 10 }, { parentId: '-root-' } ); service.addToQueue(fileFake); @@ -125,7 +125,7 @@ describe('UploadService', () => { expect(e.value).toEqual('File aborted'); done(); }); - let fileFake = new FileModel({name: 'fake-name', size: 10}); + let fileFake = new FileModel({ name: 'fake-name', size: 10 }); service.addToQueue(fileFake); service.uploadFilesInTheQueue(emitter); @@ -136,7 +136,7 @@ describe('UploadService', () => { it('If versioning is true autoRename should not be present and majorVersion should be a param', () => { let emitter = new EventEmitter(); - const filesFake = new FileModel({name: 'fake-name', size: 10}, { newVersion: true }); + const filesFake = new FileModel({ name: 'fake-name', size: 10 }, { newVersion: true }); service.addToQueue(filesFake); service.uploadFilesInTheQueue(emitter); @@ -152,7 +152,7 @@ describe('UploadService', () => { done(); }); let filesFake = new FileModel( - {name: 'fake-name', size: 10}, + { name: 'fake-name', size: 10 }, { parentId: '123', path: 'fake-dir' } ); service.addToQueue(filesFake); @@ -168,4 +168,26 @@ describe('UploadService', () => { responseText: 'File uploaded' }); }); + + it('should start downloading the next one if a file of the list is aborted', (done) => { + let emitter = new EventEmitter(); + + service.fileUploadAborted.subscribe(e => { + expect(e).not.toBeNull(); + }); + + service.fileUploadCancelled.subscribe(e => { + expect(e).not.toBeNull(); + done(); + }); + + let fileFake1 = new FileModel({ name: 'fake-name1', size: 10 }); + let fileFake2 = new FileModel({ name: 'fake-name2', size: 10 }); + let filelist = [fileFake1, fileFake2]; + service.addToQueue(...filelist); + service.uploadFilesInTheQueue(emitter); + + let file = service.getQueue(); + service.cancelUpload(...file); + }); }); diff --git a/ng2-components/ng2-alfresco-upload/src/services/upload.service.ts b/ng2-components/ng2-alfresco-upload/src/services/upload.service.ts index 649b6a0b22..d26f13348b 100644 --- a/ng2-components/ng2-alfresco-upload/src/services/upload.service.ts +++ b/ng2-components/ng2-alfresco-upload/src/services/upload.service.ts @@ -27,6 +27,7 @@ export class UploadService { private queue: FileModel[] = []; private cache: { [key: string]: any } = {}; private totalComplete: number = 0; + private totalAborted: number = 0; private activeTask: Promise = null; queueChanged: Subject = new Subject(); @@ -98,6 +99,8 @@ export class UploadService { setTimeout(() => this.uploadFilesInTheQueue(emitter), 100); }; + promise.next = next; + promise.then( () => next(), () => next() @@ -122,6 +125,12 @@ export class UploadService { }); } + clearQueue() { + this.queue = []; + this.totalComplete = 0; + this.totalAborted = 0; + } + private beginUpload(file: FileModel, /* @deprecated */emitter: EventEmitter): any { let opts: any = { renditions: 'doclib' @@ -209,7 +218,7 @@ export class UploadService { delete this.cache[file.id]; } - const event = new FileUploadCompleteEvent(file, this.totalComplete, data); + const event = new FileUploadCompleteEvent(file, this.totalComplete, data, this.totalAborted); this.fileUpload.next(event); this.fileUploadComplete.next(event); @@ -220,6 +229,7 @@ export class UploadService { private onUploadAborted(file: FileModel): void { if (file) { file.status = FileUploadStatus.Aborted; + this.totalAborted++; const promise = this.cache[file.id]; if (promise) { @@ -229,6 +239,7 @@ export class UploadService { const event = new FileUploadEvent(file, FileUploadStatus.Aborted); this.fileUpload.next(event); this.fileUploadAborted.next(event); + promise.next(); } } }