I need to close the popup(adobe flex), non modal window if I click on the parent page of the popup. I have to do the out of focus check and then do some validation before close the popup. So what I was thinking that is there any inbuilt focus check event or do we need to create custom event for that ?
1 Answers
1
votes
In your popup you need to listen for mouse up event on the popup itself, and the stage. On the popup if you catch a mouse up event, stop it from getting to the stage. Then, if you receive a mouse up event on the stage you will know that it was outside of the popup and can close the popup.
<?xml version="1.0" encoding="utf-8"?>
<mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml" width="400" height="300" creationComplete="onCreationComplete()">
<mx:Script>
<![CDATA[
import mx.managers.PopUpManager;
private function onCreationComplete():void {
//listen for MouseUp events on the popup, and the stage
this.addEventListener(MouseEvent.MOUSE_UP, handleMouseUp);
stage.addEventListener(MouseEvent.MOUSE_UP, handleStageMouseUp);
}
private function handleMouseUp(e:MouseEvent):void {
//catch any MouseUp events on the popup,
//and prevent them from getting to the stage
e.stopPropagation(); //don't let this event get to the stage
}
private function handleStageMouseUp(e:MouseEvent):void {
//if the stage fires a MouseUp event, the mouse event
//came from outside of the popup, so we can close it
closePopup();
}
private function validate():Boolean {
//add your validate code here
return true;
}
private function closePopup():void {
if ( validate() ) {
//clean up event listeners, and close popup
stage.removeEventListener(MouseEvent.MOUSE_UP, handleStageMouseUp);
PopUpManager.removePopUp(this);
}
}
]]>
</mx:Script>
</mx:Canvas>