diff --git a/ng2-components/ng2-activiti-form/src/components/widgets/functional-group/functional-group.widget.spec.ts b/ng2-components/ng2-activiti-form/src/components/widgets/functional-group/functional-group.widget.spec.ts index 238d9d6739..4522f643d0 100644 --- a/ng2-components/ng2-activiti-form/src/components/widgets/functional-group/functional-group.widget.spec.ts +++ b/ng2-components/ng2-activiti-form/src/components/widgets/functional-group/functional-group.widget.spec.ts @@ -47,6 +47,23 @@ describe('FunctionalGroupWidget', () => { expect(widget.value).toBeUndefined(); }); + it('should require form field to setup values on init', () => { + widget.field = null; + widget.ngOnInit(); + + expect(widget.value).toBeUndefined(); + expect(widget.groupId).toBeUndefined(); + }); + + it('should setup group restriction', () => { + widget.ngOnInit(); + expect(widget.groupId).toBeUndefined(); + + widget.field.params = { restrictWithGroup: { id: '' } }; + widget.ngOnInit(); + expect(widget.groupId).toBe(''); + }); + it('should flush value on blur', (done) => { spyOn(widget, 'flushValue').and.stub(); widget.onBlur(); @@ -132,7 +149,28 @@ describe('FunctionalGroupWidget', () => { widget.value = 'group'; widget.onKeyUp(null); - expect(formService.getWorkflowGroups).toHaveBeenCalledWith('group'); + expect(formService.getWorkflowGroups).toHaveBeenCalledWith('group', undefined); + expect(widget.groups).toBe(groups); + expect(widget.popupVisible).toBeTruthy(); + }); + + it('should fetch groups with a group filter', () => { + let groups: GroupModel[] = [ + new GroupModel(), + new GroupModel() + ]; + spyOn(formService, 'getWorkflowGroups').and.returnValue( + Observable.create(observer => { + observer.next(groups); + observer.complete(); + }) + ); + + widget.groupId = 'parentGroup'; + widget.value = 'group'; + widget.onKeyUp(null); + + expect(formService.getWorkflowGroups).toHaveBeenCalledWith('group', 'parentGroup'); expect(widget.groups).toBe(groups); expect(widget.popupVisible).toBeTruthy(); }); @@ -148,7 +186,7 @@ describe('FunctionalGroupWidget', () => { widget.value = 'group'; widget.onKeyUp(null); - expect(formService.getWorkflowGroups).toHaveBeenCalledWith('group'); + expect(formService.getWorkflowGroups).toHaveBeenCalledWith('group', undefined); expect(widget.groups.length).toBe(0); expect(widget.popupVisible).toBeFalsy(); }); diff --git a/ng2-components/ng2-activiti-form/src/components/widgets/functional-group/functional-group.widget.ts b/ng2-components/ng2-activiti-form/src/components/widgets/functional-group/functional-group.widget.ts index 001b6f264f..233f8deadb 100644 --- a/ng2-components/ng2-activiti-form/src/components/widgets/functional-group/functional-group.widget.ts +++ b/ng2-components/ng2-activiti-form/src/components/widgets/functional-group/functional-group.widget.ts @@ -34,6 +34,7 @@ export class FunctionalGroupWidget extends WidgetComponent implements OnInit { popupVisible: boolean = false; groups: GroupModel[] = []; minTermLength: number = 1; + groupId: string; constructor(private formService: FormService) { super(); @@ -42,15 +43,23 @@ export class FunctionalGroupWidget extends WidgetComponent implements OnInit { // TODO: investigate, called 2 times // https://github.com/angular/angular/issues/6782 ngOnInit() { - let group = this.field.value; - if (group) { - this.value = group.name; + if (this.field) { + let group = this.field.value; + if (group) { + this.value = group.name; + } + + let params = this.field.params; + if (params && params['restrictWithGroup']) { + let restrictWithGroup = params['restrictWithGroup']; + this.groupId = restrictWithGroup.id; + } } } onKeyUp(event: KeyboardEvent) { if (this.value && this.value.length >= this.minTermLength) { - this.formService.getWorkflowGroups(this.value) + this.formService.getWorkflowGroups(this.value, this.groupId) .subscribe((result: GroupModel[]) => { this.groups = result || []; this.popupVisible = this.groups.length > 0; diff --git a/ng2-components/ng2-activiti-form/src/services/form.service.ts b/ng2-components/ng2-activiti-form/src/services/form.service.ts index d9b90bb419..bcd020e02b 100644 --- a/ng2-components/ng2-activiti-form/src/services/form.service.ts +++ b/ng2-components/ng2-activiti-form/src/services/form.service.ts @@ -212,7 +212,7 @@ export class FormService { } // TODO: uses private webApp api - getWorkflowGroups(filter: string): Observable { + getWorkflowGroups(filter: string, groupId?: string): Observable { return Observable.create(observer => { let xhr: XMLHttpRequest = new XMLHttpRequest(); @@ -235,6 +235,9 @@ export class FormService { let host = this.apiService.getInstance().config.hostBpm; let url = `${host}/activiti-app/app/rest/workflow-groups?filter=${filter}`; + if (groupId) { + url += `&groupId=${groupId}`; + } xhr.open('GET', url, true); xhr.setRequestHeader('Authorization', this.apiService.getInstance().getTicketBpm()); xhr.send();