[ADF-5106] allow to redefine form widgets (APS 1.x and cloud) (#5601)

* create failing test

* fix form and start form widget registration

* update cloud form
This commit is contained in:
Denys Vuika
2020-04-21 15:47:09 +01:00
committed by GitHub
parent af7eb53251
commit 122bd9face
17 changed files with 285 additions and 56 deletions

View File

@@ -55,6 +55,7 @@ import { AttachFileWidgetDialogService } from './attach-file-widget-dialog.servi
})
export class AttachFileWidgetComponent extends UploadWidgetComponent implements OnInit, OnDestroy {
typeId = 'AttachFileWidgetComponent';
repositoryList = [];
private tempFilesList = [];
private onDestroy$ = new Subject<boolean>();

View File

@@ -45,6 +45,7 @@ import { Node } from '@alfresco/js-api';
})
export class AttachFolderWidgetComponent extends WidgetComponent implements OnInit {
typeId = 'AttachFolderWidgetComponent';
hasFolder: boolean = false;
selectedFolderName: string = '';

View File

@@ -15,30 +15,98 @@
* limitations under the License.
*/
import { SimpleChange } from '@angular/core';
import { SimpleChange, ComponentFactoryResolver, Injector, NgModule, Component } from '@angular/core';
import { TestBed, ComponentFixture } from '@angular/core/testing';
import { Observable, of, throwError } from 'rxjs';
import { FormFieldModel, FormFieldTypes, FormModel, FormOutcomeEvent, FormOutcomeModel,
FormService, WidgetVisibilityService, NodeService, LogService, ContainerModel, fakeForm, FormRenderingService } from '@alfresco/adf-core';
FormService, WidgetVisibilityService, NodeService, ContainerModel, fakeForm,
setupTestBed, CoreModule } from '@alfresco/adf-core';
import { FormComponent } from './form.component';
import { NoopAnimationsModule } from '@angular/platform-browser/animations';
import { FormModule } from './form.module';
import { ContentWidgetModule } from '../content-widget/content-widget.module';
import { ProcessFormRenderingService } from './process-form-rendering.service';
describe('FormComponent', () => {
let formService: FormService;
let fixture: ComponentFixture<FormComponent>;
let formComponent: FormComponent;
let formService: FormService;
let visibilityService: WidgetVisibilityService;
let nodeService: NodeService;
let logService: LogService;
let formRenderingService: FormRenderingService;
let formRenderingService: ProcessFormRenderingService;
@Component({
selector: 'adf-custom-widget',
template: '<div></div>'
})
class CustomWidget {
typeId = 'CustomWidget';
}
@NgModule({
declarations: [CustomWidget],
exports: [CustomWidget],
entryComponents: [CustomWidget]
})
class CustomUploadModule {}
setupTestBed({
imports: [
NoopAnimationsModule,
CoreModule.forRoot(),
FormModule,
ContentWidgetModule,
CustomUploadModule
]
});
function buildWidget(type: string, injector: Injector): any {
const resolver = formRenderingService.getComponentTypeResolver(type);
const widgetType = resolver(null);
const factoryResolver: ComponentFactoryResolver = TestBed.get(ComponentFactoryResolver);
const factory = factoryResolver.resolveComponentFactory(widgetType);
const componentRef = factory.create(injector);
return componentRef.instance;
}
beforeEach(() => {
logService = new LogService(null);
visibilityService = new WidgetVisibilityService(null, logService);
visibilityService = TestBed.get(WidgetVisibilityService);
spyOn(visibilityService, 'refreshVisibility').and.stub();
formService = new FormService(null, null, logService);
nodeService = new NodeService(null);
formRenderingService = new FormRenderingService();
formComponent = new FormComponent(formService, visibilityService, null, nodeService, formRenderingService);
formService = TestBed.get(FormService);
nodeService = TestBed.get(NodeService);
formRenderingService = TestBed.get(ProcessFormRenderingService);
fixture = TestBed.createComponent(FormComponent);
formComponent = fixture.componentInstance;
});
it('should register custom [upload] widget', () => {
const widget = buildWidget('upload', fixture.componentRef.injector);
expect(widget['typeId']).toBe('AttachFileWidgetComponent');
});
it('should register custom [select-folder] widget', () => {
const widget = buildWidget('select-folder', fixture.componentRef.injector);
expect(widget['typeId']).toBe('AttachFolderWidgetComponent');
});
it('should allow to replace custom [upload] widget', () => {
formRenderingService.setComponentTypeResolver('upload', () => CustomWidget, true);
const widget = buildWidget('upload', fixture.componentRef.injector);
expect(widget['typeId']).toBe('CustomWidget');
});
it('should allow to replace custom [select-folder] widget', () => {
formRenderingService.setComponentTypeResolver('select-folder', () => CustomWidget, true);
const widget = buildWidget('select-folder', fixture.componentRef.injector);
expect(widget['typeId']).toBe('CustomWidget');
});
it('should check form', () => {

View File

@@ -16,18 +16,21 @@
*/
import { Component, EventEmitter, Input, Output, ViewEncapsulation, SimpleChanges, OnInit, OnDestroy, OnChanges } from '@angular/core';
import { AttachFileWidgetComponent, AttachFolderWidgetComponent } from '../content-widget';
import { EcmModelService, NodeService, WidgetVisibilityService,
FormService, FormRenderingService, FormBaseComponent, FormOutcomeModel,
FormEvent, FormErrorEvent, FormFieldModel,
FormModel, FormOutcomeEvent, FormValues, ContentLinkModel } from '@alfresco/adf-core';
import { Observable, of, Subject } from 'rxjs';
import { switchMap, takeUntil } from 'rxjs/operators';
import { ProcessFormRenderingService } from './process-form-rendering.service';
@Component({
selector: 'adf-form',
templateUrl: './form.component.html',
encapsulation: ViewEncapsulation.None
encapsulation: ViewEncapsulation.None,
providers: [
{ provide: FormRenderingService, useClass: ProcessFormRenderingService }
]
})
export class FormComponent extends FormBaseComponent implements OnInit, OnDestroy, OnChanges {
@@ -86,11 +89,8 @@ export class FormComponent extends FormBaseComponent implements OnInit, OnDestro
constructor(protected formService: FormService,
protected visibilityService: WidgetVisibilityService,
protected ecmModelService: EcmModelService,
protected nodeService: NodeService,
protected formRenderingService: FormRenderingService) {
protected nodeService: NodeService) {
super();
this.formRenderingService.setComponentTypeResolver('upload', () => AttachFileWidgetComponent, true);
this.formRenderingService.setComponentTypeResolver('select-folder', () => AttachFolderWidgetComponent, true);
}
ngOnInit() {

View File

@@ -0,0 +1,32 @@
/*!
* @license
* Copyright 2019 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 { Injectable } from '@angular/core';
import { FormRenderingService } from '@alfresco/adf-core';
import { AttachFileWidgetComponent } from '../content-widget/attach-file-widget.component';
import { AttachFolderWidgetComponent } from '../content-widget/attach-folder-widget.component';
@Injectable({
providedIn: 'root'
})
export class ProcessFormRenderingService extends FormRenderingService {
constructor() {
super();
this.setComponentTypeResolver('upload', () => AttachFileWidgetComponent, true);
this.setComponentTypeResolver('select-folder', () => AttachFolderWidgetComponent, true);
}
}

View File

@@ -17,4 +17,5 @@
export * from './form.component';
export * from './start-form.component';
export * from './process-form-rendering.service';
export * from './form.module';

View File

@@ -30,12 +30,16 @@ import {
} from '@angular/core';
import { FormComponent } from './form.component';
import { ContentLinkModel, FormService, WidgetVisibilityService, FormRenderingService, FormOutcomeModel } from '@alfresco/adf-core';
import { ProcessFormRenderingService } from './process-form-rendering.service';
@Component({
selector: 'adf-start-form',
templateUrl: './start-form.component.html',
styleUrls: ['./start-form.component.scss'],
encapsulation: ViewEncapsulation.None
encapsulation: ViewEncapsulation.None,
providers: [
{ provide: FormRenderingService, useClass: ProcessFormRenderingService }
]
})
export class StartFormComponent extends FormComponent implements OnChanges, OnInit, OnDestroy {
@@ -70,10 +74,8 @@ export class StartFormComponent extends FormComponent implements OnChanges, OnIn
@ViewChild('outcomesContainer', {})
outcomesContainer: ElementRef = null;
constructor(formService: FormService,
visibilityService: WidgetVisibilityService,
formRenderingService: FormRenderingService) {
super(formService, visibilityService, null, null, formRenderingService);
constructor(formService: FormService, visibilityService: WidgetVisibilityService) {
super(formService, visibilityService, null, null);
this.showTitle = false;
}