2 Replies Latest reply on Apr 21, 2009 12:26 AM by Ratsnackbar

    Need help with Tree item move

    Ratsnackbar Level 2

      What is the easiest way to compare the item being dragged to the item you are hovering over in a tree menu to verify that the dragged item will not be dropped into it's own child?

       

      Difficulty; I am trying not to extend the Tree control so this is a standard tree inside a canvas component.

       

      I realize I could grap the parent object of the item I am over and then loop all the way up the chain and compare each to the item I am dragging.  I would prefer a more elegant method if possible.

       

      There is very likely something stupid simple I am missing here (which is why I ask) but any suggestions would be appreciated regardless of complexity.

       

      Thanks

      -Joe

        • 1. Re: Need help with Tree item move
          _Natasha_ Level 4

          Hi Joe,

           

          may be it easier to use Tree.calculateDropIndex(event:DragEvent = null):int

          You can get the index and after that get data at that position.

          • 2. Re: Need help with Tree item move
            Ratsnackbar Level 2

            Thanks!  I actually did do something like that to capture information about the item I am over when dragging but what I was hoping to avoid was to have to do something like below.  Guess I was hopen for a single line function that would tell me I am not hovering over a child of the leaf I am dragging.  In this case though I had to somewhat reinvent the wheel.  Turns out I likely should just extend the Tree component to include such a function.  Here is what I did if anyone is interested.  Not as elegant as I would like but it does the trick.

             

             

             

             

             

             

             

             

             

             

             

             

             

             

             

             

             

             

             

             

            private

             

             

            function doDragDrop(event:DragEvent):void{

             

             

             

                 var parentIDArray:Array = getParentIDStack(CFRMenuTree.selectedItem);

             

             

             

                 var denySave:Boolean = Boolean(mx.utils.ArrayUtil.getItemIndex(dragItem.menuItemID,parentIDArray)+1);

             

             

             

                 if(denySave){

                      Alert.show(

            "You cannot move a directory into a directory within itself!", "Illegal Move");

                 }

            else {

             

             

                      if(dragItem.menuItemID != useParentID){

                           dragItem.parentMenuID = useParentID;

                      }

                      cfrsm.saveItem(dragItem);

                 }

            }

             

             

             

             

            private function getParentIDStack(CFRMenuItem:Object):Array{

             

             

                 var idStack:Array = [];

             

             

                 if (CFRMenuItem == null)

             

             

                      return idStack;

             

             

             

             

                 var parent:* = CFRMenuTree.getParentItem(CFRMenuItem);

             

             

             

                 while(parent){

                      idStack.push(parent.menuItemID);

                      parent = CFRMenuTree.getParentItem(parent);

                 }

             

             

                 return idStack;

            }

             

            NOTE:

            Each leaf in the menu has two properties called menuItemID and parentMenuID which are used to build the heirarchy on the ColdFusion Server prior to sending it to Flex.

             

            dragItem is captured prior to this code via an itemRoleOver event on the tree's list.

             

            useParentID is calculated during a dragOver event and should be the menuItemID of the Leaf you are hovering over when you drop.

             

            The getParentIDStack is a slightly modified version of the tree controls own private getParentStack function.  It is used to build an array of the menuItemID's starting with the selectedItem (the item we are hovering over currently) and all its ancestors in the tree up to the root.

             

            The ArrayUtil.getItemIndex is used to see if the dragItem.menuItemID is in the array returned by the getParentIDStack function.  If the menuItemID of the dragItem is in the array it will always be 0 or greater and -1 if it is not.  So by incrementing by 1 I can use it as a boolean to indicate if I am over a child of the item I am dragging.  If I am I deny the save and if not I set the dragItem's new parentMenuID and save it to the database through a ColdFusion RemoteObject which then triggers a refresh of the datasource.

             

            Not as good as a one liner but that will have to wait until I have time to build a custom Tree Control.  For now it works.