[ADF-2054] Created a new widget to handle uploading file from Share (#2810)

* [ADF-2054] start creating custom upload widget for share integration

* [ADF-2054] changed content node selector service to allow different opening approach

* [ADF-2054] addedd support for multi resource files

* [ADF-2054] fixed base case for upload when only local files are selected

* [ADF-2054] start adding and fixing tests for new share attach button widget

* [ADF-2054] changed test to perfrom a correct check

* [ADF-2054] removed fdescribe

* [ADF-2054] added test for share-widget component

* [ADF-2054] added peer reviews changes

* [ADF-2054] created a module folder for content widgets

* [ADF-2054] fixed wrong import

* [ADF-2054] fixed rebase errors

* [ADF-2054] restored some files changed by rebase

* [ADF-2054] added link to content services to fix packaging issue

* [ADF-2054] renamed widget
This commit is contained in:
Vito
2018-01-12 14:28:18 +01:00
committed by Eugenio Romano
parent 69e40ea1c0
commit 46ad98cd8b
37 changed files with 886 additions and 597 deletions

View File

@@ -1,15 +0,0 @@
.attach-widget {
width:100%
}
.attach-widget__icon {
float: left;
}
.attach-widget__file {
margin-top: 4px;
}
.attach-widget__reset {
margin-top: 4px;
}

View File

@@ -1,32 +0,0 @@
<div class="attach-widget {{field.className}}">
<label [attr.for]="field.id">{{field.name}}<span *ngIf="isRequired()">*</span></label>
<div>
<span *ngIf="hasFile()" class="attach-widget__file mdl-chip"><span class="mdl-chip__text">{{getLinkedFileName()}}</span></span>
<button #browseFile [disabled]="field.readOnly" (click)="showDialog();" class="mdl-button mdl-jsm-button mdl-js-ripple-effect attach-widget__browser">
<mat-icon>image</mat-icon>
Browse {{selectedFolderSiteName}}
</button>
<button *ngIf="hasFile" [disabled]="field.readOnly" (click)="reset(file);" class="mdl-button mdl-js-button mdl-js-ripple-effect attach-widget__reset">Clear</button>
</div>
</div>
<dialog class="mdl-dialog" #dialog>
<h4 class="mdl-dialog__title">Select content</h4>
<div class="mdl-dialog__content">
<ul class='mdl-list'>
<li class="mdl-list__item" *ngFor="let node of selectedFolderNodes">
<span class="mdl-list__item-primary-content" *ngIf="node.folder">
<mat-icon class="mdl-list__item-icon">folder</mat-icon>
<a (click)="selectFolder(node, $event)">{{node.title}}</a>
</span>
<span class="mdl-list__item-primary-content" *ngIf="!node.folder">
<mat-icon class="mdl-list__item-icon">description</mat-icon>
<a (click)="selectFile(node, $event)">{{node.title}}</a>
</span>
</li>
</ul>
</div>
<div class="mdl-dialog__actions">
<button type="button" (click)="cancel()" class="mdl-button close">Cancel</button>
</div>
</dialog>

View File

@@ -1,302 +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 { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { Observable } from 'rxjs/Observable';
import { ActivitiContentService } from '../../../services/activiti-alfresco.service';
import { MaterialModule } from '../../../../material.module';
import { ExternalContent } from '../core/external-content';
import { ExternalContentLink } from '../core/external-content-link';
import { FormFieldTypes } from '../core/form-field-types';
import { ErrorWidgetComponent } from '../error/error.component';
import { EcmModelService } from './../../../services/ecm-model.service';
import { FormService } from './../../../services/form.service';
import { FormFieldModel } from './../core/form-field.model';
import { FormModel } from './../core/form.model';
import { AttachWidgetComponent } from './attach.widget';
describe('AttachWidgetComponent', () => {
let widget: AttachWidgetComponent;
let fixture: ComponentFixture<AttachWidgetComponent>;
let contentService: ActivitiContentService;
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [
MaterialModule
],
declarations: [
AttachWidgetComponent,
ErrorWidgetComponent
],
providers: [
FormService,
EcmModelService,
ActivitiContentService
]
}).compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(AttachWidgetComponent);
contentService = TestBed.get(ActivitiContentService);
widget = fixture.componentInstance;
});
it('should require field value to check file', () => {
widget.field = null;
widget.ngOnInit();
expect(widget.hasFile()).toBeFalsy();
widget.field = new FormFieldModel(null, {
type: FormFieldTypes.UPLOAD,
value: null
});
widget.ngOnInit();
expect(widget.hasFile()).toBeFalsy();
widget.field = new FormFieldModel(null, {
type: FormFieldTypes.UPLOAD,
value: [{ name: 'file' }]
});
widget.ngOnInit();
expect(widget.hasFile()).toBeTruthy();
});
it('should setup with form field', () => {
let nodes: any = [{}];
spyOn(contentService, 'getAlfrescoNodes').and.returnValue(
Observable.create(observer => {
observer.next(nodes);
observer.complete();
})
);
let config = {
siteId: '<id>',
site: '<site>',
pathId: '<pathId>',
accountId: '<accountId>'
};
widget.field = new FormFieldModel(null, {
type: FormFieldTypes.UPLOAD,
params: {
fileSource: {
selectedFolder: config
}
}
});
widget.ngOnInit();
expect(widget.selectedFolderSiteId).toBe(config.siteId);
expect(widget.selectedFolderSiteName).toBe(config.site);
expect(widget.selectedFolderPathId).toBe(config.pathId);
expect(widget.selectedFolderAccountId).toBe(config.accountId);
expect(widget.selectedFolderNodes).toEqual(nodes);
});
xit('should link file on select', () => {
let link = <ExternalContentLink> {};
spyOn(contentService, 'linkAlfrescoNode').and.returnValue(
Observable.create(observer => {
observer.next(link);
observer.complete();
})
);
widget.field = new FormFieldModel(null, {
type: FormFieldTypes.UPLOAD
});
widget.ngOnInit();
let node = <ExternalContent> {};
widget.selectFile(node, null);
expect(contentService.linkAlfrescoNode).toHaveBeenCalled();
expect(widget.selectedFile).toBe(node);
expect(widget.field.value).toEqual([link]);
expect(widget.field.json.value).toEqual([link]);
expect(widget.hasFile()).toBeTruthy();
});
it('should reset', () => {
widget.field = new FormFieldModel(new FormModel(), {
type: FormFieldTypes.UPLOAD,
value: [{ name: 'filename' }]
});
widget.reset();
expect(widget.hasFile()).toBeFalsy();
expect(widget.field.value).toBeNull();
expect(widget.field.json.value).toBeNull();
expect(widget.hasFile()).toBeFalsy();
});
it('should close dialog on cancel', () => {
let closed = false;
widget.dialog = {
nativeElement: {
close: function () {
closed = true;
}
}
};
widget.cancel();
expect(closed).toBeTruthy();
});
xit('should show modal dialog', () => {
spyOn(contentService, 'getAlfrescoNodes').and.returnValue(
Observable.create(observer => {
observer.next([]);
observer.complete();
})
);
widget.field = new FormFieldModel(null, {
type: FormFieldTypes.UPLOAD,
params: {
fileSource: {
selectedFolder: {}
}
}
});
let modalShown = false;
widget.dialog = {
nativeElement: {
showModal: function () {
modalShown = true;
}
}
};
widget.showDialog();
expect(modalShown).toBeTruthy();
});
it('should select folder and load nodes', () => {
let nodes: any = [{}];
spyOn(contentService, 'getAlfrescoNodes').and.returnValue(
Observable.create(observer => {
observer.next(nodes);
observer.complete();
})
);
let node = <ExternalContent> { id: '<id>' };
widget.selectFolder(node, null);
expect(widget.selectedFolderPathId).toBe(node.id);
expect(widget.selectedFolderNodes).toEqual(nodes);
});
it('should get linked file name via local variable', () => {
widget.fileName = '<fileName>';
widget.selectedFile = null;
widget.field = null;
expect(widget.getLinkedFileName()).toBe(widget.fileName);
});
it('should get linked file name via selected file', () => {
widget.fileName = null;
widget.selectedFile = <ExternalContent> { title: '<title>' };
widget.field = null;
expect(widget.getLinkedFileName()).toBe(widget.selectedFile.title);
});
it('should get linked file name via form field', () => {
widget.fileName = null;
widget.selectedFile = null;
let name = '<file>';
widget.field = new FormFieldModel(null, {
type: FormFieldTypes.UPLOAD,
value: [{ name: name }]
});
expect(widget.getLinkedFileName()).toBe(name);
});
it('should require form field to setup file browser', () => {
widget.field = null;
widget.setupFileBrowser();
expect(widget.selectedFolderPathId).toBeUndefined();
expect(widget.selectedFolderAccountId).toBeUndefined();
const pathId = '<pathId>';
const accountId = '<accountId>';
widget.field = new FormFieldModel(null, {
type: FormFieldTypes.UPLOAD,
params: {
fileSource: {
selectedFolder: {
pathId: pathId,
accountId: accountId
}
}
}
});
widget.setupFileBrowser();
expect(widget.selectedFolderPathId).toBe(pathId);
expect(widget.selectedFolderAccountId).toBe(accountId);
});
it('should get external content nodes', () => {
let nodes: any = [{}];
spyOn(contentService, 'getAlfrescoNodes').and.returnValue(
Observable.create(observer => {
observer.next(nodes);
observer.complete();
})
);
const accountId = '<accountId>';
const pathId = '<pathId>';
widget.selectedFolderAccountId = accountId;
widget.selectedFolderPathId = pathId;
widget.getExternalContentNodes();
expect(contentService.getAlfrescoNodes).toHaveBeenCalledWith(accountId, pathId);
expect(widget.selectedFolderNodes).toEqual(nodes);
});
it('should handle error', (done) => {
let error = 'error';
spyOn(contentService, 'getAlfrescoNodes').and.returnValue(
Observable.throw(error)
);
widget.error.subscribe(() => {
done();
});
widget.getExternalContentNodes();
});
it('should require configured dialog to show modal', () => {
widget.dialog = null;
spyOn(widget, 'setupFileBrowser').and.stub();
spyOn(widget, 'getExternalContentNodes').and.stub();
expect(widget.showDialog()).toBeFalsy();
});
});

