737 lines
25 KiB
TypeScript
737 lines
25 KiB
TypeScript
|
|
import { of as observableOf, Observable } from 'rxjs';
|
|
import { HttpClient, HttpErrorResponse } from '@angular/common/http';
|
|
import { Component, OnInit, ViewChild } from '@angular/core';
|
|
import { Form, FormArray, FormControl, FormGroup } from '@angular/forms';
|
|
import { MatDialog } from '@angular/material/dialog';
|
|
import { MatHorizontalStepper } from '@angular/material/stepper';
|
|
import { ActivatedRoute, ParamMap, Router } from '@angular/router';
|
|
import { TranslateService } from '@ngx-translate/core';
|
|
import { map, takeUntil } from 'rxjs/operators';
|
|
import * as FileSaver from 'file-saver';
|
|
import { BaseComponent } from '@common/base/base.component';
|
|
import { DatasetProfileEditorModel } from '@app/ui/admin/dataset-profile/editor/dataset-profile-editor-model';
|
|
import { DatasetWizardModel } from '@app/core/model/dataset/dataset-wizard';
|
|
import { BreadcrumbItem } from '@app/ui/misc/breadcrumb/definition/breadcrumb-item';
|
|
import { DatasetProfileService } from '@app/core/services/dataset-profile/dataset-profile.service';
|
|
import { LoggingService } from '@app/core/services/logging/logging-service';
|
|
import { UiNotificationService, SnackBarNotificationLevel } from '@app/core/services/notification/ui-notification-service';
|
|
import { DatasetProfile } from '@app/core/model/admin/dataset-profile/dataset-profile';
|
|
import { DatasetProfileEnum } from '@app/core/common/enum/dataset-profile';
|
|
import { SectionEditorModel } from '@app/ui/admin/dataset-profile/admin/section-editor-model';
|
|
import { PageEditorModel } from '@app/ui/admin/dataset-profile/admin/page-editor-model';
|
|
import { DatasetStatus } from '@app/core/common/enum/dataset-status';
|
|
import { ConfirmationDialogComponent } from '@common/modules/confirmation-dialog/confirmation-dialog.component';
|
|
import { LanguageInfo } from '@app/core/model/language-info';
|
|
import { LanguageInfoService } from '@app/core/services/culture/language-info-service';
|
|
import { FormValidationErrorsDialogComponent } from '@common/forms/form-validation-errors-dialog/form-validation-errors-dialog.component';
|
|
import { MatomoService } from '@app/core/services/matomo/matomo-service';
|
|
import { Link, LinkToScroll } from '@app/ui/misc/dataset-description-form/tableOfContentsMaterial/table-of-contents';
|
|
import { DatasetWizardService } from '@app/core/services/dataset-wizard/dataset-wizard.service';
|
|
import { DatasetWizardEditorModel } from '@app/ui/dataset/dataset-wizard/dataset-wizard-editor.model';
|
|
import { ToCEntry, ToCEntryType } from '../table-of-contents/table-of-contents-entry';
|
|
import { EnumUtils } from '@app/core/services/utilities/enum-utils.service';
|
|
|
|
const skipDisable: any[] = require('../../../../../assets/resources/skipDisable.json');
|
|
|
|
@Component({
|
|
selector: 'app-dataset-profile-editor-component',
|
|
templateUrl: './dataset-profile-editor.component.html',
|
|
styleUrls: ['./dataset-profile-editor.component.scss']
|
|
})
|
|
export class DatasetProfileEditorComponent extends BaseComponent implements OnInit {
|
|
|
|
isNew = true;
|
|
isNewVersion = false;
|
|
isClone = false;
|
|
isDeleted = false;
|
|
dataModel: DatasetProfileEditorModel;
|
|
form: FormGroup;
|
|
previewerFormGroup: FormGroup;
|
|
private datasetProfileId: string;
|
|
newVersionId: string;
|
|
dataWizardModel: DatasetWizardModel;
|
|
breadCrumbs: Observable<BreadcrumbItem[]>;
|
|
@ViewChild('stepper', { static: false }) stepper: MatHorizontalStepper;
|
|
viewOnly = false;
|
|
nestedCount: number[] = [];
|
|
nestedIndex: number = 0;
|
|
errorMessages: string[] = [];
|
|
tocEntryEnumValues = ToCEntryType;
|
|
|
|
|
|
|
|
profileEditorConfig: {
|
|
form: FormGroup,
|
|
index: string,
|
|
id: string,
|
|
sectionEditorModel?: SectionEditorModel
|
|
};
|
|
profileEditorCompositeConfig: {
|
|
form: FormGroup,
|
|
index: string,
|
|
id: string
|
|
};
|
|
|
|
isSectionControl: boolean = false;
|
|
|
|
// sectionIdPreviewed:string = null;
|
|
// currentSubForm:FormGroup = null;
|
|
// currentSectionIndex: number = null;
|
|
// currentSectionEditorModel: SectionEditorModel = null;
|
|
|
|
constructor(
|
|
private datasetProfileService: DatasetProfileService,
|
|
private route: ActivatedRoute,
|
|
private router: Router,
|
|
private logger: LoggingService,
|
|
private uiNotificationService: UiNotificationService,
|
|
private language: TranslateService,
|
|
private dialog: MatDialog,
|
|
private languageInfoService: LanguageInfoService,
|
|
private httpClient: HttpClient,
|
|
private matomoService: MatomoService,
|
|
private enumUtils: EnumUtils,
|
|
private datasetWizardService: DatasetWizardService
|
|
) {
|
|
super();
|
|
// this.profileID = route.snapshot.params['id'];
|
|
// this.cloneId = route.snapshot.params['cloneid'];
|
|
}
|
|
|
|
ngOnInit() {
|
|
this.matomoService.trackPageView('Admin: Dataset Profile Edit');
|
|
this.route.paramMap.pipe(takeUntil(this._destroyed)).subscribe((paramMap: ParamMap) => {
|
|
this.datasetProfileId = paramMap.get('id');
|
|
const cloneId = paramMap.get('cloneid');
|
|
this.newVersionId = paramMap.get('newversionid');
|
|
|
|
if (this.datasetProfileId != null) {
|
|
this.isNew = false;
|
|
this.datasetProfileService.getDatasetProfileById(this.datasetProfileId)
|
|
.pipe(map(data => data as DatasetProfile), takeUntil(this._destroyed))
|
|
.subscribe(
|
|
data => {
|
|
try {
|
|
this.dataModel = new DatasetProfileEditorModel().fromModel(data);
|
|
// this.isDeleted = this.masterItem.isActive === IsActive.Inactive;
|
|
if (this.dataModel.status === DatasetProfileEnum.FINALIZED) {
|
|
this.form = this.dataModel.buildForm(true, skipDisable);
|
|
this.viewOnly = true;
|
|
} else {
|
|
this.form = this.dataModel.buildForm();
|
|
}
|
|
this.prepareForm();
|
|
} catch (error) {
|
|
this.logger.error('Could not parse MasterItem: ' + data);
|
|
console.log(error)
|
|
this.uiNotificationService.snackBarNotification(this.language.instant('NOTIFICATIONS.DEFAULT.ERROR'), SnackBarNotificationLevel.Error);
|
|
}
|
|
},
|
|
error => this.onCallbackError(error)
|
|
);
|
|
this.breadCrumbs = observableOf([{
|
|
parentComponentName: 'DatasetProfileListingComponent',
|
|
label: this.language.instant('NAV-BAR.TEMPLATE'),
|
|
url: '/dataset-profiles/' + this.datasetProfileId
|
|
}]);
|
|
} else if (cloneId != null) {
|
|
this.isClone = true;
|
|
this.datasetProfileService.clone(cloneId)
|
|
.pipe(map(data => data as DatasetProfile), takeUntil(this._destroyed))
|
|
.subscribe(
|
|
data => {
|
|
try {
|
|
this.dataModel = new DatasetProfileEditorModel().fromModel(data);
|
|
// this.isDeleted = this.masterItem.isActive === IsActive.Inactive;
|
|
this.dataModel.status = DatasetProfileEnum.SAVED;
|
|
this.form = this.dataModel.buildForm();
|
|
this.prepareForm();
|
|
} catch {
|
|
this.logger.error('Could not parse MasterItem: ' + data);
|
|
this.uiNotificationService.snackBarNotification(this.language.instant('NOTIFICATIONS.DEFAULT.ERROR'), SnackBarNotificationLevel.Error);
|
|
}
|
|
},
|
|
error => this.onCallbackError(error)
|
|
);
|
|
} else if (this.newVersionId != null) {
|
|
this.isNewVersion = true;
|
|
this.datasetProfileService.getDatasetProfileById(this.newVersionId)
|
|
.pipe(map(data => data as DatasetProfile), takeUntil(this._destroyed))
|
|
.subscribe(
|
|
data => {
|
|
try {
|
|
this.dataModel = new DatasetProfileEditorModel().fromModel(data);
|
|
// this.isDeleted = this.masterItem.isActive === IsActive.Inactive;
|
|
this.form = this.dataModel.buildForm();
|
|
this.form.get('version').setValue(this.form.get('version').value + 1);
|
|
this.form.controls['label'].disable();
|
|
this.form.controls['description'].disable();
|
|
this.form.controls['language'].disable();
|
|
this.prepareForm();
|
|
} catch {
|
|
this.logger.error('Could not parse MasterItem: ' + data);
|
|
this.uiNotificationService.snackBarNotification(this.language.instant('NOTIFICATIONS.DEFAULT.ERROR'), SnackBarNotificationLevel.Error);
|
|
}
|
|
},
|
|
error => this.onCallbackError(error)
|
|
);
|
|
} else {
|
|
this.dataModel = new DatasetProfileEditorModel();
|
|
this.form = this.dataModel.buildForm();
|
|
if (this.dataModel.status === DatasetProfileEnum.FINALIZED) {
|
|
this.form.disable();
|
|
this.viewOnly = true;
|
|
}
|
|
this.addSection();
|
|
this.addPage();
|
|
}
|
|
});
|
|
}
|
|
|
|
prepareForm() {
|
|
this.form.valueChanges
|
|
.pipe(takeUntil(this._destroyed))
|
|
.subscribe(change => {
|
|
// this.datasetProfileService.preview(this.form.value)
|
|
// .pipe(takeUntil(this._destroyed))
|
|
// .subscribe(dataset => {
|
|
// const datasetModel = new DatasetWizardModel();
|
|
// datasetModel.datasetProfileDefinition = JsonSerializer.fromJSONObject(dataset, DatasetProfileDefinitionModel);
|
|
// this.dataWizardModel = datasetModel;
|
|
// this.previewerFormGroup = <FormGroup>this.dataWizardModel.buildForm().get('datasetProfileDefinition');
|
|
// });
|
|
});
|
|
this.form.updateValueAndValidity();
|
|
|
|
//this.getPreview();
|
|
|
|
}
|
|
|
|
onIsMultiplicityEnabledChange(isMultiplicityEnabled: boolean) {
|
|
if (!isMultiplicityEnabled) {
|
|
(<FormControl>this.form.get('multiplicity').get('min')).setValue(0);
|
|
(<FormControl>this.form.get('multiplicity').get('max')).setValue(0);
|
|
}
|
|
}
|
|
|
|
addSection() {
|
|
const section: SectionEditorModel = new SectionEditorModel();
|
|
this.dataModel.sections.push(section);
|
|
(<FormArray>this.form.get('sections')).push(section.buildForm());
|
|
}
|
|
|
|
addPage() {
|
|
const page: PageEditorModel = new PageEditorModel(this.dataModel.pages.length);
|
|
this.dataModel.pages.push(page);
|
|
(<FormArray>this.form.get('pages')).push(page.buildForm());
|
|
}
|
|
|
|
DeleteSection(index) {
|
|
this.dataModel.sections.splice(index, 1);
|
|
(<FormArray>this.form.get('sections')).removeAt(index);
|
|
}
|
|
|
|
onSubmit() {
|
|
let data = this.form.value;
|
|
if (this.datasetProfileId) {
|
|
this.datasetProfileService.updateForm(this.datasetProfileId, data)
|
|
.pipe(takeUntil(this._destroyed))
|
|
.subscribe(() => {
|
|
this.router.navigate(['/dataset-profiles']);
|
|
});
|
|
} else if (this.newVersionId) {
|
|
data.label = this.form.get('label').value;
|
|
data.description = this.form.get('description').value;
|
|
this.datasetProfileService.newVersion(this.newVersionId, data)
|
|
.pipe(takeUntil(this._destroyed))
|
|
.subscribe(() => {
|
|
this.router.navigate(['/dataset-profiles']);
|
|
},
|
|
error => this.onCallbackErrorNewVersion(error)
|
|
);
|
|
} else {
|
|
this.form.get('status').setValue(DatasetStatus.Draft);
|
|
data = this.form.value;
|
|
this.datasetProfileService.createForm(data)
|
|
.pipe(takeUntil(this._destroyed))
|
|
.subscribe(() => {
|
|
this.router.navigate(['/dataset-profiles']);
|
|
});
|
|
}
|
|
}
|
|
|
|
finalize() {
|
|
//const data = this.form.value;
|
|
this.form.get('status').setValue(DatasetProfileEnum.FINALIZED);
|
|
|
|
this.onSubmit();
|
|
}
|
|
|
|
updateFinalized() {
|
|
this.datasetProfileService.updateForm(this.datasetProfileId, this.form.getRawValue())
|
|
.pipe(takeUntil(this._destroyed))
|
|
.subscribe(() => {
|
|
this.router.navigate(['/dataset-profiles']);
|
|
});
|
|
}
|
|
|
|
showUpdateButton() {
|
|
return !this.isNew && this.dataModel.status === DatasetProfileEnum.FINALIZED;
|
|
}
|
|
|
|
isStepActive(step: number) {
|
|
return this.stepper && this.stepper.selectedIndex === step;
|
|
}
|
|
|
|
onCallbackSuccess(): void {
|
|
this.uiNotificationService.snackBarNotification(this.isNew ? this.language.instant('GENERAL.SNACK-BAR.SUCCESSFUL-CREATION') : this.language.instant('GENERAL.SNACK-BAR.SUCCESSFUL-UPDATE'), SnackBarNotificationLevel.Success);
|
|
this.router.navigate(['/master-items']);
|
|
}
|
|
|
|
onCallbackErrorNewVersion(errorResponse: HttpErrorResponse) {
|
|
this.uiNotificationService.snackBarNotification(errorResponse.error.message, SnackBarNotificationLevel.Error);
|
|
}
|
|
|
|
onCallbackError(errorResponse: HttpErrorResponse) {
|
|
// const error: HttpError = this.httpErrorHandlingService.getError(errorResponse);
|
|
// if (error.statusCode === 400) {
|
|
// this.masterItem.validationErrorModel.fromJSONObject(errorResponse.error);
|
|
// this.formService.validateAllFormFields(this.formGroup);
|
|
// } else {
|
|
this.uiNotificationService.snackBarNotification(errorResponse.message, SnackBarNotificationLevel.Warning);
|
|
// }
|
|
}
|
|
|
|
// DELETE Function
|
|
public delete(): void {
|
|
if (this.datasetProfileId && !this.isNew) {
|
|
const dialogRef = this.dialog.open(ConfirmationDialogComponent, {
|
|
restoreFocus: false,
|
|
data: {
|
|
message: this.language.instant('GENERAL.CONFIRMATION-DIALOG.DELETE-ITEM'),
|
|
confirmButton: this.language.instant('GENERAL.CONFIRMATION-DIALOG.ACTIONS.CONFIRM'),
|
|
cancelButton: this.language.instant('GENERAL.CONFIRMATION-DIALOG.ACTIONS.CANCEL'),
|
|
isDeleteConfirmation: true
|
|
}
|
|
});
|
|
dialogRef.afterClosed().pipe(takeUntil(this._destroyed)).subscribe(result => {
|
|
if (result) {
|
|
//this.form.get('status').setValue(DatasetProfileEnum.DELETED);
|
|
this.datasetProfileService.delete(this.datasetProfileId, this.form.value)
|
|
.pipe(takeUntil(this._destroyed))
|
|
.subscribe(
|
|
complete => {
|
|
this.uiNotificationService.snackBarNotification(this.language.instant('GENERAL.SNACK-BAR.SUCCESSFUL-DATASET-PROFILE-DELETE'), SnackBarNotificationLevel.Success);
|
|
this.router.navigate(['/dataset-profiles']);
|
|
},
|
|
error => {
|
|
this.onCallbackError(error);
|
|
if (error.error.statusCode == 674) {
|
|
this.uiNotificationService.snackBarNotification(this.language.instant('GENERAL.SNACK-BAR.UNSUCCESSFUL-DATASET-PROFILE-DELETE'), SnackBarNotificationLevel.Error);
|
|
} else {
|
|
this.uiNotificationService.snackBarNotification(this.language.instant(error.message), SnackBarNotificationLevel.Error);
|
|
}
|
|
}
|
|
);
|
|
}
|
|
});
|
|
}
|
|
}
|
|
|
|
downloadXML(): void {
|
|
this.datasetProfileService.downloadXML(this.datasetProfileId)
|
|
.pipe(takeUntil(this._destroyed))
|
|
.subscribe(response => {
|
|
const blob = new Blob([response.body], { type: 'application/xml' });
|
|
const filename = this.getFilenameFromContentDispositionHeader(response.headers.get('Content-Disposition'));
|
|
|
|
FileSaver.saveAs(blob, filename);
|
|
});
|
|
}
|
|
|
|
getFilenameFromContentDispositionHeader(header: string): string {
|
|
const regex: RegExp = new RegExp(/filename[^;=\n]*=((['"]).*?\2|[^;\n]*)/g);
|
|
|
|
const matches = header.match(regex);
|
|
let filename: string;
|
|
for (let i = 0; i < matches.length; i++) {
|
|
const match = matches[i];
|
|
if (match.includes('filename="')) {
|
|
filename = match.substring(10, match.length - 1);
|
|
break;
|
|
} else if (match.includes('filename=')) {
|
|
filename = match.substring(9);
|
|
break;
|
|
}
|
|
}
|
|
return filename;
|
|
}
|
|
|
|
getLanguageInfos(): LanguageInfo[] {
|
|
return this.languageInfoService.getLanguageInfoValues();
|
|
}
|
|
|
|
checkFormValidation() {
|
|
if (!this.form.valid) {
|
|
this.nestedIndex = -1;
|
|
this.form.markAllAsTouched();
|
|
this.printErrors(this.form);
|
|
this.showValidationErrorsDialog();
|
|
this.nestedCount = [];
|
|
this.nestedIndex = 0;
|
|
this.errorMessages = [];
|
|
}
|
|
}
|
|
|
|
printErrors(rootform: FormGroup) {
|
|
if (!rootform.valid) {
|
|
Object.keys(rootform.controls).forEach(key => {
|
|
const errors = rootform.get(key).errors;
|
|
if (errors !== null) {
|
|
let numbering: string = '';
|
|
for (let j = 0; j < this.nestedCount.length; j++) {
|
|
numbering += this.nestedCount[j];
|
|
if (j < this.nestedIndex) {
|
|
numbering += '.';
|
|
} else {
|
|
break;
|
|
}
|
|
}
|
|
Object.keys(errors).forEach(keyError => {
|
|
if (typeof errors[keyError] === 'boolean') {
|
|
this.errorMessages.push(numbering + ' ' + key + ' is ' + keyError);
|
|
} else {
|
|
this.errorMessages.push(numbering + ' ' + key + ': ' + keyError + ': ' + JSON.stringify(errors[keyError]));
|
|
}
|
|
});
|
|
} else {
|
|
if (rootform.get(key) instanceof FormGroup) {
|
|
this.printErrors(<FormGroup>rootform.get(key));
|
|
} else if (rootform.get(key) instanceof FormArray) {
|
|
this.nestedIndex++;
|
|
this.nestedCount[this.nestedIndex] = 0;
|
|
for (let childForm of (<FormArray>rootform.get(key)).controls) {
|
|
this.nestedCount[this.nestedIndex]++;
|
|
this.printErrors(<any>childForm);
|
|
}
|
|
this.nestedCount[this.nestedIndex] = 0;
|
|
this.nestedIndex--;
|
|
|
|
}
|
|
}
|
|
});
|
|
}
|
|
}
|
|
|
|
private showValidationErrorsDialog(projectOnly?: boolean) {
|
|
const dialogRef = this.dialog.open(FormValidationErrorsDialogComponent, {
|
|
disableClose: true,
|
|
autoFocus: false,
|
|
restoreFocus: false,
|
|
data: {
|
|
errorMessages: this.errorMessages,
|
|
projectOnly: projectOnly
|
|
},
|
|
});
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
links: Link[] = [];
|
|
|
|
getLinks(currentLinks: Link[]) {
|
|
this.links = currentLinks;
|
|
}
|
|
|
|
linkToScroll: LinkToScroll;
|
|
onStepFound(linkToScroll: LinkToScroll) {
|
|
this.linkToScroll = linkToScroll;
|
|
}
|
|
|
|
datasetWizardModel: DatasetWizardEditorModel;
|
|
formGroup: FormGroup;
|
|
getPreview() {
|
|
let data = this.form.value;
|
|
this.datasetProfileService.preview(data).subscribe(x => {
|
|
this.datasetWizardModel = new DatasetWizardEditorModel().fromModel({
|
|
datasetProfileDefinition: x
|
|
});
|
|
this.formGroup = <FormGroup>this.datasetWizardModel.buildForm().get('datasetProfileDefinition');
|
|
});
|
|
//this.formGroupRawValue = JSON.parse(JSON.stringify(this.formGroup.getRawValue()));
|
|
|
|
//this.editMode = this.datasetWizardModel.status === DatasetStatus.Draft;
|
|
// if (this.datasetWizardModel.status === DatasetStatus.Finalized) {
|
|
// this.formGroup.disable();
|
|
// this.viewOnly = true;
|
|
// }
|
|
//if (this.viewOnly) { this.formGroup.disable(); } // For future use, to make Dataset edit like DMP.
|
|
// this.registerFormListeners();
|
|
// this.dmpValueChanged(null);
|
|
// this.breadCrumbs = observableOf([
|
|
// {
|
|
// parentComponentName: null,
|
|
// label: this.language.instant('DATASET-LISTING.ACTIONS.CREATE-NEW').toUpperCase(),
|
|
// url: '/datasets/new/'
|
|
// }]);
|
|
|
|
|
|
|
|
// this.datasetWizardService.updateDatasetProfile(this.profileUpdateId)
|
|
// .pipe(takeUntil(this._destroyed))
|
|
// .subscribe(data => {
|
|
// this.datasetWizardModel = new DatasetWizardEditorModel().fromModel(data);
|
|
// this.formGroupRawValue = JSON.parse(JSON.stringify(this.formGroup.getRawValue()));
|
|
|
|
// this.needsUpdate();
|
|
// this.breadCrumbs = observableOf([
|
|
// {
|
|
// parentComponentName: null,
|
|
// label: this.language.instant('NAV-BAR.MY-DATASET-DESCRIPTIONS'),
|
|
// url: '/datasets',
|
|
// notFoundResolver: [
|
|
// // {
|
|
// // parentComponentName: null,
|
|
// // label: this.datasetWizardModel.dmp.grant.label,
|
|
// // url: '/grants/edit/' + this.datasetWizardModel.dmp.grant.id
|
|
// // },
|
|
// {
|
|
// parentComponentName: null,
|
|
// label: this.datasetWizardModel.dmp.label,
|
|
// url: '/plans/edit/' + this.datasetWizardModel.dmp.id,
|
|
// },
|
|
// ]
|
|
// }]);
|
|
// this.formGroup = this.datasetWizardModel.buildForm();
|
|
// this.editMode = this.datasetWizardModel.status === DatasetStatus.Draft;
|
|
// if (this.datasetWizardModel.status === DatasetStatus.Finalized) {
|
|
// this.formGroup.disable();
|
|
// this.viewOnly = true;
|
|
// }
|
|
// // if (this.viewOnly) { this.formGroup.disable(); } // For future use, to make Dataset edit like DMP.
|
|
// this.loadDatasetProfiles();
|
|
// });
|
|
}
|
|
|
|
getTocEntries(): ToCEntry[] {
|
|
if (this.form == null) { return []; }
|
|
|
|
const result: ToCEntry[] = [];
|
|
(this.form.get('pages') as FormArray).controls.forEach((pageElement, i) => {
|
|
result.push({
|
|
id: pageElement.get('id').value,
|
|
label: pageElement.get('title').value,
|
|
type: ToCEntryType.Page,
|
|
form: pageElement
|
|
} as ToCEntry)
|
|
});
|
|
|
|
(this.form.get('sections') as FormArray).controls.forEach((sectionElement, i) => {
|
|
const currentSectionPageId = sectionElement.get('page').value;
|
|
const pageToAdd = result.filter(x => x.id == currentSectionPageId)[0];
|
|
if (pageToAdd.subEntries == null) pageToAdd.subEntries = [];
|
|
|
|
const item = {
|
|
id: sectionElement.get('id').value,
|
|
label: sectionElement.get('title').value,
|
|
type: ToCEntryType.Section,
|
|
form: sectionElement,
|
|
numbering: pageToAdd.numbering + '.' + i
|
|
} as ToCEntry;
|
|
const sectionItems = this.populateSections(sectionElement.get('sections') as FormArray, item.numbering);
|
|
const fieldSetItems = this.populateFieldSets(sectionElement.get('fieldSets') as FormArray, item.numbering);
|
|
if (sectionItems != null) {
|
|
item.subEntries = sectionItems;
|
|
}
|
|
if (fieldSetItems != null) {
|
|
if (item.subEntries == null) {
|
|
item.subEntries = fieldSetItems;
|
|
} else {
|
|
item.subEntries.push(...fieldSetItems);
|
|
}
|
|
}
|
|
pageToAdd.subEntries.push(item);
|
|
|
|
});
|
|
|
|
return result;
|
|
}
|
|
|
|
private populateSections(sections: FormArray, existingNumbering: string): ToCEntry[] {
|
|
if (sections == null || sections.controls == null || sections.controls.length == 0) { return null; }
|
|
|
|
const result: ToCEntry[] = [];
|
|
sections.controls.forEach((sectionElement, i) => {
|
|
|
|
const item = {
|
|
id: sectionElement.get('id').value,
|
|
label: sectionElement.get('title').value,
|
|
type: ToCEntryType.Section,
|
|
form: sectionElement,
|
|
numbering: existingNumbering + '.' + i
|
|
} as ToCEntry;
|
|
const sectionItems = this.populateSections(sectionElement.get('sections') as FormArray, item.numbering);
|
|
const fieldSetItems = this.populateFieldSets(sectionElement.get('fieldSets') as FormArray, item.numbering);
|
|
if (sectionItems != null) {
|
|
item.subEntries = sectionItems;
|
|
}
|
|
if (fieldSetItems != null) {
|
|
if (item.subEntries == null) {
|
|
item.subEntries = fieldSetItems;
|
|
} else {
|
|
item.subEntries.push(...fieldSetItems);
|
|
}
|
|
}
|
|
result.push(item);
|
|
});
|
|
|
|
return result;
|
|
}
|
|
|
|
private populateFieldSets(fieldSets: FormArray, existingNumbering: string): ToCEntry[] {
|
|
if (fieldSets == null || fieldSets.controls == null || fieldSets.controls.length == 0) { return null; }
|
|
|
|
const result: ToCEntry[] = [];
|
|
fieldSets.controls.forEach((fieldSetElement, i) => {
|
|
|
|
result.push({
|
|
id: fieldSetElement.get('id').value,
|
|
label: fieldSetElement.get('title').value,
|
|
type: ToCEntryType.FieldSet,
|
|
subEntries: this.populateSections((fieldSetElement.get('fieldSets') as FormArray), existingNumbering + '.' + i),
|
|
form: fieldSetElement,
|
|
numbering: existingNumbering + '.' + i
|
|
} as ToCEntry)
|
|
|
|
});
|
|
|
|
return result;
|
|
}
|
|
|
|
selectedTocEntry: ToCEntry
|
|
displayItem(entry: ToCEntry): void {
|
|
this.selectedTocEntry = entry;
|
|
|
|
|
|
// console.log(this.form);
|
|
// console.log(this.dataModel);
|
|
|
|
// this.profileEditorConfig = null;
|
|
|
|
// let entry = entries[0];
|
|
// console.log(entries);
|
|
|
|
|
|
// if (entry.subEntries && (entry.subEntries.length > 0)) {// its a page
|
|
// // this.sectionIdPreviewed = entry.id;
|
|
// const valuesArray = this.form.get('sections').value;
|
|
|
|
|
|
// let currentVal = entry.id;
|
|
|
|
// let indexArray: string[] = valuesArray.map(element => element.page);
|
|
|
|
// let index = indexArray.indexOf(currentVal);
|
|
// let subForm = (this.form.get('sections') as FormArray).controls[index] as FormGroup;
|
|
// let currentEditor = this.dataModel.sections.filter(section => section.page == entry.id)[0];
|
|
|
|
|
|
// this.profileEditorConfig = {
|
|
// id: entry.id,
|
|
// form: subForm,
|
|
// index: index.toString(),
|
|
// sectionEditorModel: currentEditor
|
|
// };
|
|
|
|
// this.isSectionControl = false;
|
|
// // this.currentSubForm = subForm;
|
|
// // this.currentSectionIndex = index;
|
|
// // this.currentSectionEditorModel = currentEditor;
|
|
|
|
// // this.isCompoSiteFieldEditor = true;
|
|
|
|
// } else if (entries.length == 2) {//its section
|
|
|
|
// this.isSectionControl = true;
|
|
|
|
// const valuesArray = this.form.get('sections').value;
|
|
|
|
|
|
// let currentVal = entries[1].id // Parent node entry.id;
|
|
|
|
// let indexArray: string[] = valuesArray.map(element => element.page);
|
|
|
|
// let index = indexArray.indexOf(currentVal);
|
|
|
|
|
|
// //Section Form
|
|
// const sectionForm = (this.form.get('sections') as FormArray).controls[index] as FormGroup;
|
|
|
|
// const sectionFormControls = sectionForm.get('fieldSets')['controls'];
|
|
|
|
// const fieldSetsIds = sectionForm.get('fieldSets').value;//.map(fieldset => fieldset.id);
|
|
// // const fieldsetIndex = fieldSetsIds.indexOf(entries[0].id);
|
|
|
|
// console.log(fieldSetsIds);
|
|
|
|
|
|
// const sectionFormControl = sectionFormControls[0];
|
|
|
|
// this.profileEditorCompositeConfig = {
|
|
// id: entries[0].id,
|
|
// form: sectionFormControl,
|
|
// index: 's' + index + 'cf' + 0
|
|
// }
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
// getSectionIndex(): number{
|
|
// // if(this.sectionIdPreviewed == null) return;
|
|
// const valuesArray = this.form.get('sections').value;
|
|
// let currentVal = this.sectionIdPreviewed;
|
|
// let indexArray:string[] = valuesArray.map(element=> element.page);
|
|
// let index = indexArray.indexOf(currentVal);
|
|
// console.log(index);
|
|
// return index ? index :-1;
|
|
// }
|
|
|
|
// getCurrentEditorModel(): SectionEditorModel{
|
|
// let currentEditor = this.dataModel.sections.filter(section=> section.page == this.sectionIdPreviewed)[0];
|
|
// return currentEditor;
|
|
|
|
// }
|
|
|
|
|
|
// subForm(){
|
|
// const valuesArray = this.form.get('sections').value;
|
|
// let currentVal = this.sectionIdPreviewed;
|
|
// let indexArray:string[] = valuesArray.map(element=> element.page);
|
|
// let index = indexArray.indexOf(currentVal);
|
|
// let subForm = (this.form.get('sections') as FormArray).controls[index];
|
|
// console.log(subForm);
|
|
// return subForm;
|
|
// }
|
|
|
|
getFieldTile(formGroup: FormGroup, index: number) {
|
|
if (formGroup.get('title') && formGroup.get('title').value && formGroup.get('title').value.length > 0) { return formGroup.get('title').value; }
|
|
return "Field " + (index + 1);
|
|
}
|
|
|
|
deleteFieldSet(formArray: FormArray, index: number) {
|
|
formArray.removeAt(index);
|
|
}
|
|
|
|
|
|
} |