the activator

This commit is contained in:
Adam Bradley
2015-07-23 00:51:29 -05:00
parent d67ba6f7c1
commit 96a84e593b
10 changed files with 274 additions and 34 deletions

View File

@ -1,16 +1,15 @@
import {Directive, ElementRef, Optional, Ancestor, onDestroy} from 'angular2/angular2';
import {Directive, ElementRef, Optional, Ancestor, onDestroy, NgZone} from 'angular2/angular2';
import {IonicConfig} from '../../config/config';
import {Gesture} from '../../gestures/gesture';
import {Activator} from '../../util/activator';
import * as dom from '../../util/dom';
@Directive({
selector: 'button,[button]'
})
export class Button {
constructor(elementRef: ElementRef, config: IonicConfig) {
}
}
export class Button {}
@Directive({
selector: '[tap-disabled]'
@ -19,43 +18,126 @@ export class TapDisabled {}
@Directive({
selector: 'a,button,[tappable]'
selector: 'a,button,[tappable]',
host: {
'(^touchstart)': 'touchStart($event)',
'(^touchend)': 'touchEnd($event)',
'(^touchcancel)': 'pointerCancel()',
'(^mousedown)': 'mouseDown($event)',
'(^mouseup)': 'mouseUp($event)',
'(^click)': 'click($event)',
}
})
export class TapClick {
constructor(
elementRef: ElementRef,
config: IonicConfig,
ngZone: NgZone,
@Optional() @Ancestor() tapDisabled: TapDisabled
) {
this.ele = elementRef.nativeElement;
this.tapEnabled = !tapDisabled;
this.tapPolyfill = config.setting('tapPolyfill');
this.zone = ngZone;
if (config.setting('tapPolyfill') && !this.tapGesture && !tapDisabled) {
this.tapGesture = new Gesture(elementRef.nativeElement);
this.tapGesture.listen();
this.tapGesture.on('tap', (gestureEv) => {
this.onTap(gestureEv.gesture.srcEvent);
});
}
let self = this;
self.pointerMove = function(ev) {
let moveCoord = dom.pointerCoord(ev);
console.log('pointerMove', moveCoord, self.start)
if ( dom.hasPointerMoved(10, self.start, moveCoord) ) {
self.pointerCancel();
}
};
}
onTap(ev) {
if (ev && this.tapGesture) {
touchStart(ev) {
this.pointerStart(ev);
}
touchEnd(ev) {
let self = this;
if (this.tapPolyfill && this.tapEnabled) {
let endCoord = dom.pointerCoord(ev);
this.disableClick = true;
this.zone.runOutsideAngular(() => {
clearTimeout(self.disableTimer);
self.disableTimer = setTimeout(() => {
self.disableClick = false;
}, 600);
});
if ( this.start && !dom.hasPointerMoved(3, this.start, endCoord) ) {
let clickEvent = document.createEvent('MouseEvents');
clickEvent.initMouseEvent('click', true, true, window, 1, 0, 0, endCoord.x, endCoord.y, false, false, false, false, 0, null);
clickEvent.isIonicTap = true;
this.ele.dispatchEvent(clickEvent);
}
}
this.pointerEnd();
}
mouseDown(ev) {
if (this.disableClick) {
ev.preventDefault();
ev.stopPropagation();
let c = dom.pointerCoord(ev);
} else {
this.pointerStart(ev);
}
}
let clickEvent = document.createEvent("MouseEvents");
clickEvent.initMouseEvent('click', true, true, window, 1, 0, 0, c.x, c.y, false, false, false, false, 0, null);
clickEvent.isIonicTap = true;
this.tapGesture.element.dispatchEvent(clickEvent);
mouseUp(ev) {
if (this.disableClick) {
ev.preventDefault();
ev.stopPropagation();
}
this.pointerEnd();
}
pointerStart(ev) {
this.start = dom.pointerCoord(ev);
this.zone.runOutsideAngular(() => {
Activator.start(ev.currentTarget);
Activator.moveListeners(this.pointerMove, true);
});
}
pointerEnd() {
this.zone.runOutsideAngular(() => {
Activator.end();
Activator.moveListeners(this.pointerMove, false);
});
}
pointerCancel() {
this.start = null;
this.zone.runOutsideAngular(() => {
Activator.clear();
Activator.moveListeners(this.pointerMove, false);
});
}
click(ev) {
if (!ev.isIonicTap) {
if (this.disableClick || !this.start) {
ev.preventDefault();
ev.stopPropagation();
}
}
}
onDestroy() {
this.tapGesture && this.tapGesture.destroy();
this.ele = null;
}
}