first commit

This commit is contained in:
2020-10-20 00:58:15 +02:00
commit 7f1b9bfca5
222 changed files with 56918 additions and 0 deletions

View File

@ -0,0 +1,82 @@
/*
Copyright (c) 2009 Yahoo! Inc. All rights reserved.
The copyrights embodied in the content of this file are licensed under the BSD (revised) open source license
*/
package com.yahoo.astra.utils
{
import com.yahoo.astra.display.BitmapText;
import flash.display.Sprite;
import flash.text.*;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.PixelSnapping;
/**
* Used to measure dimensions for BitmapText objects.
*
* @author Tripp Bridges
*/
public class AxisLabelUtil
{
/**
* Returns the potential width of a string when rendered in a text field. Takes into account
* the <code>TextFormat</code> settings and the rotation.
*
* @param textValue The string that will be used.
* @param tf The TextFormat object that will be applied.
* @param rotation The rotation that will be applied
*
*/
public static function getTextWidth(textValue:String, tf:TextFormat, rotation:Number = 0):Number
{
rotation = Math.max(-90, Math.min(rotation, 90));
var textField:BitmapText = new BitmapText();
textField.selectable = false;
textField.autoSize = rotation < 0 ? TextFieldAutoSize.RIGHT : TextFieldAutoSize.LEFT;
if(tf != null) textField.defaultTextFormat = tf;
textField.text = textValue;
textField.rotation = rotation;
return textField.width;
}
/**
* Returns the potential height of a string when rendered in a text field. Takes into account
* the <code>TextFormat</code> settings and the rotation.
*
* @param textValue The string that will be used.
* @param tf The TextFormat object that will be applied.
* @param rotation The rotation that will be applied
*/
public static function getTextHeight(textValue:String, tf:TextFormat, rotation:Number = 0):Number
{
rotation = Math.max(-90, Math.min(rotation, 90));
var textField:BitmapText = new BitmapText();
textField.selectable = false;
textField.autoSize = rotation < 0 ? TextFieldAutoSize.RIGHT : TextFieldAutoSize.LEFT;
if(tf != null) textField.defaultTextFormat = tf;
textField.text = textValue;
textField.rotation = rotation;
return textField.height;
}
/**
* Returns the dimensions of a text field if rotated.
*
* @param textField The text field to be used
* @param rotation The rotation to be applied
*/
public static function getBitmapTextSize(textField:TextField, rotation:Number):Object
{
var spr:Sprite = new Sprite();
var bitmapDataText:BitmapData = new BitmapData(textField.width, textField.height, true, 0);
bitmapDataText.draw(textField);
var bm:Bitmap = new Bitmap(bitmapDataText, PixelSnapping.AUTO, true);
spr.addChild(bm);
spr.rotation = rotation;
return {width:spr.width, height:spr.height};
}
}
}

362
com/yahoo/astra/utils/DateUtil.as Executable file
View File

