Debian Codegraph @

closure-library/closure/goog/labs/events/touch.js

raw
1 // Copyright 2013 The Closure Library Authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS-IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 /**
16 * @fileoverview Utilities to abstract mouse and touch events.
17 */
18
19
20 goog.provide('goog.labs.events.touch');
21 goog.provide('goog.labs.events.touch.TouchData');
22
23 goog.require('goog.array');
24 goog.require('goog.asserts');
25 goog.require('goog.events.EventType');
26 goog.require('goog.string');
27
28
29 /**
30 * Description the geometry and target of an event.
31 *
32 * @typedef {{
33 * clientX: number,
34 * clientY: number,
35 * screenX: number,
36 * screenY: number,
37 * target: EventTarget
38 * }}
39 */
40 goog.labs.events.touch.TouchData;
41
42
43 /**
44 * Takes a mouse or touch event and returns the relevant geometry and target
45 * data.
46 * @param {!Event} e A mouse or touch event.
47 * @return {!goog.labs.events.touch.TouchData}
48 */
49 goog.labs.events.touch.getTouchData = function(e) {
50
51 var source = e;
52 goog.asserts.assert(
53 goog.string.startsWith(e.type, 'touch') ||
54 goog.string.startsWith(e.type, 'mouse'),
55 'Event must be mouse or touch event.');
56
57 if (goog.string.startsWith(e.type, 'touch')) {
58 goog.asserts.assert(
59 goog.array.contains(
60 [
61 goog.events.EventType.TOUCHCANCEL, goog.events.EventType.TOUCHEND,
62 goog.events.EventType.TOUCHMOVE, goog.events.EventType.TOUCHSTART
63 ],
64 e.type),
65 'Touch event not of valid type.');
66
67 // If the event is end or cancel, take the first changed touch,
68 // otherwise the first target touch.
69 source = (e.type == goog.events.EventType.TOUCHEND ||
70 e.type == goog.events.EventType.TOUCHCANCEL) ?
71 e.changedTouches[0] :
72 e.targetTouches[0];
73 }
74
75 return {
76 clientX: source['clientX'],
77 clientY: source['clientY'],
78 screenX: source['screenX'],
79 screenY: source['screenY'],
80 target: source['target']
81 };
82 };