-
Notifications
You must be signed in to change notification settings - Fork 538
Expand file tree
/
Copy pathsection-upload-file.component.ts
More file actions
342 lines (304 loc) · 10.2 KB
/
section-upload-file.component.ts
File metadata and controls
342 lines (304 loc) · 10.2 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
import { AsyncPipe } from '@angular/common';
import {
Component,
Input,
OnChanges,
OnDestroy,
OnInit,
SimpleChanges,
ViewChild,
} from '@angular/core';
import { SubmissionFormsModel } from '@dspace/core/config/models/config-submission-forms.model';
import { JsonPatchOperationPathCombiner } from '@dspace/core/json-patch/builder/json-patch-operation-path-combiner';
import { JsonPatchOperationsBuilder } from '@dspace/core/json-patch/builder/json-patch-operations-builder';
import { Bitstream } from '@dspace/core/shared/bitstream.model';
import { WorkspaceitemSectionUploadFileObject } from '@dspace/core/submission/models/workspaceitem-section-upload-file.model';
import { SubmissionJsonPatchOperationsService } from '@dspace/core/submission/submission-json-patch-operations.service';
import {
hasValue,
isNotUndefined,
} from '@dspace/shared/utils/empty.util';
import {
NgbModal,
NgbModalOptions,
} from '@ng-bootstrap/ng-bootstrap';
import { DynamicFormControlModel } from '@ng-dynamic-forms/core';
import { TranslateModule } from '@ngx-translate/core';
import {
BehaviorSubject,
Observable,
Subscription,
} from 'rxjs';
import { filter } from 'rxjs/operators';
import { BtnDisabledDirective } from '../../../../shared/btn-disabled.directive';
import { ThemedFileDownloadLinkComponent } from '../../../../shared/file-download-link/themed-file-download-link.component';
import { FormService } from '../../../../shared/form/form.service';
import { SubmissionService } from '../../../submission.service';
import { SectionUploadService } from '../section-upload.service';
import { SubmissionSectionUploadFileEditComponent } from './edit/section-upload-file-edit.component';
import { SubmissionSectionUploadFileViewComponent } from './view/section-upload-file-view.component';
/**
* This component represents a single bitstream contained in the submission
*/
@Component({
selector: 'ds-base-submission-upload-section-file',
styleUrls: ['./section-upload-file.component.scss'],
templateUrl: './section-upload-file.component.html',
imports: [
AsyncPipe,
BtnDisabledDirective,
SubmissionSectionUploadFileViewComponent,
ThemedFileDownloadLinkComponent,
TranslateModule,
],
})
export class SubmissionSectionUploadFileComponent implements OnChanges, OnInit, OnDestroy {
/**
* The indicator is the primary bitstream
* it will be null if no primary bitstream is set for the ORIGINAL bundle
* @type {boolean, null}
*/
@Input() isPrimary: boolean | null;
/**
* The list of available access condition
* @type {Array}
*/
@Input() availableAccessConditionOptions: any[];
/**
* The submission id
* @type {string}
*/
@Input() collectionId: string;
/**
* Define if collection access conditions policy type :
* POLICY_DEFAULT_NO_LIST : is not possible to define additional access group/s for the single file
* POLICY_DEFAULT_WITH_LIST : is possible to define additional access group/s for the single file
* @type {number}
*/
@Input() collectionPolicyType: number;
/**
* The configuration for the bitstream's metadata form
* @type {SubmissionFormsModel}
*/
@Input() configMetadataForm: SubmissionFormsModel;
/**
* The bitstream id
* @type {string}
*/
@Input() fileId: string;
/**
* The bitstream array key
* @type {string}
*/
@Input() fileIndex: string;
/**
* The bitstream id
* @type {string}
*/
@Input() fileName: string;
/**
* The section id
* @type {string}
*/
@Input() sectionId: string;
/**
* The submission id
* @type {string}
*/
@Input() submissionId: string;
/**
* The [[SubmissionSectionUploadFileEditComponent]] reference
* @type {SubmissionSectionUploadFileEditComponent}
*/
@ViewChild(SubmissionSectionUploadFileEditComponent) fileEditComp: SubmissionSectionUploadFileEditComponent;
/**
* A boolean representing if a submission save operation is pending
* @type {Observable<boolean>}
*/
public processingSaveStatus$: Observable<boolean>;
/**
* The bitstream's metadata data
* @type {WorkspaceitemSectionUploadFileObject}
*/
public fileData: WorkspaceitemSectionUploadFileObject;
/**
* The form id
* @type {string}
*/
public formId: string;
/**
* A boolean representing if to show bitstream edit form
* @type {boolean}
*/
public readMode: boolean;
/**
* The form model
* @type {DynamicFormControlModel[]}
*/
public formModel: DynamicFormControlModel[];
/**
* A boolean representing if a submission delete operation is pending
* @type {BehaviorSubject<boolean>}
*/
public processingDelete$ = new BehaviorSubject<boolean>(false);
/**
* The [JsonPatchOperationPathCombiner] object
* @type {JsonPatchOperationPathCombiner}
*/
protected pathCombiner: JsonPatchOperationPathCombiner;
/**
* The [JsonPatchOperationPathCombiner] object
* @type {JsonPatchOperationPathCombiner}
*/
protected primaryBitstreamPathCombiner: JsonPatchOperationPathCombiner;
/**
* Array to track all subscriptions and unsubscribe them onDestroy
* @type {Array}
*/
protected subscriptions: Subscription[] = [];
/**
* Array containing all the form metadata defined in configMetadataForm
* @type {Array}
*/
protected formMetadata: string[] = [];
/**
* Initialize instance variables
*
* @param {ChangeDetectorRef} cdr
* @param {FormService} formService
* @param {HALEndpointService} halService
* @param {NgbModal} modalService
* @param {JsonPatchOperationsBuilder} operationsBuilder
* @param {SubmissionJsonPatchOperationsService} operationsService
* @param {SubmissionService} submissionService
* @param {SectionUploadService} uploadService
*/
constructor(
private formService: FormService,
private modalService: NgbModal,
private operationsBuilder: JsonPatchOperationsBuilder,
private operationsService: SubmissionJsonPatchOperationsService,
private submissionService: SubmissionService,
private uploadService: SectionUploadService,
) {
this.readMode = true;
}
/**
* Retrieve bitstream's metadata
*/
ngOnChanges(changes: SimpleChanges): void {
if (this.availableAccessConditionOptions) {
// Retrieve file state
this.subscriptions.push(
this.uploadService
.getFileData(this.submissionId, this.sectionId, this.fileId)
.pipe(filter((bitstream) => isNotUndefined(bitstream)))
.subscribe((bitstream) => {
this.fileData = bitstream;
},
),
);
}
}
/**
* Initialize instance variables
*/
ngOnInit() {
this.formId = this.formService.getUniqueId(this.fileId);
this.processingSaveStatus$ = this.submissionService.getSubmissionSaveProcessingStatus(this.submissionId);
this.pathCombiner = new JsonPatchOperationPathCombiner('sections', this.sectionId);
this.loadFormMetadata();
}
/**
* Show confirmation dialog for delete
*/
public confirmDelete(content) {
this.modalService.open(content).result.then(
(result) => {
if (result === 'ok') {
this.processingDelete$.next(true);
this.deleteFile();
}
},
);
}
/**
* Build a Bitstream object by the current file uuid
*
* @return Bitstream object
*/
public getBitstream(): Bitstream {
return Object.assign(new Bitstream(), {
uuid: this.fileData.uuid,
_links: {
self: { href: this.fileData.url },
content: { href: this.fileData.url },
bundle: { href: '' },
format: { href: '' },
thumbnail: { href: this.fileData.thumbnail || '' },
accessStatus: { href: '' },
},
});
}
editBitstreamData() {
const options: NgbModalOptions = {
size: 'xl',
backdrop: 'static',
};
const activeModal = this.modalService.open(SubmissionSectionUploadFileEditComponent, options);
activeModal.componentInstance.availableAccessConditionOptions = this.availableAccessConditionOptions;
activeModal.componentInstance.collectionId = this.collectionId;
activeModal.componentInstance.collectionPolicyType = this.collectionPolicyType;
activeModal.componentInstance.configMetadataForm = this.configMetadataForm;
activeModal.componentInstance.fileData = this.fileData;
activeModal.componentInstance.fileId = this.fileId;
activeModal.componentInstance.fileIndex = this.fileIndex;
activeModal.componentInstance.formId = this.formId;
activeModal.componentInstance.sectionId = this.sectionId;
activeModal.componentInstance.formMetadata = this.formMetadata;
activeModal.componentInstance.pathCombiner = this.pathCombiner;
activeModal.componentInstance.submissionId = this.submissionId;
activeModal.componentInstance.isPrimary = this.isPrimary;
}
togglePrimaryBitstream(event) {
this.uploadService.updatePrimaryBitstreamOperation(this.pathCombiner.getPath('primary'), this.isPrimary, event.target.checked, this.fileId);
this.submissionService.dispatchSaveSection(this.submissionId, this.sectionId);
}
ngOnDestroy(): void {
this.unsubscribeAll();
}
unsubscribeAll() {
this.subscriptions.filter((sub) => hasValue(sub)).forEach((sub) => sub.unsubscribe());
}
protected loadFormMetadata() {
this.configMetadataForm.rows.forEach((row) => {
row.fields.forEach((field) => {
field.selectableMetadata.forEach((metadatum) => {
this.formMetadata.push(metadatum.metadata);
});
});
},
);
}
/**
* Delete bitstream from submission
*/
protected deleteFile() {
this.operationsBuilder.remove(this.pathCombiner.getPath(['files', this.fileIndex]));
if (this.isPrimary) {
this.operationsBuilder.remove(this.pathCombiner.getPath('primary'));
}
this.subscriptions.push(this.operationsService.jsonPatchByResourceID(
this.submissionService.getSubmissionObjectLinkName(),
this.submissionId,
this.pathCombiner.rootElement,
this.pathCombiner.subRootElement)
.subscribe(() => {
if (this.isPrimary) {
this.uploadService.updateFilePrimaryBitstream(this.submissionId, this.sectionId, null);
}
this.uploadService.removeUploadedFile(this.submissionId, this.sectionId, this.fileId);
this.processingDelete$.next(false);
}));
}
}