@wildebeest/touch
Advanced tools
| module.exports = { | ||
| transform: {'^.+\\.ts?$': 'ts-jest'}, | ||
| testEnvironment: 'jsdom', | ||
| testRegex: '/tests/.*\\.(test|spec)?\\.(ts|tsx)$', | ||
| moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'] | ||
| }; |
| import 'ts-jest'; | ||
| import { Application } from '@wildebeest/js-modules'; | ||
| import { TouchModule } from '../src/TouchModule'; | ||
| import { TouchElement } from '../src/TouchElement'; | ||
| import { DomService, EmitterService } from '@wildebeest/common'; | ||
| let app: Application = new Application(); | ||
| app.run([TouchModule]); | ||
| let domService: DomService = app.getContainer().get(DomService); | ||
| let emitterService: EmitterService = app.getContainer().get(EmitterService); | ||
| function touchEventPosition(x: number, y: number): any | ||
| { | ||
| return { | ||
| altitudeAngle: 0, | ||
| azimuthAngle: 0, | ||
| force: 1, | ||
| pageX: 0, | ||
| pageY: 0, | ||
| identifier: 0, | ||
| radiusX: 0, | ||
| radiusY: 0, | ||
| rotationAngle: 0, | ||
| screenX: 0, | ||
| screenY: 0, | ||
| target: null, | ||
| touchType: "direct", | ||
| clientX: x, | ||
| clientY: y | ||
| }; | ||
| } | ||
| test("", () => { | ||
| let element: HTMLElement = domService.create('<div></div>'); | ||
| domService.insert([element], document.body); | ||
| let touchElement: TouchElement = new TouchElement(element, emitterService.createEmitter()); | ||
| let value: any = { | ||
| vertical: 0, | ||
| horizontal: 0 | ||
| }; | ||
| touchElement.getEmitter().on('wbTouchscroll', (data: any) => { | ||
| value.vertical += data.vertical; | ||
| value.horizontal += data.horizontal; | ||
| }); | ||
| element.dispatchEvent(new TouchEvent('touchstart', { | ||
| changedTouches: [ | ||
| touchEventPosition(0, 0) | ||
| ] | ||
| })); | ||
| element.dispatchEvent(new TouchEvent('touchmove', { | ||
| changedTouches: [ | ||
| touchEventPosition(22, 13) | ||
| ] | ||
| })); | ||
| expect(value).toEqual({ | ||
| vertical: -13, | ||
| horizontal: -22 | ||
| }); | ||
| }); |
+8
-3
| { | ||
| "name": "@wildebeest/touch", | ||
| "version": "0.1.0", | ||
| "version": "0.2.0", | ||
| "description": "Touch event module", | ||
@@ -8,3 +8,4 @@ "main": "dist/index.js", | ||
| "scripts": { | ||
| "build": "tsc --declaration" | ||
| "build": "tsc --declaration", | ||
| "test": "jest" | ||
| }, | ||
@@ -16,5 +17,9 @@ "repository": { | ||
| "dependencies": { | ||
| "@wildebeest/common": "^0.1.0" | ||
| "@wildebeest/js-modules": "^0.1.0", | ||
| "@wildebeest/common": "^0.2.0" | ||
| }, | ||
| "devDependencies": { | ||
| "@types/jest": "^24.0.11", | ||
| "jest": "^24.7.1", | ||
| "ts-jest": "^24.0.2", | ||
| "typescript": "^3.4.3" | ||
@@ -21,0 +26,0 @@ }, |
+15
-14
@@ -7,3 +7,3 @@ import { Emitter } from "@wildebeest/common"; | ||
| protected emitter: Emitter; | ||
| protected touch: any = null; | ||
| protected touch: TouchEvent; | ||
@@ -14,3 +14,5 @@ constructor(element: any, emitter: Emitter) | ||
| this.emitter = emitter; | ||
| this.element.addEventListener('touchstart', this.setTouch.bind(this)); | ||
| this.element.addEventListener('touchstart', (event: TouchEvent) => { | ||
| this.touch = event; | ||
| }); | ||
| this.element.addEventListener('touchend', () => { | ||
@@ -27,13 +29,11 @@ this.touch = null; | ||
| protected setTouch(event: any): void | ||
| protected getFingerPosition(event: TouchEvent): any | ||
| { | ||
| this.touch = { | ||
| position: { | ||
| x: event.changedTouches[0].pageX, | ||
| y: event.changedTouches[0].pageY | ||
| } | ||
| return { | ||
| x: event.changedTouches[0].clientX, | ||
| y: event.changedTouches[0].clientY | ||
| }; | ||
| } | ||
| protected onMove(event: any): void | ||
| protected onMove(event: TouchEvent): void | ||
| { | ||
@@ -44,10 +44,11 @@ if (!this.touch) { | ||
| let prevTouch: any = this.touch; | ||
| this.setTouch(event); | ||
| let lastPosition: any = this.getFingerPosition(this.touch); | ||
| let currentPosition: any = this.getFingerPosition(event); | ||
| let diff = { | ||
| horizontal: prevTouch.position.x - this.touch.position.x, | ||
| vertical: prevTouch.position.y - this.touch.position.y, | ||
| horizontal: lastPosition.x - currentPosition.x, | ||
| vertical: lastPosition.y - currentPosition.y, | ||
| } | ||
| this.emitter.emit('touchScroll', diff); | ||
| this.touch = event; | ||
| this.emitter.emit('wbTouchscroll', diff); | ||
| } | ||
| } |
10568
25.08%18
12.5%213
40.13%2
100%4
300%+ Added
- Removed
Updated