UNPKG

7.69 kBJavaScriptView Raw
1var __extends = (this && this.__extends) || (function () {
2 var extendStatics = Object.setPrototypeOf ||
3 ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
4 function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
5 return function (d, b) {
6 extendStatics(d, b);
7 function __() { this.constructor = d; }
8 d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
9 };
10})();
11import { Component, ElementRef, HostListener, Input, NgZone, Optional, Renderer, ViewEncapsulation } from '@angular/core';
12import { NG_VALUE_ACCESSOR } from '@angular/forms';
13import { Config } from '../../config/config';
14import { DomController } from '../../platform/dom-controller';
15import { Form } from '../../util/form';
16import { GestureController } from '../../gestures/gesture-controller';
17import { Haptic } from '../../tap-click/haptic';
18import { isTrueProperty } from '../../util/util';
19import { BaseInput } from '../../util/base-input';
20import { Item } from '../item/item';
21import { KEY_ENTER, KEY_SPACE } from '../../platform/key';
22import { Platform } from '../../platform/platform';
23import { ToggleGesture } from './toggle-gesture';
24/**
25 * @name Toggle
26 * @description
27 * A toggle technically is the same thing as an HTML checkbox input,
28 * except it looks different and is easier to use on a touch device.
29 * Toggles can also have colors assigned to them, by adding any color
30 * attribute.
31 *
32 * See the [Angular Docs](https://angular.io/docs/ts/latest/guide/forms)
33 * for more info on forms and inputs.
34 *
35 * @usage
36 * ```html
37 *
38 * <ion-list>
39 *
40 * <ion-item>
41 * <ion-label>Pepperoni</ion-label>
42 * <ion-toggle [(ngModel)]="pepperoni"></ion-toggle>
43 * </ion-item>
44 *
45 * <ion-item>
46 * <ion-label>Sausage</ion-label>
47 * <ion-toggle [(ngModel)]="sausage" disabled="true"></ion-toggle>
48 * </ion-item>
49 *
50 * <ion-item>
51 * <ion-label>Mushrooms</ion-label>
52 * <ion-toggle [(ngModel)]="mushrooms"></ion-toggle>
53 * </ion-item>
54 *
55 * </ion-list>
56 * ```
57 *
58 * @demo /docs/demos/src/toggle/
59 * @see {@link /docs/components#toggle Toggle Component Docs}
60 */
61var Toggle = (function (_super) {
62 __extends(Toggle, _super);
63 function Toggle(form, config, _plt, elementRef, renderer, _haptic, item, _gestureCtrl, _domCtrl, _zone) {
64 var _this = _super.call(this, config, elementRef, renderer, 'toggle', false, form, item, null) || this;
65 _this._plt = _plt;
66 _this._haptic = _haptic;
67 _this._gestureCtrl = _gestureCtrl;
68 _this._domCtrl = _domCtrl;
69 _this._zone = _zone;
70 _this._activated = false;
71 return _this;
72 }
73 Object.defineProperty(Toggle.prototype, "checked", {
74 /**
75 * @input {boolean} If true, the element is selected.
76 */
77 get: function () {
78 return this.value;
79 },
80 set: function (val) {
81 this.value = val;
82 },
83 enumerable: true,
84 configurable: true
85 });
86 /**
87 * @hidden
88 */
89 Toggle.prototype.ngAfterContentInit = function () {
90 this._initialize();
91 this._gesture = new ToggleGesture(this._plt, this, this._gestureCtrl, this._domCtrl);
92 this._gesture.listen();
93 };
94 /**
95 * @hidden
96 */
97 Toggle.prototype._inputUpdated = function () { };
98 /**
99 * @hidden
100 */
101 Toggle.prototype._inputNormalize = function (val) {
102 return isTrueProperty(val);
103 };
104 /**
105 * @hidden
106 */
107 Toggle.prototype._onDragStart = function (startX) {
108 var _this = this;
109 (void 0) /* assert */;
110 (void 0) /* console.debug */;
111 this._zone.run(function () {
112 _this._startX = startX;
113 _this._fireFocus();
114 _this._activated = true;
115 });
116 };
117 /**
118 * @hidden
119 */
120 Toggle.prototype._onDragMove = function (currentX) {
121 var _this = this;
122 if (this._startX === undefined) {
123 (void 0) /* assert */;
124 return;
125 }
126 if (this._shouldToggle(currentX, -15)) {
127 this._zone.run(function () {
128 _this.value = !_this.value;
129 _this._startX = currentX;
130 _this._haptic.selection();
131 });
132 }
133 };
134 /**
135 * @hidden
136 */
137 Toggle.prototype._onDragEnd = function (endX) {
138 var _this = this;
139 if (this._startX === undefined) {
140 (void 0) /* assert */;
141 return;
142 }
143 (void 0) /* console.debug */;
144 this._zone.run(function () {
145 if (_this._shouldToggle(endX, 4)) {
146 _this.value = !_this.value;
147 _this._haptic.selection();
148 }
149 _this._activated = false;
150 _this._fireBlur();
151 _this._startX = null;
152 });
153 };
154 /**
155 * @hidden
156 */
157 Toggle.prototype._shouldToggle = function (currentX, margin) {
158 var isLTR = !this._plt.isRTL;
159 var startX = this._startX;
160 if (this._value) {
161 return (isLTR && (startX + margin > currentX)) ||
162 (!isLTR && (startX - margin < currentX));
163 }
164 else {
165 return (isLTR && (startX - margin < currentX)) ||
166 (!isLTR && (startX + margin > currentX));
167 }
168 };
169 /**
170 * @hidden
171 */
172 Toggle.prototype._keyup = function (ev) {
173 if (ev.keyCode === KEY_SPACE || ev.keyCode === KEY_ENTER) {
174 (void 0) /* console.debug */;
175 ev.preventDefault();
176 ev.stopPropagation();
177 this.value = !this.value;
178 }
179 };
180 /**
181 * @hidden
182 */
183 Toggle.prototype.ngOnDestroy = function () {
184 _super.prototype.ngOnDestroy.call(this);
185 this._gesture && this._gesture.destroy();
186 };
187 Toggle.decorators = [
188 { type: Component, args: [{
189 selector: 'ion-toggle',
190 template: '<div class="toggle-icon">' +
191 '<div class="toggle-inner"></div>' +
192 '</div>' +
193 '<button role="checkbox" ' +
194 'type="button" ' +
195 'ion-button="item-cover" ' +
196 '[id]="id" ' +
197 '[attr.aria-checked]="_value" ' +
198 '[attr.aria-labelledby]="_labelId" ' +
199 '[attr.aria-disabled]="_disabled" ' +
200 'class="item-cover" disable-activated>' +
201 '</button>',
202 host: {
203 '[class.toggle-disabled]': '_disabled',
204 '[class.toggle-checked]': '_value',
205 '[class.toggle-activated]': '_activated',
206 },
207 providers: [{ provide: NG_VALUE_ACCESSOR, useExisting: Toggle, multi: true }],
208 encapsulation: ViewEncapsulation.None,
209 },] },
210 ];
211 /** @nocollapse */
212 Toggle.ctorParameters = function () { return [
213 { type: Form, },
214 { type: Config, },
215 { type: Platform, },
216 { type: ElementRef, },
217 { type: Renderer, },
218 { type: Haptic, },
219 { type: Item, decorators: [{ type: Optional },] },
220 { type: GestureController, },
221 { type: DomController, },
222 { type: NgZone, },
223 ]; };
224 Toggle.propDecorators = {
225 'checked': [{ type: Input },],
226 '_keyup': [{ type: HostListener, args: ['keyup', ['$event'],] },],
227 };
228 return Toggle;
229}(BaseInput));
230export { Toggle };
231//# sourceMappingURL=toggle.js.map
\No newline at end of file