1
votes

With the PopupManager it's possible to add/create/remove a new popup. But i can't find a way to get the top most popup without overriding this class (which something you want to do for a big Flex application).

So far I found this solution, which is more kinna of work around. So if any body has a better solution, i will be pretty much happy to read it.

Assuming the you call the addPopup/createPopup with the parameter PopUpManagerChildList.POPUP, example :

PopUpManager.createPopUp(parent,MyPopupClass,true,PopUpManagerChildList.POPUP);

The this function will return the top most popup:

private function getTopMostPopup():void
{

     var childList:IChildList = Application.application.systemManager.popUpChildren;
     for (var i:int = childList.numChildren - 1; i > 0; i--)
     {
          var child:DisplayObject = childList.getChildAt( i );
          if (child is Container)
               return child;
     }
     return null;
}

Application.application.systemManager.popUpChildren contains all the DisplayObject displayed with PopupManager. But many of the itemRenderers of your components could be in this list eventhough there are not visible in the screen. This is why my function get the last child inheriting from Container (your popup must inherit from Container).

1
Yah short of writing an extension on PopUpManager (which honestly shouldn't be much work just need to override addPopUp and createPopUp probably in order to keep track of the last one created, would be more efficient but probably negligible time, unless you have 1000s of pop-ups in which case you have UI issues anyhow :) I think this seems somewhat reasonable if for whatever reason you feel extending PopUpManager will be problematic.shaunhusain

1 Answers

2
votes

What you need to do is create a custom popup manager. For the sake of it, I'll give you mine. I don't have the 'topmost' function, but you could easily add it. Here's the class:

package com.michelboudreau.utils {

    import flash.display.DisplayObject;
    import flash.utils.Dictionary;

    import mx.collections.ArrayCollection;
    import mx.core.IFlexDisplayObject;
    import mx.managers.PopUpManager;

    public class CustomPopUpManager {

        private static var popUps:Dictionary = new Dictionary();

        public function CustomPopUpManager()
        {
            throw new Error("CustomPopUpManager is a static class. Cannot create instance.");
        }

        /**
         * Creates a top-level window and places it above other windows in the z-order.
         * 
         * @param id:String - the id of the pop up
         * @param container:DisplayObject - DisplayObject to be used for determining which SystemManager's layers to use and optionally the reference point for centering the new top level window. It may not be the actual parent of the popup as all popups are parented by the SystemManager.
         * @param className:Class - Class of object that is to be created for the popup. The class must implement IFlexDisplayObject.
         * @param modal:Boolean(default = false) — If true, the window is modal which means that the user will not be able to interact with other popups until the window is removed
         * @param center:Boolean(default = false) - Centers a popup window over whatever window was use as container
         * @param childList:String (default = null) — The child list in which to add the popup. One of PopUpManagerChildList.APPLICATION, PopUpManagerChildList.POPUP, or PopUpManagerChildList.PARENT (default). 
         * 
         * @return IFlexDisplayObject — Reference to new top-level window
         */
        public static function createPopUp(id:String, container:DisplayObject, className:Class, modal:Boolean = false, center:Boolean = false, childList:String = null ):IFlexDisplayObject
        {
            if (getPopUpByID(id))
            {
                return getPopUpByID(id);
            }else{
                var popUp:IFlexDisplayObject = PopUpManager.createPopUp(container, className, modal, childList);

                if (center)
                {
                    PopUpManager.centerPopUp(popUp);
                }

                popUps[id] = popUp;
                return popUp;
            }

        }

        /**
         * Returns a IFlexDisplayObject based on the specified id.
         * 
         * @param id:String - the id of the pop up
         * 
         * @return IFlexDisplayObject — Reference to new top-level window
         */
        public static function getPopUpByID(id:String):IFlexDisplayObject 
        {
            return popUps[id];
        }

        /**
         * Removes all pop ups
         * 
         * @return void
         */
        public static function removeAll():void 
        {
            popUps = new Dictionary();
        }

        /**
         * Removes a popup window popped up by id.
         * 
         * @param id:String - the id of the pop up
         * 
         */
        public static function removePopUpByID(id:String):IFlexDisplayObject 
        {
            var popup:IFlexDisplayObject = getPopUpByID(id);
            if(popup)
            {
                PopUpManager.removePopUp(popup);
                removePopUpData(id);
            }
            return popup;
        }

        /**
         * Removes pop up based on IFlexDisplayObject
         * 
         * @param popUp:IFlexDisplayObject - the pop up to be removed
         */
        public static function removePopUp(popUp:IFlexDisplayObject):void 
        {
            // Always try to remove popup no matter what
            PopUpManager.removePopUp(popUp);

            // Find popup and remove from Dictionary
            for(var id:String in popUps)
            {
                if(popUps[id] == popUp)
                {
                    removePopUpData(id);
                    break;
                }
            }
        }

        /**
         * Removes the pop up data
         * 
         * @param id:String - the id of the pop up
         */
        private static function removePopUpData(id:String):void 
        {
            if(popUps[id])
            {
                popUps[id] = null;
                delete popUps[id];
            }
        }


    }
}

This should get you started. From here, if you want to implement your function, you could create an array that would add/remove popup when you create/remove them (every new popup gets added to index 0) and the function would just return the popup at index 0.

Comprender?