import {Component, ElementRef, Renderer, Input, Optional} from 'angular2/core'; import {NgControl} from 'angular2/common'; import {Config} from '../../config/config'; import {Form} from '../../util/form'; import {Item} from '../item/item'; import {pointerCoord} from '../../util/dom'; /** * @name Toggle * @description * A toggle technically is the same thing as an HTML checkbox input, * except it looks different and is easier to use on a touch device. * Toggles can also have colors assigned to them, by adding any color * attribute. * * See the [Angular 2 Docs](https://angular.io/docs/js/latest/api/forms/) for more info on forms and input. * @property {any} [value] - the inital value of the toggle * @property {boolean} [checked] - whether the toggle it toggled or not * @property {boolean} [disabled] - whether the toggle is disabled or not * @property {string} [id] - a unique ID for a toggle * @usage * ```html * * * * * Pepperoni * * * * * Sausage * * * * * Mushrooms * * * * * ``` * @demo /docs/v2/demos/toggle/ * @see {@link /docs/v2/components#toggle Toggle Component Docs} */ @Component({ selector: 'ion-toggle,ion-switch', template: '
' + '
' + '
' + '', host: { '[class.toggle-disabled]': '_disabled' } }) export class Toggle { private _checked: any = false; private _disabled: any = false; private _labelId: string; private _activated: boolean = false; private _mode: string; private _startX; private _touched: number = 0; id: string; @Input() value: string = ''; constructor( private _form: Form, private _elementRef: ElementRef, private _renderer: Renderer, config: Config, @Optional() ngControl: NgControl, @Optional() private _item: Item ) { // deprecated warning if (_elementRef.nativeElement.tagName == 'ION-SWITCH') { console.warn(' has been renamed to , please update your HTML'); } _form.register(this); this._mode = config.get('mode'); if (ngControl) { ngControl.valueAccessor = this; } if (_item) { this.id = 'tgl-' + _item.registerInput('toggle'); this._labelId = 'lbl-' + _item.id; this._item.setCssClass('item-toggle', true); } } /** * @private * Toggle the checked state of this toggle. */ toggle() { this.checked = !this.checked; } @Input() get checked() { return this._checked; } set checked(val) { if (!this._disabled) { this._checked = (val === true || val === 'true'); this.onChange(this._checked); this._item && this._item.setCssClass('item-toggle-checked', this._checked); } } @Input() get disabled() { return this._disabled; } set disabled(val) { this._disabled = (val === true || val === 'true'); this._item && this._item.setCssClass('item-toggle-disabled', this._disabled); } /** * @private */ private pointerDown(ev) { if (/touch/.test(ev.type)) { this._touched = Date.now(); } if (this.isDisabled(ev)) { return; } this._startX = pointerCoord(ev).x; this._activated = true; } /** * @private */ private pointerMove(ev) { if (this._startX) { let currentX = pointerCoord(ev).x; console.debug('toggle move', ev.type, currentX); if (this._checked) { if (currentX + 15 < this._startX) { this.toggle(); this._startX = currentX; } } else if (currentX - 15 > this._startX) { this.toggle(); this._startX = currentX; } } } /** * @private */ private pointerUp(ev) { if (this._startX) { if (this.isDisabled(ev)) { return; } let endX = pointerCoord(ev).x; if (this.checked) { if (this._startX + 4 > endX) { this.toggle(); } } else if (this._startX - 4 < endX) { this.toggle(); } this._activated = false; this._startX = null; } } /** * @private */ 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 */ registerOnChange(fn) { this.onChange = fn; } /** * @private */ registerOnTouched(fn) { this.onTouched = fn; } /** * @private */ ngOnDestroy() { this._form.deregister(this); } /** * @private */ private isDisabled(ev) { return (this._touched + 999 > Date.now() && (ev.type.indexOf('mouse') > -1)) || (this._mode == 'ios' && ev.target.tagName == 'ION-TOGGLE'); } }