add autorename as default behaviour and the possibility to use the versioning

This commit is contained in:
Eugenio Romano 2016-08-24 16:21:31 +01:00
parent e00896da4e
commit 23da361622
7 changed files with 118 additions and 47 deletions

View File

@ -3,6 +3,7 @@
[showUploadDialog]="true" [showUploadDialog]="true"
[currentFolderPath]="currentPath" [currentFolderPath]="currentPath"
[uploaddirectory]="" [uploaddirectory]=""
[versioning] = "versioning"
(onSuccess)="documentList.reload()"> (onSuccess)="documentList.reload()">
<alfresco-document-list-breadcrumb <alfresco-document-list-breadcrumb
[currentFolderPath]="currentPath" [currentFolderPath]="currentPath"
@ -147,6 +148,13 @@
</label> </label>
</p> </p>
<p style="width:250px;margin: 20px;">
<label for="switch-versioning" class="mdl-switch mdl-js-switch mdl-js-ripple-effect">
<input type="checkbox" id="switch-versioning" class="mdl-switch__input" (change)="toggleVersioning()">
<span class="mdl-switch__label">Versioning</span>
</label>
</p>
<h5>Upload</h5> <h5>Upload</h5>
<br> <br>
<div *ngIf="acceptedFilesTypeShow"> <div *ngIf="acceptedFilesTypeShow">
@ -160,6 +168,7 @@
[currentFolderPath]="currentPath" [currentFolderPath]="currentPath"
[multipleFiles]="multipleFileUpload" [multipleFiles]="multipleFileUpload"
[uploadFolders]="folderUpload" [uploadFolders]="folderUpload"
[versioning] = "versioning"
(onSuccess)="documentList.reload()"> (onSuccess)="documentList.reload()">
<div class="mdl-spinner mdl-js-spinner is-active"></div> <div class="mdl-spinner mdl-js-spinner is-active"></div>
</alfresco-upload-button> </alfresco-upload-button>
@ -171,6 +180,7 @@
acceptedFilesType="{{acceptedFilesType}}" acceptedFilesType="{{acceptedFilesType}}"
[multipleFiles]="multipleFileUpload" [multipleFiles]="multipleFileUpload"
[uploadFolders]="folderUpload" [uploadFolders]="folderUpload"
[versioning] = "versioning"
(onSuccess)="documentList.reload()"> (onSuccess)="documentList.reload()">
<div class="mdl-spinner mdl-js-spinner is-active"></div> <div class="mdl-spinner mdl-js-spinner is-active"></div>
</alfresco-upload-button> </alfresco-upload-button>

View File

