forked from DSpace/dspace-angular
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdso-edit-metadata-authority-field.component.ts
More file actions
352 lines (323 loc) · 12.3 KB
/
Copy pathdso-edit-metadata-authority-field.component.ts
File metadata and controls
352 lines (323 loc) · 12.3 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
import {
AsyncPipe,
NgClass,
} from '@angular/common';
import {
ChangeDetectorRef,
Component,
OnChanges,
OnInit,
SimpleChanges,
} from '@angular/core';
import {
FormsModule,
UntypedFormControl,
UntypedFormGroup,
} from '@angular/forms';
import { NgbTooltipModule } from '@ng-bootstrap/ng-bootstrap';
import {
TranslateModule,
TranslateService,
} from '@ngx-translate/core';
import {
BehaviorSubject,
Observable,
of,
} from 'rxjs';
import {
map,
switchMap,
take,
tap,
} from 'rxjs/operators';
import { ItemDataService } from '../../../../core/data/item-data.service';
import { RegistryService } from '../../../../core/registry/registry.service';
import { ConfidenceType } from '../../../../core/shared/confidence-type';
import {
getFirstCompletedRemoteData,
metadataFieldsToString,
} from '../../../../core/shared/operators';
import { Vocabulary } from '../../../../core/submission/vocabularies/models/vocabulary.model';
import { VocabularyOptions } from '../../../../core/submission/vocabularies/models/vocabulary-options.model';
import { isNotEmpty } from '../../../../shared/empty.util';
import { DsDynamicOneboxComponent } from '../../../../shared/form/builder/ds-dynamic-form-ui/models/onebox/dynamic-onebox.component';
import {
DsDynamicOneboxModelConfig,
DynamicOneboxModel,
} from '../../../../shared/form/builder/ds-dynamic-form-ui/models/onebox/dynamic-onebox.model';
import { DsDynamicScrollableDropdownComponent } from '../../../../shared/form/builder/ds-dynamic-form-ui/models/scrollable-dropdown/dynamic-scrollable-dropdown.component';
import {
DynamicScrollableDropdownModel,
DynamicScrollableDropdownModelConfig,
} from '../../../../shared/form/builder/ds-dynamic-form-ui/models/scrollable-dropdown/dynamic-scrollable-dropdown.model';
import { FormFieldMetadataValueObject } from '../../../../shared/form/builder/models/form-field-metadata-value.model';
import { AuthorityConfidenceStateDirective } from '../../../../shared/form/directives/authority-confidence-state.directive';
import { NotificationsService } from '../../../../shared/notifications/notifications.service';
import { DebounceDirective } from '../../../../shared/utils/debounce.directive';
import { followLink } from '../../../../shared/utils/follow-link-config.model';
import { AbstractDsoEditMetadataValueFieldComponent } from '../abstract-dso-edit-metadata-value-field.component';
import { DsoEditMetadataChangeType } from '../../dso-edit-metadata-form';
import { DsoEditMetadataFieldService } from '../dso-edit-metadata-field.service';
/**
* The component used to gather input for authority controlled metadata fields
*/
@Component({
selector: 'ds-dso-edit-metadata-authority-field',
templateUrl: './dso-edit-metadata-authority-field.component.html',
styleUrls: ['./dso-edit-metadata-authority-field.component.scss'],
standalone: true,
imports: [
AsyncPipe,
AuthorityConfidenceStateDirective,
DebounceDirective,
DsDynamicOneboxComponent,
DsDynamicScrollableDropdownComponent,
FormsModule,
NgbTooltipModule,
NgClass,
TranslateModule,
],
})
export class DsoEditMetadataAuthorityFieldComponent extends AbstractDsoEditMetadataValueFieldComponent implements OnInit, OnChanges {
/**
* Whether the authority field is currently being edited
*/
public editingAuthority = false;
/**
* Whether the free-text editing is enabled when scrollable dropdown or hierarchical vocabulary is used
*/
public enabledFreeTextEditing = false;
/**
* Field group used by authority field
*/
group = new UntypedFormGroup({ authorityField: new UntypedFormControl() });
/**
* Model to use for editing authorities values
*/
private model$: BehaviorSubject<DynamicOneboxModel | DynamicScrollableDropdownModel> = new BehaviorSubject(null);
/**
* Observable with information about the authority vocabulary used
*/
private vocabulary$: Observable<Vocabulary>;
/**
* Observables with information about the authority vocabulary type used
*/
isAuthorityControlled$: Observable<boolean>;
isHierarchicalVocabulary$: Observable<boolean>;
isScrollableVocabulary$: Observable<boolean>;
isSuggesterVocabulary$: Observable<boolean>;
constructor(
protected cdr: ChangeDetectorRef,
protected dsoEditMetadataFieldService: DsoEditMetadataFieldService,
protected itemService: ItemDataService,
protected notificationsService: NotificationsService,
protected registryService: RegistryService,
protected translate: TranslateService,
) {
super();
}
private static readonly KNOWN_AUTHORITY_FIELDS = [
'dc.contributor.author',
'dc.creator',
'dc.contributor.editor',
'dc.contributor.advisor',
'dc.contributor.other',
'dcterms.creator',
'dcterms.contributor',
];
ngOnInit(): void {
this.initAuthorityProperties();
}
/**
* Initialise potential properties of a authority controlled metadata field
*/
initAuthorityProperties(): void {
this.vocabulary$ = this.dsoEditMetadataFieldService.findDsoFieldVocabulary(this.dso, this.mdField);
this.isAuthorityControlled$ = this.vocabulary$.pipe(
// Create the model used by the authority fields to ensure its existence when the field is initialized
tap((v: Vocabulary) => this.model$.next(this.createModel(v))),
map((result: Vocabulary) => isNotEmpty(result)),
);
this.isHierarchicalVocabulary$ = this.vocabulary$.pipe(
map((result: Vocabulary) => isNotEmpty(result) && result.hierarchical),
);
this.isScrollableVocabulary$ = this.vocabulary$.pipe(
map((result: Vocabulary) => isNotEmpty(result) && result.scrollable),
);
this.isSuggesterVocabulary$ = this.vocabulary$.pipe(
map((result: Vocabulary) => isNotEmpty(result) && !result.hierarchical && !result.scrollable),
);
}
/**
* Returns a {@link DynamicOneboxModel} or {@link DynamicScrollableDropdownModel} model based on the
* vocabulary used.
*/
private createModel(vocabulary: Vocabulary): DynamicOneboxModel | DynamicScrollableDropdownModel {
if (isNotEmpty(vocabulary)) {
let formFieldValue: FormFieldMetadataValueObject | string;
if (isNotEmpty(this.mdValue.newValue.value)) {
formFieldValue = new FormFieldMetadataValueObject(
this.mdValue.newValue.value,
this.mdValue.newValue.language,
this.mdValue.newValue.authority,
this.mdValue.newValue.value,
0,
this.mdValue.newValue.confidence,
);
} else {
formFieldValue = this.mdValue.newValue.value || '';
}
const vocabularyOptions = vocabulary ? {
closed: false,
name: vocabulary.name,
} as VocabularyOptions : null;
if (!vocabulary.scrollable) {
const model: DsDynamicOneboxModelConfig = {
id: 'authorityField',
label: `${this.dsoType}.edit.metadata.edit.value`,
vocabularyOptions: vocabularyOptions,
metadataFields: [this.mdField],
value: formFieldValue,
repeatable: false,
submissionId: 'edit-metadata',
hasSelectableMetadata: false,
};
return new DynamicOneboxModel(model);
} else {
const model: DynamicScrollableDropdownModelConfig = {
id: 'authorityField',
label: `${this.dsoType}.edit.metadata.edit.value`,
placeholder: `${this.dsoType}.edit.metadata.edit.value`,
vocabularyOptions: vocabularyOptions,
metadataFields: [this.mdField],
value: formFieldValue,
repeatable: false,
submissionId: 'edit-metadata',
hasSelectableMetadata: false,
maxOptions: 10,
};
return new DynamicScrollableDropdownModel(model);
}
} else {
return null;
}
}
/**
* Change callback for the component. Check if the mdField has changed to retrieve whether it is metadata
* that uses a controlled vocabulary and update the related properties
*
* @param {SimpleChanges} changes
*/
ngOnChanges(changes: SimpleChanges): void {
if (isNotEmpty(changes.mdField) && !changes.mdField.firstChange) {
if (isNotEmpty(changes.mdField.currentValue)) {
if (isNotEmpty(changes.mdField.previousValue) &&
changes.mdField.previousValue !== changes.mdField.currentValue) {
// Clear authority value in case it has been assigned with the previous metadataField used
this.mdValue.newValue.authority = null;
this.mdValue.newValue.confidence = null;
}
// Skip validation for known authority fields and directly initialize
if (changes.mdField.currentValue.includes('.')) {
if (DsoEditMetadataAuthorityFieldComponent.KNOWN_AUTHORITY_FIELDS.includes(changes.mdField.currentValue)) {
this.initAuthorityProperties();
this.cdr.detectChanges();
} else {
this.validateMetadataField().subscribe((isValid: boolean) => {
if (isValid) {
this.initAuthorityProperties();
this.cdr.detectChanges();
}
});
}
}
}
}
}
/**
* Validate the metadata field to check if it exists on the server and return an observable boolean for success/error
*/
validateMetadataField(): Observable<boolean> {
return this.registryService.queryMetadataFields(this.mdField, null, true, false, followLink('schema')).pipe(
getFirstCompletedRemoteData(),
switchMap((rd) => {
if (rd.hasSucceeded) {
return of(rd).pipe(
metadataFieldsToString(),
take(1),
map((fields: string[]) => fields.indexOf(this.mdField) > -1),
);
} else {
this.notificationsService.error(this.translate.instant(`${this.dsoType}.edit.metadata.metadatafield.error`), rd.errorMessage);
return [false];
}
}),
);
}
/**
* Process the change of authority field value updating the authority key and confidence as necessary
*/
onChangeAuthorityField(event): void {
if (event) {
this.mdValue.newValue.value = event.value;
if (event.authority) {
this.mdValue.newValue.authority = event.authority;
this.mdValue.newValue.confidence = ConfidenceType.CF_ACCEPTED;
} else {
this.mdValue.newValue.authority = null;
this.mdValue.newValue.confidence = null;
}
this.mdValue.change = DsoEditMetadataChangeType.UPDATE;
this.confirm.emit(false);
} else {
// The event is undefined when the user clears the selection in scrollable dropdown
this.mdValue.newValue.value = '';
this.mdValue.newValue.authority = null;
this.mdValue.newValue.confidence = null;
this.mdValue.change = DsoEditMetadataChangeType.UPDATE;
this.confirm.emit(false);
}
}
/**
* Returns the {@link DynamicOneboxModel} or {@link DynamicScrollableDropdownModel} model used
* for the authority field
*/
getModel(): DynamicOneboxModel | DynamicScrollableDropdownModel {
return this.model$.value;
}
/**
* Change the status of the editingAuthority property
* @param status
*/
onChangeEditingAuthorityStatus(status: boolean) {
this.editingAuthority = status;
}
/**
* Processes the change in authority value, updating the confidence as necessary.
* If the authority key is cleared, the confidence is set to {@link ConfidenceType.CF_NOVALUE}.
* If the authority key is edited and differs from the original, the confidence is set to {@link ConfidenceType.CF_ACCEPTED}.
*/
onChangeAuthorityKey() {
if (this.mdValue.newValue.authority === '') {
this.mdValue.newValue.confidence = ConfidenceType.CF_NOVALUE;
this.mdValue.change = DsoEditMetadataChangeType.UPDATE;
this.confirm.emit(false);
} else if (this.mdValue.newValue.authority !== this.mdValue.originalValue.authority) {
this.mdValue.newValue.confidence = ConfidenceType.CF_ACCEPTED;
this.mdValue.change = DsoEditMetadataChangeType.UPDATE;
this.confirm.emit(false);
}
}
/**
* Toggles the free-text editing mode
*/
toggleFreeTextEdition() {
if (this.enabledFreeTextEditing) {
if (this.getModel().value !== this.mdValue.newValue.value) {
// Reload the model to adapt it to the new possible value modified during free text editing
this.initAuthorityProperties();
}
}
this.enabledFreeTextEditing = !this.enabledFreeTextEditing;
}
}