ng2-bootstrap-base-modified
Version:
Native Angular Bootstrap Components Typeahead modified
81 lines (64 loc) • 2.24 kB
text/typescript
import { Directive, HostBinding, HostListener, Input, OnInit, forwardRef } from '@angular/core';
import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms';
// TODO: config: activeClass - Class to apply to the checked buttons
export const CHECKBOX_CONTROL_VALUE_ACCESSOR: any = {
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => ButtonCheckboxDirective),
multi: true
};
/**
* Add checkbox functionality to any element
*/
export class ButtonCheckboxDirective implements ControlValueAccessor, OnInit {
/** Truthy value, will be set to ngModel */
public btnCheckboxTrue: any = true;
/** Falsy value, will be set to ngModel */
public btnCheckboxFalse: any = false;
public state: boolean = false;
protected value: any;
protected isDisabled: boolean;
protected onChange: any = Function.prototype;
protected onTouched: any = Function.prototype;
// view -> model
public onClick(): void {
if (this.isDisabled) {
return;
}
this.toggle(!this.state);
this.onChange(this.value);
}
public ngOnInit(): any {
this.toggle(this.trueValue === this.value);
}
protected get trueValue(): boolean {
return typeof this.btnCheckboxTrue !== 'undefined'
? this.btnCheckboxTrue
: true;
}
protected get falseValue(): boolean {
return typeof this.btnCheckboxFalse !== 'undefined'
? this.btnCheckboxFalse
: false;
}
public toggle(state: boolean): void {
this.state = state;
this.value = this.state ? this.trueValue : this.falseValue;
}
// ControlValueAccessor
// model -> view
public writeValue(value: any): void {
this.state = this.trueValue === value;
this.value = value ? this.trueValue : this.falseValue;
}
public setDisabledState(isDisabled: boolean): void {
this.isDisabled = isDisabled;
}
public registerOnChange(fn: (_: any) => {}): void {
this.onChange = fn;
}
public registerOnTouched(fn: () => {}): void {
this.onTouched = fn;
}
}