clr-checkbox-group.component.ts 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import { Component, Attribute, OnInit } from '@angular/core';
  2. import { FormControl } from '@angular/forms';
  3. import { GroupInputComponent } from '../../_abstract/group-input.component';
  4. @Component({
  5. selector: 'app-checkbox-group',
  6. templateUrl: './clr-checkbox-group.component.html',
  7. styleUrls: ['./clr-checkbox-group.component.scss']
  8. })
  9. export class ClrCheckboxGroupComponent extends GroupInputComponent implements OnInit {
  10. firstControl: FormControl;
  11. readonly componentName = 'CheckboxGroupComponent'; // For AOT compatibility, as class names don't survive minification
  12. constructor(
  13. @Attribute('firstEnablesRest') private firstEnablesRest,
  14. @Attribute('allOrNone') public showAllOrNone
  15. ) {
  16. super();
  17. this.firstEnablesRest = firstEnablesRest === ''; // True if 'firstEnablesRest' exists as component attribute
  18. this.showAllOrNone = showAllOrNone === ''; // True if 'firstEnablesRest' exists as component attribute
  19. }
  20. ngOnInit() {
  21. super.ngOnInit();
  22. if (this._meta.firstEnablesRest) {
  23. this.firstEnablesRest = this._meta.firstEnablesRest;
  24. }
  25. if (this._meta.showAllOrNone) {
  26. this.showAllOrNone = this._meta.showAllOrNone;
  27. }
  28. if (this.firstEnablesRest) {
  29. this.firstControl = this.formGoA.controls[this.controlNames[0]] as FormControl;
  30. this.childMetaArray[0].disabled = false;
  31. this.childMetaArray.slice(1).map(meta => { meta.disabled = !this.firstControl.value; return meta; });
  32. // Observe value changes on first control
  33. this.firstControl.valueChanges.subscribe(val => {
  34. for (let i = 1; i < this.childMetaArray.length; i++) {
  35. // NOTE: We reassign the input (array member) to trigger change detection (otherwise it doesn't run)
  36. // See https://juristr.com/blog/2016/04/angular2-change-detection/
  37. this.childMetaArray[i] = {
  38. ...this.childMetaArray[i],
  39. disabled: !val
  40. };
  41. }
  42. });
  43. }
  44. }
  45. selectAll(e: MouseEvent): false {
  46. this.controlNames.forEach(c => this.formGoA.get(c).setValue(this._meta.meta[c].value));
  47. (e.target as HTMLLinkElement).blur();
  48. return false;
  49. }
  50. selectNone(e: MouseEvent): false {
  51. this.controlNames.forEach(c => this.formGoA.get(c).setValue(null));
  52. (e.target as HTMLLinkElement).blur();
  53. return false;
  54. }
  55. }