import { Component, Attribute, OnInit } from '@angular/core'; import { FormControl } from '@angular/forms'; import { GroupInputComponent } from './../../_abstract/group-input.component'; @Component({ selector: 'app-checkbutton-group', templateUrl: './checkbutton-group.component.html', styleUrls: ['./checkbutton-group.component.scss'] }) export class CheckbuttonGroupComponent extends GroupInputComponent implements OnInit { firstControl: FormControl; readonly componentName = 'CheckbuttonGroupComponent'; // For AOT compatibility, as class names don't survive minification constructor( @Attribute('firstEnablesRest') private firstEnablesRest, @Attribute('allOrNone') public showAllOrNone ) { super(); this.firstEnablesRest = firstEnablesRest === ''; // True if 'firstEnablesRest' exists as component attribute this.showAllOrNone = showAllOrNone === ''; // True if 'firstEnablesRest' exists as component attribute } ngOnInit() { super.ngOnInit(); if (this._meta.firstEnablesRest) { this.firstEnablesRest = this._meta.firstEnablesRest; } if (this._meta.showAllOrNone) { this.showAllOrNone = this._meta.showAllOrNone; } if (this.firstEnablesRest) { this.firstControl = this.formGroup.controls[this.controlNames[0]] as FormControl; this.childMetaArray[0].disabled = false; this.childMetaArray.slice(1).map(meta => { meta.disabled = !this.firstControl.value; return meta; }); // Observe value changes on first control this.firstControl.valueChanges.subscribe(val => { for (let i = 1; i < this.childMetaArray.length; i++) { // NOTE: We reassign the input (array member) to trigger change detection (otherwise it doesn't run) // See https://juristr.com/blog/2016/04/angular2-change-detection/ this.childMetaArray[i] = { ...this.childMetaArray[i], disabled: !val }; } }); } } selectAll(e: MouseEvent): false { this.controlNames.forEach(c => this.formGroup.get(c).setValue(this._meta.meta[c].value)); (e.target as HTMLLinkElement).blur(); return false; } selectNone(e: MouseEvent): false { this.controlNames.forEach(c => this.formGroup.get(c).setValue(null)); (e.target as HTMLLinkElement).blur(); return false; } }