mirror of
https://github.com/DSpace/dspace-angular.git
synced 2025-10-08 18:44:14 +00:00
Created abstract methods to section model
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import { ChangeDetectorRef, Component, Inject, OnDestroy, ViewChild } from '@angular/core';
|
||||
import { DynamicFormControlEvent, DynamicFormControlModel } from '@ng-dynamic-forms/core';
|
||||
|
||||
import { Observable } from 'rxjs/Observable';
|
||||
import { Subscription } from 'rxjs/Subscription';
|
||||
import { TranslateService } from '@ngx-translate/core';
|
||||
import { isEqual } from 'lodash';
|
||||
@@ -10,7 +11,7 @@ import { FormComponent } from '../../../shared/form/form.component';
|
||||
import { FormService } from '../../../shared/form/form.service';
|
||||
import { SectionModelComponent } from '../models/section.model';
|
||||
import { SubmissionFormsConfigService } from '../../../core/config/submission-forms-config.service';
|
||||
import { hasValue, isNotEmpty, isNotUndefined, isUndefined } from '../../../shared/empty.util';
|
||||
import { hasValue, isNotEmpty, isUndefined } from '../../../shared/empty.util';
|
||||
import { ConfigData } from '../../../core/config/config-data';
|
||||
import { JsonPatchOperationPathCombiner } from '../../../core/json-patch/builder/json-patch-operation-path-combiner';
|
||||
import { SubmissionFormsModel } from '../../../core/shared/config/config-submission-forms.model';
|
||||
@@ -65,13 +66,14 @@ export class FormSectionComponent extends SectionModelComponent implements OnDes
|
||||
super(injectedCollectionId, injectedSectionData, injectedSubmissionId);
|
||||
}
|
||||
|
||||
ngOnInit() {
|
||||
onSectionInit() {
|
||||
this.pathCombiner = new JsonPatchOperationPathCombiner('sections', this.sectionData.id);
|
||||
this.formId = this.formService.getUniqueId(this.sectionData.id);
|
||||
|
||||
this.formConfigService.getConfigByHref(this.sectionData.config)
|
||||
.flatMap((config: ConfigData) => config.payload)
|
||||
.subscribe((config: SubmissionFormsModel) => {
|
||||
this.formConfig = config;
|
||||
this.formId = this.formService.getUniqueId(this.sectionData.id);
|
||||
this.sectionService.getSectionData(this.submissionId, this.sectionData.id)
|
||||
.take(1)
|
||||
.subscribe((sectionData: WorkspaceitemSectionDataType) => {
|
||||
@@ -94,6 +96,10 @@ export class FormSectionComponent extends SectionModelComponent implements OnDes
|
||||
.forEach((subscription) => subscription.unsubscribe());
|
||||
}
|
||||
|
||||
protected getSectionStatus(): Observable<boolean> {
|
||||
return this.formService.isValid(this.formId);
|
||||
}
|
||||
|
||||
hasMetadataEnrichment(sectionData): boolean {
|
||||
const diffResult = [];
|
||||
|
||||
@@ -167,16 +173,7 @@ export class FormSectionComponent extends SectionModelComponent implements OnDes
|
||||
|
||||
subscriptions() {
|
||||
this.subs.push(
|
||||
/**
|
||||
* Subscribe to form status
|
||||
*/
|
||||
this.formService.isValid(this.formId)
|
||||
.filter((formValid) => isNotUndefined(formValid))
|
||||
.filter((formValid) => formValid !== this.valid)
|
||||
.subscribe((formState) => {
|
||||
this.valid = formState;
|
||||
this.sectionService.setSectionStatus(this.submissionId, this.sectionData.id, this.valid);
|
||||
}),
|
||||
|
||||
/**
|
||||
* Subscribe to form's data
|
||||
*/
|
||||
|
@@ -1,6 +1,7 @@
|
||||
import { ChangeDetectorRef, Component, Inject, OnDestroy, OnInit } from '@angular/core';
|
||||
import { ChangeDetectorRef, Component, Inject, OnDestroy, OnInit, ViewChild } from '@angular/core';
|
||||
|
||||
import { DynamicCheckboxModel, DynamicFormControlEvent, DynamicFormControlModel } from '@ng-dynamic-forms/core';
|
||||
import { Observable } from 'rxjs/Observable';
|
||||
import { Subscription } from 'rxjs/Subscription';
|
||||
|
||||
import { SectionModelComponent } from '../models/section.model';
|
||||
@@ -21,6 +22,7 @@ import { WorkspaceitemSectionLicenseObject } from '../../../core/submission/mode
|
||||
import { SubmissionService } from '../../submission.service';
|
||||
import { SectionsService } from '../sections.service';
|
||||
import { FormOperationsService } from '../form/form-operations.service';
|
||||
import { FormComponent } from '../../../shared/form/form.component';
|
||||
|
||||
@Component({
|
||||
selector: 'ds-submission-section-license',
|
||||
@@ -28,7 +30,7 @@ import { FormOperationsService } from '../form/form-operations.service';
|
||||
templateUrl: './section-license.component.html',
|
||||
})
|
||||
@renderSectionFor(SectionsType.License)
|
||||
export class LicenseSectionComponent extends SectionModelComponent implements OnDestroy, OnInit {
|
||||
export class LicenseSectionComponent extends SectionModelComponent implements OnDestroy {
|
||||
|
||||
public formId;
|
||||
public formModel: DynamicFormControlModel[];
|
||||
@@ -38,6 +40,8 @@ export class LicenseSectionComponent extends SectionModelComponent implements On
|
||||
protected pathCombiner: JsonPatchOperationPathCombiner;
|
||||
protected subs: Subscription[] = [];
|
||||
|
||||
@ViewChild('formRef') private formRef: FormComponent;
|
||||
|
||||
constructor(protected changeDetectorRef: ChangeDetectorRef,
|
||||
protected collectionDataService: CollectionDataService,
|
||||
protected formBuilderService: FormBuilderService,
|
||||
@@ -52,8 +56,11 @@ export class LicenseSectionComponent extends SectionModelComponent implements On
|
||||
super(injectedCollectionId, injectedSectionData, injectedSubmissionId);
|
||||
}
|
||||
|
||||
ngOnInit() {
|
||||
onSectionInit() {
|
||||
this.pathCombiner = new JsonPatchOperationPathCombiner('sections', this.sectionData.id);
|
||||
this.formId = this.formService.getUniqueId(this.sectionData.id);
|
||||
this.formModel = this.formBuilderService.fromJSON(SECTION_LICENSE_FORM_MODEL);
|
||||
const model = this.formBuilderService.findById('granted', this.formModel);
|
||||
|
||||
this.subs.push(
|
||||
this.collectionDataService.findById(this.collectionId)
|
||||
@@ -63,13 +70,13 @@ export class LicenseSectionComponent extends SectionModelComponent implements On
|
||||
.take(1)
|
||||
.subscribe((licenseData: RemoteData<License>) => {
|
||||
this.licenseText = licenseData.payload.text;
|
||||
this.formId = this.formService.getUniqueId(this.sectionData.id);
|
||||
this.formModel = this.formBuilderService.fromJSON(SECTION_LICENSE_FORM_MODEL);
|
||||
const model = this.formBuilderService.findById('granted', this.formModel);
|
||||
|
||||
// Retrieve license accepted status
|
||||
if ((this.sectionData.data as WorkspaceitemSectionLicenseObject).granted) {
|
||||
(model as DynamicCheckboxModel).checked = true;
|
||||
this.sectionService.setSectionStatus(this.submissionId, this.sectionData.id, true);
|
||||
(model as DynamicCheckboxModel).valueUpdates.next(true);
|
||||
// this.sectionService.setSectionStatus(this.submissionId, this.sectionData.id, true);
|
||||
} else {
|
||||
(model as DynamicCheckboxModel).valueUpdates.next(false);
|
||||
}
|
||||
|
||||
// Disable checkbox whether it's in workflow or item scope
|
||||
@@ -91,7 +98,6 @@ export class LicenseSectionComponent extends SectionModelComponent implements On
|
||||
// When the error path is only on the section,
|
||||
// replace it with the path to the form field to display error also on the form
|
||||
if (error.path === '/sections/license') {
|
||||
const model = this.formBuilderService.findById('granted', this.formModel);
|
||||
// check whether license is not accepted
|
||||
if (!(model as DynamicCheckboxModel).checked) {
|
||||
return Object.assign({}, error, {path: '/sections/license/granted'});
|
||||
@@ -115,10 +121,16 @@ export class LicenseSectionComponent extends SectionModelComponent implements On
|
||||
);
|
||||
}
|
||||
|
||||
protected getSectionStatus(): Observable<boolean> {
|
||||
const model = this.formBuilderService.findById('granted', this.formModel);
|
||||
return (model as DynamicCheckboxModel).valueUpdates
|
||||
.map((value) => value === true);
|
||||
}
|
||||
|
||||
onChange(event: DynamicFormControlEvent) {
|
||||
const path = this.formOperationsService.getFieldPathSegmentedFromChangeEvent(event);
|
||||
const value = this.formOperationsService.getFieldValueFromChangeEvent(event);
|
||||
this.sectionService.setSectionStatus(this.submissionId, this.sectionData.id, value.value);
|
||||
// this.sectionService.setSectionStatus(this.submissionId, this.sectionData.id, value.value);
|
||||
if (value) {
|
||||
this.operationsBuilder.add(this.pathCombiner.getPath(path), value.value.toString(), false, true);
|
||||
// Remove any section's errors
|
||||
|
@@ -1,5 +1,8 @@
|
||||
import { Inject } from '@angular/core';
|
||||
import { Inject, OnInit } from '@angular/core';
|
||||
import { SectionDataObject } from './section-data.model';
|
||||
import { Observable } from 'rxjs/Observable';
|
||||
import { SectionsService } from '../sections.service';
|
||||
import { isNotUndefined } from '../../../shared/empty.util';
|
||||
|
||||
export interface SectionDataModel {
|
||||
sectionData: SectionDataObject
|
||||
@@ -8,7 +11,8 @@ export interface SectionDataModel {
|
||||
/**
|
||||
* An abstract model class for a submission edit form section.
|
||||
*/
|
||||
export abstract class SectionModelComponent implements SectionDataModel {
|
||||
export abstract class SectionModelComponent implements OnInit, SectionDataModel {
|
||||
protected abstract sectionService: SectionsService;
|
||||
collectionId: string;
|
||||
sectionData: SectionDataObject;
|
||||
submissionId: string;
|
||||
@@ -21,4 +25,21 @@ export abstract class SectionModelComponent implements SectionDataModel {
|
||||
this.sectionData = injectedSectionData;
|
||||
this.submissionId = injectedSubmissionId;
|
||||
}
|
||||
|
||||
ngOnInit(): void {
|
||||
this.onSectionInit();
|
||||
this.updateSectionStatus();
|
||||
}
|
||||
|
||||
protected abstract getSectionStatus(): Observable<boolean>;
|
||||
protected abstract onSectionInit(): void;
|
||||
|
||||
protected updateSectionStatus(): void {
|
||||
this.getSectionStatus()
|
||||
.filter((sectionStatus: boolean) => isNotUndefined(sectionStatus))
|
||||
.startWith(true)
|
||||
.subscribe((sectionStatus: boolean) => {
|
||||
this.sectionService.setSectionStatus(this.submissionId, this.sectionData.id, sectionStatus);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
@@ -5,10 +5,10 @@ import { renderSectionFor } from '../sections-decorator';
|
||||
import { SectionDataObject } from '../models/section-data.model';
|
||||
import { SubmissionState } from '../../submission.reducers';
|
||||
import { Store } from '@ngrx/store';
|
||||
import { WorkspaceitemSectionRecycleObject } from '../../../core/submission/models/workspaceitem-section-recycle.model';
|
||||
import { submissionSectionDataFromIdSelector } from '../../selectors';
|
||||
import { isNotEmpty } from '../../../shared/empty.util';
|
||||
import { Observable } from 'rxjs/Observable';
|
||||
import { SectionsService } from '../sections.service';
|
||||
|
||||
@Component({
|
||||
selector: 'ds-recycle-section',
|
||||
@@ -26,13 +26,14 @@ export class RecycleSectionComponent extends SectionModelComponent {
|
||||
public files: any[]; // WorkspaceitemSectionUploadFileObject[];
|
||||
|
||||
constructor(protected store: Store<SubmissionState>,
|
||||
protected sectionService: SectionsService,
|
||||
@Inject('collectionIdProvider') public injectedCollectionId: string,
|
||||
@Inject('sectionDataProvider') public injectedSectionData: SectionDataObject,
|
||||
@Inject('submissionIdProvider') public injectedSubmissionId: string) {
|
||||
super(injectedCollectionId, injectedSectionData, injectedSubmissionId);
|
||||
}
|
||||
|
||||
ngOnInit() {
|
||||
onSectionInit() {
|
||||
|
||||
this.sectionDataObs = this.store.select(submissionSectionDataFromIdSelector(this.submissionId, this.sectionData.id))
|
||||
.filter((sd) => isNotEmpty(sd))
|
||||
@@ -52,4 +53,8 @@ export class RecycleSectionComponent extends SectionModelComponent {
|
||||
this.isLoading = false;
|
||||
}
|
||||
|
||||
protected getSectionStatus(): Observable<boolean> {
|
||||
return Observable.of(true);
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -1,4 +1,4 @@
|
||||
import { ChangeDetectorRef, Component, Inject, OnInit } from '@angular/core';
|
||||
import { ChangeDetectorRef, Component, Inject, OnDestroy, OnInit } from '@angular/core';
|
||||
|
||||
import { Observable } from 'rxjs/Observable';
|
||||
|
||||
@@ -29,7 +29,7 @@ export const POLICY_DEFAULT_WITH_LIST = 2; // Banner2
|
||||
templateUrl: './section-upload.component.html',
|
||||
})
|
||||
@renderSectionFor(SectionsType.Upload)
|
||||
export class UploadSectionComponent extends SectionModelComponent implements OnInit {
|
||||
export class UploadSectionComponent extends SectionModelComponent implements OnDestroy {
|
||||
|
||||
public AlertTypeEnum = AlertType;
|
||||
public fileIndexes = [];
|
||||
@@ -74,7 +74,7 @@ export class UploadSectionComponent extends SectionModelComponent implements OnI
|
||||
super(undefined, injectedSectionData, injectedSubmissionId);
|
||||
}
|
||||
|
||||
ngOnInit() {
|
||||
onSectionInit() {
|
||||
const config$ = this.uploadsConfigService.getConfigByHref(this.sectionData.config)
|
||||
.flatMap((config) => config.payload);
|
||||
|
||||
@@ -162,7 +162,6 @@ export class UploadSectionComponent extends SectionModelComponent implements OnI
|
||||
})
|
||||
.distinctUntilChanged()
|
||||
.subscribe(([configMetadataForm, fileList]:[SubmissionFormsModel, any[]]) => {
|
||||
let sectionStatus = false;
|
||||
this.fileList = [];
|
||||
this.fileIndexes = [];
|
||||
this.fileNames = [];
|
||||
@@ -173,9 +172,7 @@ export class UploadSectionComponent extends SectionModelComponent implements OnI
|
||||
this.fileIndexes.push(file.uuid);
|
||||
this.fileNames.push(this.getFileName(configMetadataForm, file));
|
||||
});
|
||||
sectionStatus = true;
|
||||
}
|
||||
this.sectionService.setSectionStatus(this.submissionId, this.sectionData.id, sectionStatus);
|
||||
this.changeDetectorRef.detectChanges();
|
||||
}
|
||||
)
|
||||
@@ -194,6 +191,11 @@ export class UploadSectionComponent extends SectionModelComponent implements OnI
|
||||
return title;
|
||||
}
|
||||
|
||||
protected getSectionStatus(): Observable<boolean> {
|
||||
return this.bitstreamService.getUploadedFileList(this.submissionId, this.sectionData.id)
|
||||
.map((fileList: any[]) => (isNotUndefined(fileList) && fileList.length > 0));
|
||||
}
|
||||
|
||||
/**
|
||||
* Method provided by Angular. Invoked when the instance is destroyed.
|
||||
*/
|
||||
|
Reference in New Issue
Block a user