Path: blob/trunk/third_party/closure/goog/labs/events/touch.js
2868 views
// Copyright 2013 The Closure Library Authors. All Rights Reserved.1//2// Licensed under the Apache License, Version 2.0 (the "License");3// you may not use this file except in compliance with the License.4// You may obtain a copy of the License at5//6// http://www.apache.org/licenses/LICENSE-2.07//8// Unless required by applicable law or agreed to in writing, software9// distributed under the License is distributed on an "AS-IS" BASIS,10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.11// See the License for the specific language governing permissions and12// limitations under the License.1314/**15* @fileoverview Utilities to abstract mouse and touch events.16*/171819goog.provide('goog.labs.events.touch');20goog.provide('goog.labs.events.touch.TouchData');2122goog.require('goog.array');23goog.require('goog.asserts');24goog.require('goog.events.EventType');25goog.require('goog.string');262728/**29* Description the geometry and target of an event.30*31* @typedef {{32* clientX: number,33* clientY: number,34* screenX: number,35* screenY: number,36* target: EventTarget37* }}38*/39goog.labs.events.touch.TouchData;404142/**43* Takes a mouse or touch event and returns the relevant geometry and target44* data.45* @param {!Event} e A mouse or touch event.46* @return {!goog.labs.events.touch.TouchData}47*/48goog.labs.events.touch.getTouchData = function(e) {4950var source = e;51goog.asserts.assert(52goog.string.startsWith(e.type, 'touch') ||53goog.string.startsWith(e.type, 'mouse'),54'Event must be mouse or touch event.');5556if (goog.string.startsWith(e.type, 'touch')) {57goog.asserts.assert(58goog.array.contains(59[60goog.events.EventType.TOUCHCANCEL, goog.events.EventType.TOUCHEND,61goog.events.EventType.TOUCHMOVE, goog.events.EventType.TOUCHSTART62],63e.type),64'Touch event not of valid type.');6566// If the event is end or cancel, take the first changed touch,67// otherwise the first target touch.68source = (e.type == goog.events.EventType.TOUCHEND ||69e.type == goog.events.EventType.TOUCHCANCEL) ?70e.changedTouches[0] :71e.targetTouches[0];72}7374return {75clientX: source['clientX'],76clientY: source['clientY'],77screenX: source['screenX'],78screenY: source['screenY'],79target: source['target']80};81};828384