1// Copyright 2013 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5// This file provides common functionality for synthetic gesture actions.
6'use strict';
7
8(function() {
9
10  // Returns the bounding rectangle wrt to the top-most document.
11  function getBoundingRect(el) {
12    var client_rect = el.getBoundingClientRect();
13    var bound = { left: client_rect.left,
14                  top: client_rect.top,
15                  width: client_rect.width,
16                  height: client_rect.height };
17
18    var frame = el.ownerDocument.defaultView.frameElement;
19    while (frame) {
20      var frame_bound = frame.getBoundingClientRect();
21      // This computation doesn't account for more complex CSS transforms on the
22      // frame (e.g. scaling or rotations).
23      bound.left += frame_bound.left;
24      bound.top += frame_bound.top;
25
26      frame = frame.ownerDocument.frameElement;
27    }
28    return bound;
29  }
30
31  function getBoundingVisibleRect(el) {
32    var rect = getBoundingRect(el);
33    if (rect.top < 0) {
34      rect.height += rect.top;
35      rect.top = 0;
36    }
37    if (rect.left < 0) {
38      rect.width += rect.left;
39      rect.left = 0;
40    }
41
42    var outsideHeight = (rect.top + rect.height) - window.innerHeight;
43    var outsideWidth = (rect.left + rect.width) - window.innerWidth;
44
45    if (outsideHeight > 0) {
46      rect.height -= outsideHeight;
47    }
48    if (outsideWidth > 0) {
49      rect.width -= outsideWidth;
50    }
51    return rect;
52  };
53
54  window.__GestureCommon_GetBoundingVisibleRect = getBoundingVisibleRect;
55})();
56