[ADF-1374] Move download and create folder dialogs to ADF (#2205)

* move create folder and download zip dialogs to core

* code cleanup
This commit is contained in:
Denys Vuika
2017-08-14 11:09:55 +01:00
committed by Mario Romano
parent f3515ad02b
commit 8b4261acb3
6 changed files with 37 additions and 26 deletions

View File

@@ -30,6 +30,9 @@ import { CardViewModule } from './src/components/view/card-view.module';
import { MaterialModule } from './src/material.module';
import { AppConfigModule } from './src/services/app-config.service';
import { CreateFolderDialogComponent } from './src/dialogs/create-folder.dialog';
import { DownloadZipDialogComponent } from './src/dialogs/download-zip.dialog';
import { AlfrescoApiService } from './src/services/alfresco-api.service';
import { AlfrescoContentService } from './src/services/alfresco-content.service';
import { AlfrescoSettingsService } from './src/services/alfresco-settings.service';
@@ -67,6 +70,9 @@ import { SitesApiService } from './src/services/sites-api.service';
export { MomentDateAdapter, MOMENT_DATE_FORMATS } from './src/utils/momentDateAdapter';
import { MomentDateAdapter } from './src/utils/momentDateAdapter';
export { CreateFolderDialogComponent } from './src/dialogs/create-folder.dialog';
export { DownloadZipDialogComponent } from './src/dialogs/download-zip.dialog';
export { ContentService } from './src/services/content.service';
export { StorageService } from './src/services/storage.service';
export { CookieService } from './src/services/cookie.service';
@@ -230,7 +236,9 @@ export function createTranslateLoader(http: Http, logService: LogService) {
DataColumnListComponent,
FileSizePipe,
HighlightPipe,
TimeAgoPipe
TimeAgoPipe,
CreateFolderDialogComponent,
DownloadZipDialogComponent
],
providers: [
...providers(),
@@ -266,7 +274,13 @@ export function createTranslateLoader(http: Http, logService: LogService) {
DataColumnListComponent,
FileSizePipe,
HighlightPipe,
TimeAgoPipe
TimeAgoPipe,
CreateFolderDialogComponent,
DownloadZipDialogComponent
],
entryComponents: [
CreateFolderDialogComponent,
DownloadZipDialogComponent
]
})
export class CoreModule {

View File

@@ -0,0 +1,45 @@
/*!
* @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, ViewEncapsulation } from '@angular/core';
@Component({
selector: 'adf-create-folder-dialog',
template: `
<h1 md-dialog-title>Create a new folder</h1>
<div md-dialog-content>
<md-input-container class="create-folder--name">
<input mdInput placeholder="Folder name" [(ngModel)]="value">
</md-input-container>
</div>
<div md-dialog-actions>
<button md-button md-dialog-close>Cancel</button>
<button md-button [md-dialog-close]="value">Create</button>
</div>
`,
styles: [
`
.create-folder--name {
width: 100%;
}
`
],
encapsulation: ViewEncapsulation.None
})
export class CreateFolderDialogComponent {
value: string = '';
}

View File

@@ -0,0 +1,142 @@
/*!
* @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 { MD_DIALOG_DATA, MdDialogRef } from '@angular/material';
import { DownloadEntry, MinimalNodeEntity } from 'alfresco-js-api';
import { AlfrescoApiService } from './../services/alfresco-api.service';
@Component({
selector: 'adf-download-zip-dialog',
template: `
<h1 md-dialog-title>Download as ZIP</h1>
<div md-dialog-content>
<md-progress-bar color="primary" mode="indeterminate"></md-progress-bar>
</div>
<div md-dialog-actions>
<span class="spacer"></span>
<button md-button color="primary" (click)="cancelDownload()">Cancel</button>
</div>
`,
styles: [`
.spacer { flex: 1 1 auto; }
.adf-download-zip-dialog .mat-dialog-actions .mat-button-wrapper {
text-transform: uppercase;
}
`],
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: MdDialogRef<DownloadZipDialogComponent>,
@Inject(MD_DIALOG_DATA) private data: { nodeIds?: string[] }) {
}
private get downloadsApi() {
return this.apiService.getInstance().core.downloadsApi;
}
private get nodesApi() {
return this.apiService.getInstance().core.nodesApi;
}
private get contentApi() {
return this.apiService.getInstance().content;
}
ngOnInit() {
if (this.data && this.data.nodeIds && this.data.nodeIds.length > 0) {
// change timeout to have a delay for demo purposes
setTimeout(() => {
if (!this.cancelled) {
this.downloadZip(this.data.nodeIds);
} else {
console.log('Cancelled');
}
}, 0);
}
}
cancelDownload() {
this.cancelled = true;
this.dialogRef.close(false);
}
downloadZip(nodeIds: string[]) {
if (nodeIds && nodeIds.length > 0) {
const promise: any = this.downloadsApi.createDownload({ nodeIds });
promise.on('progress', progress => console.log('Progress', progress));
promise.on('error', error => console.log('Error', error));
promise.on('abort', data => console.log('Abort', data));
promise.on('success', (data: DownloadEntry) => {
console.log('Success', data);
if (data && data.entry && data.entry.id) {
const url = this.contentApi.getContentUrl(data.entry.id, true);
// the call is needed only to get the name of the package
this.nodesApi.getNode(data.entry.id).then((downloadNode: MinimalNodeEntity) => {
console.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.downloadsApi.getDownload(downloadId).then((d: DownloadEntry) => {
if (d.entry) {
if (d.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);
}
}

View File

@@ -16,13 +16,23 @@
*/
import { NgModule } from '@angular/core';
import { MdButtonModule, MdSnackBarModule, MdToolbarModule } from '@angular/material';
import {
MdButtonModule,
MdDialogModule,
MdInputModule,
MdProgressBarModule,
MdSnackBarModule,
MdToolbarModule
} from '@angular/material';
export function modules() {
return [
MdButtonModule,
MdDialogModule,
MdInputModule,
MdProgressBarModule,
MdSnackBarModule,
MdToolbarModule,
MdButtonModule
MdToolbarModule
];
}