12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 |
- import { Component, Attribute, OnInit } from '@angular/core';
- import { FormControl } from '@angular/forms';
- import { GroupInputComponent } from '../../_abstract/group-input.component';
- @Component({
- selector: 'app-checkbox-group',
- templateUrl: './clr-checkbox-group.component.html',
- styleUrls: ['./clr-checkbox-group.component.scss']
- })
- export class ClrCheckboxGroupComponent extends GroupInputComponent implements OnInit {
- firstControl: FormControl;
- readonly componentName = 'CheckboxGroupComponent'; // 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.formGoA.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.formGoA.get(c).setValue(this._meta.meta[c].value));
- (e.target as HTMLLinkElement).blur();
- return false;
- }
- selectNone(e: MouseEvent): false {
- this.controlNames.forEach(c => this.formGoA.get(c).setValue(null));
- (e.target as HTMLLinkElement).blur();
- return false;
- }
- }
|