You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DDUtil.java 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /*
  2. * Copyright 2000-2014 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.client.ui.dd;
  17. import com.google.gwt.dom.client.Element;
  18. import com.google.gwt.dom.client.NativeEvent;
  19. import com.google.gwt.user.client.Window;
  20. import com.vaadin.client.Util;
  21. import com.vaadin.shared.ui.dd.HorizontalDropLocation;
  22. import com.vaadin.shared.ui.dd.VerticalDropLocation;
  23. public class DDUtil {
  24. public static VerticalDropLocation getVerticalDropLocation(Element element,
  25. NativeEvent event, double topBottomRatio) {
  26. int offsetHeight = element.getOffsetHeight();
  27. return getVerticalDropLocation(element, offsetHeight, event,
  28. topBottomRatio);
  29. }
  30. public static VerticalDropLocation getVerticalDropLocation(Element element,
  31. int offsetHeight, NativeEvent event, double topBottomRatio) {
  32. int clientY = Util.getTouchOrMouseClientY(event);
  33. return getVerticalDropLocation(element, offsetHeight, clientY,
  34. topBottomRatio);
  35. }
  36. public static VerticalDropLocation getVerticalDropLocation(Element element,
  37. int offsetHeight, int clientY, double topBottomRatio) {
  38. // Event coordinates are relative to the viewport, element absolute
  39. // position is relative to the document. Make element position relative
  40. // to viewport by adjusting for viewport scrolling. See #6021
  41. int elementTop = element.getAbsoluteTop() - Window.getScrollTop();
  42. int fromTop = clientY - elementTop;
  43. float percentageFromTop = (fromTop / (float) offsetHeight);
  44. if (percentageFromTop < topBottomRatio) {
  45. return VerticalDropLocation.TOP;
  46. } else if (percentageFromTop > 1 - topBottomRatio) {
  47. return VerticalDropLocation.BOTTOM;
  48. } else {
  49. return VerticalDropLocation.MIDDLE;
  50. }
  51. }
  52. public static HorizontalDropLocation getHorizontalDropLocation(
  53. Element element, NativeEvent event, double leftRightRatio) {
  54. int clientX = Util.getTouchOrMouseClientX(event);
  55. // Event coordinates are relative to the viewport, element absolute
  56. // position is relative to the document. Make element position relative
  57. // to viewport by adjusting for viewport scrolling. See #6021
  58. int elementLeft = element.getAbsoluteLeft() - Window.getScrollLeft();
  59. int offsetWidth = element.getOffsetWidth();
  60. int fromLeft = clientX - elementLeft;
  61. float percentageFromLeft = (fromLeft / (float) offsetWidth);
  62. if (percentageFromLeft < leftRightRatio) {
  63. return HorizontalDropLocation.LEFT;
  64. } else if (percentageFromLeft > 1 - leftRightRatio) {
  65. return HorizontalDropLocation.RIGHT;
  66. } else {
  67. return HorizontalDropLocation.CENTER;
  68. }
  69. }
  70. }