Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 | import * as dom from '../dom/dom';
import {DomWatcher} from '../dom/dom-watcher';
import {type EventCallback, type EventDispatcher, EventManager} from './events';
export enum SwipeEvent {
UP = 'swipeUp',
DOWN = 'swipeDown',
LEFT = 'swipeLeft',
RIGHT = 'swipeRight',
}
/**
* A basic class that allows you to watch directional swipe events on a given
* element.
*
*
* Usage:
* ```ts
* const swiper = new Swipe(myElement);
*
* swiper.on(Swipe.Events.UP, ()=> {
* // swipe up.
* })
* swiper.on(Swipe.Events.LEFT, ()=> {
* // swipe left
* })
*
* // Later
* swiper.dispose();
* ```
*
*
*
* Events are also fired on the element itself.
*
* ```ts
* const swiper = new Swipe(myElement);
* myElement.addEventListener(Swipe.Events.UP, ()=> {
* // swipe up.
* })
* ```
*/
export class Swipe implements EventDispatcher {
private rootElement: HTMLElement;
private eventManager: EventManager;
private watcher: DomWatcher;
private x: number;
private y: number;
private mouseX: number;
private mouseY: number;
private allowSwipe = false;
constructor(rootElement: HTMLElement) {
this.rootElement = rootElement;
this.eventManager = new EventManager();
this.watcher = new DomWatcher();
this.listenToEvents();
}
static Events = SwipeEvent;
private listenToEvents() {
this.watcher.add({
element: this.rootElement as HTMLElement,
on: 'touchstart',
eventOptions: {passive: true},
callback: this.onPointerDown.bind(this),
});
this.watcher.add({
element: this.rootElement as HTMLElement,
on: 'touchend',
eventOptions: {passive: true},
callback: this.onPointerUp.bind(this),
});
this.watcher.add({
element: this.rootElement as HTMLElement,
on: 'touchmove',
eventOptions: {passive: true},
callback: this.onPointerMove.bind(this),
});
this.watcher.add({
element: this.rootElement as HTMLElement,
on: 'mousedown',
eventOptions: {passive: true},
callback: this.onPointerDown.bind(this),
});
this.watcher.add({
element: this.rootElement as HTMLElement,
on: 'mouseup',
eventOptions: {passive: true},
callback: this.onPointerUp.bind(this),
});
this.watcher.add({
element: this.rootElement as HTMLElement,
on: 'mousemove',
eventOptions: {passive: true},
callback: this.onPointerMove.bind(this),
});
}
private onPointerDown(e: TouchEvent | MouseEvent) {
if (window.TouchEvent && e instanceof TouchEvent) {
this.x = e.touches[0].clientX;
this.y = e.touches[0].clientY;
}
if (e instanceof MouseEvent) {
this.x = e.x;
this.y = e.y;
}
this.allowSwipe = true;
}
private onPointerUp() {
if (!this.x || !this.y || !this.allowSwipe) {
return;
}
const diffX = this.x - this.mouseX;
const diffY = this.y - this.mouseY;
// Determine swipe direction
if (Math.abs(diffX) > Math.abs(diffY)) {
if (diffX > 0) {
this.eventManager.dispatch(Swipe.Events.LEFT);
dom.event(this.rootElement, Swipe.Events.LEFT, {});
} else {
this.eventManager.dispatch(Swipe.Events.RIGHT);
dom.event(this.rootElement, Swipe.Events.RIGHT, {});
}
} else {
if (diffY > 0) {
this.eventManager.dispatch(Swipe.Events.UP);
dom.event(this.rootElement, Swipe.Events.UP, {});
} else {
this.eventManager.dispatch(Swipe.Events.DOWN);
dom.event(this.rootElement, Swipe.Events.DOWN, {});
}
}
this.allowSwipe = false;
}
private onPointerMove(e: TouchEvent | MouseEvent) {
if (window.TouchEvent && e instanceof TouchEvent) {
this.mouseX = e.touches[0].clientX;
this.mouseY = e.touches[0].clientY;
}
if (e instanceof MouseEvent) {
this.mouseX = e.x;
this.mouseY = e.y;
}
}
on(eventName: string, callback: Function) {
this.eventManager.on(eventName, callback as EventCallback);
}
off(eventName: string, callback: Function) {
this.eventManager.off(eventName, callback as EventCallback);
}
dispose() {
this.eventManager.dispose();
this.watcher.dispose();
}
}
|