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
260 lines (229 loc) · 6.1 KB
/
toggle.ts
File metadata and controls
260 lines (229 loc) · 6.1 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
import { AfterContentInit, Component, ElementRef, EventEmitter, forwardRef, Input, OnDestroy, Optional, Output, Provider, Renderer, ViewEncapsulation } from '@angular/core';
import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms';
import { Form } from '../../util/form';
import { isTrueProperty } from '../../util/util';
import { Item } from '../item/item';
import { pointerCoord } from '../../util/dom';
import { UIEventManager } from '../../util/ui-event-manager';
export const TOGGLE_VALUE_ACCESSOR = new Provider(
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/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"
category="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 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;
/**
* @output {Toggle} expression to evaluate when the toggle value changes
*/
@Output() ionChange: EventEmitter<Toggle> = new EventEmitter<Toggle>();
constructor(
private _form: Form,
private _elementRef: ElementRef,
private _renderer: Renderer,
@Optional() private _item: Item
) {
this._form.register(this);
if (_item) {
this.id = 'tgl-' + _item.registerInput('toggle');
this._labelId = 'lbl-' + _item.id;
this._item.setCssClass('item-toggle', true);
}
}
/**
* @private
*/
private pointerDown(ev: UIEvent): boolean {
this._startX = pointerCoord(ev).x;
this._activated = true;
return true;
}
/**
* @private
*/
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
*/
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 (isChecked !== this._checked) {
this._checked = isChecked;
if (this._init) {
this.ionChange.emit(this);
}
this._item && this._item.setCssClass('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.setCssClass('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.pointerEventsRef(this._elementRef,
(ev: any) => this.pointerDown(ev),
(ev: any) => this.pointerMove(ev),
(ev: any) => this.pointerUp(ev)
);
}
/**
* @private
*/
ngOnDestroy() {
this._form.deregister(this);
this._events.unlistenAll();
}
}