import { Component, Input, OnInit } from '@angular/core';
import { FormArray, FormControl, FormGroup, Validators } from '@angular/forms';
import { Item } from '../../../item/item.model';
@Component({
selector: 'civ-create-item',
template: `
`,
styles: [ `
:host { display: block; width: 100% }
.num-sign { font-size: 0.6em; margin-right: 2px }
.num-sign, .item-number { font-weight: 600 }
.remove-resource { color: #ff5722; cursor: pointer; transition: 150ms linear color }
.remove-resource:hover { color: #8B3112 }
.input.text textarea { padding-left: 10px }
` ]
})
export class CreateItemComponent implements OnInit {
@Input() form: FormGroup;
static build(itemNo: number, item?: Item): FormGroup {
let resourceLinks: FormArray = new FormArray([]);
if (item && item.resourceLinks) {
item.resourceLinks.forEach(link =>
resourceLinks.push(new FormGroup({ url: new FormControl(link, [ Validators.required ]) }))
);
}
return new FormGroup({
itemNumber: new FormControl(itemNo || null),
text: new FormControl(item && item.text || '', [ Validators.required ]),
resourceLinks
});
}
hasResources() {
return (this.form.controls[ 'resourceLinks' ] as FormArray).controls.length > 0
}
addResource() {
if (!!this.form) {
(this.form.controls[ 'resourceLinks' ] as FormArray).push(
new FormGroup({ url: new FormControl('https://', [ Validators.required ]) })
)
}
}
removeResource(idx: number) {
(this.form.controls[ 'resourceLinks' ] as FormArray).removeAt(idx);
}
constructor() { }
ngOnInit() {
}
}