mirror of
https://github.com/ionic-team/ionic-framework.git
synced 2025-08-22 05:21:52 +08:00
refactor(toggle): uses new UIEventManager
This commit is contained in:
@ -5,6 +5,7 @@ import {Form} from '../../util/form';
|
|||||||
import {isTrueProperty} from '../../util/util';
|
import {isTrueProperty} from '../../util/util';
|
||||||
import {Item} from '../item/item';
|
import {Item} from '../item/item';
|
||||||
import {pointerCoord} from '../../util/dom';
|
import {pointerCoord} from '../../util/dom';
|
||||||
|
import {UIEventManager} from '../../util/ui-event-manager';
|
||||||
|
|
||||||
|
|
||||||
const TOGGLE_VALUE_ACCESSOR = new Provider(
|
const TOGGLE_VALUE_ACCESSOR = new Provider(
|
||||||
@ -87,6 +88,7 @@ export class Toggle implements ControlValueAccessor {
|
|||||||
private _startX: number;
|
private _startX: number;
|
||||||
private _msPrv: number = 0;
|
private _msPrv: number = 0;
|
||||||
private _fn: Function;
|
private _fn: Function;
|
||||||
|
private _events: UIEventManager = new UIEventManager();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @private
|
* @private
|
||||||
@ -113,27 +115,14 @@ export class Toggle implements ControlValueAccessor {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
private pointerDown(ev: UIEvent): boolean {
|
||||||
* @private
|
|
||||||
*/
|
|
||||||
private pointerDown(ev: UIEvent) {
|
|
||||||
if (this._isPrevented(ev)) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
this._startX = pointerCoord(ev).x;
|
this._startX = pointerCoord(ev).x;
|
||||||
this._activated = true;
|
this._activated = true;
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @private
|
|
||||||
*/
|
|
||||||
private pointerMove(ev: UIEvent) {
|
private pointerMove(ev: UIEvent) {
|
||||||
if (this._startX) {
|
if (this._startX) {
|
||||||
if (this._isPrevented(ev)) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
let currentX = pointerCoord(ev).x;
|
let currentX = pointerCoord(ev).x;
|
||||||
console.debug('toggle, pointerMove', ev.type, currentX);
|
console.debug('toggle, pointerMove', ev.type, currentX);
|
||||||
|
|
||||||
@ -152,16 +141,8 @@ export class Toggle implements ControlValueAccessor {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @private
|
|
||||||
*/
|
|
||||||
private pointerUp(ev: UIEvent) {
|
private pointerUp(ev: UIEvent) {
|
||||||
if (this._startX) {
|
if (this._startX) {
|
||||||
|
|
||||||
if (this._isPrevented(ev)) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
let endX = pointerCoord(ev).x;
|
let endX = pointerCoord(ev).x;
|
||||||
|
|
||||||
if (this.checked) {
|
if (this.checked) {
|
||||||
@ -188,9 +169,7 @@ export class Toggle implements ControlValueAccessor {
|
|||||||
this.onChange(this._checked);
|
this.onChange(this._checked);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @private
|
|
||||||
*/
|
|
||||||
private _setChecked(isChecked: boolean) {
|
private _setChecked(isChecked: boolean) {
|
||||||
if (isChecked !== this._checked) {
|
if (isChecked !== this._checked) {
|
||||||
this._checked = isChecked;
|
this._checked = isChecked;
|
||||||
@ -256,6 +235,11 @@ export class Toggle implements ControlValueAccessor {
|
|||||||
*/
|
*/
|
||||||
ngAfterContentInit() {
|
ngAfterContentInit() {
|
||||||
this._init = true;
|
this._init = true;
|
||||||
|
this._events.pointerEventsRef(this._elementRef,
|
||||||
|
(ev: any) => this.pointerDown(ev),
|
||||||
|
(ev: any) => this.pointerMove(ev),
|
||||||
|
(ev: any) => this.pointerUp(ev)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -263,20 +247,7 @@ export class Toggle implements ControlValueAccessor {
|
|||||||
*/
|
*/
|
||||||
ngOnDestroy() {
|
ngOnDestroy() {
|
||||||
this._form.deregister(this);
|
this._form.deregister(this);
|
||||||
}
|
this._events.unlistenAll();
|
||||||
|
|
||||||
/**
|
|
||||||
* @private
|
|
||||||
*/
|
|
||||||
private _isPrevented(ev: UIEvent) {
|
|
||||||
if (ev.type.indexOf('touch') > -1) {
|
|
||||||
this._msPrv = Date.now() + 2000;
|
|
||||||
|
|
||||||
} else if (this._msPrv > Date.now() && ev.type.indexOf('mouse') > -1) {
|
|
||||||
ev.preventDefault();
|
|
||||||
ev.stopPropagation();
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user