@ -0,0 +1,362 @@
/*
Copyright (c) 2009 Yahoo! Inc. All rights reserved.
The copyrights embodied in the content of this file are licensed under the BSD (revised) open source license
*/
package com.yahoo.astra.utils
{
/**
* A collection of utility functions for the manipulation and inspection of date and time values.
*
* @see Date
*
* @author Josh Tynjala, Allen Rabinovich
*/
public class DateUtil
{
/**
* The names of months in English. The index in the array corresponds to the value of the month
* in a date object.
*/
public static var months:Array = [
"January", "February", "March", "April", "May", "June", "July",
"August", "September", "October", "November", "December"];
/**
* The number of days in January.
*/
public static const DAYS_IN_JANUARY:int = 31
/**
* The number of days in February on a standard year.
*/
public static const DAYS_IN_FEBRUARY:int = 28;
/**
* The number of days in February on a leap year.
*/
public static const DAYS_IN_FEBRUARY_LEAP_YEAR:int = 29;
/**
* The number of days in March.
*/
public static const DAYS_IN_MARCH:int = 31;
/**
* The number of days in April.
*/
public static const DAYS_IN_APRIL:int = 30;
/**
* The number of days in May.
*/
public static const DAYS_IN_MAY:int = 31;
/**
* The number of days in June.
*/
public static const DAYS_IN_JUNE:int = 30;
/**
* The number of days in July.
*/
public static const DAYS_IN_JULY:int = 31;
/**
* The number of days in August.
*/
public static const DAYS_IN_AUGUST:int = 31;
/**
* The number of days in September.
*/
public static const DAYS_IN_SEPTEMBER:int = 30;
/**
* The number of days in October.
*/
public static const DAYS_IN_OCTOBER:int = 31;
/**
* The number of days in November.
*/
public static const DAYS_IN_NOVEMBER:int = 30;
/**
* The number of days in December.
*/
public static const DAYS_IN_DECEMBER:int = 31;
/**
* The number of days in a standard year.
*/
public static const DAYS_IN_YEAR:int = 365;
/**
* The number of days in a leap year.
*/
public static const DAYS_IN_LEAP_YEAR:int = 366;
/**
* The number of days appearing in each month. May be used for easy index lookups.
* The stored value for February corresponds to a standard year--not a leap year.
*/
public static var daysInMonths:Array = [
DAYS_IN_JANUARY, DAYS_IN_FEBRUARY, DAYS_IN_MARCH, DAYS_IN_APRIL,
DAYS_IN_MAY, DAYS_IN_JUNE, DAYS_IN_JULY, DAYS_IN_AUGUST, DAYS_IN_SEPTEMBER,
DAYS_IN_OCTOBER, DAYS_IN_NOVEMBER, DAYS_IN_DECEMBER];
/**
* Determines the number of days between the start value and the end value. The result
* may contain a fractional part, so cast it to int if a whole number is desired.
*
* @param start the starting date of the range
* @param end the ending date of the range
* @return the number of dats between start and end
*/
public static function countDays(start:Date, end:Date):Number
{
return Math.abs(end.valueOf() - start.valueOf()) / (1000 * 60 * 60 * 24);
}
/**
* Determines if the input year is a leap year (with 366 days, rather than 365).
*
* @param year the year value as stored in a Date object.
* @return true if the year input is a leap year
*/
public static function isLeapYear(year:int):Boolean
{
if(year % 100 == 0) return year % 400 == 0;
return year % 4 == 0;
}
/**
* Gets the English name of the month specified by index. This is the month value
* as stored in a Date object.
*
* @param index the numeric value of the month
* @return the string name of the month in English
*/
public static function getMonthName(index:int):String
{
return months[index];
}
/**
* Gets the abbreviated month name specified by index. This is the month value
* as stored in a Date object.
*
* @param index the numeric value of the month
* @return the short string name of the month in English
*/
public static function getShortMonthName(index:int):String
{
return getMonthName(index).substr(0, 3);
}
/**
* Rounds a Date value up to the nearest value on the specified time unit.
*
* @see com.yahoo.astra.utils.TimeUnit
*/
public static function roundUp(dateToRound:Date, timeUnit:String = "day"):Date
{
dateToRound = new Date(dateToRound.valueOf());
switch(timeUnit)
{
case TimeUnit.YEAR:
dateToRound.year++;
dateToRound.month = 0;
dateToRound.date = 1;
dateToRound.hours = 0;
dateToRound.minutes = 0;
dateToRound.seconds = 0;
dateToRound.milliseconds = 0;
break;
case TimeUnit.MONTH:
dateToRound.month++;
dateToRound.date = 1;
dateToRound.hours = 0;
dateToRound.minutes = 0;
dateToRound.seconds = 0;
dateToRound.milliseconds = 0;
break;
case TimeUnit.DAY:
dateToRound.date++;
dateToRound.hours = 0;
dateToRound.minutes = 0;
dateToRound.seconds = 0;
dateToRound.milliseconds = 0;
break;
case TimeUnit.HOURS:
dateToRound.hours++;
dateToRound.minutes = 0;
dateToRound.seconds = 0;
dateToRound.milliseconds = 0;
break;
case TimeUnit.MINUTES:
dateToRound.minutes++;
dateToRound.seconds = 0;
dateToRound.milliseconds = 0;
break;
case TimeUnit.SECONDS:
dateToRound.seconds++;
dateToRound.milliseconds = 0;
break;
case TimeUnit.MILLISECONDS:
dateToRound.milliseconds++;
break;
}
return dateToRound;
}
/**
* Rounds a Date value down to the nearest value on the specified time unit.
*
* @see com.yahoo.astra.utils.TimeUnit
*/
public static function roundDown(dateToRound:Date, timeUnit:String = "day"):Date
{
dateToRound = new Date(dateToRound.valueOf());
switch(timeUnit)
{
case TimeUnit.YEAR:
dateToRound.month = 0;
dateToRound.date = 1;
dateToRound.hours = 0;
dateToRound.minutes = 0;
dateToRound.seconds = 0;
dateToRound.milliseconds = 0;
break;
case TimeUnit.MONTH:
dateToRound.date = 1;
dateToRound.hours = 0;
dateToRound.minutes = 0;
dateToRound.seconds = 0;
dateToRound.milliseconds = 0;
break;
case TimeUnit.DAY:
dateToRound.hours = 0;
dateToRound.minutes = 0;
dateToRound.seconds = 0;
dateToRound.milliseconds = 0;
break;
case TimeUnit.HOURS:
dateToRound.minutes = 0;
dateToRound.seconds = 0;
dateToRound.milliseconds = 0;
break;
case TimeUnit.MINUTES:
dateToRound.seconds = 0;
dateToRound.milliseconds = 0;
break;
case TimeUnit.SECONDS:
dateToRound.milliseconds = 0;
break;
}
return dateToRound;
}
/**
* Converts a time code to UTC.
*
* @param timecode the input timecode
* @return the UTC value
*/
public static function timeCodeToUTC(timecode:String):String {
switch (timecode) {
case "GMT", "UT", "UTC", "WET": return "UTC+0000";
case "CET": return "UTC+0100";
case "EET": return "UTC+0200";
case "MSK": return "UTC+0300";
case "IRT": return "UTC+0330";
case "SAMT": return "UTC+0400";
case "YEKT", "TMT", "TJT": return "UTC+0500";
case "OMST", "NOVT", "LKT": return "UTC+0600";
case "MMT": return "UTC+0630";
case "KRAT", "ICT", "WIT", "WAST": return "UTC+0700";
case "IRKT", "ULAT", "CST", "CIT", "BNT": return "UTC+0800";
case "YAKT", "JST", "KST", "EIT": return "UTC+0900";
case "ACST": return "UTC+0930";
case "VLAT", "SAKT", "GST": return "UTC+1000";
case "MAGT": return "UTC+1100";
case "IDLE", "PETT", "NZST": return "UTC+1200";
case "WAT": return "UTC-0100";
case "AT": return "UTC-0200";
case "EBT": return "UTC-0300";
case "NT": return "UTC-0330";
case "WBT", "AST": return "UTC-0400";
case "EST": return "UTC-0500";
case "CST": return "UTC-0600";
case "MST": return "UTC-0700";
case "PST": return "UTC-0800";
case "YST": return "UTC-0900";
case "AHST", "CAT", "HST": return "UTC-1000";
case "NT": return "UTC-1100";
case "IDLW": return "UTC-1200";
}
return "UTC+0000";
}
/**
* Determines the hours value in the range 1 - 12 for the AM/PM time format.
*
* @param value the input Date value
* @return the calculated hours value
*/
public static function getHoursIn12HourFormat(value:Date):Number
{
var hours:Number = value.getHours();
if(hours == 0)
{
return 12;
}
if(hours > 0 && hours <= 12)
{
return hours;
}
return hours - 12;
}
public static function getDateDifferenceByTimeUnit(minDate:Date, maxDate:Date, timeUnit:String):Number
{
var dateDifference:Number = 0;
var maxDateNumber:Number;
var minDateNumber:Number;
switch(timeUnit)
{
case TimeUnit.YEAR:
maxDateNumber = (maxDate.getFullYear() - minDate.getFullYear());
break;
case TimeUnit.MONTH:
dateDifference = (12 - minDate.getMonth()) + (maxDate.getFullYear() - minDate.getFullYear() - 1)*12 + maxDate.getMonth();
break;
case TimeUnit.DAY:
dateDifference = Math.round(Math.abs(maxDate.valueOf() - minDate.valueOf()) / (1000 * 60 * 60 * 24));
break;
case TimeUnit.HOURS:
dateDifference = Math.abs(minDate.valueOf() - maxDate.valueOf()) / (60 * 60 * 1000);
break;
case TimeUnit.MINUTES:
dateDifference = Math.abs(minDate.valueOf() - maxDate.valueOf()) / (60 * 1000);
break;
case TimeUnit.SECONDS:
dateDifference = Math.abs(minDate.valueOf() - maxDate.valueOf()) / 1000;
break;
case TimeUnit.MILLISECONDS:
dateDifference = Math.abs(minDate.valueOf() - maxDate.valueOf());
break;
}
return dateDifference;
}
}
}

