Search code examples
apache-flexflex4

Drag and Drop - How to get the DragSource on the DragStart event


When I start a drag operation I would like to be able to highlight the possible valid Drop objects. For this I need to know when the drag operation starts and which items are being dragged. I am trying to do this using the dragStart, but the event.dragSource is null on this event.

I have this list:

<s:List
    width="100%"
    height="100%"
    id="productsListing" 
    dragEnabled="true"
    dataProvider="{products}"
    dragStart="dragStartHandler(event);"
    dragComplete="dragCompleteHandler(event);"
    itemRenderer="views.productListed" />

And I have the listener as:

public function dragStartHandler(event:DragEvent):void {
    var itemsVector:Vector.<Object> = event.dragSource.dataForFormat('itemsByIndex') as Vector.<Object>;
    //Verify Items
    //Highlight appropriated dropZones
}

Anyone have a good sugestion how to overcome this?


Solution

  • The problem here is that your dragStartHandler is taking higher precedence than the List components internal dragStartHandler - which is where the drag operation is started and the dragSource property created.

    Suggestion, manually add your dragStartHandler with a lower precedence than the List components dragStartHandler method - looking at the code this needs to be less than -50.

    MXML Code:

    <s:List width="100%" height="100%"
            id="productsListing"  
            dragEnabled="true"   
            dataProvider="{products}"
            initialize="productsListing_initializeHandler(event)"
            dragComplete="productsListing_dragCompleteHandler(event)"
            itemRenderer="views.productListed"
            />
    

    AS Code:

    protected function productsListing_initializeHandler(event:FlexEvent):void
    {
        // Needs to be handled AFTER the List component has handled the event and attached the dragSource data, hence priority is -51
        this.productsListing.addEventListener(DragEvent.DRAG_START, productsListing_dragStartHandler, false, -51, true);
    }
    
    protected function productsListing_dragStartHandler(event:DragEvent):void
    {
        // Your code here...
    }
    

    I hope you find that useful.