Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

editor: fix button validation #32

Merged
merged 1 commit into from
Nov 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ export class FieldsetComponent implements OnInit {
}
for (const item of field.items) {
this.resetFormGroup(item);
this.disableFormGroup(item);
}
}

Expand All @@ -186,4 +187,17 @@ export class FieldsetComponent implements OnInit {
control.reset();
return true;
}

/**
* Mark a layoutNode FormControl as disabled.
* @param layoutNode - object, angular6 json schema form layout node
*/
private disableFormGroup(layoutNode) {
const control = getControl(this.jsf.formGroup, layoutNode.dataPointer);
if (!control) {
return false;
}
control.disable();
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
-->

<div>
<h5 *ngIf="fieldsToShow.length" translate>New</h5>
<button class="btn btn-light mr-2 mb-2" *ngFor="let field of fieldsToShow" (click)="show(field)">
<h5 *ngIf="hiddenFields.length" translate>New</h5>
<button class="btn btn-light mr-2 mb-2" *ngFor="let field of hiddenFields" (click)="show(field)">
{{field.options.title}}
</button>
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/

import { Component, OnInit } from '@angular/core';
import { JsonSchemaFormService, forEach } from 'angular6-json-schema-form';
import { JsonSchemaFormService, forEach, getControl } from 'angular6-json-schema-form';

@Component({
selector: 'ng-core-main-fields-manager',
Expand All @@ -25,11 +25,17 @@ import { JsonSchemaFormService, forEach } from 'angular6-json-schema-form';
})
export class MainFieldsManagerComponent implements OnInit {
// ref to field to show/hide given the key element
private layoutRefField = {};
constructor(
private jsf: JsonSchemaFormService
) { }
private layoutRefField: any = {};

/**
* Constructor
* @param jsf: angular6 jsonschema form service
*/
constructor(private jsf: JsonSchemaFormService) { }

/**
* Component initialisation.
*/
ngOnInit() {
// keep in cache a dict with the field key name as a key and the layoutNode as a value
let fieldToShow;
Expand Down Expand Up @@ -70,14 +76,67 @@ export class MainFieldsManagerComponent implements OnInit {
field.options.show = true;
}
}
// disable FormControls for hidden fields
for (const fieldSet of Object.values(this.layoutRefField)) {
if (this.isHidden(fieldSet)) {
for (const field of fieldSet['items']) {
this.disableFormGroup(field);
}
}
}
}

get fieldsToShow() {
return this.jsf.layout.filter(item => 'show' in item.options && item.options.show === false);
/**
* Return the list of the hidden form layout.
* @param formLayout - object, angular6 json schema form layout
*/
get hiddenFields() {
return this.jsf.layout.filter(layout => this.isHidden(layout) );
}

show(field) {
field.options.show = true;
/**
* Compute the visibility of a given formLayout.
* @param formLayout - object, angular6 json schema form layout
*/
isHidden(formLayout) {
return 'show' in formLayout.options && formLayout.options.show === false;
}

/**
* Show a given fieldset.
* @param formLayout - object, angular6 json schema form layout
*/
show(formLayout) {
formLayout.options.show = true;
for (const item of formLayout.items) {
this.enableFormGroup(item);
}
}

/**
* Mark a layoutNode FormControl as enabled.
* @param layoutNode - object, angular6 json schema form layout node
*/
private enableFormGroup(layoutNode) {
const control = getControl(this.jsf.formGroup, layoutNode.dataPointer);
if (!control) {
return false;
}
control.enable();
return true;
}

/**
* Mark a layoutNode FormControl as disabled.
* @param layoutNode - object, angular6 json schema form layout node
*/
private disableFormGroup(layoutNode) {
const control = getControl(this.jsf.formGroup, layoutNode.dataPointer);
if (!control) {
return false;
}
control.disable();
return true;
}

}