mirror of
https://github.com/Alfresco/alfresco-content-app.git
synced 2025-07-24 17:31:52 +00:00
[ACA-793] use download directive from ADF (#190)
This commit is contained in:
@@ -35,7 +35,6 @@ import { MaterialModule } from './material.module';
|
||||
import { NodeCopyDirective } from './directives/node-copy.directive';
|
||||
import { NodeDeleteDirective } from './directives/node-delete.directive';
|
||||
import { NodeMoveDirective } from './directives/node-move.directive';
|
||||
import { DownloadFileDirective } from './directives/node-download.directive';
|
||||
import { NodeRestoreDirective } from './directives/node-restore.directive';
|
||||
import { NodePermanentDeleteDirective } from './directives/node-permanent-delete.directive';
|
||||
import { NodeUnshareDirective } from './directives/node-unshare.directive';
|
||||
@@ -60,7 +59,6 @@ export function declarations() {
|
||||
NodeCopyDirective,
|
||||
NodeDeleteDirective,
|
||||
NodeMoveDirective,
|
||||
DownloadFileDirective,
|
||||
NodeRestoreDirective,
|
||||
NodePermanentDeleteDirective,
|
||||
NodeUnshareDirective
|
||||
|
@@ -1,147 +0,0 @@
|
||||
/*!
|
||||
* @license
|
||||
* Alfresco Example Content Application
|
||||
*
|
||||
* Copyright (C) 2005 - 2017 Alfresco Software Limited
|
||||
*
|
||||
* This file is part of the Alfresco Example Content Application.
|
||||
* If the software was purchased under a paid Alfresco license, the terms of
|
||||
* the paid license agreement will prevail. Otherwise, the software is
|
||||
* provided under the following open source license terms:
|
||||
*
|
||||
* The Alfresco Example Content Application is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* The Alfresco Example Content Application is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
import { TestBed, ComponentFixture } from '@angular/core/testing';
|
||||
import { By } from '@angular/platform-browser';
|
||||
import { AlfrescoApiService } from '@alfresco/adf-core';
|
||||
import { MatDialog } from '@angular/material';
|
||||
import { Component, DebugElement } from '@angular/core';
|
||||
|
||||
import { CommonModule } from '../common.module';
|
||||
import { DownloadFileDirective } from './node-download.directive';
|
||||
|
||||
@Component({
|
||||
template: '<div [app-download-node]="selection"></div>'
|
||||
})
|
||||
class TestComponent {
|
||||
selection;
|
||||
}
|
||||
|
||||
describe('DownloadFileDirective', () => {
|
||||
let component: TestComponent;
|
||||
let fixture: ComponentFixture<TestComponent>;
|
||||
let element: DebugElement;
|
||||
let dialog: MatDialog;
|
||||
let apiService: AlfrescoApiService;
|
||||
let contentService;
|
||||
let dialogSpy;
|
||||
|
||||
beforeEach(() => {
|
||||
TestBed.configureTestingModule({
|
||||
imports: [
|
||||
CommonModule
|
||||
],
|
||||
declarations: [
|
||||
TestComponent
|
||||
]
|
||||
});
|
||||
|
||||
fixture = TestBed.createComponent(TestComponent);
|
||||
component = fixture.componentInstance;
|
||||
element = fixture.debugElement.query(By.directive(DownloadFileDirective));
|
||||
dialog = TestBed.get(MatDialog);
|
||||
apiService = TestBed.get(AlfrescoApiService);
|
||||
contentService = apiService.getInstance().content;
|
||||
dialogSpy = spyOn(dialog, 'open');
|
||||
});
|
||||
|
||||
it('should not download node when selection is empty', () => {
|
||||
spyOn(apiService, 'getInstance');
|
||||
component.selection = [];
|
||||
|
||||
fixture.detectChanges();
|
||||
element.triggerEventHandler('click', null);
|
||||
|
||||
expect(apiService.getInstance).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should not download zip when selection has no nodes', () => {
|
||||
component.selection = [];
|
||||
|
||||
fixture.detectChanges();
|
||||
element.triggerEventHandler('click', null);
|
||||
|
||||
expect(dialogSpy).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should download selected node as file', () => {
|
||||
spyOn(contentService, 'getContentUrl');
|
||||
const node = { entry: { id: 'node-id', isFile: true } };
|
||||
component.selection = [node];
|
||||
|
||||
fixture.detectChanges();
|
||||
element.triggerEventHandler('click', null);
|
||||
|
||||
expect(contentService.getContentUrl).toHaveBeenCalledWith(node.entry.id, true);
|
||||
});
|
||||
|
||||
it('should download selected files nodes as zip', () => {
|
||||
const node1 = { entry: { id: 'node-1' } };
|
||||
const node2 = { entry: { id: 'node-2' } };
|
||||
component.selection = [node1, node2];
|
||||
|
||||
fixture.detectChanges();
|
||||
element.triggerEventHandler('click', null);
|
||||
|
||||
expect(dialogSpy.calls.argsFor(0)[1].data).toEqual({ nodeIds: [ 'node-1', 'node-2' ] });
|
||||
});
|
||||
|
||||
it('should download selected folder node as zip', () => {
|
||||
const node = { entry: { isFolder: true, id: 'node-id' } };
|
||||
component.selection = [node];
|
||||
|
||||
fixture.detectChanges();
|
||||
element.triggerEventHandler('click', null);
|
||||
|
||||
expect(dialogSpy.calls.argsFor(0)[1].data).toEqual({ nodeIds: [ 'node-id' ] });
|
||||
});
|
||||
|
||||
it('should create link element to download file node', () => {
|
||||
const dummyLinkElement = {
|
||||
download: null,
|
||||
href: null,
|
||||
click: () => null,
|
||||
style: {
|
||||
display: null
|
||||
}
|
||||
};
|
||||
|
||||
const node = { entry: { name: 'dummy', isFile: true, id: 'node-id' } };
|
||||
|
||||
spyOn(contentService, 'getContentUrl').and.returnValue('somewhere-over-the-rainbow');
|
||||
spyOn(document, 'createElement').and.returnValue(dummyLinkElement);
|
||||
spyOn(document.body, 'appendChild').and.stub();
|
||||
spyOn(document.body, 'removeChild').and.stub();
|
||||
|
||||
component.selection = [node];
|
||||
|
||||
fixture.detectChanges();
|
||||
element.triggerEventHandler('click', null);
|
||||
|
||||
expect(document.createElement).toHaveBeenCalled();
|
||||
expect(dummyLinkElement.download).toBe('dummy');
|
||||
expect(dummyLinkElement.href).toContain('somewhere-over-the-rainbow');
|
||||
});
|
||||
});
|
@@ -1,120 +0,0 @@
|
||||
/*!
|
||||
* @license
|
||||
* Alfresco Example Content Application
|
||||
*
|
||||
* Copyright (C) 2005 - 2017 Alfresco Software Limited
|
||||
*
|
||||
* This file is part of the Alfresco Example Content Application.
|
||||
* If the software was purchased under a paid Alfresco license, the terms of
|
||||
* the paid license agreement will prevail. Otherwise, the software is
|
||||
* provided under the following open source license terms:
|
||||
*
|
||||
* The Alfresco Example Content Application is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* The Alfresco Example Content Application is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
import { Directive, Input, HostListener } from '@angular/core';
|
||||
import { MatDialog } from '@angular/material';
|
||||
import { MinimalNodeEntity } from 'alfresco-js-api';
|
||||
import { AlfrescoApiService } from '@alfresco/adf-core';
|
||||
import { DownloadZipDialogComponent } from '@alfresco/adf-content-services';
|
||||
|
||||
@Directive({
|
||||
selector: '[app-download-node]'
|
||||
})
|
||||
export class DownloadFileDirective {
|
||||
|
||||
@Input('app-download-node')
|
||||
nodes: MinimalNodeEntity[];
|
||||
|
||||
@HostListener('click')
|
||||
onClick() {
|
||||
this.downloadNodes(this.nodes);
|
||||
}
|
||||
|
||||
constructor(
|
||||
private apiService: AlfrescoApiService,
|
||||
private dialog: MatDialog
|
||||
) {}
|
||||
|
||||
private downloadNodes(selection: Array<MinimalNodeEntity>) {
|
||||
if (!selection || selection.length === 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (selection.length === 1) {
|
||||
this.downloadNode(selection[0]);
|
||||
} else {
|
||||
this.downloadZip(selection);
|
||||
}
|
||||
}
|
||||
|
||||
private downloadNode(node: MinimalNodeEntity) {
|
||||
if (node && node.entry) {
|
||||
const entry = node.entry;
|
||||
|
||||
if (entry.isFile) {
|
||||
this.downloadFile(node);
|
||||
}
|
||||
|
||||
if (entry.isFolder) {
|
||||
this.downloadZip([node]);
|
||||
}
|
||||
|
||||
// Check if there's nodeId for Shared Files
|
||||
if (!entry.isFile && !entry.isFolder && (<any>entry).nodeId) {
|
||||
this.downloadFile(node);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private downloadFile(node: MinimalNodeEntity) {
|
||||
if (node && node.entry) {
|
||||
const contentApi = this.apiService.getInstance().content;
|
||||
|
||||
const url = contentApi.getContentUrl(node.entry.id, true);
|
||||
const fileName = node.entry.name;
|
||||
|
||||
this.download(url, fileName);
|
||||
}
|
||||
}
|
||||
|
||||
private downloadZip(selection: Array<MinimalNodeEntity>) {
|
||||
if (selection && selection.length > 0) {
|
||||
// nodeId for Shared node
|
||||
const nodeIds = selection.map((node: any) => (node.entry.nodeId || node.entry.id));
|
||||
|
||||
this.dialog.open(DownloadZipDialogComponent, {
|
||||
width: '600px',
|
||||
disableClose: true,
|
||||
data: {
|
||||
nodeIds
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
private 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);
|
||||
}
|
||||
}
|
||||
}
|
@@ -17,7 +17,7 @@
|
||||
mat-icon-button
|
||||
*ngIf="hasSelection(documentList.selection)"
|
||||
title="{{ 'APP.ACTIONS.DOWNLOAD' | translate }}"
|
||||
[app-download-node]="documentList.selection">
|
||||
[adfNodeDownload]="documentList.selection">
|
||||
<mat-icon>get_app</mat-icon>
|
||||
</button>
|
||||
|
||||
|
@@ -19,7 +19,7 @@
|
||||
mat-icon-button
|
||||
*ngIf="hasSelection(documentList.selection)"
|
||||
title="{{ 'APP.ACTIONS.DOWNLOAD' | translate }}"
|
||||
[app-download-node]="documentList.selection">
|
||||
[adfNodeDownload]="documentList.selection">
|
||||
<mat-icon>get_app</mat-icon>
|
||||
</button>
|
||||
|
||||
|
@@ -17,7 +17,7 @@
|
||||
mat-icon-button
|
||||
*ngIf="hasSelection(documentList.selection)"
|
||||
title="{{ 'APP.ACTIONS.DOWNLOAD' | translate }}"
|
||||
[app-download-node]="documentList.selection">
|
||||
[adfNodeDownload]="documentList.selection">
|
||||
<mat-icon>get_app</mat-icon>
|
||||
</button>
|
||||
|
||||
|
@@ -17,7 +17,7 @@
|
||||
mat-icon-button
|
||||
*ngIf="hasSelection(documentList.selection)"
|
||||
title="{{ 'APP.ACTIONS.DOWNLOAD' | translate }}"
|
||||
[app-download-node]="documentList.selection">
|
||||
[adfNodeDownload]="documentList.selection">
|
||||
<mat-icon>get_app</mat-icon>
|
||||
</button>
|
||||
|
||||
|
Reference in New Issue
Block a user