diff --git a/projects/netgrif-components-core/src/lib/data-fields/case-ref-field/case-ref-default/case-ref-default.component.ts b/projects/netgrif-components-core/src/lib/data-fields/case-ref-field/case-ref-default/case-ref-default.component.ts index 612a8b439..72d9a97a8 100644 --- a/projects/netgrif-components-core/src/lib/data-fields/case-ref-field/case-ref-default/case-ref-default.component.ts +++ b/projects/netgrif-components-core/src/lib/data-fields/case-ref-field/case-ref-default/case-ref-default.component.ts @@ -1,40 +1,27 @@ import {AfterViewInit, Component, Inject, Injector, OnDestroy, Optional, Type} from "@angular/core"; -import {NAE_BASE_FILTER} from "../../../search/models/base-filter-injection-token"; -import {SimpleFilter} from "../../../filter/models/simple-filter"; -import {BaseFilter} from "../../../search/models/base-filter"; -import {NAE_VIEW_ID_SEGMENT} from "../../../user/models/view-id-injection-tokens"; -import {ViewIdService} from "../../../user/services/view-id.service"; -import {AbstractBaseDataFieldComponent} from "../../base-component/abstract-base-data-field.component"; import {DATA_FIELD_PORTAL_DATA, DataFieldPortalData} from "../../models/data-field-portal-data-injection-token"; -import {ComponentPortal} from "@angular/cdk/portal"; import {CaseRefField} from '../model/case-ref-field'; -import {NAE_DEFAULT_HEADERS} from '../../../header/models/default-headers-token'; -import { - NAE_CASE_REF_CREATE_CASE, - NAE_CASE_REF_DATAFIELD, - NAE_CASE_REF_SEARCH -} from '../model/case-ref-injection-tokens'; import {Subscription} from 'rxjs'; +import {AbstractCaseRefBaseFieldComponent} from '../model/abstract-case-ref-base-field-component'; @Component({ selector: 'ncc-abstract-case-ref-default', template: '' }) -export abstract class AbstractCaseRefDefaultComponent extends AbstractBaseDataFieldComponent implements AfterViewInit, OnDestroy { +export abstract class AbstractCaseRefDefaultComponent extends AbstractCaseRefBaseFieldComponent implements AfterViewInit, OnDestroy { - public componentPortal: ComponentPortal; protected _sub: Subscription; protected constructor(protected injector: Injector, protected caseViewType: Type, @Optional() @Inject(DATA_FIELD_PORTAL_DATA) dataFieldPortalData: DataFieldPortalData) { - super(dataFieldPortalData); + super(injector, caseViewType, dataFieldPortalData); } ngAfterViewInit(): void { - this.createFilter(); + this.createFilter(this.dataField.value.length > 0 ? this.dataField.value : ''); this._sub = this.dataField.valueChanges().subscribe(() => { - this.createFilter(); + this.createFilter(this.dataField.value.length > 0 ? this.dataField.value : ''); }); } @@ -43,36 +30,4 @@ export abstract class AbstractCaseRefDefaultComponent extends AbstractBaseDataFi this._sub.unsubscribe(); } - createFilter() { - let portalInjector; - const filterValue : string | string[] = this.dataField.value.length > 0 ? this.dataField.value : ''; - portalInjector = Injector.create({ - providers: [ - { - provide: NAE_DEFAULT_HEADERS, useValue: this.dataField.component?.properties?.headers.split(',') - }, - { - provide: NAE_CASE_REF_CREATE_CASE, useValue: this.dataField.component?.properties?.createCase === 'true' - }, - { - provide: NAE_CASE_REF_SEARCH, useValue: this.dataField.component?.properties?.search === 'true' - }, - { - provide: NAE_BASE_FILTER, - useValue: { filter: SimpleFilter.fromCaseQuery({stringId: filterValue}) } as BaseFilter - }, - { - provide: NAE_VIEW_ID_SEGMENT, - useValue: this.dataField.parentCaseId + '_' + this.dataField.parentTaskId + '_' + this.dataField.stringId - }, - { - provide: NAE_CASE_REF_DATAFIELD, - useValue: this.dataField - }, - { provide: ViewIdService, useClass: ViewIdService }], - parent: this.injector - }); - this.componentPortal = new ComponentPortal(this.caseViewType, null, portalInjector); - } - } diff --git a/projects/netgrif-components-core/src/lib/data-fields/case-ref-field/model/abstract-case-ref-base-field-component.ts b/projects/netgrif-components-core/src/lib/data-fields/case-ref-field/model/abstract-case-ref-base-field-component.ts new file mode 100644 index 000000000..0ccb01a1e --- /dev/null +++ b/projects/netgrif-components-core/src/lib/data-fields/case-ref-field/model/abstract-case-ref-base-field-component.ts @@ -0,0 +1,66 @@ +import {AbstractBaseDataFieldComponent} from '../../base-component/abstract-base-data-field.component'; +import {CaseRefField} from './case-ref-field'; +import { Inject, Injector, Optional, Type} from '@angular/core'; +import {ComponentPortal} from '@angular/cdk/portal'; +import {DATA_FIELD_PORTAL_DATA, DataFieldPortalData} from '../../models/data-field-portal-data-injection-token'; +import {CaseSearchRequestBody} from '../../../filter/models/case-search-request-body'; +import {NAE_DEFAULT_HEADERS} from '../../../header/models/default-headers-token'; +import {NAE_CASE_REF_CREATE_CASE, NAE_CASE_REF_DATAFIELD, NAE_CASE_REF_SEARCH} from './case-ref-injection-tokens'; +import {NAE_BASE_FILTER} from '../../../search/models/base-filter-injection-token'; +import {SimpleFilter} from '../../../filter/models/simple-filter'; +import {BaseFilter} from '../../../search/models/base-filter'; +import {NAE_VIEW_ID_SEGMENT} from '../../../user/models/view-id-injection-tokens'; +import {ViewIdService} from '../../../user/services/view-id.service'; +import {DataField} from '../../models/abstract-data-field'; + +export abstract class AbstractCaseRefBaseFieldComponent> extends AbstractBaseDataFieldComponent { + + public componentPortal: ComponentPortal; + + protected constructor(protected injector: Injector, + protected caseViewType: Type, + @Optional() @Inject(DATA_FIELD_PORTAL_DATA) dataFieldPortalData: DataFieldPortalData) { + super(dataFieldPortalData); + } + + createFilter(filterValue: string | string[]) { + let portalInjector; + const filterProperty: boolean = this.dataField?.component?.properties?.filter === 'true'; + let query: CaseSearchRequestBody; + if (filterProperty) { + query = JSON.parse(this.dataField?.component?.properties?.filterQuery) as CaseSearchRequestBody; + } + let providers = [ + { + provide: NAE_DEFAULT_HEADERS, useValue: this.dataField.component?.properties?.headers.split(',') + }, + { + provide: NAE_CASE_REF_CREATE_CASE, useValue: this.dataField.component?.properties?.createCase === 'true' + }, + { + provide: NAE_CASE_REF_SEARCH, useValue: this.dataField.component?.properties?.search === 'true' + }, + { + provide: NAE_BASE_FILTER, + useValue: { filter: SimpleFilter.fromCaseQuery((filterProperty && query ? query : {stringId: filterValue})) } as BaseFilter + }, + { + provide: NAE_VIEW_ID_SEGMENT, + useValue: this.dataField.parentCaseId + '_' + this.dataField.parentTaskId + '_' + this.dataField.stringId + }, + { provide: ViewIdService, useClass: ViewIdService } + ]; + if (this.dataField instanceof CaseRefField) { + providers.push({ + provide: NAE_CASE_REF_DATAFIELD, + useValue: this.dataField + }) + } + portalInjector = Injector.create({ + providers, + parent: this.injector + }); + this.componentPortal = new ComponentPortal(this.caseViewType, null, portalInjector); + } +} + diff --git a/projects/netgrif-components-core/src/lib/data-fields/enumeration-field/enumeration-caseref-field/abstract-enumeration-caseref-field.component.ts b/projects/netgrif-components-core/src/lib/data-fields/enumeration-field/enumeration-caseref-field/abstract-enumeration-caseref-field.component.ts index a855cbd6a..8a456bc8b 100644 --- a/projects/netgrif-components-core/src/lib/data-fields/enumeration-field/enumeration-caseref-field/abstract-enumeration-caseref-field.component.ts +++ b/projects/netgrif-components-core/src/lib/data-fields/enumeration-field/enumeration-caseref-field/abstract-enumeration-caseref-field.component.ts @@ -1,36 +1,27 @@ import {AfterViewInit, Component, Inject, Injector, OnDestroy, Optional, Type} from "@angular/core"; -import {NAE_BASE_FILTER} from "../../../search/models/base-filter-injection-token"; -import {SimpleFilter} from "../../../filter/models/simple-filter"; -import {BaseFilter} from "../../../search/models/base-filter"; -import {NAE_VIEW_ID_SEGMENT} from "../../../user/models/view-id-injection-tokens"; -import {ViewIdService} from "../../../user/services/view-id.service"; -import {AbstractBaseDataFieldComponent} from "../../base-component/abstract-base-data-field.component"; import {DATA_FIELD_PORTAL_DATA, DataFieldPortalData} from "../../models/data-field-portal-data-injection-token"; -import {ComponentPortal} from "@angular/cdk/portal"; -import {NAE_DEFAULT_HEADERS} from '../../../header/models/default-headers-token'; -import {NAE_CASE_REF_CREATE_CASE, NAE_CASE_REF_SEARCH} from '../../case-ref-field/model/case-ref-injection-tokens'; import {EnumerationField} from '../models/enumeration-field'; import {Subscription} from 'rxjs'; +import {AbstractCaseRefBaseFieldComponent} from '../../case-ref-field/model/abstract-case-ref-base-field-component'; @Component({ selector: 'ncc-abstract-case-ref-default', template: '' }) -export abstract class AbstractEnumerationCaseRefComponent extends AbstractBaseDataFieldComponent implements AfterViewInit, OnDestroy { +export abstract class AbstractEnumerationCaseRefComponent extends AbstractCaseRefBaseFieldComponent implements AfterViewInit, OnDestroy { - public componentPortal: ComponentPortal; protected _sub: Subscription; protected constructor(protected injector: Injector, protected caseViewType: Type, @Optional() @Inject(DATA_FIELD_PORTAL_DATA) dataFieldPortalData: DataFieldPortalData) { - super(dataFieldPortalData); + super(injector, caseViewType, dataFieldPortalData); } ngAfterViewInit(): void { - this.createFilter(); + this.createFilter(this.dataField.choices.length > 0 ? this.dataField.choices.map(value => value.key) : ''); this._sub = this.dataField.updatedChoices.subscribe(() => { - this.createFilter(); + this.createFilter(this.dataField.choices.length > 0 ? this.dataField.choices.map(value => value.key) : ''); }); } @@ -39,32 +30,4 @@ export abstract class AbstractEnumerationCaseRefComponent extends AbstractBaseDa this._sub.unsubscribe(); } - createFilter() { - let portalInjector; - const filterValue : string | string[] = this.dataField.choices.length > 0 ? this.dataField.choices.map(value => value.key) : ''; - portalInjector = Injector.create({ - providers: [ - { - provide: NAE_DEFAULT_HEADERS, useValue: this.dataField.component?.properties?.headers.split(',') - }, - { - provide: NAE_CASE_REF_CREATE_CASE, useValue: this.dataField.component?.properties?.createCase === 'true' - }, - { - provide: NAE_CASE_REF_SEARCH, useValue: this.dataField.component?.properties?.search === 'true' - }, - { - provide: NAE_BASE_FILTER, - useValue: { filter: SimpleFilter.fromCaseQuery({stringId: filterValue}) } as BaseFilter - }, - { - provide: NAE_VIEW_ID_SEGMENT, - useValue: this.dataField.parentCaseId + '_' + this.dataField.parentTaskId + '_' + this.dataField.stringId - }, - { provide: ViewIdService, useClass: ViewIdService }], - parent: this.injector - }); - this.componentPortal = new ComponentPortal(this.caseViewType, null, portalInjector); - } - } diff --git a/projects/netgrif-components-core/src/lib/data-fields/multichoice-field/multichoice-caseref-field/abstract-multichoice-caseref-field.component.ts b/projects/netgrif-components-core/src/lib/data-fields/multichoice-field/multichoice-caseref-field/abstract-multichoice-caseref-field.component.ts index 3fc9628af..236a1952b 100644 --- a/projects/netgrif-components-core/src/lib/data-fields/multichoice-field/multichoice-caseref-field/abstract-multichoice-caseref-field.component.ts +++ b/projects/netgrif-components-core/src/lib/data-fields/multichoice-field/multichoice-caseref-field/abstract-multichoice-caseref-field.component.ts @@ -1,63 +1,28 @@ import {AfterViewInit, Component, Inject, Injector, Optional, Type} from "@angular/core"; -import {NAE_BASE_FILTER} from "../../../search/models/base-filter-injection-token"; -import {SimpleFilter} from "../../../filter/models/simple-filter"; -import {BaseFilter} from "../../../search/models/base-filter"; -import {NAE_VIEW_ID_SEGMENT} from "../../../user/models/view-id-injection-tokens"; -import {ViewIdService} from "../../../user/services/view-id.service"; -import {AbstractBaseDataFieldComponent} from "../../base-component/abstract-base-data-field.component"; import {DATA_FIELD_PORTAL_DATA, DataFieldPortalData} from "../../models/data-field-portal-data-injection-token"; import {ComponentPortal} from "@angular/cdk/portal"; -import {NAE_DEFAULT_HEADERS} from '../../../header/models/default-headers-token'; import {MultichoiceField} from '../models/multichoice-field'; -import {NAE_CASE_REF_CREATE_CASE, NAE_CASE_REF_SEARCH} from '../../case-ref-field/model/case-ref-injection-tokens'; +import {AbstractCaseRefBaseFieldComponent} from '../../case-ref-field/model/abstract-case-ref-base-field-component'; @Component({ selector: 'ncc-abstract-case-ref-default', template: '' }) -export abstract class AbstractMultichoiceCaseRefComponent extends AbstractBaseDataFieldComponent implements AfterViewInit { +export abstract class AbstractMultichoiceCaseRefComponent extends AbstractCaseRefBaseFieldComponent implements AfterViewInit { public componentPortal: ComponentPortal; protected constructor(protected injector: Injector, protected caseViewType: Type, @Optional() @Inject(DATA_FIELD_PORTAL_DATA) dataFieldPortalData: DataFieldPortalData) { - super(dataFieldPortalData); + super(injector, caseViewType, dataFieldPortalData); } ngAfterViewInit(): void { - this.createFilter(); + this.createFilter(this.dataField.choices.length > 0 ? this.dataField.choices.map(value => value.key) : ''); this.dataField.updatedChoices.subscribe(() => { - this.createFilter(); + this.createFilter(this.dataField.choices.length > 0 ? this.dataField.choices.map(value => value.key) : ''); }); } - createFilter() { - let portalInjector; - const filterValue : string | string[] = this.dataField.choices.length > 0 ? this.dataField.choices.map(value => value.key) : ''; - portalInjector = Injector.create({ - providers: [ - { - provide: NAE_DEFAULT_HEADERS, useValue: this.dataField.component?.properties?.headers.split(',') - }, - { - provide: NAE_CASE_REF_CREATE_CASE, useValue: this.dataField.component?.properties?.createCase === 'true' - }, - { - provide: NAE_CASE_REF_SEARCH, useValue: this.dataField.component?.properties?.search === 'true' - }, - { - provide: NAE_BASE_FILTER, - useValue: { filter: SimpleFilter.fromCaseQuery({stringId: filterValue}) } as BaseFilter - }, - { - provide: NAE_VIEW_ID_SEGMENT, - useValue: this.dataField.parentCaseId + '_' + this.dataField.parentTaskId + '_' + this.dataField.stringId - }, - { provide: ViewIdService, useClass: ViewIdService }], - parent: this.injector - }); - this.componentPortal = new ComponentPortal(this.caseViewType, null, portalInjector); - } - }