123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196 |
- /*
- * Copyright 2000-2016 Vaadin Ltd.
- *
- * Licensed under the Apache License, Version 2.0 (the "License"); you may not
- * use this file except in compliance with the License. You may obtain a copy of
- * the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
- * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
- * License for the specific language governing permissions and limitations under
- * the License.
- */
- package com.vaadin.client.connectors.grid;
-
- import java.util.Objects;
- import java.util.Optional;
-
- import com.google.gwt.dom.client.Element;
- import com.google.gwt.dom.client.NativeEvent;
- import com.google.gwt.dom.client.TableRowElement;
- import com.google.gwt.user.client.Window;
- import com.vaadin.client.ServerConnector;
- import com.vaadin.client.WidgetUtil;
- import com.vaadin.client.extensions.DropTargetExtensionConnector;
- import com.vaadin.client.widget.escalator.RowContainer;
- import com.vaadin.client.widgets.Escalator;
- import com.vaadin.shared.ui.Connect;
- import com.vaadin.shared.ui.grid.DropLocation;
- import com.vaadin.shared.ui.grid.DropMode;
- import com.vaadin.shared.ui.grid.GridDropTargetExtensionRpc;
- import com.vaadin.shared.ui.grid.GridDropTargetExtensionState;
- import com.vaadin.shared.ui.grid.GridState;
- import com.vaadin.ui.GridDropTargetExtension;
-
- import elemental.events.Event;
- import elemental.json.JsonObject;
-
- /**
- * Makes Grid an HTML5 drop target. This is the client side counterpart of
- * {@link GridDropTargetExtension}.
- *
- * @author Vaadin Ltd
- * @since
- */
- @Connect(GridDropTargetExtension.class)
- public class GridDropTargetExtensionConnector extends
- DropTargetExtensionConnector {
-
- // Drag over class name suffixes
- private final static String CLASS_SUFFIX_BEFORE = "-before";
- private final static String CLASS_SUFFIX_AFTER = "-after";
-
- // Drag over class names
- private final static String CLASS_DRAG_OVER_BEFORE =
- CLASS_DRAG_OVER + CLASS_SUFFIX_BEFORE;
- private final static String CLASS_DRAG_OVER_AFTER =
- CLASS_DRAG_OVER + CLASS_SUFFIX_AFTER;
-
- /**
- * Current drag over class name
- */
- private String dragOverClassName;
-
- private GridConnector gridConnector;
-
- @Override
- protected void extend(ServerConnector target) {
- gridConnector = (GridConnector) target;
-
- super.extend(target);
- }
-
- @Override
- protected void sendDropEventToServer(String dataTransferText,
- Event dropEvent) {
-
- String rowKey = null;
- DropLocation dropLocation = null;
-
- Optional<TableRowElement> targetRow = getTargetRow(
- (Element) dropEvent.getTarget());
- if (targetRow.isPresent()) {
- rowKey = getRowData(targetRow.get())
- .getString(GridState.JSONKEY_ROWKEY);
- dropLocation = getDropLocation(targetRow.get(),
- (NativeEvent) dropEvent);
- }
-
- getRpcProxy(GridDropTargetExtensionRpc.class)
- .drop(dataTransferText, rowKey, dropLocation);
- }
-
- private JsonObject getRowData(TableRowElement row) {
- int rowIndex = ((Escalator.AbstractRowContainer) getGridBody())
- .getLogicalRowIndex(row);
- return gridConnector.getDataSource().getRow(rowIndex);
- }
-
- /**
- * Returns the location of the event within the row.
- */
- private DropLocation getDropLocation(Element target, NativeEvent event) {
- if (getState().dropMode == DropMode.BETWEEN) {
- if (getRelativeY(target, event) < (target.getOffsetHeight() / 2)) {
- return DropLocation.ABOVE;
- } else {
- return DropLocation.BELOW;
- }
- }
- return DropLocation.ON_TOP;
- }
-
- private int getRelativeY(Element element, NativeEvent event) {
- int relativeTop = element.getAbsoluteTop() - Window.getScrollTop();
- return WidgetUtil.getTouchOrMouseClientY(event) - relativeTop;
- }
-
- @Override
- protected void setTargetIndicator(Event event) {
- getTargetRow(((Element) event.getTarget())).ifPresent(target -> {
-
- // Get required class name
- String className = getTargetClassName(target, (NativeEvent) event);
-
- // Add or replace class name if changed
- if (!target.hasClassName(className)) {
- if (dragOverClassName != null) {
- target.removeClassName(dragOverClassName);
- }
- target.addClassName(className);
- dragOverClassName = className;
- }
- });
- }
-
- private String getTargetClassName(Element target, NativeEvent event) {
- String classSuffix;
-
- switch (getDropLocation(target, event)) {
- case ABOVE:
- classSuffix = CLASS_SUFFIX_BEFORE;
- break;
- case BELOW:
- classSuffix = CLASS_SUFFIX_AFTER;
- break;
- case ON_TOP:
- default:
- classSuffix = "";
- break;
- }
-
- return CLASS_DRAG_OVER + classSuffix;
- }
-
- @Override
- protected void removeTargetIndicator(Event event) {
-
- // Remove all possible drag over class names
- getTargetRow((Element) event.getTarget()).ifPresent(e -> {
- e.removeClassName(CLASS_DRAG_OVER);
- e.removeClassName(CLASS_DRAG_OVER_BEFORE);
- e.removeClassName(CLASS_DRAG_OVER_AFTER);
- });
- }
-
- private Optional<TableRowElement> getTargetRow(Element source) {
- while (!Objects.equals(source, getGridBody().getElement())) {
- if (TableRowElement.is(source)) {
- return Optional.of(source.cast());
- }
- source = source.getParentElement();
- }
- return Optional.empty();
- }
-
- @Override
- protected Element getDropTargetElement() {
- return getGridBody().getElement();
- }
-
- private Escalator getEscalator() {
- return gridConnector.getWidget().getEscalator();
- }
-
- private RowContainer.BodyRowContainer getGridBody() {
- return getEscalator().getBody();
- }
-
- @Override
- public GridDropTargetExtensionState getState() {
- return (GridDropTargetExtensionState) super.getState();
- }
- }
|