[AAE-8086] Propagate form events (#7572)

* [AAE-8086] Propagate events

* [AAE-8086] Add form rules manager to the form renderer

* [AAE-8086] Extensibility improvements

* [AAE-8086] Fix wrong import

* [AAE-8087] Add form actions

* [AAE-8086] Initialize form rules manager on form renderer component changes

* [AAE-8087] Fix form actions

* [AAE-8087] Fix unit tests for field visibility

* trigger travis
This commit is contained in:
Pablo Martinez Garcia
2022-04-14 11:59:12 +02:00
committed by GitHub
parent 48c3fac018
commit a02a8a4ad9
18 changed files with 464 additions and 34 deletions

View File

@@ -0,0 +1,93 @@
/*!
* @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 { setupTestBed } from '../../testing/setup-test-bed';
import { FormBaseModule } from '../form-base.module';
import { CoreTestingModule } from '../../testing';
import { TranslateModule } from '@ngx-translate/core';
import { ByPassFormRuleManager, FormRulesManager, formRulesManagerFactory, FORM_RULES_MANAGER } from '../models/form-rules.model';
import { Injector } from '@angular/core';
import { TestBed } from '@angular/core/testing';
class CustomRuleManager extends FormRulesManager<any> {
protected getRules() {
return null;
}
protected handleRuleEvent(): void {
return;
}
}
describe('Form Rules', () => {
let injector: Injector;
const customRuleManager = new CustomRuleManager(null);
describe('Injection token provided', () => {
setupTestBed({
imports: [
TranslateModule.forRoot(),
CoreTestingModule,
FormBaseModule
],
providers: [
{
provide: FORM_RULES_MANAGER,
useValue: customRuleManager
}
]
});
beforeEach(() => {
injector = TestBed.inject(Injector);
});
it('factory function should not return bypass service', () => {
spyOn(customRuleManager, 'destroy');
spyOn(customRuleManager, 'initialize');
const rulesManager = formRulesManagerFactory<any>(injector);
expect(rulesManager instanceof CustomRuleManager).toBeTruthy();
rulesManager.destroy();
expect(customRuleManager.destroy).toHaveBeenCalled();
rulesManager.initialize(null);
expect(customRuleManager.initialize).toHaveBeenCalled();
});
});
describe('Injection token not provided', () => {
setupTestBed({
imports: [
TranslateModule.forRoot(),
CoreTestingModule,
FormBaseModule
]
});
beforeEach(() => {
injector = TestBed.inject(Injector);
});
it('factory function should return bypass service', () => {
const rulesManager = formRulesManagerFactory<any>(injector);
expect(rulesManager instanceof ByPassFormRuleManager).toBeTruthy();
});
});
});

View File

@@ -0,0 +1,81 @@
/*!
* @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 { InjectionToken, Injector } from '@angular/core';
import { Subject } from 'rxjs';
import { filter, takeUntil } from 'rxjs/operators';
import { FormRulesEvent } from '../events/form-rules.event';
import { FormModel, FormService } from '../public-api';
export const FORM_RULES_MANAGER = new InjectionToken<FormRulesManager<any>>('form.rule.manager');
export function formRulesManagerFactory<T>(injector: Injector): FormRulesManager<T> {
try {
return injector.get(FORM_RULES_MANAGER);
} catch {
return new ByPassFormRuleManager<T>(null);
}
}
export abstract class FormRulesManager<T> {
constructor(private formService: FormService) { }
protected formModel: FormModel;
private onDestroy$ = new Subject<boolean>();
private initialized = false;
initialize(formModel: FormModel) {
if (this.initialized) {
this.destroy();
this.onDestroy$ = new Subject<boolean>();
}
this.formModel = formModel;
const rules = this.getRules();
if (!!rules) {
this.formService.formRulesEvent
.pipe(
filter(event => !!event.form.id && event.form.id === formModel?.id),
takeUntil(this.onDestroy$)
).subscribe(event => {
this.handleRuleEvent(event, rules);
});
}
this.initialized = true;
}
protected abstract getRules(): T;
protected abstract handleRuleEvent(event: FormRulesEvent, rules: T): void;
destroy() {
this.onDestroy$.next(true);
this.onDestroy$.complete();
}
}
export class ByPassFormRuleManager<T> extends FormRulesManager<T> {
protected getRules(): T {
return null;
}
protected handleRuleEvent(): void {
return;
}
}