|
1 | | -import { |
2 | | - Directive, HostBinding, HostListener, Input, OnInit, Self, ElementRef |
3 | | -} from '@angular/core'; |
4 | | -import { ControlValueAccessor, NgModel } from '@angular/forms'; |
| 1 | +import { Directive, HostBinding, HostListener, Input, OnInit, forwardRef } from '@angular/core'; |
| 2 | +import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms'; |
5 | 3 |
|
6 | | -// TODO: config: activeClass - Class to apply to the checked buttons. |
| 4 | +// TODO: config: activeClass - Class to apply to the checked buttons |
7 | 5 |
|
8 | | -@Directive({selector: '[btnCheckbox][ngModel]'}) |
| 6 | +export const CHECKBOX_CONTROL_VALUE_ACCESSOR: any = { |
| 7 | + provide: NG_VALUE_ACCESSOR, |
| 8 | + useExisting: forwardRef(() => ButtonCheckboxDirective), |
| 9 | + multi: true |
| 10 | +}; |
| 11 | + |
| 12 | +@Directive({selector: '[btnCheckbox]', providers: [CHECKBOX_CONTROL_VALUE_ACCESSOR]}) |
9 | 13 | export class ButtonCheckboxDirective implements ControlValueAccessor, OnInit { |
10 | | - public cd:NgModel; |
11 | | - public el: ElementRef; |
12 | | - @Input() public btnCheckboxTrue:any; |
| 14 | + @Input() public btnCheckboxTrue: any; |
| 15 | + @Input() public btnCheckboxFalse: any; |
13 | 16 |
|
14 | | - @Input() public btnCheckboxFalse:any; |
15 | | - @HostBinding('class.active') |
16 | | - public state:boolean = false; |
| 17 | + @HostBinding('class.active') public state: boolean = false; |
17 | 18 |
|
18 | | - protected onChange:any = Function.prototype; |
19 | | - protected onTouched:any = Function.prototype; |
| 19 | + protected value: any; |
| 20 | + protected isDisabled: boolean; |
20 | 21 |
|
21 | | - protected value:any; |
| 22 | + protected onChange: any = Function.prototype; |
| 23 | + protected onTouched: any = Function.prototype; |
22 | 24 |
|
23 | 25 | // view -> model |
24 | 26 | @HostListener('click') |
25 | | - public onClick():void { |
26 | | - if (this.el.nativeElement.attributes.disabled) { |
| 27 | + public onClick(): void { |
| 28 | + if (this.isDisabled) { |
27 | 29 | return; |
28 | 30 | } |
29 | | - this.toggle(!this.state); |
30 | | - this.cd.viewToModelUpdate(this.value); |
31 | | - } |
32 | 31 |
|
33 | | - public constructor(@Self() cd:NgModel, el: ElementRef) { |
34 | | - this.el = el; |
35 | | - this.cd = cd; |
36 | | - // hack ! |
37 | | - cd.valueAccessor = this; |
| 32 | + this.toggle(!this.state); |
| 33 | + this.onChange(this.value); |
38 | 34 | } |
39 | 35 |
|
40 | | - public ngOnInit():any { |
| 36 | + public ngOnInit(): any { |
41 | 37 | this.toggle(this.trueValue === this.value); |
42 | 38 | } |
43 | 39 |
|
44 | | - protected get trueValue():boolean { |
| 40 | + protected get trueValue(): boolean { |
45 | 41 | return typeof this.btnCheckboxTrue !== 'undefined' |
46 | 42 | ? this.btnCheckboxTrue |
47 | 43 | : true; |
48 | 44 | } |
49 | 45 |
|
50 | | - protected get falseValue():boolean { |
| 46 | + protected get falseValue(): boolean { |
51 | 47 | return typeof this.btnCheckboxFalse !== 'undefined' |
52 | 48 | ? this.btnCheckboxFalse |
53 | 49 | : false; |
54 | 50 | } |
55 | 51 |
|
56 | | - public toggle(state:boolean):void { |
| 52 | + public toggle(state: boolean): void { |
57 | 53 | this.state = state; |
58 | 54 | this.value = this.state ? this.trueValue : this.falseValue; |
59 | 55 | } |
60 | 56 |
|
61 | 57 | // ControlValueAccessor |
62 | 58 | // model -> view |
63 | | - public writeValue(value:any):void { |
| 59 | + public writeValue(value: any): void { |
64 | 60 | this.state = this.trueValue === value; |
65 | | - this.value = value; |
| 61 | + this.value = value ? this.trueValue : this.falseValue; |
| 62 | + } |
| 63 | + |
| 64 | + public setDisabledState(isDisabled: boolean): void { |
| 65 | + this.isDisabled = isDisabled; |
66 | 66 | } |
67 | 67 |
|
68 | | - public registerOnChange(fn:(_:any) => {}):void { |
| 68 | + public registerOnChange(fn: (_: any) => {}): void { |
69 | 69 | this.onChange = fn; |
70 | 70 | } |
71 | 71 |
|
72 | | - public registerOnTouched(fn:() => {}):void { |
| 72 | + public registerOnTouched(fn: () => {}): void { |
73 | 73 | this.onTouched = fn; |
74 | 74 | } |
75 | 75 | } |
0 commit comments