@@ -4,27 +4,32 @@ import {
44 Component ,
55 Input ,
66 OnChanges ,
7+ OnDestroy ,
78 OnInit ,
89 SimpleChange ,
910 SimpleChanges ,
1011} from '@angular/core' ;
1112import { AuthService } from '@dspace/core/auth/auth.service' ;
1213import { ObjectCacheService } from '@dspace/core/cache/object-cache.service' ;
14+ import { ConfigObject } from '@dspace/core/config/models/config.model' ;
15+ import { SubmissionDefinitionModel } from '@dspace/core/config/models/config-submission-definition.model' ;
16+ import { SubmissionDefinitionsConfigDataService } from '@dspace/core/config/submission-definitions-config-data.service' ;
1317import { CollectionDataService } from '@dspace/core/data/collection-data.service' ;
1418import { EntityTypeDataService } from '@dspace/core/data/entity-type-data.service' ;
1519import { RequestService } from '@dspace/core/data/request.service' ;
1620import { NotificationsService } from '@dspace/core/notification-system/notifications.service' ;
1721import { Collection } from '@dspace/core/shared/collection.model' ;
1822import { ItemType } from '@dspace/core/shared/item-relationships/item-type.model' ;
19- import { NONE_ENTITY_TYPE } from '@dspace/core/shared/item-relationships/item-type.resource-type' ;
2023import { MetadataValue } from '@dspace/core/shared/metadata.models' ;
2124import { getFirstSucceededRemoteListPayload } from '@dspace/core/shared/operators' ;
2225import {
2326 hasNoValue ,
27+ hasValue ,
2428 isNotNull ,
2529} from '@dspace/shared/utils/empty.util' ;
2630import { NgbModal } from '@ng-bootstrap/ng-bootstrap' ;
2731import {
32+ DynamicCheckboxModel ,
2833 DynamicFormControlModel ,
2934 DynamicFormOptionConfig ,
3035 DynamicFormService ,
@@ -34,7 +39,13 @@ import {
3439 TranslateModule ,
3540 TranslateService ,
3641} from '@ngx-translate/core' ;
37- import { Observable } from 'rxjs' ;
42+ import {
43+ catchError ,
44+ forkJoin ,
45+ Observable ,
46+ of ,
47+ Subscription ,
48+ } from 'rxjs' ;
3849
3950import { ComColFormComponent } from '../../shared/comcol/comcol-forms/comcol-form/comcol-form.component' ;
4051import { ComcolPageLogoComponent } from '../../shared/comcol/comcol-page-logo/comcol-page-logo.component' ;
@@ -44,6 +55,8 @@ import { VarDirective } from '../../shared/utils/var.directive';
4455import {
4556 collectionFormEntityTypeSelectionConfig ,
4657 collectionFormModels ,
58+ collectionFormSharedWorkspaceCheckboxConfig ,
59+ collectionFormSubmissionDefinitionSelectionConfig ,
4760} from './collection-form.models' ;
4861
4962/**
@@ -62,7 +75,7 @@ import {
6275 VarDirective ,
6376 ] ,
6477} )
65- export class CollectionFormComponent extends ComColFormComponent < Collection > implements OnInit , OnChanges {
78+ export class CollectionFormComponent extends ComColFormComponent < Collection > implements OnInit , OnChanges , OnDestroy {
6679 /**
6780 * @type {Collection } A new collection when a collection is being created, an existing Input collection when a collection is being edited
6881 */
@@ -79,12 +92,27 @@ export class CollectionFormComponent extends ComColFormComponent<Collection> imp
7992 */
8093 entityTypeSelection : DynamicSelectModel < string > = new DynamicSelectModel ( collectionFormEntityTypeSelectionConfig ) ;
8194
95+ /**
96+ * The dynamic form field used for submission definition selection
97+ * @type {DynamicSelectModel<string> }
98+ */
99+ submissionDefinitionSelection : DynamicSelectModel < string > = new DynamicSelectModel ( collectionFormSubmissionDefinitionSelectionConfig ) ;
100+
101+ sharedWorkspaceChekbox : DynamicCheckboxModel = new DynamicCheckboxModel ( collectionFormSharedWorkspaceCheckboxConfig ) ;
102+
82103 /**
83104 * The dynamic form fields used for creating/editing a collection
84105 * @type {DynamicFormControlModel[] }
85106 */
86107 formModel : DynamicFormControlModel [ ] ;
87108
109+ /**
110+ * Subscription to unsubscribe on destroy
111+ *
112+ * @private
113+ */
114+ private initSubscription : Subscription ;
115+
88116 public constructor ( protected formService : DynamicFormService ,
89117 protected translate : TranslateService ,
90118 protected notificationsService : NotificationsService ,
@@ -94,6 +122,7 @@ export class CollectionFormComponent extends ComColFormComponent<Collection> imp
94122 protected objectCache : ObjectCacheService ,
95123 protected entityTypeService : EntityTypeDataService ,
96124 protected chd : ChangeDetectorRef ,
125+ protected submissionDefinitionService : SubmissionDefinitionsConfigDataService ,
97126 protected modalService : NgbModal ) {
98127 super ( formService , translate , notificationsService , authService , requestService , objectCache , modalService ) ;
99128 }
@@ -115,21 +144,45 @@ export class CollectionFormComponent extends ComColFormComponent<Collection> imp
115144 }
116145 }
117146
147+ /**
148+ * Clean up eventual subscription when component gets destroyed
149+ */
150+ ngOnDestroy ( ) {
151+ super . ngOnDestroy ( ) ;
152+ if ( hasValue ( this . initSubscription ) ) {
153+ this . initSubscription . unsubscribe ( ) ;
154+ }
155+ }
156+
118157 initializeForm ( ) {
119158 let currentRelationshipValue : MetadataValue [ ] ;
159+ let currentDefinitionValue : MetadataValue [ ] ;
160+ let currentSharedWorkspaceValue : MetadataValue [ ] ;
120161 if ( this . dso && this . dso . metadata ) {
121162 currentRelationshipValue = this . dso . metadata [ 'dspace.entity.type' ] ;
163+ currentDefinitionValue = this . dso . metadata [ 'dspace.submission.definition' ] ;
164+ currentSharedWorkspaceValue = this . dso . metadata [ 'dspace.workspace.shared' ] ;
122165 }
123166
124167 const entities$ : Observable < ItemType [ ] > = this . entityTypeService . findAll ( { elementsPerPage : 100 , currentPage : 1 } ) . pipe (
125168 getFirstSucceededRemoteListPayload ( ) ,
126169 ) ;
127170
128- // retrieve all entity types to populate the dropdowns selection
129- entities$ . subscribe ( ( entityTypes : ItemType [ ] ) => {
171+ const definitions$ : Observable < ConfigObject [ ] > = this . submissionDefinitionService
172+ . findAll ( { elementsPerPage : 100 , currentPage : 1 } ) . pipe (
173+ getFirstSucceededRemoteListPayload ( ) ,
174+ catchError ( ( ) => of ( [ ] ) ) ,
175+ ) ;
130176
131- entityTypes = entityTypes . filter ( ( type : ItemType ) => type . label !== NONE_ENTITY_TYPE ) ;
132- entityTypes . forEach ( ( type : ItemType , index : number ) => {
177+ // retrieve all entity types and submission definitions to populate the dropdowns selection
178+ this . initSubscription = forkJoin ( {
179+ entityTypes : entities$ ,
180+ definitions : definitions$ ,
181+ } ) . subscribe ( ( { entityTypes, definitions } : { entityTypes : ItemType [ ] ; definitions : ConfigObject [ ] } ) => {
182+ const sortedEntityTypes = entityTypes
183+ . sort ( ( a , b ) => a . label . localeCompare ( b . label ) ) ;
184+
185+ sortedEntityTypes . forEach ( ( type : ItemType , index : number ) => {
133186 this . entityTypeSelection . add ( {
134187 disabled : false ,
135188 label : type . label ,
@@ -141,9 +194,26 @@ export class CollectionFormComponent extends ComColFormComponent<Collection> imp
141194 }
142195 } ) ;
143196
144- this . formModel = entityTypes . length === 0 ? collectionFormModels : [ ...collectionFormModels , this . entityTypeSelection ] ;
197+ definitions . filter ( def => ! def . id . includes ( '-edit' ) ) . forEach ( ( definition : SubmissionDefinitionModel , index : number ) => {
198+ this . submissionDefinitionSelection . add ( {
199+ disabled : false ,
200+ label : definition . name ,
201+ value : definition . name ,
202+ } as DynamicFormOptionConfig < string > ) ;
203+ if ( currentDefinitionValue && currentDefinitionValue . length > 0 && currentDefinitionValue [ 0 ] . value === definition . name ) {
204+ this . submissionDefinitionSelection . select ( index ) ;
205+ }
206+ } ) ;
207+
208+ this . formModel = entityTypes . length === 0 ?
209+ [ ...collectionFormModels , this . submissionDefinitionSelection , this . sharedWorkspaceChekbox ] :
210+ [ ...collectionFormModels , this . entityTypeSelection , this . submissionDefinitionSelection , this . sharedWorkspaceChekbox ] ;
145211
146212 super . ngOnInit ( ) ;
213+
214+ if ( currentSharedWorkspaceValue && currentSharedWorkspaceValue . length > 0 ) {
215+ this . sharedWorkspaceChekbox . value = currentSharedWorkspaceValue [ 0 ] . value === 'true' ;
216+ }
147217 this . chd . detectChanges ( ) ;
148218 } ) ;
149219
0 commit comments