Source: main/webapp/modules/Position.js

  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. var Guacamole = Guacamole || {};
  20. /**
  21. * A position in 2-D space.
  22. *
  23. * @constructor
  24. * @param {Guacamole.Position|object} [template={}]
  25. * The object whose properties should be copied within the new
  26. * Guacamole.Position.
  27. */
  28. Guacamole.Position = function Position(template) {
  29. template = template || {};
  30. /**
  31. * The current X position, in pixels.
  32. *
  33. * @type {!number}
  34. * @default 0
  35. */
  36. this.x = template.x || 0;
  37. /**
  38. * The current Y position, in pixels.
  39. *
  40. * @type {!number}
  41. * @default 0
  42. */
  43. this.y = template.y || 0;
  44. /**
  45. * Assigns the position represented by the given element and
  46. * clientX/clientY coordinates. The clientX and clientY coordinates are
  47. * relative to the browser viewport and are commonly available within
  48. * JavaScript event objects. The final position is translated to
  49. * coordinates that are relative the given element.
  50. *
  51. * @param {!Element} element
  52. * The element the coordinates should be relative to.
  53. *
  54. * @param {!number} clientX
  55. * The viewport-relative X coordinate to translate.
  56. *
  57. * @param {!number} clientY
  58. * The viewport-relative Y coordinate to translate.
  59. */
  60. this.fromClientPosition = function fromClientPosition(element, clientX, clientY) {
  61. this.x = clientX - element.offsetLeft;
  62. this.y = clientY - element.offsetTop;
  63. // This is all JUST so we can get the position within the element
  64. var parent = element.offsetParent;
  65. while (parent && !(parent === document.body)) {
  66. this.x -= parent.offsetLeft - parent.scrollLeft;
  67. this.y -= parent.offsetTop - parent.scrollTop;
  68. parent = parent.offsetParent;
  69. }
  70. // Element ultimately depends on positioning within document body,
  71. // take document scroll into account.
  72. if (parent) {
  73. var documentScrollLeft = document.body.scrollLeft || document.documentElement.scrollLeft;
  74. var documentScrollTop = document.body.scrollTop || document.documentElement.scrollTop;
  75. this.x -= parent.offsetLeft - documentScrollLeft;
  76. this.y -= parent.offsetTop - documentScrollTop;
  77. }
  78. };
  79. };
  80. /**
  81. * Returns a new {@link Guacamole.Position} representing the relative position
  82. * of the given clientX/clientY coordinates within the given element. The
  83. * clientX and clientY coordinates are relative to the browser viewport and are
  84. * commonly available within JavaScript event objects. The final position is
  85. * translated to coordinates that are relative the given element.
  86. *
  87. * @param {!Element} element
  88. * The element the coordinates should be relative to.
  89. *
  90. * @param {!number} clientX
  91. * The viewport-relative X coordinate to translate.
  92. *
  93. * @param {!number} clientY
  94. * The viewport-relative Y coordinate to translate.
  95. *
  96. * @returns {!Guacamole.Position}
  97. * A new Guacamole.Position representing the relative position of the given
  98. * client coordinates.
  99. */
  100. Guacamole.Position.fromClientPosition = function fromClientPosition(element, clientX, clientY) {
  101. var position = new Guacamole.Position();
  102. position.fromClientPosition(element, clientX, clientY);
  103. return position;
  104. };