View File

@ -0,0 +1,101 @@
/*
Copyright (c) 2009 Yahoo! Inc. All rights reserved.
The copyrights embodied in the content of this file are licensed under the BSD (revised) open source license
*/
package com.yahoo.astra.utils
{
import flash.display.DisplayObject;
import flash.geom.Point;
import flash.geom.Rectangle;
/**
* Utility functions for use with DisplayObjects.
*
* @author Josh Tynjala
*/
public class DisplayObjectUtil
{
/**
* Converts a point from the local coordinate system of one DisplayObject to
* the local coordinate system of another DisplayObject.
*
* @param point the point to convert
* @param firstDisplayObject the original coordinate system
* @param secondDisplayObject the new coordinate system
*/
public static function localToLocal(point:Point, firstDisplayObject:DisplayObject, secondDisplayObject:DisplayObject):Point
{
point = firstDisplayObject.localToGlobal(point);
return secondDisplayObject.globalToLocal(point);
}
/**
* Aligns a DisplayObject vertically and horizontally within specific bounds.
*
* @param target The DisplayObject to align.
* @param bounds The rectangle in which to align the target DisplayObject.
* @param horizontalAlign The alignment position along the horizontal axis. If <code>null</code>,
* the target's horizontal position will not change.
* @param verticalAlign The alignment position along the vertical axis. If <code>null</code>,
* the target's vertical position will not change.
*/
public static function align(target:DisplayObject, bounds:Rectangle, horizontalAlign:String = null, verticalAlign:String = null):void
{
var horizontalDifference:Number = bounds.width - target.width;
switch(horizontalAlign)
{
case "left":
target.x = bounds.x;
break;
case "center":
target.x = bounds.x + (horizontalDifference) / 2;
break;
case "right":
target.x = bounds.x + horizontalDifference;
break;
}
var verticalDifference:Number = bounds.height - target.height;
switch(verticalAlign)
{
case "top":
target.y = bounds.y;
break;
case "middle":
target.y = bounds.y + (verticalDifference) / 2;
break;
case "bottom":
target.y = bounds.y + verticalDifference;
break;
}
}
/**
* Resizes a DisplayObject to fit into specified bounds such that the
* aspect ratio of the target's width and height does not change.
*
* @param target The DisplayObject to resize.
* @param width The desired width for the target.
* @param height The desired height for the target.
* @param aspectRatio The desired aspect ratio. If NaN, the aspect
* ratio is calculated from the target's current
* width and height.
*/
public static function resizeAndMaintainAspectRatio(target:DisplayObject, width:Number, height:Number, aspectRatio:Number = NaN):void
{
var currentAspectRatio:Number = !isNaN(aspectRatio) ? aspectRatio : target.width / target.height;
var boundsAspectRatio:Number = width / height;
if(currentAspectRatio < boundsAspectRatio)
{
target.width = Math.floor(height * currentAspectRatio);
target.height = height;
}
else
{
target.width = width;
target.height = Math.floor(width / currentAspectRatio);
}
}
}
}