View File

@@ -1,156 +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.
*/
/* tslint:disable:component-selector */
import { Component, EventEmitter, OnInit, Output, ViewChild, ViewEncapsulation } from '@angular/core';
import { ActivitiContentService } from '../../../services/activiti-alfresco.service';
import { ExternalContent } from '../core/external-content';
import { ExternalContentLink } from '../core/external-content-link';
import { FormFieldModel } from '../core/form-field.model';
import { FormService } from './../../../services/form.service';
import { baseHost , WidgetComponent } from './../widget.component';
@Component({
selector: 'attach-widget',
templateUrl: './attach.widget.html',
styleUrls: ['./attach.widget.css'],
host: baseHost,
encapsulation: ViewEncapsulation.None
})
export class AttachWidgetComponent extends WidgetComponent implements OnInit {
selectedFolderPathId: string;
selectedFolderSiteId: string;
selectedFolderSiteName: string;
selectedFolderAccountId: string;
fileName: string;
selectedFolderNodes: [ExternalContent];
selectedFile: ExternalContent;
@Output()
fieldChanged: EventEmitter<FormFieldModel> = new EventEmitter<FormFieldModel>();
@Output()
error: EventEmitter<any> = new EventEmitter<any>();
@ViewChild('dialog')
dialog: any;
constructor(public formService: FormService,
private contentService: ActivitiContentService) {
super(formService);
}
ngOnInit() {
if (this.field) {
let params = this.field.params;
if (params &&
params.fileSource &&
params.fileSource.selectedFolder) {
this.selectedFolderSiteId = params.fileSource.selectedFolder.siteId;
this.selectedFolderSiteName = params.fileSource.selectedFolder.site;
this.setupFileBrowser();
this.getExternalContentNodes();
}
}
}
setupFileBrowser() {
if (this.field) {
let params = this.field.params;
this.selectedFolderPathId = params.fileSource.selectedFolder.pathId;
this.selectedFolderAccountId = params.fileSource.selectedFolder.accountId;
}
}
getLinkedFileName(): string {
let result = this.fileName;
if (this.selectedFile &&
this.selectedFile.title) {
result = this.selectedFile.title;
}
if (this.field &&
this.field.value &&
this.field.value.length > 0 &&
this.field.value[0].name) {
result = this.field.value[0].name;
}
return result;
}
getExternalContentNodes() {
this.contentService.getAlfrescoNodes(this.selectedFolderAccountId, this.selectedFolderPathId)
.subscribe(
nodes => this.selectedFolderNodes = nodes,
(err) => {
this.error.emit(err);
}
);
}
selectFile(node: ExternalContent, $event: any) {
this.contentService.linkAlfrescoNode(this.selectedFolderAccountId, node, this.selectedFolderSiteId).subscribe(
(link: ExternalContentLink) => {
this.selectedFile = node;
this.field.value = [link];
this.field.json.value = [link];
this.closeDialog();
this.fieldChanged.emit(this.field);
}
);
}
selectFolder(node: ExternalContent, $event: any) {
this.selectedFolderPathId = node.id;
this.getExternalContentNodes();
}
showDialog(): boolean {
this.setupFileBrowser();
this.getExternalContentNodes();
if (this.dialog) {
// todo: show dialog
return true;
}
return false;
}
private closeDialog() {
if (this.dialog) {
this.dialog.nativeElement.close();
}
}
cancel() {
this.closeDialog();
}
reset() {
this.field.value = null;
this.field.json.value = null;
}
hasFile(): boolean {
return this.field && this.field.value;
}
}

