Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

DDUtil.java 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*
  2. * Copyright 2000-2013 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.NativeEvent;
  18. import com.google.gwt.user.client.Window;
  19. import com.vaadin.client.Util;
  20. import com.vaadin.shared.ui.dd.HorizontalDropLocation;
  21. import com.vaadin.shared.ui.dd.VerticalDropLocation;
  22. public class DDUtil {
  23. public static VerticalDropLocation getVerticalDropLocation(
  24. com.google.gwt.user.client.Element element, NativeEvent event,
  25. double topBottomRatio) {
  26. int offsetHeight = element.getOffsetHeight();
  27. return getVerticalDropLocation(element, offsetHeight, event,
  28. topBottomRatio);
  29. }
  30. public static VerticalDropLocation getVerticalDropLocation(
  31. com.google.gwt.user.client.Element element, int offsetHeight,
  32. NativeEvent event, double topBottomRatio) {
  33. int clientY = Util.getTouchOrMouseClientY(event);
  34. return getVerticalDropLocation(element, offsetHeight, clientY,
  35. topBottomRatio);
  36. }
  37. public static VerticalDropLocation getVerticalDropLocation(
  38. com.google.gwt.user.client.Element element, int offsetHeight,
  39. int clientY, double topBottomRatio) {
  40. // Event coordinates are relative to the viewport, element absolute
  41. // position is relative to the document. Make element position relative
  42. // to viewport by adjusting for viewport scrolling. See #6021
  43. int elementTop = element.getAbsoluteTop() - Window.getScrollTop();
  44. int fromTop = clientY - elementTop;
  45. float percentageFromTop = (fromTop / (float) offsetHeight);
  46. if (percentageFromTop < topBottomRatio) {
  47. return VerticalDropLocation.TOP;
  48. } else if (percentageFromTop > 1 - topBottomRatio) {
  49. return VerticalDropLocation.BOTTOM;
  50. } else {
  51. return VerticalDropLocation.MIDDLE;
  52. }
  53. }
  54. public static HorizontalDropLocation getHorizontalDropLocation(
  55. com.google.gwt.user.client.Element element, NativeEvent event,
  56. double leftRightRatio) {
  57. int clientX = Util.getTouchOrMouseClientX(event);
  58. // Event coordinates are relative to the viewport, element absolute
  59. // position is relative to the document. Make element position relative
  60. // to viewport by adjusting for viewport scrolling. See #6021
  61. int elementLeft = element.getAbsoluteLeft() - Window.getScrollLeft();
  62. int offsetWidth = element.getOffsetWidth();
  63. int fromLeft = clientX - elementLeft;
  64. float percentageFromLeft = (fromLeft / (float) offsetWidth);
  65. if (percentageFromLeft < leftRightRatio) {
  66. return HorizontalDropLocation.LEFT;
  67. } else if (percentageFromLeft > 1 - leftRightRatio) {
  68. return HorizontalDropLocation.RIGHT;
  69. } else {
  70. return HorizontalDropLocation.CENTER;
  71. }
  72. }
  73. }