View File

@ -0,0 +1,95 @@
/*
Copyright (c) 2009 Yahoo! Inc. All rights reserved.
The copyrights embodied in the content of this file are licensed under the BSD (revised) open source license
*/
package com.yahoo.astra.utils
{
import flash.geom.Point;
import flash.display.DisplayObject;
/**
* Allows you to manipulate display objects based on a registration point other
* than the standard (0,0).
*
* @author Josh Tynjala
*/
public class DynamicRegistration
{
/**
* Moves a <code>DisplayObject</code> to a new position (x,y) based on a registration point. The
* true position of the object will be (x - registration.x, y - registration.y).
*
* @param target the DisplayObject to move
* @param registration the registration point of the DisplayObject
* @param x the new x position, in pixels
* @param y the new y position, in pixels
*/
public static function move(target:DisplayObject, registration:Point, x:Number = 0, y:Number = 0):void
{
//generate the location of the registration point in the parent
registration = target.localToGlobal(registration);
registration = target.parent.globalToLocal(registration);
//move the target and offset by the registration point
target.x += x - registration.x;
target.y += y - registration.y;
}
/**
* Rotates a <code>DisplayObject</code> based on a registration point.
*
* @param target the DisplayObject to move
* @param registration the registration point of the DisplayObject
* @param rotation the new rotation angle
*/
public static function rotate(target:DisplayObject, registration:Point, degrees:Number = 0):void
{
changePropertyOnRegistrationPoint(target, registration, "rotation", degrees);
}
/**
* Scales a <code>DisplayObject</code> based on a registration point.
*
* @param target the DisplayObject to move
* @param registration the registration point of the DisplayObject
* @param scaleX the new x scaling factor
* @param scaleY the new y scaling factor
*/
public static function scale(target:DisplayObject, registration:Point, scaleX:Number = 0, scaleY:Number = 0):void
{
changePropertyOnRegistrationPoint(target, registration, "scaleX", scaleX);
changePropertyOnRegistrationPoint(target, registration, "scaleY", scaleY);
}
/**
* @private
* Alters an arbitary property based on the registration point.
*
* @param target the DisplayObject to move
* @param registration the registration point of the DisplayObject
* @param propertyName the property to change
* @param value the new value of the property to change
*/
private static function changePropertyOnRegistrationPoint(target:DisplayObject, registration:Point, propertyName:String, value:Number):void
{
//generate the location of the registration point in the parent
var a:Point = registration.clone();
a = target.localToGlobal(a);
a = target.parent.globalToLocal(a);
target[propertyName] = value;
//after the property change, regenerate the location of the registration
//point in the parent
var b:Point = registration.clone();
b = target.localToGlobal(b);
b = target.parent.globalToLocal(b);
//move the target based on the difference to make it appear the change
//happened based on the registration point
target.x -= b.x - a.x;
target.y -= b.y - a.y;
}
}
}

