angular2-json-schema-form
Version:
Angular 2 JSON Schema Form builder
100 lines (92 loc) • 3.29 kB
text/typescript
import { Component, Input, OnInit } from '@angular/core';
import { FormArray, AbstractControl } from '@angular/forms';
import { JsonSchemaFormService, CheckboxItem } from '../../library/json-schema-form.service';
import { buildFormGroup, buildTitleMap, JsonPointer } from '../../library/utilities/index';
export class MaterialCheckboxesComponent implements OnInit {
private formControl: AbstractControl;
private controlName: string;
private controlValue: any;
private boundControl: boolean = false;
private options: any;
private horizontalList: boolean = false;
private formArray: AbstractControl;
private checkboxList: CheckboxItem[] = [];
formID: number;
layoutNode: any;
layoutIndex: number[];
dataIndex: number[];
constructor(
private jsf: JsonSchemaFormService
) { }
ngOnInit() {
this.options = this.layoutNode.options;
this.horizontalList = this.layoutNode.type === 'checkboxes-inline' ||
this.layoutNode.type === 'checkboxbuttons';
this.jsf.initializeControl(this);
this.checkboxList = buildTitleMap(
this.options.titleMap || this.options.enumNames, this.options.enum, true
);
if (this.boundControl) {
const formArray = this.jsf.getControl(this);
for (let checkboxItem of this.checkboxList) {
checkboxItem.checked = formArray.value.indexOf(checkboxItem.value) !== -1;
}
}
}
get allChecked(): boolean {
return this.checkboxList.filter(t => t.checked).length === this.checkboxList.length;
}
get someChecked(): boolean {
const checkedItems = this.checkboxList.filter(t => t.checked).length;
return checkedItems > 0 && checkedItems < this.checkboxList.length;
}
updateValue(event: any, checkAll: boolean = false) {
if (checkAll) {
this.checkboxList.forEach(t => t.checked = event.checked);
}
if (this.boundControl) {
this.jsf.updateArrayCheckboxList(this, this.checkboxList);
}
}
}