* Fix Button as a Html5 DragSourcei It was completely broken, not being clickable, and also preventing anything else from being clicked.tags/8.1.0.beta1
clickPending = true; | clickPending = true; | ||||
setFocus(true); | setFocus(true); | ||||
DOM.setCapture(getElement()); | DOM.setCapture(getElement()); | ||||
isCapturing = isCapturingEnabled(); | |||||
isCapturing = true; | |||||
addStyleName(CLASSNAME_PRESSED); | addStyleName(CLASSNAME_PRESSED); | ||||
} | } | ||||
break; | break; | ||||
break; | break; | ||||
case Event.ONMOUSEMOVE: | case Event.ONMOUSEMOVE: | ||||
clickPending = false; | clickPending = false; | ||||
if (isCapturing) { | |||||
if (isCapturing && !isDraggable()) { | |||||
// Prevent dragging (on other browsers); | // Prevent dragging (on other browsers); | ||||
DOM.eventPreventDefault(event); | DOM.eventPreventDefault(event); | ||||
} | } | ||||
} | } | ||||
/** | /** | ||||
* Enables or disables the widget's capturing of mouse events with the mouse | |||||
* held down. | |||||
* | |||||
* @param enabled | |||||
* {@literal true} if capturing enabled, {@literal false} | |||||
* otherwise | |||||
* Returns if this button has been made <code>draggable</code> or not. | |||||
* | * | ||||
* @since 8.1 | |||||
*/ | |||||
public void setCapturingEnabled(boolean enabled) { | |||||
capturingEnabled = enabled; | |||||
} | |||||
/** | |||||
* Returns if the widget's capturing of mouse events are enabled. | |||||
* | |||||
* @return {@literal true} if mouse capturing is enabled, {@literal false} | |||||
* @return {@literal true} if draggable is enabled, {@literal false} | |||||
* otherwise | * otherwise | ||||
* | |||||
* @since 8.1 | |||||
*/ | */ | ||||
public boolean isCapturingEnabled() { | |||||
return capturingEnabled; | |||||
private boolean isDraggable() { | |||||
return getElement().getPropertyBoolean("draggable"); | |||||
} | } | ||||
private static native int getHorizontalBorderAndPaddingWidth(Element elem) | private static native int getHorizontalBorderAndPaddingWidth(Element elem) |
} | } | ||||
@Override | |||||
public void onDragSourceAttached() { | |||||
// Disable mouse event capturing to make the widget draggable | |||||
getWidget().setCapturingEnabled(false); | |||||
} | |||||
@Override | |||||
public void onDragSourceDetached() { | |||||
// Reset mouse event capturing | |||||
getWidget().setCapturingEnabled(true); | |||||
} | |||||
} | } |
*/ | */ | ||||
package com.vaadin.tests.dnd; | package com.vaadin.tests.dnd; | ||||
import com.vaadin.annotations.Widgetset; | |||||
import com.vaadin.server.Page; | import com.vaadin.server.Page; | ||||
import com.vaadin.server.VaadinRequest; | import com.vaadin.server.VaadinRequest; | ||||
import com.vaadin.tests.components.AbstractTestUIWithLog; | import com.vaadin.tests.components.AbstractTestUIWithLog; | ||||
import com.vaadin.ui.dnd.DragSourceExtension; | import com.vaadin.ui.dnd.DragSourceExtension; | ||||
import com.vaadin.ui.dnd.DropTargetExtension; | import com.vaadin.ui.dnd.DropTargetExtension; | ||||
@Widgetset("com.vaadin.DefaultWidgetSet") | |||||
public class DraggableButton extends AbstractTestUIWithLog { | public class DraggableButton extends AbstractTestUIWithLog { | ||||
@Override | @Override | ||||
protected void setup(VaadinRequest request) { | protected void setup(VaadinRequest request) { | ||||
Button draggableButton = new Button("Draggable Button"); | |||||
Button draggableButton = new Button("Draggable Button", | |||||
event -> log("clicked draggable button")); | |||||
DragSourceExtension<Button> dragSourceExtension = new DragSourceExtension<>( | DragSourceExtension<Button> dragSourceExtension = new DragSourceExtension<>( | ||||
draggableButton); | draggableButton); | ||||
dragSourceExtension.setDataTransferText( | dragSourceExtension.setDataTransferText( | ||||
.addDropListener(event -> log(event.getDataTransferText())); | .addDropListener(event -> log(event.getDataTransferText())); | ||||
Layout layout = new HorizontalLayout(); | Layout layout = new HorizontalLayout(); | ||||
layout.addComponents(draggableButton, dropTarget); | |||||
layout.addComponents(draggableButton, dropTarget, new Button( | |||||
"another button", event -> log("click on another button"))); | |||||
addComponent(layout); | addComponent(layout); | ||||
// Add styling | // Add styling | ||||
@Override | @Override | ||||
protected String getTestDescription() { | protected String getTestDescription() { | ||||
return "Test if Button is draggable"; | |||||
return "Test if Button is draggable, and it won't steal all other clicks"; | |||||
} | } | ||||
} | } |