angular2-json-schema-form
Version:
Angular 2 JSON Schema Form builder
68 lines (59 loc) • 1.96 kB
text/typescript
import { Component, DoCheck, Input, OnInit } from '@angular/core';
import { FormGroup } from '@angular/forms';
import { JsonSchemaFormService } from '../library/json-schema-form.service';
export class AddReferenceComponent implements OnInit, DoCheck {
private options: any;
private itemCount: number;
private showAddButton: boolean = true;
private previousLayoutIndex: number[];
private previousDataIndex: number[];
formID: number;
layoutNode: any;
layoutIndex: number[];
dataIndex: number[];
constructor(
private jsf: JsonSchemaFormService
) { }
ngOnInit() {
this.options = this.layoutNode.options;
this.updateControl();
}
ngDoCheck() {
if (this.previousLayoutIndex !== this.layoutIndex ||
this.previousDataIndex !== this.dataIndex
) {
this.updateControl();
}
}
private addItem(event) {
event.preventDefault();
this.itemCount = this.layoutIndex[this.layoutIndex.length - 1] + 1;
this.jsf.addItem(this);
this.updateControl();
}
private updateControl() {
this.itemCount = this.layoutIndex[this.layoutIndex.length - 1];
this.previousLayoutIndex = this.layoutIndex;
this.previousDataIndex = this.dataIndex;
this.showAddButton = this.layoutNode.arrayItem &&
this.itemCount < (this.options.maxItems || 1000000);
}
private setTitle(): string {
const parent: any = {
dataIndex: this.dataIndex.slice(0, -1),
layoutNode: this.jsf.getParentNode(this)
};
return this.jsf.setTitle(parent, this.layoutNode, this.itemCount);
}
}