Monday, May 2, 2016

Re: DragOverHandler & not accepting drop



On Monday, May 2, 2016 at 10:39:39 AM UTC+2, shimmeri...@gmail.com wrote:
According to the HTML5 drag & drop spec, element having dragOver listener and calling preventDefault on it's parameter event tells the browser that this element is a valid drop target.

So, like this:

    function onDragOver(event) {
        event.preventDefault();
    }

If the element does not want to accept the drop, preventDefault is not called.

Now I tried to do the same with GWT's DragOverHandler.

        DragOverHandler dragOverHandler = new DragOverHandler() {
           
            @Override
            public void onDragOver(DragOverEvent event) {
                event.preventDefault();
            }
        };
       
        flowPanel.addBitlessDomHandler(dragOverHandler, DragOverEvent.getType());

Now the problem is that that panel always indicates itself as a drop target, regardless whether I call preventDefault or not. Only by not adding the handler does the panel refuse drop operations. So for some reason just by simply adding a DragOverHandler to a GWT widget makes it accept drag operations. Thus making it impossible for me indicate for user what can be dragged and what not.

GWT calls preventDefault for you, automatically: https://gwt.googlesource.com/gwt/+/2.7.0/user/src/com/google/gwt/user/client/impl/DOMImplStandard.java#328
This is, in part, due to legacy IE behavior where you had to "return false" instead of "preventDefault" (in addition to listening to both dragenter and dragover to signal a drop target), so this had to be abstracted away.
This is some really old code (added 5 years ago, as part of updating the mobilewebapp sample, so I believe it was also done a bit in a hurry in preparation to Google I/O) that hasn't changed much since then, but would likely benefit from an update and some polish.

IIRC, you can "reject the drop" by setting the data transfer's dropEffect to "none" (dropEffect isn't exposed by GWT before 2.8.0-beta1 though; you'd have to use JSNI or JsInterop to set it in 2.7; you'll also note that 'types' is not exposed either, so you cannot look at what's being dragged to decide whether to accept it or not either, at least not without JSNI/JsInterop). I believe that, at the time the API was added, you simply couldn't look at what was being dragged so you could only declare yourself as a drop target unconditionally, and process the drop event to eventually ignore what was dropped; that also would explain why preventDefault is called unconditionally.


--
You received this message because you are subscribed to the Google Groups "GWT Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-web-toolkit+unsubscribe@googlegroups.com.
To post to this group, send email to google-web-toolkit@googlegroups.com.
Visit this group at https://groups.google.com/group/google-web-toolkit.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment