forked from DSpace/dspace-angular
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcollection-source.component.ts
More file actions
532 lines (492 loc) · 16.1 KB
/
Copy pathcollection-source.component.ts
File metadata and controls
532 lines (492 loc) · 16.1 KB
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
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
import {
AsyncPipe,
Location,
NgClass,
} from '@angular/common';
import {
Component,
OnDestroy,
OnInit,
} from '@angular/core';
import { UntypedFormGroup } from '@angular/forms';
import {
ActivatedRoute,
Router,
} from '@angular/router';
import {
DynamicFormControlModel,
DynamicFormGroupModel,
DynamicFormLayout,
DynamicFormService,
DynamicInputModel,
DynamicOptionControlModel,
DynamicRadioGroupModel,
DynamicSelectModel,
} from '@ng-dynamic-forms/core';
import {
TranslateModule,
TranslateService,
} from '@ngx-translate/core';
import cloneDeep from 'lodash/cloneDeep';
import {
Observable,
Subscription,
} from 'rxjs';
import {
first,
map,
switchMap,
take,
} from 'rxjs/operators';
import { environment } from '../../../../environments/environment';
import { CollectionDataService } from '../../../core/data/collection-data.service';
import { FieldUpdate } from '../../../core/data/object-updates/field-update.model';
import { FieldUpdates } from '../../../core/data/object-updates/field-updates.model';
import { ObjectUpdatesService } from '../../../core/data/object-updates/object-updates.service';
import { RemoteData } from '../../../core/data/remote-data';
import { RequestService } from '../../../core/data/request.service';
import { Collection } from '../../../core/shared/collection.model';
import {
ContentSource,
ContentSourceHarvestType,
} from '../../../core/shared/content-source.model';
import { MetadataConfig } from '../../../core/shared/metadata-config.model';
import {
getFirstCompletedRemoteData,
getFirstSucceededRemoteData,
} from '../../../core/shared/operators';
import { BtnDisabledDirective } from '../../../shared/btn-disabled.directive';
import {
hasNoValue,
hasValue,
isNotEmpty,
} from '../../../shared/empty.util';
import { FormComponent } from '../../../shared/form/form.component';
import { ThemedLoadingComponent } from '../../../shared/loading/themed-loading.component';
import { INotification } from '../../../shared/notifications/models/notification.model';
import { NotificationsService } from '../../../shared/notifications/notifications.service';
import { AbstractTrackableComponent } from '../../../shared/trackable/abstract-trackable.component';
import { CollectionSourceControlsComponent } from './collection-source-controls/collection-source-controls.component';
/**
* Component for managing the content source of the collection
*/
@Component({
selector: 'ds-collection-source',
templateUrl: './collection-source.component.html',
imports: [
AsyncPipe,
BtnDisabledDirective,
CollectionSourceControlsComponent,
FormComponent,
NgClass,
ThemedLoadingComponent,
TranslateModule,
],
standalone: true,
})
export class CollectionSourceComponent extends AbstractTrackableComponent implements OnInit, OnDestroy {
/**
* The current collection's remote data
*/
collectionRD$: Observable<RemoteData<Collection>>;
/**
* The collection's content source
*/
contentSource: ContentSource;
/**
* The current update to the content source
*/
update$: Observable<FieldUpdate>;
/**
* The initial harvest type we started off with
* Used to compare changes
*/
initialHarvestType: ContentSourceHarvestType;
/**
* @type {string} Key prefix used to generate form labels
*/
LABEL_KEY_PREFIX = 'collection.edit.tabs.source.form.';
/**
* @type {string} Key prefix used to generate form error messages
*/
ERROR_KEY_PREFIX = 'collection.edit.tabs.source.form.errors.';
/**
* @type {string} Key prefix used to generate form option labels
*/
OPTIONS_KEY_PREFIX = 'collection.edit.tabs.source.form.options.';
/**
* The Dynamic Input Model for the OAI Provider
*/
oaiSourceModel = new DynamicInputModel({
id: 'oaiSource',
name: 'oaiSource',
required: true,
validators: {
required: null,
},
errorMessages: {
required: 'You must provide a set id of the target collection.',
},
});
/**
* The Dynamic Input Model for the OAI Set
*/
oaiSetIdModel = new DynamicInputModel({
id: 'oaiSetId',
name: 'oaiSetId',
});
/**
* The Dynamic Input Model for the Metadata Format used
*/
metadataConfigIdModel = new DynamicSelectModel({
id: 'metadataConfigId',
name: 'metadataConfigId',
});
/**
* The Dynamic Input Model for the type of harvesting
*/
harvestTypeModel = new DynamicRadioGroupModel<string>({
id: 'harvestType',
name: 'harvestType',
options: [
{
value: ContentSourceHarvestType.Metadata,
},
{
value: ContentSourceHarvestType.MetadataAndRef,
},
{
value: ContentSourceHarvestType.MetadataAndBitstreams,
},
],
});
/**
* All input models in a simple array for easier iterations
*/
inputModels = [this.oaiSourceModel, this.oaiSetIdModel, this.metadataConfigIdModel, this.harvestTypeModel];
/**
* The dynamic form fields used for editing the content source of a collection
* @type {(DynamicInputModel | DynamicTextAreaModel)[]}
*/
formModel: DynamicFormControlModel[] = [
new DynamicFormGroupModel({
id: 'oaiSourceContainer',
group: [
this.oaiSourceModel,
],
}),
new DynamicFormGroupModel({
id: 'oaiSetContainer',
group: [
this.oaiSetIdModel,
this.metadataConfigIdModel,
],
}),
new DynamicFormGroupModel({
id: 'harvestTypeContainer',
group: [
this.harvestTypeModel,
],
}),
];
/**
* Layout used for structuring the form inputs
*/
formLayout: DynamicFormLayout = {
oaiSource: {
grid: {
host: 'col-12 d-inline-block',
},
},
oaiSetId: {
grid: {
host: 'col col-sm-6 d-inline-block',
},
},
metadataConfigId: {
grid: {
host: 'col col-sm-6 d-inline-block',
},
},
harvestType: {
grid: {
host: 'col-12',
option: 'btn-outline-secondary',
},
},
oaiSetContainer: {
grid: {
host: 'row',
},
},
oaiSourceContainer: {
grid: {
host: 'row',
},
},
harvestTypeContainer: {
grid: {
host: 'row',
},
},
};
/**
* The form group of this form
*/
formGroup: UntypedFormGroup;
/**
* The content harvesting type used when harvesting is disabled
*/
harvestTypeNone = ContentSourceHarvestType.None;
/**
* The content harvesting type that downloads bitstreams, the only one the external URL flag applies to
*/
harvestTypeMetadataAndBitstreams = ContentSourceHarvestType.MetadataAndBitstreams;
/**
* The previously selected harvesting type
* Used for switching between ContentSourceHarvestType.None and the previously selected value when enabling / disabling harvesting
* Defaults to ContentSourceHarvestType.Metadata
*/
previouslySelectedHarvestType = ContentSourceHarvestType.Metadata;
/**
* Notifications displayed after clicking submit
* These are cleaned up every time a user submits the form to prevent error or other notifications from staying active
* while they shouldn't be.
*/
displayedNotifications: INotification[] = [];
subs: Subscription[] = [];
public constructor(
public objectUpdatesService: ObjectUpdatesService,
public notificationsService: NotificationsService,
public translateService: TranslateService,
public router: Router,
protected location: Location,
protected formService: DynamicFormService,
protected route: ActivatedRoute,
protected collectionService: CollectionDataService,
protected requestService: RequestService,
) {
super(objectUpdatesService, notificationsService, translateService, router);
}
/**
* Initialize properties to setup the Field Update and Form
*/
ngOnInit(): void {
super.ngOnInit();
this.notificationsPrefix = 'collection.edit.tabs.source.notifications.';
this.discardTimeOut = environment.collection.edit.undoTimeout;
this.formGroup = this.formService.createFormGroup(this.formModel);
this.collectionRD$ = this.route.parent.data.pipe(first(), map((data) => data.dso));
this.collectionRD$.pipe(
getFirstSucceededRemoteData(),
map((col) => col.payload.uuid),
switchMap((uuid) => this.collectionService.getContentSource(uuid)),
getFirstCompletedRemoteData(),
).subscribe((rd: RemoteData<ContentSource>) => {
this.initializeOriginalContentSource(rd.payload);
});
this.updateFieldTranslations();
this.subs.push(this.translateService.onLangChange.subscribe(() => {
this.updateFieldTranslations();
}));
}
/**
* Initialize the Field Update and subscribe on it to fire updates to the form whenever it changes
*/
initializeOriginalContentSource(contentSource: ContentSource) {
this.contentSource = contentSource;
this.initialHarvestType = contentSource.harvestType;
this.initializeMetadataConfigs();
const initialContentSource = cloneDeep(this.contentSource);
this.objectUpdatesService.initialize(this.url, [initialContentSource], new Date());
this.update$ = this.objectUpdatesService.getFieldUpdates(this.url, [initialContentSource]).pipe(
map((updates: FieldUpdates) => updates[initialContentSource.uuid]),
);
this.subs.push(this.update$.subscribe((update: FieldUpdate) => {
if (update) {
const field = update.field as ContentSource;
let configId;
if (hasValue(this.contentSource) && isNotEmpty(this.contentSource.metadataConfigs)) {
configId = this.contentSource.metadataConfigs[0].id;
}
if (hasValue(field) && hasValue(field.metadataConfigId)) {
configId = field.metadataConfigId;
}
if (hasValue(field)) {
this.formGroup.patchValue({
oaiSourceContainer: {
oaiSource: field.oaiSource,
},
oaiSetContainer: {
oaiSetId: field.oaiSetId,
metadataConfigId: configId,
},
harvestTypeContainer: {
harvestType: field.harvestType,
},
});
this.contentSource = cloneDeep(field);
}
this.contentSource.metadataConfigId = configId;
}
}));
}
/**
* Fill the metadataConfigIdModel's options using the contentSource's metadataConfigs property
*/
initializeMetadataConfigs() {
this.metadataConfigIdModel.options = this.contentSource.metadataConfigs
.map((metadataConfig: MetadataConfig) => Object.assign({ value: metadataConfig.id, label: metadataConfig.label }));
if (this.metadataConfigIdModel.options.length > 0) {
this.formGroup.patchValue({
oaiSetContainer: {
metadataConfigId: this.metadataConfigIdModel.options[0].value,
},
});
}
}
/**
* Used the update translations of errors and labels on init and on language change
*/
private updateFieldTranslations() {
this.inputModels.forEach(
(fieldModel: DynamicFormControlModel) => {
this.updateFieldTranslation(fieldModel);
},
);
}
/**
* Update the translations of a DynamicInputModel
* @param fieldModel
*/
private updateFieldTranslation(fieldModel: DynamicFormControlModel) {
fieldModel.label = this.translateService.instant(this.LABEL_KEY_PREFIX + fieldModel.id);
if (isNotEmpty(fieldModel.validators)) {
fieldModel.errorMessages = {};
Object.keys(fieldModel.validators).forEach((key) => {
fieldModel.errorMessages[key] = this.translateService.instant(this.ERROR_KEY_PREFIX + fieldModel.id + '.' + key);
});
}
if (fieldModel instanceof DynamicOptionControlModel) {
if (isNotEmpty(fieldModel.options)) {
fieldModel.options.forEach((option) => {
if (hasNoValue(option.label)) {
option.label = this.translateService.instant(this.OPTIONS_KEY_PREFIX + fieldModel.id + '.' + option.value);
}
});
}
}
}
/**
* Fired whenever the form receives an update and makes sure the Content Source and field update is up-to-date with the changes
* @param event
*/
onChange(event) {
this.updateContentSourceField(event.model, true);
this.saveFieldUpdate();
}
/**
* Submit the edited Content Source to the REST API, re-initialize the field update and display a notification
*/
onSubmit() {
// Remove cached harvester request to allow for latest harvester to be displayed when switching tabs
this.collectionRD$.pipe(
getFirstSucceededRemoteData(),
map((col) => col.payload.uuid),
switchMap((uuid) => this.collectionService.getHarvesterEndpoint(uuid)),
take(1),
).subscribe((endpoint) => this.requestService.removeByHrefSubstring(endpoint));
this.requestService.setStaleByHrefSubstring(this.contentSource._links.self.href);
// Update harvester
this.collectionRD$.pipe(
getFirstSucceededRemoteData(),
map((col) => col.payload.uuid),
switchMap((uuid) => this.collectionService.updateContentSource(uuid, this.contentSource)),
take(1),
).subscribe((result: ContentSource | INotification) => {
if (hasValue((result as any).harvestType)) {
this.clearNotifications();
this.initializeOriginalContentSource(result as ContentSource);
this.displayedNotifications.push(this.notificationsService.success(this.getNotificationTitle('saved'), this.getNotificationContent('saved')));
} else {
this.displayedNotifications.push(result as INotification);
}
});
}
/**
* Cancel the edit and return to the previous page
*/
onCancel() {
this.location.back();
}
/**
* Is the current form valid to be submitted ?
*/
isValid(): boolean {
return (this.contentSource.harvestType === ContentSourceHarvestType.None) || this.formGroup.valid;
}
/**
* Switch the external source on or off and fire a field update
*/
changeExternalSource() {
if (this.contentSource.harvestType === ContentSourceHarvestType.None) {
this.contentSource.harvestType = this.previouslySelectedHarvestType;
} else {
this.previouslySelectedHarvestType = this.contentSource.harvestType;
this.contentSource.harvestType = ContentSourceHarvestType.None;
}
this.updateContentSource(false);
}
/**
* Switch the allowExternalUrls flag on or off and fire a field update
* Deliberately not a dynamic form control: the patchValue() in initializeOriginalContentSource() only names the
* three form containers, so a control here would not revert on Discard/Reinstate
*/
changeAllowExternalUrls() {
this.contentSource.allowExternalUrls = !this.contentSource.allowExternalUrls;
this.saveFieldUpdate();
}
/**
* Loop over all inputs and update the Content Source with their value
* @param updateHarvestType When set to false, the harvestType of the contentSource will be ignored in the update
*/
updateContentSource(updateHarvestType: boolean) {
this.inputModels.forEach(
(fieldModel: DynamicInputModel) => {
this.updateContentSourceField(fieldModel, updateHarvestType);
},
);
this.saveFieldUpdate();
}
/**
* Update the Content Source with the value from a DynamicInputModel
* @param fieldModel The fieldModel to fetch the value from and update the contentSource with
* @param updateHarvestType When set to false, the harvestType of the contentSource will be ignored in the update
*/
updateContentSourceField(fieldModel: DynamicInputModel, updateHarvestType: boolean) {
if (hasValue(fieldModel.value) && !(fieldModel.id === this.harvestTypeModel.id && !updateHarvestType)) {
this.contentSource[fieldModel.id] = fieldModel.value;
}
}
/**
* Save the current Content Source to the Object Updates cache
*/
saveFieldUpdate() {
this.objectUpdatesService.saveAddFieldUpdate(this.url, cloneDeep(this.contentSource));
}
/**
* Clear possible active notifications
*/
clearNotifications() {
this.displayedNotifications.forEach((notification: INotification) => {
this.notificationsService.remove(notification);
});
this.displayedNotifications = [];
}
/**
* Make sure open subscriptions are closed
*/
ngOnDestroy(): void {
this.subs.forEach((sub: Subscription) => sub.unsubscribe());
}
}