refactor description template editor, change belongsToTenant on copied items

This commit is contained in:
CITE\spapacharalampous 2024-08-28 16:04:10 +03:00
parent 1587a413cf
commit 7836ac1218
3 changed files with 77 additions and 50 deletions

View File

@ -107,7 +107,7 @@
<td>{{usersMap.get(user?.get('userId')?.value)?.name}}</td>
<td>{{enumUtils.toUserDescriptionTemplateRoleString(user?.get('role')?.value)}}</td>
<td>
<button [disabled]="formGroup.disabled" mat-button class="delete-btn" (click)="verifyAndRemoveUser(i)" [matTooltip]="'DESCRIPTION-TEMPLATE-EDITOR.STEPS.GENERAL-INFO.DESCRIPTION-TEMPLATE-REMOVE-USER'| translate"><mat-icon>person_remove</mat-icon></button>
<button [disabled]="hideEditActions" mat-button class="delete-btn" (click)="verifyAndRemoveUser(i)" [matTooltip]="'DESCRIPTION-TEMPLATE-EDITOR.STEPS.GENERAL-INFO.DESCRIPTION-TEMPLATE-REMOVE-USER'| translate"><mat-icon>person_remove</mat-icon></button>
</td>
</tr>
<tr *ngIf="formGroup.get('users')?.controls?.length === 0">
@ -123,7 +123,7 @@
<div class="col-12">
<div class="row justify-content-end">
<div class="col-12 col-lg-4 d-flex justify-content-end" style="overflow: hidden;">
<mat-form-field class="full-width basic-info-input" *ngIf="!formGroup.disabled">
<mat-form-field class="full-width basic-info-input" *ngIf="!hideEditActions">
<mat-label>{{'DESCRIPTION-TEMPLATE-EDITOR.STEPS.GENERAL-INFO.DESCRIPTION-TEMPLATE-USERS' | translate}}</mat-label>
<app-single-auto-complete [required]="false" [formControl]="userFormControl" (optionSelected)="addUser($event)" placeholder="{{'DESCRIPTION-TEMPLATE-EDITOR.STEPS.GENERAL-INFO.DESCRIPTION-TEMPLATE-USERS' | translate}}" [configuration]="userService.singleAutocompleteConfiguration">
</app-single-auto-complete>
@ -147,7 +147,7 @@
<description-template-table-of-contents class="toc-pane-container col" style="margin-bottom: 2em;"
[links]="toCEntries"
[itemSelected]="selectedTocEntry"
[viewOnly]="formGroup.disabled"
[viewOnly]="hideEditActions"
[colorizeInvalid]="colorizeInvalid"
[showErrors]="showErrors"
(dataNeedsRefresh)="onDataNeedsRefresh($event)"
@ -185,7 +185,7 @@
</mat-form-field>
</div>
<div class="col-12" *ngIf="!viewOnly && (!selectedTocEntry?.subEntries?.length)">
<div class="col-12" *ngIf="!hideEditActions && (!selectedTocEntry?.subEntries?.length)">
<button class="create-section-btn" (click)="addNewEntry({parent:selectedTocEntry, childType: tocEntryEnumValues.Section})">{{'DESCRIPTION-TEMPLATE-EDITOR.STEPS.PAGE-INFO.ACTIONS.CREATE-SECTION' | translate}}</button>
</div>
</div>
@ -195,7 +195,7 @@
<div class="col-12" *ngIf="(selectedTocEntry.type === tocEntryEnumValues.Section) || (selectedTocEntry.type === tocEntryEnumValues.FieldSet)">
<app-description-template-editor-section-fieldset-component
[tocentry]="selectedTocEntry"
[viewOnly]="formGroup.disabled"
[viewOnly]="hideEditActions"
[descriptionTemplateId]="descriptionTemplateId"
[validationErrorModel]="editorModel.validationErrorModel"
[validationRootPath]="selectedTocEntry.validationRootPath"
@ -278,7 +278,7 @@
<ng-template #actions>
<div>
<button mat-raised-button class="template_action_btn mr-3" (click)="cancel()">{{'DESCRIPTION-TEMPLATE-EDITOR.ACTIONS.CLOSE' | translate}}</button>
<button *ngIf="!formGroup.disabled && formGroup.get('status').value!=finalized" mat-raised-button class="template_action_btn save-btn" type="button">
<button *ngIf="!hideEditActions" [disabled]="!(formGroup?.valid ?? false)" mat-raised-button class="template_action_btn save-btn" type="button">
<span class="d-flex flex-row row">
<span (click)="save(); formSubmit()" class="col">{{'DESCRIPTION-TEMPLATE-EDITOR.ACTIONS.SAVE' | translate}}</span>
<mat-divider [vertical]="true"></mat-divider>
@ -292,7 +292,7 @@
<button mat-menu-item (click)="saveWithClose(false)" type="button">{{ 'DESCRIPTION-TEMPLATE-EDITOR.ACTIONS.SAVE-AND-CONTINUE' | translate }}</button>
</mat-menu>
<button *ngIf="!formGroup.disabled && formGroup.get('status').value!= finalized && steps?.length-1 === stepper?.selectedIndex" [@finalize_btn] mat-button class="finalize-btn ml-3" [disabled]="!formGroup.valid" [class.invisible]="steps?.length-1 !== stepper?.selectedIndex" (click)="finalize()">
<button *ngIf="!hideEditActions && canFinalize && steps?.length-1 === stepper?.selectedIndex" [@finalize_btn] mat-button class="finalize-btn ml-3" [disabled]="!formGroup.valid" [class.invisible]="steps?.length-1 !== stepper?.selectedIndex" (click)="finalize()">
{{'DESCRIPTION-TEMPLATE-EDITOR.ACTIONS.FINALIZE' | translate}}
</button>
</div>

