forked from ionic-team/ionic-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtoggle.ts
More file actions
294 lines (263 loc) · 6.78 KB
/
toggle.ts
File metadata and controls
294 lines (263 loc) · 6.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
import { AfterContentInit, Component, ElementRef, EventEmitter, forwardRef, Input, OnDestroy, Optional, Output, Renderer, ViewEncapsulation } from '@angular/core';
import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms';
import { Config } from '../../config/config';
import { Form } from '../../util/form';
import { isTrueProperty } from '../../util/util';
import { Ion } from '../ion';
import { Item } from '../item/item';
import { pointerCoord } from '../../util/dom';
import { UIEventManager } from '../../util/ui-event-manager';
export const TOGGLE_VALUE_ACCESSOR: any = {
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => Toggle),
multi: true
};
/**
* @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/ts/latest/guide/forms.html)
* for more info on forms and inputs.
*
* @usage
* ```html
*
* <ion-list>
*
* <ion-item>
* <ion-label>Pepperoni</ion-label>
* <ion-toggle [(ngModel)]="pepperoni"></ion-toggle>
* </ion-item>
*
* <ion-item>
* <ion-label>Sausage</ion-label>
* <ion-toggle [(ngModel)]="sausage" disabled="true"></ion-toggle>
* </ion-item>
*
* <ion-item>
* <ion-label>Mushrooms</ion-label>
* <ion-toggle [(ngModel)]="mushrooms"></ion-toggle>
* </ion-item>
*
* </ion-list>
* ```
*
* @demo /docs/v2/demos/src/toggle/
* @see {@link /docs/v2/components#toggle Toggle Component Docs}
*/
@Component({
selector: 'ion-toggle',
template:
'<div class="toggle-icon" [class.toggle-checked]="_checked" [class.toggle-activated]="_activated">' +
'<div class="toggle-inner"></div>' +
'</div>' +
'<button role="checkbox" ' +
'type="button" ' +
'ion-button="item-cover" ' +
'[id]="id" ' +
'[attr.aria-checked]="_checked" ' +
'[attr.aria-labelledby]="_labelId" ' +
'[attr.aria-disabled]="_disabled" ' +
'class="item-cover">' +
'</button>',
host: {
'[class.toggle-disabled]': '_disabled'
},
providers: [TOGGLE_VALUE_ACCESSOR],
encapsulation: ViewEncapsulation.None,
})
export class Toggle extends Ion implements AfterContentInit, ControlValueAccessor, OnDestroy {
/** @private */
_checked: boolean = false;
/** @private */
_init: boolean;
/** @private */
_disabled: boolean = false;
/** @private */
_labelId: string;
/** @private */
_activated: boolean = false;
/** @private */
_startX: number;
/** @private */
_msPrv: number = 0;
/** @private */
_fn: Function;
/** @private */
_events: UIEventManager = new UIEventManager();
/**
* @private
*/
id: string;
/**
* @input {string} The predefined color to use. For example: `"primary"`, `"secondary"`, `"danger"`.
*/
@Input()
set color(val: string) {
this._setColor('toggle', val);
}
/**
* @input {string} The mode to apply to this component.
*/
@Input()
set mode(val: string) {
this._setMode('toggle', val);
}
/**
* @output {Toggle} expression to evaluate when the toggle value changes
*/
@Output() ionChange: EventEmitter<Toggle> = new EventEmitter<Toggle>();
constructor(
public _form: Form,
config: Config,
elementRef: ElementRef,
renderer: Renderer,
@Optional() public _item: Item
) {
super(config, elementRef, renderer);
this.mode = config.get('mode');
_form.register(this);
if (_item) {
this.id = 'tgl-' + _item.registerInput('toggle');
this._labelId = 'lbl-' + _item.id;
this._item.setElementClass('item-toggle', true);
}
}
/**
* @private
*/
pointerDown(ev: UIEvent): boolean {
this._startX = pointerCoord(ev).x;
this._activated = true;
return true;
}
/**
* @private
*/
pointerMove(ev: UIEvent) {
if (this._startX) {
let currentX = pointerCoord(ev).x;
console.debug('toggle, pointerMove', ev.type, currentX);
if (this._checked) {
if (currentX + 15 < this._startX) {
this.onChange(false);
this._startX = currentX;
this._activated = true;
}
} else if (currentX - 15 > this._startX) {
this.onChange(true);
this._startX = currentX;
this._activated = (currentX < this._startX + 5);
}
}
}
/**
* @private
*/
pointerUp(ev: UIEvent) {
if (this._startX) {
let endX = pointerCoord(ev).x;
if (this.checked) {
if (this._startX + 4 > endX) {
this.onChange(false);
}
} else if (this._startX - 4 < endX) {
this.onChange(true);
}
this._activated = false;
this._startX = null;
}
}
/**
* @input {boolean} whether the toggle it toggled or not
*/
@Input()
get checked(): boolean {
return this._checked;
}
set checked(val: boolean) {
this._setChecked(isTrueProperty(val));
this.onChange(this._checked);
}
/**
* @private
*/
_setChecked(isChecked: boolean) {
if (!this._disabled && isChecked !== this._checked) {
this._checked = isChecked;
if (this._init) {
this.ionChange.emit(this);
}
this._item && this._item.setElementClass('item-toggle-checked', isChecked);
}
}
/**
* @private
*/
writeValue(val: any) {
this._setChecked( isTrueProperty(val) );
}
/**
* @private
*/
registerOnChange(fn: Function): void {
this._fn = fn;
this.onChange = (isChecked: boolean) => {
console.debug('toggle, onChange', isChecked);
fn(isChecked);
this._setChecked(isChecked);
this.onTouched();
};
}
/**
* @private
*/
registerOnTouched(fn: any) { this.onTouched = fn; }
/**
* @input {boolean} whether the toggle is disabled or not
*/
@Input()
get disabled(): boolean {
return this._disabled;
}
set disabled(val: boolean) {
this._disabled = isTrueProperty(val);
this._item && this._item.setElementClass('item-toggle-disabled', this._disabled);
}
/**
* @private
*/
onChange(isChecked: boolean) {
// used when this input does not have an ngModel or formControlName
console.debug('toggle, onChange (no ngModel)', isChecked);
this._setChecked(isChecked);
this.onTouched();
}
/**
* @private
*/
onTouched() {}
/**
* @private
*/
ngAfterContentInit() {
this._init = true;
this._events.pointerEvents({
elementRef: this._elementRef,
pointerDown: this.pointerDown.bind(this),
pointerMove: this.pointerMove.bind(this),
pointerUp: this.pointerUp.bind(this)
});
}
/**
* @private
*/
ngOnDestroy() {
this._form.deregister(this);
this._events.unlistenAll();
}
}