targetj 1.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,454 @@
1
+ function EventListener() {
2
+
3
+ this.currentTouch = {
4
+ deltaY: 0,
5
+ deltaX: 0,
6
+ pinchDelta: 0,
7
+ key: '',
8
+ manualMomentumFlag: false,
9
+ orientation: "none" ,
10
+ dir: "",
11
+ timeStamp: 0
12
+ };
13
+
14
+ this.eventMap = {
15
+ mousedown: 'touchstart',
16
+ touchstart: 'touchstart',
17
+ mousemove: 'touchmove',
18
+ touchmove: 'touchmove',
19
+ touchend: 'touchend',
20
+ touchcancel: 'touchend',
21
+ pointerup: 'touchend',
22
+ MSPointerUp: 'touchend',
23
+ mouseup: 'touchend',
24
+ pointercancel: 'touchend',
25
+ MSPointerCancel: 'touchend',
26
+ mousecancel: 'touchend',
27
+ mouseleave: 'touchleave',
28
+ wheel: 'wheel',
29
+ DOMMouseScroll: 'wheel',
30
+ mousewheel: 'wheel',
31
+ keyup: 'key',
32
+ resize: 'resize',
33
+ orientationchange: 'resize'
34
+ };
35
+
36
+ this.lastEvent = undefined;
37
+ this.eventName = "";
38
+ this.eventTagName = "";
39
+
40
+ this.cursor = { x: 0, y: 0};
41
+ this.start0 = undefined;
42
+ this.start1 = undefined;
43
+ this.end0 = undefined;
44
+ this.end1 = undefined;
45
+ this.touchCount = 0;
46
+
47
+ this.currentEvent = "";
48
+ this.currentHandlers = { touch: null, scrollLeft: null, scrollTop: null, pinch: null };
49
+ }
50
+
51
+ EventListener.prototype.removeHandlers = function () {
52
+ if (!this.bindedHandleEvent) return;
53
+
54
+ var self = this;
55
+ Object.keys(this.eventMap).forEach(function(key) {
56
+ var target = self.eventMap[key] === 'resize' ? tapp.window : tapp.$dom;
57
+ target.detachEvent(key, self.bindedHandleEvent);
58
+ });
59
+ };
60
+
61
+ EventListener.prototype.addHandlers = function () {
62
+ this.bindedHandleEvent = this.bindedHandleEvent ? this.bindedHandleEvent : this.handleEvent.bind(this);
63
+
64
+ var self = this;
65
+ Object.keys(this.eventMap).forEach(function(key) {
66
+ var target = self.eventMap[key] === 'resize' ? tapp.window : tapp.$dom;
67
+ target.addEvent(key, self.bindedHandleEvent);
68
+ });
69
+ };
70
+
71
+ EventListener.prototype.captureEvents = function() {
72
+
73
+ if (!this.lastEvent) {
74
+ this.currentEvent = "";
75
+ return;
76
+ }
77
+
78
+ this.findEventHandlers(this.lastEvent);
79
+ this.currentEvent = this.eventName;
80
+ this.eventName = "";
81
+ this.lastEvent = undefined;
82
+ };
83
+
84
+ EventListener.prototype.handleEvent = function (event) {
85
+
86
+ if (this.eventName && this.eventName !== this.currentEvent) {
87
+ return;
88
+ }
89
+
90
+ this.eventTagName = (event.target.tagName || "").toUpperCase();
91
+ this.eventName = this.eventMap[event.type];
92
+
93
+ switch (this.eventName) {
94
+ case 'touchstart':
95
+ this.clear();
96
+ if (!['INPUT', 'TEXTAREA'].includes(this.eventTagName)) {
97
+ this.touchCount = this.countTouches(event);
98
+ event.preventDefault();
99
+ this.start(event);
100
+ }
101
+ break;
102
+
103
+ case 'touchmove':
104
+ var touch = this.getTouch(event);
105
+
106
+ this.cursor.x = touch.x;
107
+ this.cursor.y = touch.y;
108
+
109
+ event.preventDefault();
110
+
111
+ if (this.touchCount > 0) {
112
+ this.move(event);
113
+ }
114
+
115
+ break;
116
+
117
+ case 'touchend':
118
+ event.preventDefault();
119
+ this.end(event);
120
+ break;
121
+
122
+ case 'wheel':
123
+ event.preventDefault();
124
+ this.wheel(event);
125
+ break;
126
+
127
+ case 'key':
128
+ this.keyUpHandler(event);
129
+ break;
130
+
131
+ case 'resize':
132
+ tapp.dim.measureScreen();
133
+ break;
134
+
135
+ }
136
+
137
+ this.lastEvent = event;
138
+
139
+ tapp.manager.scheduleRun(0, this.eventName + '-' + this.eventTagName);
140
+ };
141
+
142
+ EventListener.prototype.findEventHandlers = function(event) {
143
+
144
+ var oid = typeof event.target.getAttribute === 'function' ? event.target.getAttribute('id') : '';
145
+
146
+ if (!oid) {
147
+ oid = $Dom.findNearestParentWithId(event.target);
148
+ }
149
+
150
+ var tmodel = tapp.manager.visibleOidMap[oid];
151
+
152
+ var touchHandler = tmodel ? SearchUtil.findFirstTouchHandler(tmodel) : null;
153
+ var scrollLeftHandler = tmodel ? SearchUtil.findFirstScrollLeftHandler(tmodel) : null;
154
+ var scrollTopHandler = tmodel ? SearchUtil.findFirstScrollTopHandler(tmodel) : null;
155
+ var pinchHandler = tmodel ? SearchUtil.findFirstPinchHandler(tmodel) : null;
156
+
157
+ this.currentHandlers.touch = touchHandler;
158
+ this.currentHandlers.scrollLeft = scrollLeftHandler;
159
+ this.currentHandlers.scrollTop = scrollTopHandler;
160
+ this.currentHandlers.pinch = pinchHandler;
161
+ };
162
+
163
+ EventListener.prototype.clear = function () {
164
+ this.start0 = undefined;
165
+ this.start1 = undefined;
166
+ this.end0 = undefined;
167
+ this.end1 = undefined;
168
+ this.touchCount = 0;
169
+ this.resetCurrentTouch();
170
+ };
171
+
172
+ EventListener.prototype.resetCurrentTouch = function() {
173
+ this.currentTouch.deltaY = 0;
174
+ this.currentTouch.deltaX = 0;
175
+ this.currentTouch.pinchDelta = 0;
176
+ this.currentTouch.manualMomentumFlag = false;
177
+ this.currentTouch.orientation = "none";
178
+ this.currentTouch.key = '';
179
+ };
180
+
181
+ EventListener.prototype.resetEvents = function () {
182
+ if (this.currentTouch.timeStamp > 0) {
183
+
184
+ var diff = browser.now() - this.currentTouch.timeStamp;
185
+ var runDelay = 0;
186
+
187
+ if (Math.abs(this.currentTouch.deltaY) > 0.001
188
+ || Math.abs(this.currentTouch.deltaX) > 0.001
189
+ || Math.abs(this.currentTouch.pinchDelta) > 0.001)
190
+ {
191
+ if (diff > 70) {
192
+ this.currentTouch.deltaY = 0;
193
+ this.currentTouch.deltaX = 0;
194
+ this.currentTouch.pinchDelta = 0;
195
+ } else if (this.currentTouch.manualMomentumFlag) {
196
+ this.currentTouch.deltaY *= 0.95;
197
+ this.currentTouch.deltaX *= 0.95;
198
+ runDelay = 10;
199
+ }
200
+ } else if (diff > 600) {
201
+ this.clear();
202
+ this.currentTouch.timeStamp = 0;
203
+ }
204
+
205
+ tapp.manager.scheduleRun(runDelay, "scroll decay");
206
+ } else if (this.eventName || this.lastEvent) {
207
+ this.eventName = "";
208
+ tapp.manager.scheduleRun(1, "reseting current event");
209
+ }
210
+
211
+ };
212
+
213
+ EventListener.prototype.getTouchHandler = function() {
214
+ return this.currentHandlers.touch;
215
+ };
216
+
217
+ EventListener.prototype.getTouchHandlerType = function() {
218
+ return this.currentHandlers.touch ? this.currentHandlers.touch.type : null;
219
+ };
220
+
221
+
222
+ EventListener.prototype.getTouchHandlerOid = function() {
223
+ return this.currentHandlers.touch ? this.currentHandlers.touch.oid : null;
224
+ };
225
+
226
+ EventListener.prototype.isClickEvent = function() {
227
+ return this.currentEvent === 'click';
228
+ };
229
+
230
+ EventListener.prototype.isResizeEvent = function() {
231
+ return this.currentEvent === 'resize';
232
+ };
233
+
234
+ EventListener.prototype.getCurrentEvent = function() {
235
+ return this.currentEvent;
236
+ };
237
+
238
+ EventListener.prototype.isClickHandler = function(target) {
239
+ return this.getTouchHandler() === target && this.isClickEvent();
240
+ };
241
+
242
+ EventListener.prototype.isClickHandlerType = function(type) {
243
+ return this.getTouchHandlerType() === type && this.isClickEvent();
244
+ };
245
+
246
+ EventListener.prototype.isTouchHandler = function(target) {
247
+ return this.getTouchHandler() === target;
248
+ };
249
+
250
+ EventListener.prototype.isTouchHandlerType = function(type) {
251
+ return this.getTouchHandlerType() === type;
252
+ };
253
+
254
+ EventListener.prototype.isTouchHandlerOrAncestor = function(target) {
255
+ var handler = this.getTouchHandler();
256
+
257
+ while (handler) {
258
+ if (handler === target) {
259
+ return true;
260
+ }
261
+
262
+ handler = handler.getParent();
263
+ }
264
+
265
+ return false;
266
+ };
267
+
268
+ EventListener.prototype.isScrollLeftHandler = function(handler) {
269
+ return this.currentHandlers.scrollLeft === handler;
270
+ };
271
+
272
+ EventListener.prototype.isScrollTopHandler = function(handler) {
273
+ return this.currentHandlers.scrollTop === handler;
274
+ };
275
+
276
+ EventListener.prototype.isPinchHandler = function(handler) {
277
+ return this.currentHandlers.pinch === handler;
278
+ };
279
+
280
+ EventListener.prototype.countTouches = function(event) {
281
+ var count = event.touches && event.touches.length ? event.touches.length :
282
+ event.originalEvent && event.originalEvent.touches && event.originalEvent.touches.length ? event.originalEvent.touches.length : 1;
283
+
284
+ return count;
285
+ };
286
+
287
+ EventListener.prototype.keyUpHandler = function (e) {
288
+ e = e || window.event;
289
+ var key = e.which || e.keyCode;
290
+
291
+ this.currentTouch.key = key;
292
+
293
+ if (key === 37) {
294
+ this.setDeltaXDeltaY(-10, 0);
295
+ } else if (key === 38) {
296
+ this.setDeltaXDeltaY(0, -10);
297
+ } else if (key === 39) {
298
+ this.setDeltaXDeltaY(10, 0);
299
+ } else if (key === 40) {
300
+ this.setDeltaXDeltaY(0, 10);
301
+ }
302
+ };
303
+
304
+ EventListener.prototype.getTouch = function (event, index) {
305
+ if (!event) return undefined;
306
+ index = index || 0;
307
+ var e = event.touches && event.touches[index] ? event.touches[index] : event;
308
+ if (e.originalEvent && e.originalEvent.touches) {
309
+ e = e.originalEvent.touches[index];
310
+ }
311
+
312
+ return {
313
+ x: e.pageX || e.clientX || 0,
314
+ y: e.pageY || e.clientY || 0,
315
+ target: e.target,
316
+ timeStamp: browser.now()
317
+ };
318
+ };
319
+
320
+ EventListener.prototype.start = function (event) {
321
+ this.start0 = this.getTouch(event);
322
+ this.start1 = this.getTouch(event, 1);
323
+
324
+ this.cursor.x = this.start0.x;
325
+ this.cursor.y = this.start0.y;
326
+
327
+ return event.stopPropagation();
328
+ };
329
+
330
+ EventListener.prototype.move = function (event) {
331
+
332
+ var deltaX, deltaY;
333
+
334
+ if (this.touchCount === 1 ) {
335
+ this.start0.y = this.end0 ? this.end0.y : this.start0.y;
336
+ this.start0.x = this.end0 ? this.end0.x : this.start0.x;
337
+
338
+ this.end0 = this.getTouch(event);
339
+ this.start1 = undefined;
340
+ this.end1 = undefined;
341
+
342
+ if (TUtil.isDefined(this.end0)) {
343
+ deltaX = this.start0.x - this.end0.x;
344
+ deltaY = this.start0.y - this.end0.y;
345
+
346
+ this.setDeltaXDeltaY(deltaX, deltaY);
347
+ }
348
+
349
+ } else if (this.touchCount >= 2) {
350
+
351
+ this.end0 = this.getTouch(event);
352
+ this.end1 = this.getTouch(event, 1);
353
+
354
+ var length1 = TUtil.distance(this.start0.x, this.start0.y, this.start1.x, this.start1.y);
355
+ var length2 = TUtil.distance(this.end0.x, this.end0.y, this.end1.x, this.end1.y);
356
+
357
+ this.currentTouch.diff = length2 - length1;
358
+
359
+ this.setCurrentTouchParam('pinchDelta', this.currentTouch.diff > 0 ? 0.3 : this.currentTouch.diff < 0 ? -0.3 : 0);
360
+ }
361
+
362
+ return event.stopPropagation();
363
+ };
364
+
365
+ EventListener.prototype.end = function (event) {
366
+ var momentum;
367
+
368
+ if (!TUtil.isDefined(this.end0)) {
369
+ this.end0 = this.getTouch(event);
370
+ }
371
+
372
+ var startToEndTime = TUtil.isDefined(this.end0) && TUtil.isDefined(this.start0) ? this.end0.timeStamp - this.start0.timeStamp : 0;
373
+
374
+ if (this.touchCount <= 1) {
375
+
376
+ if (TUtil.isDefined(this.end0) && TUtil.isDefined(this.start0)) {
377
+
378
+ var deltaX = this.start0.x - this.end0.x;
379
+ var deltaY = this.start0.y - this.end0.y;
380
+
381
+ var period = this.end0.timeStamp - this.start0.timeStamp;
382
+
383
+
384
+ if (this.currentTouch.orientation === "horizontal" && Math.abs(deltaX) > 1) {
385
+ momentum = TUtil.momentum(0, deltaX, period);
386
+ this.setCurrentTouchParam('deltaX', momentum.distance, momentum.duration);
387
+ this.currentTouch.manualMomentumFlag = true;
388
+ } else if (this.currentTouch.orientation === "vertical" && Math.abs(deltaY) > 1) {
389
+ momentum = TUtil.momentum(0, deltaY, period);
390
+ this.setCurrentTouchParam('deltaY', momentum.distance, momentum.duration);
391
+ this.currentTouch.manualMomentumFlag = true;
392
+ }
393
+ }
394
+ }
395
+
396
+ if (!momentum && this.touchCount === 1 && startToEndTime < 300) {
397
+ this.eventName = 'click';
398
+ this.clear();
399
+ this.currentTouch.timeStamp = 0;
400
+ }
401
+
402
+ this.touchCount = 0;
403
+ return event.stopPropagation();
404
+ };
405
+
406
+ EventListener.prototype.setDeltaXDeltaY = function(deltaX, deltaY) {
407
+ var diff = Math.abs(deltaX) - Math.abs(deltaY);
408
+
409
+ if (diff >= 1) {
410
+ if (this.currentTouch.orientation === "none" || (this.currentTouch.orientation === "vertical" && diff > 3) || this.currentTouch.orientation === "horizontal") {
411
+ this.currentTouch.orientation = "horizontal";
412
+ this.currentTouch.dir = this.currentTouch.deltaX <= -1 ? "left" : this.currentTouch.deltaX >= 1 ? "right" : this.currentTouch.dir;
413
+ this.setCurrentTouchParam('deltaX', deltaX);
414
+ this.currentTouch.deltaY = 0;
415
+ }
416
+ } else if (this.currentTouch.orientation === "none" || (this.currentTouch.orientation === "horizontal" && diff < -3) || this.currentTouch.orientation === "vertical") {
417
+ this.currentTouch.orientation = "vertical";
418
+ this.currentTouch.dir = this.currentTouch.deltaY <= -1 ? "up" : this.currentTouch.deltaY >= 1 ? "down" : this.currentTouch.dir;
419
+ this.setCurrentTouchParam('deltaY', deltaY);
420
+ this.currentTouch.deltaX = 0;
421
+ } else {
422
+ this.currentTouch.deltaX = 0;
423
+ this.currentTouch.deltaY = 0;
424
+ }
425
+ };
426
+
427
+ EventListener.prototype.setCurrentTouchParam = function(name, value, timeOffset) {
428
+ this.currentTouch[name] = value;
429
+ this.currentTouch.timeStamp = TUtil.isDefined(timeOffset) ? browser.now() + timeOffset : Math.max(this.currentTouch.timeStamp, browser.now());
430
+ };
431
+
432
+ EventListener.prototype.wheel = function (event) {
433
+ var deltaX = 0, deltaY = 0;
434
+
435
+ this.currentTouch.pinchDelta = 0;
436
+
437
+ this.start0 = this.getTouch(event);
438
+
439
+ if (event.ctrlKey && 'deltaY' in event) {
440
+ this.setCurrentTouchParam('pinchDelta', -event.deltaY / 10);
441
+ } else if ('deltaX' in event) {
442
+ deltaX = event.deltaX;
443
+ deltaY = event.deltaY;
444
+ } else if ('wheelDeltaX' in event) {
445
+ deltaX = -event.wheelDeltaX / 120;
446
+ deltaY = -event.wheelDeltaY / 120;
447
+ } else if ('wheelDelta' in event) {
448
+ deltaX = -event.wheelDelta / 120;
449
+ } else if ('detail' in event) {
450
+ deltaX = event.detail / 3;
451
+ }
452
+
453
+ this.setDeltaXDeltaY(deltaX, deltaY);
454
+ };