import {Component, Directive, Optional, ElementRef, Input, Renderer, HostListener} from 'angular2/core'; import {NgControl} from 'angular2/common'; import {Form} from '../../util/form'; /** * The checkbox is no different than the HTML checkbox input, except it's styled differently. * * See the [Angular 2 Docs](https://angular.io/docs/js/latest/api/core/Form-interface.html) for more info on forms and input. * * @property [checked] - whether or not the checkbox is checked (defaults to false) * @property [value] - the value of the checkbox component * @property [disabled] - whether or not the checkbox is disabled or not. * * @usage * ```html * * HTML5 * * ``` * @demo /docs/v2/demos/checkbox/ * @see {@link /docs/v2/components#checkbox Checkbox Component Docs} */ @Component({ selector: 'ion-checkbox', host: { 'role': 'checkbox', 'class': 'item', 'tappable': '', 'tabindex': '0', '[attr.aria-disabled]': 'disabled' }, template: '
' + '
' + '
' + '
' + '' + '' + '' + '
' }) export class Checkbox { private _checked: boolean; private labelId: string; @Input() value: string = ''; @Input() disabled: boolean = false; @Input() id: string; constructor( private _form: Form, private _elementRef: ElementRef, private _renderer: Renderer, @Optional() ngControl: NgControl ) { _form.register(this); if (ngControl) { ngControl.valueAccessor = this; } } /** * @private */ ngOnInit() { if (!this.id) { this.id = 'chk-' + this._form.nextId(); this._renderer.setElementAttribute(this._elementRef, 'id', this.id); } this.labelId = 'lbl-' + this.id; this._renderer.setElementAttribute(this._elementRef, 'aria-labelledby', this.labelId); } /** * @private * Toggle the checked state of the checkbox. Calls onChange to pass the updated checked state to the model (Control). */ toggle() { this.checked = !this.checked; } get checked() { return !!this._checked; } @Input() set checked(val) { this._checked = !!val; this._renderer.setElementAttribute(this._elementRef, 'aria-checked', this._checked.toString()); this.onChange(this._checked); } /** * @private */ @HostListener('click', ['$event']) _click(ev) { ev.preventDefault(); ev.stopPropagation(); this.toggle(); } /** * @private * Angular2 Forms API method called by the model (Control) on change to update * the checked value. * https://github.com/angular/angular/blob/master/modules/angular2/src/forms/directives/shared.ts#L34 */ writeValue(value) { this.checked = value; } /** * @private */ onChange(val) { // TODO: figure the whys and the becauses } /** * @private */ onTouched(val) { // TODO: figure the whys and the becauses } /** * @private * Angular2 Forms API method called by the view (NgControl) to register the * onChange event handler that updates the model (Control). * https://github.com/angular/angular/blob/master/modules/angular2/src/forms/directives/shared.ts#L27 * @param {Function} fn the onChange event handler. */ registerOnChange(fn) { this.onChange = fn; } /** * @private * Angular2 Forms API method called by the the view (NgControl) to register * the onTouched event handler that marks model (Control) as touched. * @param {Function} fn onTouched event handler. */ registerOnTouched(fn) { this.onTouched = fn; } /** * @private */ ngOnDestroy() { this._form.deregister(this); } }