Made Resource details page read only for LwM2M resources

This commit is contained in:
mpetrov 2024-09-11 16:34:30 +03:00
parent 176d9e5494
commit d435f91fac
2 changed files with 22 additions and 19 deletions

View File

@ -58,7 +58,7 @@
</mat-form-field> </mat-form-field>
<mat-form-field class="mat-block" *ngIf="entityForm.get('resourceType').value !== resourceType.LWM2M_MODEL || !isAdd"> <mat-form-field class="mat-block" *ngIf="entityForm.get('resourceType').value !== resourceType.LWM2M_MODEL || !isAdd">
<mat-label translate>resource.title</mat-label> <mat-label translate>resource.title</mat-label>
<input matInput formControlName="title" required [readonly]="entityForm.get('resourceType').value === resourceType.LWM2M_MODEL"> <input matInput formControlName="title" required>
<mat-error *ngIf="entityForm.get('title').hasError('required')"> <mat-error *ngIf="entityForm.get('title').hasError('required')">
{{ 'resource.title-required' | translate }} {{ 'resource.title-required' | translate }}
</mat-error> </mat-error>

View File

@ -43,8 +43,7 @@ export class ResourcesLibraryComponent extends EntityComponent<Resource> impleme
readonly resourceType = ResourceType; readonly resourceType = ResourceType;
readonly resourceTypes: ResourceType[] = Object.values(this.resourceType); readonly resourceTypes: ResourceType[] = Object.values(this.resourceType);
readonly resourceTypesTranslationMap = ResourceTypeTranslationMap; readonly resourceTypesTranslationMap = ResourceTypeTranslationMap;
readonly maxResourceSize = getCurrentAuthState(this.store).maxResourceSize;
maxResourceSize = getCurrentAuthState(this.store).maxResourceSize;
private destroy$ = new Subject<void>(); private destroy$ = new Subject<void>();
@ -57,20 +56,20 @@ export class ResourcesLibraryComponent extends EntityComponent<Resource> impleme
super(store, fb, entityValue, entitiesTableConfigValue, cd); super(store, fb, entityValue, entitiesTableConfigValue, cd);
} }
ngOnInit() { ngOnInit(): void {
super.ngOnInit(); super.ngOnInit();
if (this.isAdd) { if (this.isAdd) {
this.observeResourceTypeChange(); this.observeResourceTypeChange();
} }
} }
ngOnDestroy() { ngOnDestroy(): void {
super.ngOnDestroy(); super.ngOnDestroy();
this.destroy$.next(); this.destroy$.next();
this.destroy$.complete(); this.destroy$.complete();
} }
hideDelete() { hideDelete(): boolean {
if (this.entitiesTableConfig) { if (this.entitiesTableConfig) {
return !this.entitiesTableConfig.deleteEnabled(this.entity); return !this.entitiesTableConfig.deleteEnabled(this.entity);
} else { } else {
@ -87,19 +86,18 @@ export class ResourcesLibraryComponent extends EntityComponent<Resource> impleme
}); });
} }
updateForm(entity: Resource) { updateForm(entity: Resource): void {
const { resourceType, fileName, title, data } = entity;
this.updateTableReadonlyConfig();
if (this.isEdit) { if (this.isEdit) {
this.entityForm.get('resourceType').disable({ emitEvent: false }); this.entityForm.get('resourceType').disable({ emitEvent: false });
if (entity.resourceType !== ResourceType.JS_MODULE) { if (resourceType !== ResourceType.JS_MODULE) {
this.entityForm.get('fileName').disable({ emitEvent: false }); this.entityForm.get('fileName').disable({ emitEvent: false });
} }
} }
this.entityForm.patchValue({
resourceType: entity.resourceType, this.entityForm.patchValue({ resourceType, fileName, title, data });
fileName: entity.fileName,
title: entity.title,
data: entity.data
});
} }
prepareFormValue(formValue: Resource): Resource { prepareFormValue(formValue: Resource): Resource {
@ -109,7 +107,7 @@ export class ResourcesLibraryComponent extends EntityComponent<Resource> impleme
return super.prepareFormValue(formValue); return super.prepareFormValue(formValue);
} }
getAllowedExtensions() { getAllowedExtensions(): string {
try { try {
return ResourceTypeExtension.get(this.entityForm.get('resourceType').value); return ResourceTypeExtension.get(this.entityForm.get('resourceType').value);
} catch (e) { } catch (e) {
@ -117,7 +115,7 @@ export class ResourcesLibraryComponent extends EntityComponent<Resource> impleme
} }
} }
getAcceptType() { getAcceptType(): string {
try { try {
return ResourceTypeMIMETypes.get(this.entityForm.get('resourceType').value); return ResourceTypeMIMETypes.get(this.entityForm.get('resourceType').value);
} catch (e) { } catch (e) {
@ -129,7 +127,7 @@ export class ResourcesLibraryComponent extends EntityComponent<Resource> impleme
return window.btoa(data); return window.btoa(data);
} }
onResourceIdCopied() { onResourceIdCopied(): void {
this.store.dispatch(new ActionNotificationShow( this.store.dispatch(new ActionNotificationShow(
{ {
message: this.translate.instant('resource.idCopiedMessage'), message: this.translate.instant('resource.idCopiedMessage'),
@ -140,6 +138,11 @@ export class ResourcesLibraryComponent extends EntityComponent<Resource> impleme
})); }));
} }
private updateTableReadonlyConfig(): void {
this.entitiesTableConfigValue.detailsReadonly = () =>
this.resourceType.LWM2M_MODEL === this.entityForm.get('resourceType').value;
}
private observeResourceTypeChange(): void { private observeResourceTypeChange(): void {
this.entityForm.get('resourceType').valueChanges.pipe( this.entityForm.get('resourceType').valueChanges.pipe(
startWith(ResourceType.JS_MODULE), startWith(ResourceType.JS_MODULE),