View File

@ -0,0 +1,37 @@
/*
Copyright (c) 2009 Yahoo! Inc. All rights reserved.
The copyrights embodied in the content of this file are licensed under the BSD (revised) open source license
*/
package com.yahoo.astra.utils
{
/**
* Utility functions for common geometric operations.
*
* @author Josh Tynjala
*/
public class GeomUtil
{
/**
* Converts an angle from radians to degrees.
*
* @param radians The angle in radians
* @return The angle in degrees
*/
public static function radiansToDegrees(radians:Number):Number
{
return radians * 180 / Math.PI;
}
/**
* Converts an angle from degrees to radians.
*
* @param degrees The angle in degrees
* @return The angle in radians
*/
public static function degreesToRadians(degrees:Number):Number
{
return degrees * Math.PI / 180;
}
}
}

View File

@ -0,0 +1,148 @@
/*
Copyright (c) 2009 Yahoo! Inc. All rights reserved.
The copyrights embodied in the content of this file are licensed under the BSD (revised) open source license
*/
package com.yahoo.astra.utils
{
import flash.display.Graphics;
import flash.geom.Point;
/**
* Utility functions for drawing to <code>Graphics</code> objects.
*
* @author Josh Tynjala
* @see flash.display.Graphics
*/
public class GraphicsUtil
{
/**
* @private
* Draws a wedge.
*
* @param x x component of the wedge's center point
* @param y y component of the wedge's center point
* @param startAngle starting angle in degrees
* @param arc sweep of the wedge. Negative values draw clockwise.
* @param radius radius of wedge. If [optional] yRadius is defined, then radius is the x radius.
* @param yRadius [optional] y radius for wedge.
*/
public static function drawWedge(target:Graphics, x:Number, y:Number, startAngle:Number, arc:Number, radius:Number, yRadius:Number = NaN):void
{
// move to x,y position
target.moveTo(x, y);
// if yRadius is undefined, yRadius = radius
if(isNaN(yRadius))
{
yRadius = radius;
}
// limit sweep to reasonable numbers
if(Math.abs(arc) > 360)
{
arc = 360;
}
// Flash uses 8 segments per circle, to match that, we draw in a maximum
// of 45 degree segments. First we calculate how many segments are needed
// for our arc.
var segs:int = Math.ceil(Math.abs(arc) / 45);
// Now calculate the sweep of each segment.
var segAngle:Number = arc / segs;
// The math requires radians rather than degrees. To convert from degrees
// use the formula (degrees/180)*Math.PI to get radians.
var theta:Number = -(segAngle / 180) * Math.PI;
// convert angle startAngle to radians
var angle:Number = -(startAngle / 180) * Math.PI;
// draw the curve in segments no larger than 45 degrees.
if(segs > 0)
{
// draw a line from the center to the start of the curve
var ax:Number = x + Math.cos(startAngle / 180 * Math.PI) * radius;
var ay:Number = y + Math.sin(-startAngle / 180 * Math.PI) * yRadius;
target.lineTo(ax, ay);
// Loop for drawing curve segments
for(var i:int = 0; i < segs; i++)
{
angle += theta;
var angleMid:Number = angle - (theta / 2);
var bx:Number = x + Math.cos(angle) * radius;
var by:Number = y + Math.sin(angle) * yRadius;
var cx:Number = x + Math.cos(angleMid) * (radius / Math.cos(theta / 2));
var cy:Number = y + Math.sin(angleMid) * (yRadius / Math.cos(theta / 2));
target.curveTo(cx, cy, bx, by);
}
// close the wedge by drawing a line to the center
target.lineTo(x, y);
}
}
/**
* Draws a dashed line between two points.
*
* @param xStart The x position of the start of the line
* @param yStart The y position of the start of the line
* @param xEnd The x position of the end of the line
* @param yEnd The y position of the end of the line
* @param dashSize the size of dashes, in pixels
* @param gapSize the size of gaps between dashes, in pixels
*/
public static function drawDashedLine(target:Graphics, xStart:Number, yStart:Number, xEnd:Number, yEnd:Number, dashSize:Number = 10, gapSize:Number = 10):void
{
// calculate the length of a segment
var segmentLength:Number = dashSize + gapSize;
// calculate the length of the dashed line
var xDelta:Number = xEnd - xStart;
var yDelta:Number = yEnd - yStart;
var delta:Number = Math.sqrt(Math.pow(xDelta, 2) + Math.pow(yDelta, 2));
// calculate the number of segments needed
var segmentCount:int = Math.floor(Math.abs(delta / segmentLength));
// get the angle of the line in radians
var radians:Number = Math.atan2(yDelta, xDelta);
// start the line here
var xCurrent:Number = xStart;
var yCurrent:Number = yStart;
// add these to cx, cy to get next seg start
xDelta = Math.cos(radians) * segmentLength;
yDelta = Math.sin(radians) * segmentLength;
// loop through each segment
for(var i:int = 0; i < segmentCount; i++)
{
target.moveTo(xCurrent, yCurrent);
target.lineTo(xCurrent + Math.cos(radians) * dashSize, yCurrent + Math.sin(radians) * dashSize);
xCurrent += xDelta;
yCurrent += yDelta;
}
// handle last segment as it is likely to be partial
target.moveTo(xCurrent, yCurrent);
delta = Math.sqrt((xEnd - xCurrent) * (xEnd - xCurrent) + (yEnd - yCurrent) * (yEnd - yCurrent));
if(delta > dashSize)
{
// segment ends in the gap, so draw a full dash
target.lineTo(xCurrent + Math.cos(radians) * dashSize, yCurrent + Math.sin(radians) * dashSize);
}
else if(delta > 0)
{
// segment is shorter than dash so only draw what is needed
target.lineTo(xCurrent + Math.cos(radians) * delta, yCurrent + Math.sin(radians) * delta);
}
// move the pen to the end position
target.moveTo(xEnd, yEnd);
}
}
}

