mirror of
https://github.com/Alfresco/alfresco-ng2-components.git
synced 2025-07-24 17:32:15 +00:00
[ADF-1443] Refactor Download directive (#4028)
* [ADF-1443] Refactor Download directive * [ADF-1443] Node Download Directive now accepts single node and an array as input * [ADF-1443] Fix Unit tests * [ADF-1443] Fix unit test related to viewer component
This commit is contained in:
committed by
Eugenio Romano
parent
b99f6d57dc
commit
7197e1e13a
@@ -21,7 +21,6 @@ import { FormsModule, ReactiveFormsModule } from '@angular/forms';
|
||||
import { CoreModule } from '@alfresco/adf-core';
|
||||
|
||||
import { MaterialModule } from '../material.module';
|
||||
import { DownloadZipDialogComponent } from './download-zip.dialog';
|
||||
import { FolderDialogComponent } from './folder.dialog';
|
||||
import { NodeLockDialogComponent } from './node-lock.dialog';
|
||||
import { ConfirmDialogComponent } from './confirm.dialog';
|
||||
@@ -40,21 +39,18 @@ import { LibraryDialogComponent } from './library/library.dialog';
|
||||
MatDatetimepickerModule
|
||||
],
|
||||
declarations: [
|
||||
DownloadZipDialogComponent,
|
||||
FolderDialogComponent,
|
||||
NodeLockDialogComponent,
|
||||
ConfirmDialogComponent,
|
||||
LibraryDialogComponent
|
||||
],
|
||||
exports: [
|
||||
DownloadZipDialogComponent,
|
||||
FolderDialogComponent,
|
||||
NodeLockDialogComponent,
|
||||
ConfirmDialogComponent,
|
||||
LibraryDialogComponent
|
||||
],
|
||||
entryComponents: [
|
||||
DownloadZipDialogComponent,
|
||||
FolderDialogComponent,
|
||||
NodeLockDialogComponent,
|
||||
ConfirmDialogComponent,
|
||||
|
@@ -1,10 +0,0 @@
|
||||
<h1 matDialogTitle>{{ 'CORE.DIALOG.DOWNLOAD_ZIP.TITLE' | translate }}</h1>
|
||||
<div mat-dialog-content>
|
||||
<mat-progress-bar color="primary" mode="indeterminate"></mat-progress-bar>
|
||||
</div>
|
||||
<div mat-dialog-actions>
|
||||
<span class="adf-spacer"></span>
|
||||
<button mat-button color="primary" (click)="cancelDownload()">
|
||||
{{ 'CORE.DIALOG.DOWNLOAD_ZIP.ACTIONS.CANCEL' | translate }}
|
||||
</button>
|
||||
</div>
|
@@ -1,5 +0,0 @@
|
||||
.adf-spacer { flex: 1 1 auto; }
|
||||
|
||||
.adf-download-zip-dialog .mat-dialog-actions .mat-button-wrapper {
|
||||
text-transform: uppercase;
|
||||
}
|
@@ -1,111 +0,0 @@
|
||||
/*!
|
||||
* @license
|
||||
* Copyright 2016 Alfresco Software, Ltd.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import { Component, Inject, OnInit, ViewEncapsulation } from '@angular/core';
|
||||
import { MAT_DIALOG_DATA, MatDialogRef } from '@angular/material';
|
||||
import { DownloadEntry, MinimalNodeEntity } from 'alfresco-js-api';
|
||||
import { LogService, AlfrescoApiService } from '@alfresco/adf-core';
|
||||
|
||||
@Component({
|
||||
selector: 'adf-download-zip-dialog',
|
||||
templateUrl: './download-zip.dialog.html',
|
||||
styleUrls: ['./download-zip.dialog.scss'],
|
||||
host: { 'class': 'adf-download-zip-dialog' },
|
||||
encapsulation: ViewEncapsulation.None
|
||||
})
|
||||
export class DownloadZipDialogComponent implements OnInit {
|
||||
|
||||
// flag for async threads
|
||||
private cancelled = false;
|
||||
|
||||
constructor(private apiService: AlfrescoApiService,
|
||||
private dialogRef: MatDialogRef<DownloadZipDialogComponent>,
|
||||
@Inject(MAT_DIALOG_DATA) private data: any,
|
||||
private logService: LogService) {
|
||||
}
|
||||
|
||||
ngOnInit() {
|
||||
if (this.data && this.data.nodeIds && this.data.nodeIds.length > 0) {
|
||||
if (!this.cancelled) {
|
||||
this.downloadZip(this.data.nodeIds);
|
||||
} else {
|
||||
this.logService.log('Cancelled');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
cancelDownload() {
|
||||
this.cancelled = true;
|
||||
this.dialogRef.close(false);
|
||||
}
|
||||
|
||||
downloadZip(nodeIds: string[]) {
|
||||
if (nodeIds && nodeIds.length > 0) {
|
||||
|
||||
const promise: any = this.apiService.getInstance().core.downloadsApi.createDownload({ nodeIds });
|
||||
|
||||
promise.on('progress', (progress) => this.logService.log('Progress', progress));
|
||||
promise.on('error', (error) => this.logService.error('Error', error));
|
||||
promise.on('abort', (data) => this.logService.log('Abort', data));
|
||||
|
||||
promise.on('success', (data: DownloadEntry) => {
|
||||
if (data && data.entry && data.entry.id) {
|
||||
const url = this.apiService.getInstance().content.getContentUrl(data.entry.id, true);
|
||||
|
||||
this.apiService.getInstance().core.nodesApi.getNode(data.entry.id).then((downloadNode: MinimalNodeEntity) => {
|
||||
this.logService.log(downloadNode);
|
||||
const fileName = downloadNode.entry.name;
|
||||
this.waitAndDownload(data.entry.id, url, fileName);
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
waitAndDownload(downloadId: string, url: string, fileName: string) {
|
||||
if (this.cancelled) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.apiService.getInstance().core.downloadsApi.getDownload(downloadId).then((downloadEntry: DownloadEntry) => {
|
||||
if (downloadEntry.entry) {
|
||||
if (downloadEntry.entry.status === 'DONE') {
|
||||
this.download(url, fileName);
|
||||
} else {
|
||||
setTimeout(() => {
|
||||
this.waitAndDownload(downloadId, url, fileName);
|
||||
}, 1000);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
download(url: string, fileName: string) {
|
||||
if (url && fileName) {
|
||||
const link = document.createElement('a');
|
||||
|
||||
link.style.display = 'none';
|
||||
link.download = fileName;
|
||||
link.href = url;
|
||||
|
||||
document.body.appendChild(link);
|
||||
link.click();
|
||||
document.body.removeChild(link);
|
||||
}
|
||||
this.dialogRef.close(true);
|
||||
}
|
||||
}
|
@@ -15,7 +15,6 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
export * from './download-zip.dialog';
|
||||
export * from './folder.dialog';
|
||||
export * from './node-lock.dialog';
|
||||
export * from './confirm.dialog';
|
||||
|
Reference in New Issue
Block a user