View File

@@ -20,7 +20,6 @@ import { TabsWidgetComponent } from './tabs/tabs.widget';
import { UnknownWidgetComponent } from './unknown/unknown.widget';
import { AmountWidgetComponent } from './amount/amount.widget';
import { AttachWidgetComponent } from './attach/attach.widget';
import { CheckboxWidgetComponent } from './checkbox/checkbox.widget';
import { DateWidgetComponent } from './date/date.widget';
import { DisplayTextWidgetComponentComponent } from './display-text/display-text.widget';
@@ -63,7 +62,6 @@ export * from './hyperlink/hyperlink.widget';
export * from './radio-buttons/radio-buttons.widget';
export * from './display-text/display-text.widget';
export * from './upload/upload.widget';
export * from './attach/attach.widget';
export * from './typeahead/typeahead.widget';
export * from './functional-group/functional-group.widget';
export * from './people/people.widget';
@@ -95,7 +93,6 @@ export const WIDGET_DIRECTIVES: any[] = [
RadioButtonsWidgetComponent,
DisplayTextWidgetComponentComponent,
UploadWidgetComponent,
AttachWidgetComponent,
TypeaheadWidgetComponent,
FunctionalGroupWidgetComponent,
PeopleWidgetComponent,

View File

@@ -98,7 +98,7 @@ export class UploadWidgetComponent extends WidgetComponent implements OnInit {
});
}
private getMultipleFileParam() {
getMultipleFileParam() {
if (this.field &&
this.field.params &&
this.field.params.multiple) {

View File

@@ -27,5 +27,6 @@ export * from './services/ecm-model.service';
export * from './services/node.service';
export * from './services/form-rendering.service';
export * from './services/process-content.service';
export * from './services/activiti-alfresco.service';
export * from './events/index';

View File

@@ -18,7 +18,7 @@
import { AlfrescoApiService } from '../../services/alfresco-api.service';
import { LogService } from '../../services/log.service';
import { Injectable } from '@angular/core';
import { AlfrescoApi } from 'alfresco-js-api';
import { AlfrescoApi, MinimalNodeEntryEntity } from 'alfresco-js-api';
import { Observable } from 'rxjs/Observable';
import { ExternalContent } from '../components/widgets/core/external-content';
import { ExternalContentLink } from '../components/widgets/core/external-content-link';
@@ -49,6 +49,24 @@ export class ActivitiContentService {
.catch(err => this.handleError(err));
}
/**
* Returns a list of all the repositories configured
*
* @param accountId
* @param folderId
* @returns {any}
*/
getAlfrescoRepositories(tenantId: number, includeAccount: boolean): Observable<any> {
let apiService: AlfrescoApi = this.apiService.getInstance();
const opts = {
tenantId: tenantId,
includeAccounts: includeAccount
};
return Observable.fromPromise(apiService.activiti.alfrescoApi.getRepositories(opts))
.map(this.toJsonArray)
.catch(err => this.handleError(err));
}
/**
* Returns a list of child nodes below the specified folder
*
@@ -68,6 +86,20 @@ export class ActivitiContentService {
})).map(this.toJson).catch(err => this.handleError(err));
}
applyAlfrescoNode(node: MinimalNodeEntryEntity, siteId: string, accountId: string) {
let apiService: AlfrescoApi = this.apiService.getInstance();
let params: any = {
source: accountId,
sourceId: node.id + ';1.0@' + siteId,
name: node.name,
link: false
};
return Observable.fromPromise(
apiService.activiti.contentApi.createTemporaryRelatedContent(params))
.map(this.toJson)
.catch(err => this.handleError(err));
}
toJson(res: any) {
if (res) {
return res || {};

View File

@@ -17,7 +17,6 @@
import { DynamicComponentResolver } from '../../index';
import {
AttachWidgetComponent,
FormFieldModel,
FormFieldTypes,
UnknownWidgetComponent,
@@ -33,17 +32,6 @@ describe('FormRenderingService', () => {
service = new FormRenderingService();
});
it('should resolve Upload field as Attach widget', () => {
let field = new FormFieldModel(null, {
type: FormFieldTypes.UPLOAD,
params: {
link: 'link'
}
});
let type = service.resolveComponentType(field);
expect(type).toBe(AttachWidgetComponent);
});
it('should resolve Upload field as Upload widget', () => {
let field = new FormFieldModel(null, {
type: FormFieldTypes.UPLOAD,
@@ -55,10 +43,10 @@ describe('FormRenderingService', () => {
expect(type).toBe(UploadWidgetComponent);
});
it('should resolve Unknown widget for Upload field', () => {
it('should resolve Upload widget for Upload field', () => {
let resolver = service.getComponentTypeResolver(FormFieldTypes.UPLOAD);
let type = resolver(null);
expect(type).toBe(UnknownWidgetComponent);
expect(type).toBe(UploadWidgetComponent);
});
it('should resolve Uknown widget for unknown field type', () => {
@@ -67,12 +55,6 @@ describe('FormRenderingService', () => {
expect(type).toBe(UnknownWidgetComponent);
});
it('shoulld resolve custom value for unknown field type', () => {
let resolver = service.getComponentTypeResolver('missing-type', AttachWidgetComponent);
let type = resolver(null);
expect(type).toBe(AttachWidgetComponent);
});
it('should fallback to default resolver when field type missing', () => {
let resolver = service.getComponentTypeResolver(null);
let type = resolver(null);
@@ -123,7 +105,7 @@ describe('FormRenderingService', () => {
});
it('should return custom value when resolving with no field', () => {
expect(service.resolveComponentType(null, AttachWidgetComponent)).toBe(AttachWidgetComponent);
expect(service.resolveComponentType(null, UploadWidgetComponent)).toBe(UploadWidgetComponent);
});
});

View File

@@ -20,7 +20,6 @@ import { Injectable, Type } from '@angular/core';
import {
AmountWidgetComponent,
AttachWidgetComponent,
CheckboxWidgetComponent,
ContainerWidgetComponent,
DateWidgetComponent,
@@ -28,7 +27,6 @@ import {
DocumentWidgetComponent,
DropdownWidgetComponent,
DynamicTableWidgetComponent,
FormFieldModel,
FunctionalGroupWidgetComponent,
HyperlinkWidgetComponent,
MultilineTextWidgetComponentComponent,
@@ -63,21 +61,11 @@ export class FormRenderingService extends DynamicComponentMapper {
'dynamic-table': DynamicComponentResolver.fromType(DynamicTableWidgetComponent),
'container': DynamicComponentResolver.fromType(ContainerWidgetComponent),
'group': DynamicComponentResolver.fromType(ContainerWidgetComponent),
'document': DynamicComponentResolver.fromType(DocumentWidgetComponent)
'document': DynamicComponentResolver.fromType(DocumentWidgetComponent),
'upload': DynamicComponentResolver.fromType(UploadWidgetComponent)
};
constructor() {
super();
this.types['upload'] = (field: FormFieldModel): Type<{}> => {
if (field) {
let params = field.params;
if (params && params.link) {
return AttachWidgetComponent;
}
return UploadWidgetComponent;
}
return UnknownWidgetComponent;
};
}
}

View File

@@ -21,6 +21,7 @@ import { Observable } from 'rxjs/Observable';
import { AlfrescoApiService } from './alfresco-api.service';
import 'rxjs/add/observable/fromPromise';
import 'rxjs/add/operator/catch';
import { SitePaging, SiteEntry } from 'alfresco-js-api';
@Injectable()
export class SitesService {
@@ -28,7 +29,7 @@ export class SitesService {
constructor(
private apiService: AlfrescoApiService) { }
getSites(opts: any = {}): any {
getSites(opts: any = {}): Observable<SitePaging> {
const defaultOptions = {
skipCount: 0,
include: ['properties']
@@ -38,23 +39,23 @@ export class SitesService {
.catch(this.handleError);
}
getSite(siteId: string, opts?: any): any {
getSite(siteId: string, opts?: any): Observable<SiteEntry> {
return Observable.fromPromise(this.apiService.getInstance().core.sitesApi.getSite(siteId, opts))
.catch(this.handleError);
}
deleteSite(siteId: string, permanentFlag: boolean = true): any {
deleteSite(siteId: string, permanentFlag: boolean = true): Observable<any> {
let options: any = {};
options.permanent = permanentFlag;
return Observable.fromPromise(this.apiService.getInstance().core.sitesApi.deleteSite(siteId, options)
.catch(this.handleError));
}
getSiteContent(siteId: string): Observable<any> {
getSiteContent(siteId: string): Observable<SiteEntry> {
return this.getSite(siteId, { relations: ['containers'] });
}
getSiteMembers(siteId: string): Observable<any> {
getSiteMembers(siteId: string): Observable<SiteEntry> {
return this.getSite(siteId, { relations: ['members'] });
}