View File

@ -0,0 +1,5 @@
/*
Copyright (c) 2009 Yahoo! Inc. All rights reserved.
The copyrights embodied in the content of this file are licensed under the BSD (revised) open source license
*/
package com.yahoo.astra.utils {

View File

@ -0,0 +1,161 @@
/*
Copyright (c) 2009 Yahoo! Inc. All rights reserved.
The copyrights embodied in the content of this file are licensed under the BSD (revised) open source license
*/
package com.yahoo.astra.utils
{
import flash.utils.getQualifiedClassName;
/**
* Creates an instance of the specified class. Sets initial properties, and calls specified methods.
*
* @author Josh Tynjala
*/
public class InstanceFactory
{
//--------------------------------------
// Constructor
//--------------------------------------
/**
* Constructor.
*/
public function InstanceFactory(targetClass:Class, properties:Object = null, methods:Object = null)
{
this.targetClass = targetClass;
this.properties = properties;
this.methods = methods;
}
//--------------------------------------
// Properties
//--------------------------------------
/**
* @private
* Storage for the targetClass property.
*/
private var _targetClass:Class = Object;
/**
* The class that will be instantiated.
*/
public function get targetClass():Class
{
return this._targetClass;
}
/**
* @private
*/
public function set targetClass(value:Class):void
{
this._targetClass = value;
}
/**
* Storage for the properties property.
*/
private var _properties:Object;
/**
* The initial values to pass to the properties of the
* newly-instantiated object.
*/
public function get properties():Object
{
return this._properties;
}
/**
* @private
*/
public function set properties(value:Object):void
{
this._properties = value;
}
/**
* @private
* Storage for the methods property.
*/
private var _methods:Object;
/**
* A set of methods to call once the object has been created and
* properties have been initialized. Format is a set of key-value pairs
* where the key is the name of the method and the value is an Array
* of parameter values.
*
* <p>Example: <code>{ load: [ "image.gif" ] }</code></p>
*/
public function get methods():Object
{
return this._methods;
}
/**
* @private
*/
public function set methods(value:Object):void
{
this._methods = value;
}
//--------------------------------------
// Public Methods
//--------------------------------------
/**
* Creates a new instance of the target class and initializes it.
*/
public function createInstance():Object
{
var instance:Object = new targetClass();
this.restoreInstance(instance);
return instance;
}
/**
* Initializes an object with the properties and methods. The object
* must be an instance of the <code>targetClass</code> property, or
* this method will throw an <code>ArgumentError</code>.
*/
public function restoreInstance(instance:Object):void
{
if(!(instance is targetClass))
{
throw new ArgumentError("Value to be initialized must be an instance of " + getQualifiedClassName(this.targetClass));
}
//set initial properties
if(this.properties)
{
for(var propName:String in this.properties)
{
if(instance.hasOwnProperty(propName))
{
instance[propName] = properties[propName];
}
}
}
//make initial method calls
//use case: Loader.load()
if(this.methods)
{
for(var methodName:String in this.methods)
{
if(instance[methodName] is Function)
{
var args:Array = this.methods[methodName] as Array;
instance[methodName].apply(instance, args);
}
}
}
}
}
}

View File

@ -0,0 +1,18 @@
/*
Copyright (c) 2009 Yahoo! Inc. All rights reserved.
The copyrights embodied in the content of this file are licensed under the BSD (revised) open source license
*/
package com.yahoo.astra.utils {
import mx.validators.CreditCardValidator;
import mx.validators.CurrencyValidator;
import mx.validators.DateValidator;
import mx.validators.EmailValidator;
import mx.validators.NumberValidator;
import mx.validators.PhoneNumberValidator;
import mx.validators.StringValidator;
import mx.validators.Validator;
import mx.validators.ZipCodeValidator;
import mx.validators.ZipCodeValidatorDomainType;
/**
* A helper class to be used associated with <code>MX.validators</code> classes.

View File

@ -0,0 +1,114 @@
/*
Copyright (c) 2009 Yahoo! Inc. All rights reserved.
The copyrights embodied in the content of this file are licensed under the BSD (revised) open source license
*/
package com.yahoo.astra.utils
{
/**
* A collection of utility functions for the manipulation of numeric values.
*
* @author Josh Tynjala
*/
public class NumberUtil
{
/**
* Rounds a Number to the nearest multiple of an input. For example, by rounding
* 16 to the nearest 10, you will receive 20. Similar to the built-in function Math.round().
*
* @param numberToRound the number to round
* @param nearest the number whose mutiple must be found
* @return the rounded number
*
* @see Math#round
*/
public static function roundToNearest(number:Number, nearest:Number = 1):Number
{
if(nearest == 0)
{
return number;
}
var roundedNumber:Number = Math.round(NumberUtil.roundToPrecision(number / nearest, 10)) * nearest;
return NumberUtil.roundToPrecision(roundedNumber, 10);
}
/**
* Rounds a Number <em>up</em> to the nearest multiple of an input. For example, by rounding
* 16 up to the nearest 10, you will receive 20. Similar to the built-in function Math.ceil().
*
* @param numberToRound the number to round up
* @param nearest the number whose mutiple must be found
* @return the rounded number
*
* @see Math#ceil
*/
public static function roundUpToNearest(number:Number, nearest:Number = 1):Number
{
if(nearest == 0)
{
return number;
}
return Math.ceil(NumberUtil.roundToPrecision(number / nearest, 10)) * nearest;
}
/**
* Rounds a Number <em>down</em> to the nearest multiple of an input. For example, by rounding
* 16 down to the nearest 10, you will receive 10. Similar to the built-in function Math.floor().
*
* @param numberToRound the number to round down
* @param nearest the number whose mutiple must be found
* @return the rounded number
*
* @see Math#floor
*/
public static function roundDownToNearest(number:Number, nearest:Number = 1):Number
{
if(nearest == 0)
{
return number;
}
return Math.floor(NumberUtil.roundToPrecision(number / nearest, 10)) * nearest;
}
/**
* Rounds a number to a certain level of precision. Useful for limiting the number of
* decimal places on a fractional number.
*
* @param number the input number to round.
* @param precision the number of decimal digits to keep
* @return the rounded number, or the original input if no rounding is needed
*
* @see Math#round
*/
public static function roundToPrecision(number:Number, precision:int = 0):Number
{
var decimalPlaces:Number = Math.pow(10, precision);
return Math.round(decimalPlaces * number) / decimalPlaces;
}
/**
* Tests equality for numbers that may have been generated by faulty floating point math.
* This is not an issue exclusive to the Flash Player, but all modern computing in general.
* The value is generally offset by an insignificant fraction, and it may be corrected.
*
* <p>Alternatively, this function could be used for other purposes than to correct floating
* point errors. Certainly, it could determine if two very large numbers are within a certain
* range of difference. This might be useful for determining "ballpark" estimates or similar
* statistical analysis that may not need complete accuracy.</p>
*
* @param number1 the first number to test
* @param number2 the second number to test
* @param precision the number of digits in the fractional portion to keep
* @return true, if the numbers are close enough to be considered equal, false if not.
*/
public static function fuzzyEquals(number1:Number, number2:Number, precision:int = 5):Boolean
{
var difference:Number = number1 - number2;
var range:Number = Math.pow(10, -precision);
//default precision checks the following:
//0.00001 < difference > -0.00001
return difference < range && difference > -range;
}
}
}

View File

@ -0,0 +1,74 @@
/*
Copyright (c) 2009 Yahoo! Inc. All rights reserved.
The copyrights embodied in the content of this file are licensed under the BSD (revised) open source license
*/
package com.yahoo.astra.utils
{
import flash.text.*;
/**
* Utility class for text fields
*
* @author Tripp Bridges
*/
public class TextUtil
{
/**
* Returns the width of a text field based on a <code>TextFormat</code> object and a string to be displayed
*
* @param textValue The text
* @param tf
*
* @return Number
*/
public static function getTextWidth(textValue:String, tf:TextFormat):Number
{
var textField:TextField = new TextField();
textField.selectable = false;
textField.autoSize = TextFieldAutoSize.LEFT;
textField.text = textValue;
textField.setTextFormat(tf);
return Math.max(textField.textWidth, textField.width);
}
/**
* Returns the height of a text field based on a <code>TextFormat</code> object and a string to be displayed
*
* @param textValue The text
* @param tf
*
* @return Number
*/
public static function getTextHeight(textValue:String, tf:TextFormat):Number
{
var textField:TextField = new TextField();
textField.selectable = false;
textField.autoSize = TextFieldAutoSize.LEFT;
textField.text = textValue;
textField.setTextFormat(tf);
return textField.textHeight;
}
/**
* Changes individual property of a <code>TextFormat</code> object
*/
public static function changeTextFormatProps(tf:TextFormat, tfProps:Object):TextFormat
{
for(var i:String in tfProps)
{
tf[i] = tfProps[i];
}
return tf;
}
/**
* Creates a copy of a <code>TextFormat</code> object
*/
public static function cloneTextFormat(tf:TextFormat):TextFormat
{
return new TextFormat(tf.font, tf.size, tf.color, tf.bold, tf.italic, tf.underline, tf.url, tf.target, tf.align, tf.leftMargin, tf.rightMargin, tf.indent, tf.leading);
}
}
}

View File

@ -0,0 +1,49 @@
/*
Copyright (c) 2009 Yahoo! Inc. All rights reserved.
The copyrights embodied in the content of this file are licensed under the BSD (revised) open source license
*/
package com.yahoo.astra.utils
{
/**
* A collection of constants for date and time units.
*
* @author Josh Tynjala
*/
public class TimeUnit
{
/**
* A constant representing the year unit in date and time values.
*/
public static const YEAR:String = "year";
/**
* A constant representing the month unit in date and time values.
*/
public static const MONTH:String = "month";
/**
* A constant representing the day unit in date and time values.
*/
public static const DAY:String = "day";
/**
* A constant representing the hours unit in date and time values.
*/
public static const HOURS:String = "hours";
/**
* A constant representing the minutes unit in date and time values.
*/
public static const MINUTES:String = "minutes";
/**
* A constant representing the seconds unit in date and time values.
*/
public static const SECONDS:String = "seconds";
/**
* A constant representing the milliseconds unit in date and time values.
*/
public static const MILLISECONDS:String = "milliseconds";
}
}

View File

@ -0,0 +1,6 @@
/*
Copyright (c) 2009 Yahoo! Inc. All rights reserved.
The copyrights embodied in the content of this file are licensed under the BSD (revised) open source license
*/
package com.yahoo.astra.utils {
import com.yahoo.astra.utils.IValueParser;