@ -63,6 +63,7 @@ export class FilesComponent implements OnInit {
multipleFileUpload: boolean = false; multipleFileUpload: boolean = false;
folderUpload: boolean = false; folderUpload: boolean = false;
acceptedFilesTypeShow: boolean = false; acceptedFilesTypeShow: boolean = false;
versioning: boolean = false;
acceptedFilesType: string = '.jpg,.pdf,.js'; acceptedFilesType: string = '.jpg,.pdf,.js';
@ -119,6 +120,11 @@ export class FilesComponent implements OnInit {
return this.acceptedFilesTypeShow; return this.acceptedFilesTypeShow;
} }
toggleVersioning(){
this.versioning = !this.versioning;
return this.versioning;
}
ngOnInit() { ngOnInit() {
this.formService.getProcessDefinitions().subscribe( this.formService.getProcessDefinitions().subscribe(
defs => this.setupBpmActions(defs || []), defs => this.setupBpmActions(defs || []),

View File

@ -137,14 +137,21 @@ bootstrap(MyDemoApp, [
``` ```
#### Events #### Events
**onSuccess**: The event is emitted when the file is uploaded<br /> Attribute | Description
--- | ---
`onSuccess` | The event is emitted when the file is uploaded
#### Options #### Options
**showUdoNotificationBar**: {boolean} (optional) default true. Hide/show notification bar.<br /> Attribute | Options | Default | Description | Mandatory
**uploadFolders**: {boolean} (optional) default false. Allow/disallow upload folders (only for chrome).<br /> --- | --- | --- | --- | ---
**multipleFiles**: {boolean} (optional) default false. Allow/disallow multiple files.<br /> `showUdoNotificationBar` | *boolean* | true | Hide/show notification bar |
**acceptedFilesType**: {string} (optional) default "*". array of allowed file extensions , example: ".jpg,.gif,.png,.svg" .<br /> `uploadFolders` | *boolean* | false | Allow/disallow upload folders (only for chrome) |
`multipleFiles` | *boolean* | false | Allow/disallow multiple files |
`acceptedFilesType` | *string* | * | array of allowed file extensions , example: ".jpg,.gif,.png,.svg" |
`versioning` | *boolean* | false | Versioning false is the default uploader behaviour and it rename using an integer suffix if there is a name clash. Versioning true to indicate that a major version should be created |
### Drag and drop ### Drag and drop
This component, provide a drag and drop are to upload files to alfresco. This component, provide a drag and drop are to upload files to alfresco.
@ -195,8 +202,11 @@ bootstrap(MyDemoApp, [
]); ]);
``` ```
#### Events #### Events
**onSuccess**: The event is emitted when the file is uploaded<br /> Attribute | Description
--- | ---
`onSuccess` | The event is emitted when the file is uploaded
### Files Dialog ### Files Dialog
This component provides a dialog that shows all the files uploaded This component provides a dialog that shows all the files uploaded

View File

@ -38,10 +38,11 @@ const ERROR_FOLDER_ALREADY_EXIST = 409;
* This component, provide a set of buttons to upload files to alfresco. * This component, provide a set of buttons to upload files to alfresco.
* *
* @InputParam {boolean} [true] showUdoNotificationBar - hide/show notification bar. * @InputParam {boolean} [true] showUdoNotificationBar - hide/show notification bar.
* @InputParam {boolean} [false] versioning - true to indicate that a major version should be created
* @InputParam {boolean} [false] uploadFolders - allow/disallow upload folders (only for chrome). * @InputParam {boolean} [false] uploadFolders - allow/disallow upload folders (only for chrome).
* @InputParam {boolean} [false] multipleFiles - allow/disallow multiple files. * @InputParam {boolean} [false] multipleFiles - allow/disallow multiple files.
* @InputParam {string} [*] acceptedFilesType - array of allowed file extensions. * @InputParam {string} [*] acceptedFilesType - array of allowed file extensions.
* * @InputParam {boolean} [false] versioning - true to indicate that a major version should be created
* @Output - onSuccess - The event is emitted when the file is uploaded * @Output - onSuccess - The event is emitted when the file is uploaded
* *
* @returns {UploadDragAreaComponent} . * @returns {UploadDragAreaComponent} .
@ -67,6 +68,9 @@ export class UploadButtonComponent {
@Input() @Input()
multipleFiles: boolean = false; multipleFiles: boolean = false;
@Input()
versioning: boolean = false;
@Input() @Input()
acceptedFilesType: string = '*'; acceptedFilesType: string = '*';
@ -85,16 +89,16 @@ export class UploadButtonComponent {
translate: AlfrescoTranslationService; translate: AlfrescoTranslationService;
constructor(public el: ElementRef, constructor(public el: ElementRef, private _uploaderService: UploadService, translate: AlfrescoTranslationService) {
private _uploaderService: UploadService,
translate: AlfrescoTranslationService) {
let formFields = this.createFormFields();
this._uploaderService.setOptions(formFields);
this.translate = translate; this.translate = translate;
this.translate.addTranslationFolder('node_modules/ng2-alfresco-upload/dist/src'); this.translate.addTranslationFolder('node_modules/ng2-alfresco-upload/dist/src');
} }
ngOnChanges(changes) {
let formFields = this.createFormFields();
this._uploaderService.setOptions(formFields, this.versioning);
}
/** /**
* Method called when files are dropped in the drag area. * Method called when files are dropped in the drag area.
* *

View File

@ -51,6 +51,9 @@ export class UploadDragAreaComponent {
@Input() @Input()
showUdoNotificationBar: boolean = true; showUdoNotificationBar: boolean = true;
@Input()
versioning: boolean = false;
@Input() @Input()
currentFolderPath: string = '/Sites/swsdp/documentLibrary'; currentFolderPath: string = '/Sites/swsdp/documentLibrary';
@ -59,16 +62,16 @@ export class UploadDragAreaComponent {
translate: AlfrescoTranslationService; translate: AlfrescoTranslationService;
constructor(private _uploaderService: UploadService, constructor(private _uploaderService: UploadService, translate: AlfrescoTranslationService) {
translate: AlfrescoTranslationService) {
let formFields = this.createFormFields();
this._uploaderService.setOptions(formFields);
this.translate = translate; this.translate = translate;
this.translate.addTranslationFolder('node_modules/ng2-alfresco-upload/dist/src'); this.translate.addTranslationFolder('node_modules/ng2-alfresco-upload/dist/src');
} }
ngOnChanges(changes) {
let formFields = this.createFormFields();
this._uploaderService.setOptions(formFields, this.versioning);
}
/** /**
* Method called when files are dropped in the drag area. * Method called when files are dropped in the drag area.
* *
@ -227,7 +230,7 @@ export class UploadDragAreaComponent {
* @returns {string} * @returns {string}
*/ */
private getErrorMessage(response: any): string { private getErrorMessage(response: any): string {
if (response.body.error.statusCode === ERROR_FOLDER_ALREADY_EXIST ) { if (response.body.error.statusCode === ERROR_FOLDER_ALREADY_EXIST) {
let errorMessage: any; let errorMessage: any;
errorMessage = this.translate.get('FILE_UPLOAD.MESSAGES.FOLDER_ALREADY_EXIST'); errorMessage = this.translate.get('FILE_UPLOAD.MESSAGES.FOLDER_ALREADY_EXIST');
return errorMessage.value; return errorMessage.value;

View File

@ -44,7 +44,7 @@ describe('AlfrescoUploadService', () => {
]; ];
}); });
beforeEach( inject([UploadService], (uploadService: UploadService) => { beforeEach(inject([UploadService], (uploadService: UploadService) => {
jasmine.Ajax.install(); jasmine.Ajax.install();
service = uploadService; service = uploadService;
})); }));
@ -85,7 +85,7 @@ describe('AlfrescoUploadService', () => {
service.uploadFilesInTheQueue('fake-dir', emitter); service.uploadFilesInTheQueue('fake-dir', emitter);
let request = jasmine.Ajax.requests.mostRecent(); let request = jasmine.Ajax.requests.mostRecent();
expect(request.url).toBe('http://localhost:8080/alfresco/api/-default-/public/alfresco/versions/1/nodes/-root-/children'); expect(request.url).toBe('http://localhost:8080/alfresco/api/-default-/public/alfresco/versions/1/nodes/-root-/children?autoRename=true');
expect(request.method).toBe('POST'); expect(request.method).toBe('POST');
jasmine.Ajax.requests.mostRecent().respondWith({ jasmine.Ajax.requests.mostRecent().respondWith({
@ -107,7 +107,8 @@ describe('AlfrescoUploadService', () => {
service.addToQueue(filesFake); service.addToQueue(filesFake);
service.uploadFilesInTheQueue('', emitter); service.uploadFilesInTheQueue('', emitter);
expect(jasmine.Ajax.requests.mostRecent().url) expect(jasmine.Ajax.requests.mostRecent().url)
.toBe('http://localhost:8080/alfresco/api/-default-/public/alfresco/versions/1/nodes/-root-/children'); .toBe('http://localhost:8080/alfresco/api/-default-/public/alfresco/versions/1/nodes/-root-/children?autoRename=true');
jasmine.Ajax.requests.mostRecent().respondWith({ jasmine.Ajax.requests.mostRecent().respondWith({
'status': 404, 'status': 404,
contentType: 'text/plain', contentType: 'text/plain',
@ -156,7 +157,7 @@ describe('AlfrescoUploadService', () => {
}; };
let filesFake = [{name: 'fake-name', size: 10}]; let filesFake = [{name: 'fake-name', size: 10}];
service.addToQueue(filesFake); service.addToQueue(filesFake);
service.filesUpload$.subscribe( (file) => { service.filesUpload$.subscribe((file) => {
expect(file).toBeDefined(); expect(file).toBeDefined();
expect(file[0]).toBeDefined(); expect(file[0]).toBeDefined();
expect(file[0].progress).toEqual(fakeProgress); expect(file[0].progress).toEqual(fakeProgress);
@ -217,4 +218,28 @@ describe('AlfrescoUploadService', () => {
); );
}); });
it('Default upload behaviour should be autorename', () => {
let emitter = new EventEmitter();
service.setOptions(options);
let filesFake = [{name: 'fake-name', size: 10}];
service.addToQueue(filesFake);
service.uploadFilesInTheQueue('', emitter);
expect(jasmine.Ajax.requests.mostRecent().url.endsWith('autoRename=true')).toBe(true);
});
it('If versioning is true autoRename should not be present and majorVersion should be a param', () => {
let emitter = new EventEmitter();
let enableVersioning = true;
service.setOptions(options, enableVersioning);
let filesFake = [{name: 'fake-name', size: 10}];
service.addToQueue(filesFake);
service.uploadFilesInTheQueue('', emitter);
console.log(jasmine.Ajax.requests.mostRecent().url);
expect(jasmine.Ajax.requests.mostRecent().url.endsWith('autoRename=true')).toBe(false);
expect(jasmine.Ajax.requests.mostRecent().params.has('majorVersion')).toBe(true);
});
}); });

View File

@ -19,7 +19,7 @@ import { EventEmitter, Injectable } from '@angular/core';
import { Response } from '@angular/http'; import { Response } from '@angular/http';
import { Observable } from 'rxjs/Observable'; import { Observable } from 'rxjs/Observable';
import { Observer } from 'rxjs/Observer'; import { Observer } from 'rxjs/Observer';
import { AlfrescoAuthenticationService} from 'ng2-alfresco-core'; import { AlfrescoAuthenticationService } from 'ng2-alfresco-core';
import { FileModel } from '../models/file.model'; import { FileModel } from '../models/file.model';
/** /**
@ -34,6 +34,8 @@ export class UploadService {
private formFields: Object = {}; private formFields: Object = {};
private queue: FileModel[] = []; private queue: FileModel[] = [];
private versioning: boolean = false;
private filesUploadObserverProgressBar: Observer<FileModel[]>; private filesUploadObserverProgressBar: Observer<FileModel[]>;
private totalCompletedObserver: Observer<number>; private totalCompletedObserver: Observer<number>;
@ -50,11 +52,13 @@ export class UploadService {
/** /**
* Configure the service * Configure the service
* *
* @param {Object} - options to init the object * @param {Object} - options formFields to init the object
* @param {boolean} - versioning true to indicate that a major version should be created
* *
*/ */
public setOptions(options: any): void { public setOptions(options: any, versioning: boolean): void {
this.formFields = options.formFields != null ? options.formFields : this.formFields; this.formFields = options.formFields != null ? options.formFields : this.formFields;
this.versioning = versioning != null ? versioning : this.versioning;
} }
/** /**
@ -88,11 +92,20 @@ export class UploadService {
return !uploadingFileModel.uploading && !uploadingFileModel.done && !uploadingFileModel.abort && !uploadingFileModel.error; return !uploadingFileModel.uploading && !uploadingFileModel.done && !uploadingFileModel.abort && !uploadingFileModel.error;
}); });
let opts: any = {};
opts.renditions = 'doclib';
if (this.versioning) {
opts.overwrite = true;
opts.majorVersion = true;
} else {
opts.autoRename = true;
}
filesToUpload.forEach((uploadingFileModel: FileModel) => { filesToUpload.forEach((uploadingFileModel: FileModel) => {
uploadingFileModel.setUploading(); uploadingFileModel.setUploading();
let promiseUpload = this.authService.getAlfrescoApi(). let promiseUpload = this.authService.getAlfrescoApi().upload.uploadFile(uploadingFileModel.file, directory, null, null, opts)
upload.uploadFile(uploadingFileModel.file, directory, null, null, {renditions: 'doclib'})
.on('progress', (progress: any) => { .on('progress', (progress: any) => {
uploadingFileModel.setProgres(progress); uploadingFileModel.setProgres(progress);
this.updateFileListStream(this.queue); this.updateFileListStream(this.queue);