<commit_before>package org.openforis.collect.util 
{
	import flash.display.DisplayObject;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	import mx.collections.ArrayCollection;
	import mx.core.FlexGlobals;
	import mx.core.UIComponent;

	/**
	 * @author <NAME>
	 */
	public class PopUpUtil {
		
		
		public static const POSITION_RIGHT:String = "right";
		public static const POSITION_ABOVE:String = "top";
		public static const POSITION_BELOW:String = "bottom";
		public static const POSITION_LEFT:String = "left";
		
		public static const POSITIONS:Array = [POSITION_RIGHT, POSITION_ABOVE, POSITION_BELOW, POSITION_LEFT];
		
		public static const VERTICAL_ALIGN_TOP:String = "top";
		public static const VERTICAL_ALIGN_BOTTOM:String = "bottom";
		public static const VERTICAL_ALIGN_MIDDLE:String = "middle";
		
		public static const HORIZONTAL_ALIGN_LEFT:String = "left";
		public static const HORIZONTAL_ALIGN_RIGHT:String = "right";
		public static const HORIZONTAL_ALIGN_CENTER:String = "center";
		
		
		public static function center(parent:DisplayObject, popup:UIComponent):void {
			var pt:Point = new Point(0, 0);
			pt = parent.localToGlobal(pt); // Convert refAnchor's local 0,0 into global coordinate
			//pt = popup.globalToLocal(pt); // Convert the result into local coordinate of myPop
			var popupX:Number = Math.round((parent.width - popup.width) / 2) + pt.x;
			var popupY:Number = Math.round((parent.height - popup.height) / 2) + pt.y;
			popup.move(popupX, popupY);
		}
		
		/**
		 * Try to position the popUp in the specified preferredPosition.
		 * If at the end the popUp will exceed the viewPort, than try to position the popUp in other possible positions,
		 * according to the order of POSITIONS array.
		 */
		public static function alignToField(popUp:DisplayObject, inputField:DisplayObject, 
				preferredPosition:String = "right", verticalAlign:String = "middle", horizontalAlign:String = "left",
				ensureVisibility:Boolean = true):String {
			var positions:ArrayCollection = new ArrayCollection(POSITIONS);
			CollectionUtil.moveItem(positions, preferredPosition, 0);
			
			for each (var pos:String in positions) {
				internalAlignToField(popUp, inputField, pos, verticalAlign, horizontalAlign, false);
				if(! exceedViewport(popUp)) {
					return pos;
				}
			}
			if(ensureVisibility) {
				PopUpUtil.ensureVisibility(popUp);
			}
			return pos;
		}
		
		protected static function internalAlignToField(popUp:DisplayObject, inputField:DisplayObject, 
				position:String = "right", verticalAlign:String = "middle", horizontalAlign:String = "left",
				ensureVisibility:Boolean = true):void {
			//auto positioning tooltip (first on right, then below, otherwiese on top) 
			var componentBounds:Rectangle = inputField.getBounds(inputField.stage);
			
			var x:Number, y:Number;
			switch(position) {
				case POSITION_RIGHT:
					x = componentBounds.x + inputField.width;
					switch(verticalAlign) {
						case VERTICAL_ALIGN_MIDDLE:
							y = componentBounds.y + (inputField.height - popUp.height) / 2;
							break;
						case VERTICAL_ALIGN_TOP:
							y = componentBounds.y + inputField.height - popUp.height;
							break;
						case VERTICAL_ALIGN_BOTTOM:
							y = componentBounds.y;
							break;
					}
					var adjustedPosition:Point = getAdjustedPositionVertically(popUp, new Point(x, y));
					x = adjustedPosition.x;
					y = adjustedPosition.y;
					break;
				case POSITION_LEFT:
					x = componentBounds.x - popUp.width;
					switch(verticalAlign) {
						case VERTICAL_ALIGN_MIDDLE:
							y = componentBounds.y + (inputField.height - popUp.height) / 2;
							break;
						case VERTICAL_ALIGN_TOP:
							y = componentBounds.y + inputField.height - popUp.height;
							break;
						case VERTICAL_ALIGN_BOTTOM:
							y = componentBounds.y;
							break;
					}
					var adjustedPosition:Point = getAdjustedPositionVertically(popUp, new Point(x, y));
					x = adjustedPosition.x;
					y = adjustedPosition.y;
					break;
				case POSITION_BELOW:
					switch(horizontalAlign) {
						case HORIZONTAL_ALIGN_LEFT:
							x = componentBounds.x;
							break;
						case HORIZONTAL_ALIGN_RIGHT:
							x = componentBounds.x + inputField.width - popUp.width;
							break;
						case HORIZONTAL_ALIGN_CENTER:
							x = componentBounds.x + (inputField.width - popUp.width) / 2;
							break;
					}
					y = componentBounds.y + inputField.height;
					var adjustedPosition:Point = getAdjustedPositionHorizontally(popUp, new Point(x, y));
					x = adjustedPosition.x;
					y = adjustedPosition.y;
					break;
				case POSITION_ABOVE:
					switch(horizontalAlign) {
						case HORIZONTAL_ALIGN_LEFT:
							x = componentBounds.x;
							break;
						case HORIZONTAL_ALIGN_RIGHT:
							x = componentBounds.x + inputField.width - popUp.width;
							break;
						case HORIZONTAL_ALIGN_CENTER:
							x = componentBounds.x + (inputField.width - popUp.width) / 2;
							break;
					}
					y = componentBounds.y - popUp.height;
					var adjustedPosition:Point = getAdjustedPositionHorizontally(popUp, new Point(x, y));
					x = adjustedPosition.x;
					y = adjustedPosition.y;
					break;
			}
			popUp.x = x;
			popUp.y = y;
			
			if(ensureVisibility) {
				PopUpUtil.ensureVisibility(popUp);
			}
		}
		
		public static function alignToMousePoint(popUp:DisplayObject, xOffset:Number = NaN, yOffset:Number = NaN, ensureVisibility:Boolean = true):void {
			var alignmentPoint:Point = new Point(FlexGlobals.topLevelApplication.mouseX, FlexGlobals.topLevelApplication.mouseY);
			if(! isNaN(xOffset))
				alignmentPoint.x += xOffset;
			if(! isNaN(yOffset))
				alignmentPoint.y += yOffset;
			
			alignToPoint(popUp, alignmentPoint, ensureVisibility);
		}
		
		public static function alignToPoint(popUp:DisplayObject, alignmentPoint:Point, ensureVisibility:Boolean = true):void {
			popUp.x = alignmentPoint.x;
			popUp.y = alignmentPoint.y;
			
			if(ensureVisibility) {
				PopUpUtil.ensureVisibility(popUp);<commit_msg>Code clean<commit_after>package org.openforis.collect.util 
{
	import flash.display.DisplayObject;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	import mx.collections.ArrayCollection;
	import mx.core.FlexGlobals;
	import mx.core.UIComponent;

	/**
	 * @author <NAME>
	 */
	public class PopUpUtil {
		
		
		public static const POSITION_RIGHT:String = "right";
		public static const POSITION_ABOVE:String = "top";
		public static const POSITION_BELOW:String = "bottom";
		public static const POSITION_LEFT:String = "left";
		
		public static const POSITIONS:Array = [POSITION_RIGHT, POSITION_ABOVE, POSITION_BELOW, POSITION_LEFT];
		
		public static const VERTICAL_ALIGN_TOP:String = "top";
		public static const VERTICAL_ALIGN_BOTTOM:String = "bottom";
		public static const VERTICAL_ALIGN_MIDDLE:String = "middle";
		
		public static const HORIZONTAL_ALIGN_LEFT:String = "left";
		public static const HORIZONTAL_ALIGN_RIGHT:String = "right";
		public static const HORIZONTAL_ALIGN_CENTER:String = "center";
		
		
		public static function center(parent:DisplayObject, popup:UIComponent):void {
			var pt:Point = new Point(0, 0);
			pt = parent.localToGlobal(pt); // Convert refAnchor's local 0,0 into global coordinate
			//pt = popup.globalToLocal(pt); // Convert the result into local coordinate of myPop
			var popupX:Number = Math.round((parent.width - popup.width) / 2) + pt.x;
			var popupY:Number = Math.round((parent.height - popup.height) / 2) + pt.y;
			popup.move(popupX, popupY);
		}
		
		/**
		 * Try to position the popUp in the specified preferredPosition.
		 * If at the end the popUp will exceed the viewPort, than try to position the popUp in other possible positions,
		 * according to the order of POSITIONS array.
		 */
		public static function alignToField(popUp:DisplayObject, inputField:DisplayObject, 
				preferredPosition:String = "right", verticalAlign:String = "middle", horizontalAlign:String = "left",
				ensureVisibility:Boolean = true):String {
			var positions:ArrayCollection = new ArrayCollection(POSITIONS);
			CollectionUtil.moveItem(positions, preferredPosition, 0);
			
			for each (var pos:String in positions) {
				internalAlignToField(popUp, inputField, pos, verticalAlign, horizontalAlign, false);
				if(! exceedViewport(popUp)) {
					return pos;
				}
			}
			if(ensureVisibility) {
				PopUpUtil.ensureVisibility(popUp);
			}
			return pos;
		}
		
		protected static function internalAlignToField(popUp:DisplayObject, inputField:DisplayObject, 
				position:String = "right", verticalAlign:String = "middle", horizontalAlign:String = "left",
				ensureVisibility:Boolean = true):void {
			//auto positioning tooltip (first on right, then below, otherwiese on top) 
			var componentBounds:Rectangle = inputField.getBounds(inputField.stage);
			
			var x:Number, y:Number;
			var adjustedPosition:Point;
			switch(position) {
				case POSITION_RIGHT:
					x = componentBounds.x + inputField.width;
					switch(verticalAlign) {
						case VERTICAL_ALIGN_MIDDLE:
							y = componentBounds.y + (inputField.height - popUp.height) / 2;
							break;
						case VERTICAL_ALIGN_TOP:
							y = componentBounds.y + inputField.height - popUp.height;
							break;
						case VERTICAL_ALIGN_BOTTOM:
							y = componentBounds.y;
							break;
					}
					adjustedPosition = getAdjustedPositionVertically(popUp, new Point(x, y));
					x = adjustedPosition.x;
					y = adjustedPosition.y;
					break;
				case POSITION_LEFT:
					x = componentBounds.x - popUp.width;
					switch(verticalAlign) {
						case VERTICAL_ALIGN_MIDDLE:
							y = componentBounds.y + (inputField.height - popUp.height) / 2;
							break;
						case VERTICAL_ALIGN_TOP:
							y = componentBounds.y + inputField.height - popUp.height;
							break;
						case VERTICAL_ALIGN_BOTTOM:
							y = componentBounds.y;
							break;
					}
					adjustedPosition = getAdjustedPositionVertically(popUp, new Point(x, y));
					x = adjustedPosition.x;
					y = adjustedPosition.y;
					break;
				case POSITION_BELOW:
					switch(horizontalAlign) {
						case HORIZONTAL_ALIGN_LEFT:
							x = componentBounds.x;
							break;
						case HORIZONTAL_ALIGN_RIGHT:
							x = componentBounds.x + inputField.width - popUp.width;
							break;
						case HORIZONTAL_ALIGN_CENTER:
							x = componentBounds.x + (inputField.width - popUp.width) / 2;
							break;
					}
					y = componentBounds.y + inputField.height;
					adjustedPosition = getAdjustedPositionHorizontally(popUp, new Point(x, y));
					x = adjustedPosition.x;
					y = adjustedPosition.y;
					break;
				case POSITION_ABOVE:
					switch(horizontalAlign) {
						case HORIZONTAL_ALIGN_LEFT:
							x = componentBounds.x;
							break;
						case HORIZONTAL_ALIGN_RIGHT:
							x = componentBounds.x + inputField.width - popUp.width;
							break;
						case HORIZONTAL_ALIGN_CENTER:
							x = componentBounds.x + (inputField.width - popUp.width) / 2;
							break;
					}
					y = componentBounds.y - popUp.height;
					adjustedPosition = getAdjustedPositionHorizontally(popUp, new Point(x, y));
					x = adjustedPosition.x;
					y = adjustedPosition.y;
					break;
			}
			popUp.x = x;
			popUp.y = y;
			
			if(ensureVisibility) {
				PopUpUtil.ensureVisibility(popUp);
			}
		}
		
		public static function alignToMousePoint(popUp:DisplayObject, xOffset:Number = NaN, yOffset:Number = NaN, ensureVisibility:Boolean = true):void {
			var alignmentPoint:Point = new Point(FlexGlobals.topLevelApplication.mouseX, FlexGlobals.topLevelApplication.mouseY);
			if(! isNaN(xOffset))
				alignmentPoint.x += xOffset;
			if(! isNaN(yOffset))
				alignmentPoint.y += yOffset;
			
			alignToPoint(popUp, alignmentPoint, ensureVisibility);
		}
		
		public static function alignToPoint(popUp:DisplayObject, alignmentPoint:Point, ensureVisibility:Boolean = true):void {
			popUp.x = alignmentPoint.x;
			popUp.y = alignmentPoint.y;
			
			if(ensureVisibility) {
				PopUpUtil.ensureVisibility(popUp);
{'fraction_non_alphanumeric': 0.09678499665103818, 'fraction_numerical': 0.001841929002009377, 'mean_word_length': 3.4654205607476634, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3144933', 'n_tokens_mistral': 4744, 'n_tokens_neox': 3693, 'n_words': 838}