View File

@ -27,7 +27,6 @@ import { AnalyticsService } from '@app/core/services/matomo/analytics-service';
import { SnackBarNotificationLevel, UiNotificationService } from '@app/core/services/notification/ui-notification-service';
import { UserService } from '@app/core/services/user/user.service';
import { EnumUtils } from '@app/core/services/utilities/enum-utils.service';
import { FileUtils } from '@app/core/services/utilities/file-utils.service';
import { QueryParamsService } from '@app/core/services/utilities/query-params.service';
import { SingleAutoCompleteConfiguration } from '@app/library/auto-complete/single/single-auto-complete-configuration';
import { BaseEditor } from '@common/base/base-editor';
@ -60,9 +59,6 @@ export class DescriptionTemplateEditorComponent extends BaseEditor<DescriptionTe
@ViewChild('stepper') stepper: MatStepper;
isNew = true;
isDeleted = false;
isFinalized = false;
formGroup: UntypedFormGroup = null;
item: DescriptionTemplate;
showInactiveDetails = false;
@ -70,8 +66,6 @@ export class DescriptionTemplateEditorComponent extends BaseEditor<DescriptionTe
availableLanguages: LanguageInfo[] = this.languageInfoService.getLanguageInfoValues();
isNewVersion = false;
isClone = false;
showErrors: boolean = false;
steps: QueryList<CdkStep>;
toCEntries: ToCEntry[];
@ -84,6 +78,31 @@ export class DescriptionTemplateEditorComponent extends BaseEditor<DescriptionTe
singleAutocompleteDescriptionTemplateTypeConfiguration: SingleAutoCompleteConfiguration;
isNew = true;
isClone = false;
isNewVersion = false;
isDeleted = false;
belongsToCurrentTenant = true;
protected get isTransient(): boolean {
return this.isNew || this.isClone || this.isNewVersion;
}
protected get hideEditActions(): boolean {
return this.isDeleted || this.isFinalized || !this.authService.hasPermission(AppPermission.EditDescriptionTemplate) || !this.belongsToCurrentTenant;
}
protected get canDelete(): boolean {
return !this.isDeleted && !this.isTransient && this.belongsToCurrentTenant && this.authService.hasPermission(AppPermission.DeleteDescriptionTemplate);
}
protected get canFinalize(): boolean {
return !this.isTransient && !this.isFinalized;
}
protected get isFinalized(): boolean {
return this.editorModel.status == DescriptionTemplateStatus.Finalized;
}
//Preview
previewFieldSet: DescriptionTemplate = null;
@ -104,18 +123,10 @@ export class DescriptionTemplateEditorComponent extends BaseEditor<DescriptionTe
return '3 ' + label;
}
protected get canDelete(): boolean {
return !this.isDeleted && !this.isNew && (this.hasPermission(this.authService.permissionEnum.DeleteDescriptionTemplate) || this.item?.authorizationFlags?.some(x => x === AppPermission.DeleteDescriptionTemplate));
}
protected get canSave(): boolean {
return !this.isDeleted && (this.hasPermission(this.authService.permissionEnum.EditDescriptionTemplate) || this.item?.authorizationFlags?.some(x => x === AppPermission.EditDescriptionTemplate));
}
protected get canFinalize(): boolean {
return !this.isDeleted && (this.hasPermission(this.authService.permissionEnum.EditDescriptionTemplate) || this.item?.authorizationFlags?.some(x => x === AppPermission.EditDescriptionTemplate));
}
private hasPermission(permission: AppPermission): boolean {
return this.authService.hasPermission(permission) || this.editorModel?.permissions?.includes(permission) || this.item?.authorizationFlags?.some(x => x === permission);
}
@ -140,7 +151,6 @@ export class DescriptionTemplateEditorComponent extends BaseEditor<DescriptionTe
public descriptionTemplateTypeService: DescriptionTemplateTypeService,
private logger: LoggingService,
private descriptionTemplateEditorService: DescriptionTemplateEditorService,
private fileUtils: FileUtils,
private languageInfoService: LanguageInfoService,
public userService: UserService,
public titleService: Title,
@ -190,11 +200,19 @@ export class DescriptionTemplateEditorComponent extends BaseEditor<DescriptionTe
prepareForm(data: DescriptionTemplate) {
try {
this.editorModel = data ? new DescriptionTemplateEditorModel().fromModel(data) : new DescriptionTemplateEditorModel();
const dataCopy = data ? JSON.parse(JSON.stringify(data)) : null;
if(dataCopy && this.isClone) {
dataCopy.belongsToCurrentTenant = true;
}
this.editorModel = dataCopy ? new DescriptionTemplateEditorModel().fromModel(dataCopy) : new DescriptionTemplateEditorModel();
this.item = data;
// Add user info to Map, to present them.
(this.item?.users ?? []).forEach(obj => { this.usersMap.set(obj.user.id, obj.user); });
this.isDeleted = data ? data.isActive === IsActive.Inactive : false;
this.isDeleted = dataCopy ? dataCopy.isActive === IsActive.Inactive : false;
this.belongsToCurrentTenant = this.isNew || dataCopy.belongsToCurrentTenant;
this.buildForm();
if (data && data.id) this.checkLock(data.id, LockTargetType.DescriptionTemplate, 'DESCRIPTION-TEMPLATE-EDITOR.LOCKED-DIALOG.TITLE', 'DESCRIPTION-TEMPLATE-EDITOR.LOCKED-DIALOG.MESSAGE');
@ -212,15 +230,11 @@ export class DescriptionTemplateEditorComponent extends BaseEditor<DescriptionTe
}
buildForm() {
this.formGroup = this.editorModel.buildForm(null, this.isDeleted || !(this.authService.hasPermission(AppPermission.EditDescriptionTemplate) || this.item?.authorizationFlags?.some(x => x === AppPermission.EditDescriptionTemplate)), (this.isNew || this.isClone));
this.formGroup = this.editorModel.buildForm(null, this.hideEditActions, (this.isNew || this.isClone));
this.descriptionTemplateEditorService.setValidationErrorModel(this.editorModel.validationErrorModel);
this.isFinalized = this.editorModel.status == DescriptionTemplateStatus.Finalized;
if (this.isFinalized || this.isDeleted) {
this.formGroup.disable();
}
const action = this.route.snapshot.data['action'];
if (action && action == 'new-version') {
this.formGroup.enable();
this.formGroup.get('code').disable();
this.formGroup.get('status').setValue(DescriptionTemplateStatus.Draft);
}
@ -247,7 +261,7 @@ export class DescriptionTemplateEditorComponent extends BaseEditor<DescriptionTe
}
persistEntity(onSuccess?: (response) => void): void {
if (this.isNewVersion == false) {
if (!this.isNewVersion) {
const formData = this.formService.getValue(this.formGroup.value) as DescriptionTemplatePersist;
formData.code = this.formGroup.get('code').getRawValue();

View File

@ -49,15 +49,16 @@ export class DescriptionTemplateEditorModel extends BaseEditorModel implements D
if (context == null) { context = this.createValidationContext(); }
return this.formBuilder.group({
id: [{ value: this.id, disabled: disabled }, context.getValidation('id').validators],
label: [{ value: this.label, disabled: disabled }, context.getValidation('label').validators],
id: [{ value: this.id, disabled }, context.getValidation('id').validators],
label: [{ value: this.label, disabled }, context.getValidation('label').validators],
code: [{ value: this.code, disabled: !isNewOrClone }, context.getValidation('code').validators],
description: [{ value: this.description, disabled: disabled }, context.getValidation('description').validators],
language: [{ value: this.language, disabled: disabled }, context.getValidation('language').validators],
type: [{ value: this.type, disabled: disabled }, context.getValidation('type').validators],
status: [{ value: this.status, disabled: disabled }, context.getValidation('status').validators],
description: [{ value: this.description, disabled }, context.getValidation('description').validators],
language: [{ value: this.language, disabled }, context.getValidation('language').validators],
type: [{ value: this.type, disabled }, context.getValidation('type').validators],
status: [{ value: this.status, disabled }, context.getValidation('status').validators],
definition: this.definition.buildForm({
rootPath: `definition.`
rootPath: `definition.`,
disabled
}),
users: this.formBuilder.array(
(this.users ?? []).map(
@ -219,7 +220,8 @@ export class DescriptionTemplateDefinitionEditorModel implements DescriptionTemp
pages: this.formBuilder.array(
(this.pages ?? []).map(
(item, index) => item.buildForm({
rootPath: `${rootPath}pages[${index}].`
rootPath: `${rootPath}pages[${index}].`,
disabled
})
), context.getValidation('pages').validators
),
@ -299,7 +301,8 @@ export class DescriptionTemplatePageEditorModel implements DescriptionTemplatePa
sections: this.formBuilder.array(
(this.sections ?? []).map(
(item, index) => item.buildForm({
rootPath: `${rootPath}sections[${index}].`
rootPath: `${rootPath}sections[${index}].`,
disabled
})
), context.getValidation('sections').validators
),
@ -408,14 +411,16 @@ export class DescriptionTemplateSectionEditorModel implements DescriptionTemplat
sections: this.formBuilder.array(
(this.sections ?? []).map(
(item, index) => item.buildForm({
rootPath: `${rootPath}sections[${index}].`
rootPath: `${rootPath}sections[${index}].`,
disabled
})
), context.getValidation('sections').validators
),
fieldSets: this.formBuilder.array(
(this.fieldSets ?? []).map(
(item, index) => item.buildForm({
rootPath: `${rootPath}fieldSets[${index}].`
rootPath: `${rootPath}fieldSets[${index}].`,
disabled
})
), context.getValidation('fieldSets').validators
)
@ -540,12 +545,14 @@ export class DescriptionTemplateFieldSetEditorModel implements DescriptionTempla
hasCommentField: [{ value: this.hasCommentField, disabled: disabled }, context.getValidation('hasCommentField').validators],
hasMultiplicity: [{ value: this.hasMultiplicity, disabled: disabled }, context.getValidation('hasMultiplicity').validators],
multiplicity: this.multiplicity.buildForm({
rootPath: `${rootPath}multiplicity.`
rootPath: `${rootPath}multiplicity.`,
disabled
}),
fields: this.formBuilder.array(
(this.fields ?? []).map(
(item, index) => item.buildForm({
rootPath: `${rootPath}fields[${index}].`
rootPath: `${rootPath}fields[${index}].`,
disabled
})
), context.getValidation('fields').validators
)
@ -745,15 +752,18 @@ export class DescriptionTemplateFieldEditorModel implements DescriptionTemplateF
validations: [{ value: this.validations, disabled: disabled }, context.getValidation('validations').validators],
includeInExport: [{ value: this.includeInExport, disabled: disabled }, context.getValidation('includeInExport').validators],
data: this.data.buildForm({
rootPath: `${rootPath}data.`
rootPath: `${rootPath}data.`,
disabled
}),
defaultValue: this.defaultValue.buildForm({
rootPath: `${rootPath}defaultValue.`
rootPath: `${rootPath}defaultValue.`,
disabled
}),
visibilityRules: this.formBuilder.array(
(this.visibilityRules ?? []).map(
(item, index) => item.buildForm({
rootPath: `${rootPath}visibilityRules[${index}].`
rootPath: `${rootPath}visibilityRules[${index}].`,
disabled
})
), context.getValidation('visibilityRules').validators
)
@ -1350,7 +1360,8 @@ export class DescriptionTemplateRadioBoxDataEditorModel extends DescriptionTempl
formGroup.setControl('options', this.formBuilder.array(
(this.options ?? []).map(
(item, index) => item.buildForm({
rootPath: `${rootPath}options[${index}].`
rootPath: `${rootPath}options[${index}].`,
disabled
})
), context.getValidation('options').validators
));
@ -1495,7 +1506,8 @@ export class DescriptionTemplateSelectDataEditorModel extends DescriptionTemplat
formGroup.setControl('options', this.formBuilder.array(
(this.options ?? []).map(
(item, index) => item.buildForm({
rootPath: `${rootPath}options[${index}].`
rootPath: `${rootPath}options[${index}].`,
disabled
})
), context.getValidation('options').validators
));
@ -1654,7 +1666,8 @@ export class DescriptionTemplateUploadDataEditorModel extends DescriptionTemplat
formGroup.setControl('types', this.formBuilder.array(
(this.types ?? []).map(
(item, index) => item.buildForm({
rootPath: `${rootPath}types[${index}].`
rootPath: `${rootPath}types[${index}].`,
disabled
})
), context.getValidation('types').validators
));