-
Notifications
You must be signed in to change notification settings - Fork 447
/
Copy pathds-dynamic-form-control-container.component.ts
482 lines (423 loc) · 20.3 KB
/
ds-dynamic-form-control-container.component.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
import {
ChangeDetectionStrategy,
ChangeDetectorRef,
Component,
ComponentFactoryResolver,
ContentChildren,
EventEmitter,
Input,
NgZone,
OnChanges,
OnDestroy,
OnInit,
Output,
QueryList,
SimpleChanges,
Type,
ViewChild,
ViewContainerRef
} from '@angular/core';
import { FormArray, FormGroup } from '@angular/forms';
import {
DYNAMIC_FORM_CONTROL_TYPE_ARRAY,
DYNAMIC_FORM_CONTROL_TYPE_CHECKBOX,
DYNAMIC_FORM_CONTROL_TYPE_CHECKBOX_GROUP,
DYNAMIC_FORM_CONTROL_TYPE_DATEPICKER,
DYNAMIC_FORM_CONTROL_TYPE_GROUP,
DYNAMIC_FORM_CONTROL_TYPE_INPUT,
DYNAMIC_FORM_CONTROL_TYPE_RADIO_GROUP,
DYNAMIC_FORM_CONTROL_TYPE_SELECT,
DYNAMIC_FORM_CONTROL_TYPE_TEXTAREA,
DYNAMIC_FORM_CONTROL_TYPE_TIMEPICKER,
DynamicDatePickerModel,
DynamicFormArrayGroupModel,
DynamicFormArrayModel,
DynamicFormComponentService,
DynamicFormControl,
DynamicFormControlContainerComponent,
DynamicFormControlEvent,
DynamicFormControlEventType,
DynamicFormControlModel,
DynamicFormLayout,
DynamicFormLayoutService,
DynamicFormRelationService,
DynamicFormValidationService,
DynamicTemplateDirective,
} from '@ng-dynamic-forms/core';
import {
DynamicNGBootstrapCalendarComponent,
DynamicNGBootstrapCheckboxComponent,
DynamicNGBootstrapCheckboxGroupComponent,
DynamicNGBootstrapInputComponent,
DynamicNGBootstrapRadioGroupComponent,
DynamicNGBootstrapSelectComponent,
DynamicNGBootstrapTextAreaComponent,
DynamicNGBootstrapTimePickerComponent
} from '@ng-dynamic-forms/ui-ng-bootstrap';
import { TranslateService } from '@ngx-translate/core';
import { ReorderableRelationship } from './existing-metadata-list-element/existing-metadata-list-element.component';
import { DYNAMIC_FORM_CONTROL_TYPE_ONEBOX } from './models/onebox/dynamic-onebox.model';
import { DYNAMIC_FORM_CONTROL_TYPE_SCROLLABLE_DROPDOWN } from './models/scrollable-dropdown/dynamic-scrollable-dropdown.model';
import { DYNAMIC_FORM_CONTROL_TYPE_TAG } from './models/tag/dynamic-tag.model';
import { DYNAMIC_FORM_CONTROL_TYPE_DSDATEPICKER } from './models/date-picker/date-picker.model';
import { DYNAMIC_FORM_CONTROL_TYPE_LOOKUP } from './models/lookup/dynamic-lookup.model';
import { DynamicListCheckboxGroupModel } from './models/list/dynamic-list-checkbox-group.model';
import { DynamicListRadioGroupModel } from './models/list/dynamic-list-radio-group.model';
import { hasNoValue, hasValue, isNotEmpty, isNotUndefined } from '../../../empty.util';
import { DYNAMIC_FORM_CONTROL_TYPE_LOOKUP_NAME } from './models/lookup/dynamic-lookup-name.model';
import { DsDynamicTagComponent } from './models/tag/dynamic-tag.component';
import { DsDatePickerComponent } from './models/date-picker/date-picker.component';
import { DsDynamicListComponent } from './models/list/dynamic-list.component';
import { DsDynamicOneboxComponent } from './models/onebox/dynamic-onebox.component';
import { DsDynamicScrollableDropdownComponent } from './models/scrollable-dropdown/dynamic-scrollable-dropdown.component';
import { DsDynamicLookupComponent } from './models/lookup/dynamic-lookup.component';
import { DsDynamicFormGroupComponent } from './models/form-group/dynamic-form-group.component';
import { DsDynamicFormArrayComponent } from './models/array-group/dynamic-form-array.component';
import { DsDynamicRelationGroupComponent } from './models/relation-group/dynamic-relation-group.components';
import { DsDatePickerInlineComponent } from './models/date-picker-inline/dynamic-date-picker-inline.component';
import { DYNAMIC_FORM_CONTROL_TYPE_CUSTOM_SWITCH } from './models/custom-switch/custom-switch.model';
import { CustomSwitchComponent } from './models/custom-switch/custom-switch.component';
import { find, map, startWith, switchMap, take } from 'rxjs/operators';
import { combineLatest as observableCombineLatest, Observable, Subscription } from 'rxjs';
import { SearchResult } from '../../../search/search-result.model';
import { DSpaceObject } from '../../../../core/shared/dspace-object.model';
import { NgbModal, NgbModalRef } from '@ng-bootstrap/ng-bootstrap';
import { RelationshipService } from '../../../../core/data/relationship.service';
import { SelectableListService } from '../../../object-list/selectable-list/selectable-list.service';
import { DsDynamicDisabledComponent } from './models/disabled/dynamic-disabled.component';
import { DYNAMIC_FORM_CONTROL_TYPE_DISABLED } from './models/disabled/dynamic-disabled.model';
import { DsDynamicLookupRelationModalComponent } from './relation-lookup-modal/dynamic-lookup-relation-modal.component';
import {
getAllSucceededRemoteData,
getFirstSucceededRemoteData,
getFirstSucceededRemoteDataPayload,
getPaginatedListPayload,
getRemoteDataPayload
} from '../../../../core/shared/operators';
import { RemoteData } from '../../../../core/data/remote-data';
import { Item } from '../../../../core/shared/item.model';
import { ItemDataService } from '../../../../core/data/item-data.service';
import { Store } from '@ngrx/store';
import { AppState } from '../../../../app.reducer';
import { SubmissionObjectDataService } from '../../../../core/submission/submission-object-data.service';
import { SubmissionObject } from '../../../../core/submission/models/submission-object.model';
import { PaginatedList } from '../../../../core/data/paginated-list.model';
import { ItemSearchResult } from '../../../object-collection/shared/item-search-result.model';
import { Relationship } from '../../../../core/shared/item-relationships/relationship.model';
import { Collection } from '../../../../core/shared/collection.model';
import { MetadataValue, VIRTUAL_METADATA_PREFIX } from '../../../../core/shared/metadata.models';
import { FormService } from '../../form.service';
import { SelectableListState } from '../../../object-list/selectable-list/selectable-list.reducer';
import { SubmissionService } from '../../../../submission/submission.service';
import { followLink } from '../../../utils/follow-link-config.model';
import { paginatedRelationsToItems } from '../../../../+item-page/simple/item-types/shared/item-relationships-utils';
import { RelationshipOptions } from '../models/relationship-options.model';
import { FormBuilderService } from '../form-builder.service';
import { DYNAMIC_FORM_CONTROL_TYPE_RELATION_GROUP } from './ds-dynamic-form-constants';
import { FormFieldMetadataValueObject } from '../models/form-field-metadata-value.model';
export function dsDynamicFormControlMapFn(model: DynamicFormControlModel): Type<DynamicFormControl> | null {
switch (model.type) {
case DYNAMIC_FORM_CONTROL_TYPE_ARRAY:
return DsDynamicFormArrayComponent;
case DYNAMIC_FORM_CONTROL_TYPE_CHECKBOX:
return DynamicNGBootstrapCheckboxComponent;
case DYNAMIC_FORM_CONTROL_TYPE_CHECKBOX_GROUP:
return (model instanceof DynamicListCheckboxGroupModel) ? DsDynamicListComponent : DynamicNGBootstrapCheckboxGroupComponent;
case DYNAMIC_FORM_CONTROL_TYPE_DATEPICKER:
const datepickerModel = model as DynamicDatePickerModel;
return datepickerModel.inline ? DynamicNGBootstrapCalendarComponent : DsDatePickerInlineComponent;
case DYNAMIC_FORM_CONTROL_TYPE_GROUP:
return DsDynamicFormGroupComponent;
case DYNAMIC_FORM_CONTROL_TYPE_INPUT:
return DynamicNGBootstrapInputComponent;
case DYNAMIC_FORM_CONTROL_TYPE_RADIO_GROUP:
return (model instanceof DynamicListRadioGroupModel) ? DsDynamicListComponent : DynamicNGBootstrapRadioGroupComponent;
case DYNAMIC_FORM_CONTROL_TYPE_SELECT:
return DynamicNGBootstrapSelectComponent;
case DYNAMIC_FORM_CONTROL_TYPE_TEXTAREA:
return DynamicNGBootstrapTextAreaComponent;
case DYNAMIC_FORM_CONTROL_TYPE_TIMEPICKER:
return DynamicNGBootstrapTimePickerComponent;
case DYNAMIC_FORM_CONTROL_TYPE_ONEBOX:
return DsDynamicOneboxComponent;
case DYNAMIC_FORM_CONTROL_TYPE_SCROLLABLE_DROPDOWN:
return DsDynamicScrollableDropdownComponent;
case DYNAMIC_FORM_CONTROL_TYPE_TAG:
return DsDynamicTagComponent;
case DYNAMIC_FORM_CONTROL_TYPE_RELATION_GROUP:
return DsDynamicRelationGroupComponent;
case DYNAMIC_FORM_CONTROL_TYPE_DSDATEPICKER:
return DsDatePickerComponent;
case DYNAMIC_FORM_CONTROL_TYPE_LOOKUP:
return DsDynamicLookupComponent;
case DYNAMIC_FORM_CONTROL_TYPE_LOOKUP_NAME:
return DsDynamicLookupComponent;
case DYNAMIC_FORM_CONTROL_TYPE_DISABLED:
return DsDynamicDisabledComponent;
case DYNAMIC_FORM_CONTROL_TYPE_CUSTOM_SWITCH:
return CustomSwitchComponent;
default:
return null;
}
}
@Component({
selector: 'ds-dynamic-form-control-container',
styleUrls: ['./ds-dynamic-form-control-container.component.scss'],
templateUrl: './ds-dynamic-form-control-container.component.html',
changeDetection: ChangeDetectionStrategy.Default
})
export class DsDynamicFormControlContainerComponent extends DynamicFormControlContainerComponent implements OnInit, OnChanges, OnDestroy {
@ContentChildren(DynamicTemplateDirective) contentTemplateList: QueryList<DynamicTemplateDirective>;
// tslint:disable-next-line:no-input-rename
@Input('templates') inputTemplateList: QueryList<DynamicTemplateDirective>;
@Input() formId: string;
@Input() asBootstrapFormGroup = false;
@Input() bindId = true;
@Input() context: any | null = null;
@Input() group: FormGroup;
@Input() hostClass: string[];
@Input() hasErrorMessaging = false;
@Input() layout = null as DynamicFormLayout;
@Input() model: any;
relationshipValue$: Observable<ReorderableRelationship>;
isRelationship: boolean;
modalRef: NgbModalRef;
item: Item;
item$: Observable<Item>;
collection: Collection;
listId: string;
searchConfig: string;
value: MetadataValue;
/**
* List of subscriptions to unsubscribe from
*/
private subs: Subscription[] = [];
/* tslint:disable:no-output-rename */
@Output('dfBlur') blur: EventEmitter<DynamicFormControlEvent> = new EventEmitter<DynamicFormControlEvent>();
@Output('dfChange') change: EventEmitter<DynamicFormControlEvent> = new EventEmitter<DynamicFormControlEvent>();
@Output('dfFocus') focus: EventEmitter<DynamicFormControlEvent> = new EventEmitter<DynamicFormControlEvent>();
@Output('ngbEvent') customEvent: EventEmitter<DynamicFormControlEvent> = new EventEmitter<DynamicFormControlEvent>();
/* tslint:enable:no-output-rename */
@ViewChild('componentViewContainer', { read: ViewContainerRef, static: true }) componentViewContainerRef: ViewContainerRef;
private showErrorMessagesPreviousStage: boolean;
get componentType(): Type<DynamicFormControl> | null {
return dsDynamicFormControlMapFn(this.model);
}
constructor(
protected componentFactoryResolver: ComponentFactoryResolver,
protected dynamicFormComponentService: DynamicFormComponentService,
protected layoutService: DynamicFormLayoutService,
protected validationService: DynamicFormValidationService,
protected translateService: TranslateService,
protected relationService: DynamicFormRelationService,
private modalService: NgbModal,
private relationshipService: RelationshipService,
private selectableListService: SelectableListService,
private itemService: ItemDataService,
private zone: NgZone,
private store: Store<AppState>,
private submissionObjectService: SubmissionObjectDataService,
private ref: ChangeDetectorRef,
private formService: FormService,
private formBuilderService: FormBuilderService,
private submissionService: SubmissionService
) {
super(ref, componentFactoryResolver, layoutService, validationService, dynamicFormComponentService, relationService);
}
/**
* Sets up the necessary variables for when this control can be used to add relationships to the submitted item
*/
ngOnInit(): void {
this.isRelationship = hasValue(this.model.relationship);
const isWrapperAroundRelationshipList = hasValue(this.model.relationshipConfig);
if (this.isRelationship || isWrapperAroundRelationshipList) {
const config = this.model.relationshipConfig || this.model.relationship;
const relationshipOptions = Object.assign(new RelationshipOptions(), config);
this.listId = `list-${this.model.submissionId}-${relationshipOptions.relationshipType}`;
this.setItem();
if (isWrapperAroundRelationshipList || !this.model.repeatable) {
const subscription = this.selectableListService.getSelectableList(this.listId).pipe(
find((list: SelectableListState) => hasNoValue(list)),
switchMap(() => this.item$.pipe(take(1))),
switchMap((item) => {
const relationshipsRD$ = this.relationshipService.getItemRelationshipsByLabel(item,
relationshipOptions.relationshipType,
undefined,
true,
true,
followLink('leftItem'),
followLink('rightItem'),
followLink('relationshipType')
);
relationshipsRD$.pipe(
getFirstSucceededRemoteDataPayload(),
getPaginatedListPayload()
).subscribe((relationships: Relationship[]) => {
// set initial namevariants for pre-existing relationships
relationships.forEach((relationship: Relationship) => {
const relationshipMD: MetadataValue = item.firstMetadata(relationshipOptions.metadataField, { authority: `${VIRTUAL_METADATA_PREFIX}${relationship.id}` });
const nameVariantMD: MetadataValue = item.firstMetadata(this.model.metadataFields, { authority: `${VIRTUAL_METADATA_PREFIX}${relationship.id}` });
if (hasValue(relationshipMD) && isNotEmpty(relationshipMD.value) && hasValue(nameVariantMD) && isNotEmpty(nameVariantMD.value)) {
this.relationshipService.setNameVariant(this.listId, relationshipMD.value, nameVariantMD.value);
}
});
});
return relationshipsRD$.pipe(
paginatedRelationsToItems(item.uuid),
getFirstSucceededRemoteData(),
map((items: RemoteData<PaginatedList<Item>>) => items.payload.page.map((i) => Object.assign(new ItemSearchResult(), { indexableObject: i }))),
);
})
).subscribe((relatedItems: SearchResult<Item>[]) => this.selectableListService.select(this.listId, relatedItems));
this.subs.push(subscription);
}
if (hasValue(this.model.metadataValue)) {
this.value = Object.assign(new FormFieldMetadataValueObject(), this.model.metadataValue);
} else {
this.value = Object.assign(new FormFieldMetadataValueObject(), this.model.value);
}
if (hasValue(this.value) && this.value.isVirtual) {
const relationship$ = this.relationshipService.findById(this.value.virtualValue, true, true, followLink('leftItem'), followLink('rightItem'), followLink('relationshipType'))
.pipe(
getAllSucceededRemoteData(),
getRemoteDataPayload());
this.relationshipValue$ = observableCombineLatest([this.item$.pipe(take(1)), relationship$]).pipe(
switchMap(([item, relationship]: [Item, Relationship]) =>
relationship.leftItem.pipe(
getAllSucceededRemoteData(),
getRemoteDataPayload(),
map((leftItem: Item) => {
return new ReorderableRelationship(relationship, leftItem.uuid !== item.uuid, this.relationshipService, this.store, this.model.submissionId);
}),
)
),
startWith(undefined)
);
}
}
}
get isCheckbox(): boolean {
return this.model.type === DYNAMIC_FORM_CONTROL_TYPE_CHECKBOX || this.model.type === DYNAMIC_FORM_CONTROL_TYPE_CUSTOM_SWITCH;
}
ngOnChanges(changes: SimpleChanges) {
if (changes && !this.isRelationship && hasValue(this.group.get(this.model.id))) {
super.ngOnChanges(changes);
if (this.model && this.model.placeholder) {
this.model.placeholder = this.translateService.instant(this.model.placeholder);
}
}
}
ngDoCheck() {
if (isNotUndefined(this.showErrorMessagesPreviousStage) && this.showErrorMessagesPreviousStage !== this.showErrorMessages) {
this.showErrorMessagesPreviousStage = this.showErrorMessages;
this.forceShowErrorDetection();
}
}
ngAfterViewInit() {
this.showErrorMessagesPreviousStage = this.showErrorMessages;
}
/**
* Since Form Control Components created dynamically have 'OnPush' change detection strategy,
* changes are not propagated. So use this method to force an update
*/
protected forceShowErrorDetection() {
if (this.showErrorMessages) {
this.destroyFormControlComponent();
this.createFormControlComponent();
}
}
onChangeLanguage(event) {
if (isNotEmpty((this.model as any).value)) {
this.onChange(event);
}
}
hasRelationship() {
return isNotEmpty(this.model) && this.model.hasOwnProperty('relationship') && isNotEmpty(this.model.relationship);
}
isVirtual() {
const value: FormFieldMetadataValueObject = this.model.metadataValue;
return isNotEmpty(value) && value.isVirtual;
}
public hasResultsSelected(): Observable<boolean> {
return this.model.value.pipe(map((list: SearchResult<DSpaceObject>[]) => isNotEmpty(list)));
}
/**
* Open a modal where the user can select relationships to be added to item being submitted
*/
openLookup() {
this.modalRef = this.modalService.open(DsDynamicLookupRelationModalComponent, {
size: 'lg'
});
if (hasValue(this.model.value)) {
this.focus.emit({
$event: new Event('focus'),
context: this.context,
control: this.control,
model: this.model,
type: DynamicFormControlEventType.Focus
} as DynamicFormControlEvent);
this.change.emit({
$event: new Event('change'),
context: this.context,
control: this.control,
model: this.model,
type: DynamicFormControlEventType.Change
} as DynamicFormControlEvent);
}
this.submissionService.dispatchSave(this.model.submissionId);
const modalComp = this.modalRef.componentInstance;
if (hasValue(this.model.value) && !this.model.readOnly) {
if (typeof this.model.value === 'string') {
modalComp.query = this.model.value;
} else if (typeof this.model.value.value === 'string') {
modalComp.query = this.model.value.value;
}
}
modalComp.repeatable = this.model.repeatable;
modalComp.listId = this.listId;
modalComp.relationshipOptions = this.model.relationship;
modalComp.label = this.model.relationship.relationshipType;
modalComp.metadataFields = this.model.metadataFields;
modalComp.item = this.item;
modalComp.collection = this.collection;
modalComp.submissionId = this.model.submissionId;
}
/**
* Callback for the remove event,
* remove the current control from its array
*/
onRemove(): void {
const arrayContext: DynamicFormArrayModel = (this.context as DynamicFormArrayGroupModel).context;
const path = this.formBuilderService.getPath(arrayContext);
const formArrayControl = this.group.root.get(path) as FormArray;
this.formBuilderService.removeFormArrayGroup(this.context.index, formArrayControl, arrayContext);
if (this.model.parent.context.groups.length === 0) {
this.formBuilderService.addFormArrayGroup(formArrayControl, arrayContext);
}
}
/**
* Unsubscribe from all subscriptions
*/
ngOnDestroy(): void {
this.subs
.filter((sub) => hasValue(sub))
.forEach((sub) => sub.unsubscribe());
}
get hasHint(): boolean {
return isNotEmpty(this.model.hint) && this.model.hint !== ' ';
}
/**
* Initialize this.item$ based on this.model.submissionId
*/
private setItem() {
const submissionObject$ = this.submissionObjectService
.findById(this.model.submissionId, true, true, followLink('item'), followLink('collection')).pipe(
getAllSucceededRemoteData(),
getRemoteDataPayload()
);
this.item$ = submissionObject$.pipe(switchMap((submissionObject: SubmissionObject) => (submissionObject.item as Observable<RemoteData<Item>>).pipe(getAllSucceededRemoteData(), getRemoteDataPayload())));
const collection$ = submissionObject$.pipe(switchMap((submissionObject: SubmissionObject) => (submissionObject.collection as Observable<RemoteData<Collection>>).pipe(getAllSucceededRemoteData(), getRemoteDataPayload())));
this.subs.push(this.item$.subscribe((item) => this.item = item));
this.subs.push(collection$.subscribe((collection) => this.collection = collection));
}
}