package app { import mx.collections.ArrayCollection; import mx.controls.dataGridClasses.DataGridColumn; import mx.formatters.DateFormatter; import mx.formatters.NumberFormatter; import app.vo.SessionInfo; import mx.formatters.NumberBaseRoundType; [Bindable] public class Model { private static var _instance:Model; private static var timestampFormat:DateFormatter = new DateFormatter(); private static var mileageFormat:NumberFormatter = new NumberFormatter(); private static var speedFormat:NumberFormatter = new NumberFormatter(); { timestampFormat.formatString = "MMM D @ JJ:NN"; mileageFormat.precision = 2; mileageFormat.rounding = NumberBaseRoundType.NEAREST; speedFormat.precision = 1; speedFormat.rounding = NumberBaseRoundType.NEAREST; } public static function getInstance():Model { if (_instance != null) { return _instance; } else { return new Model(); } } public static function getEntityName(item:Object, column:DataGridColumn):Object { if (item[column.dataField] == null) { return column.headerText + " " + item[column.dataField + "Id"]; } else { return item[column.dataField].name; } } public static function formatMileage(item:Object, column:DataGridColumn):Object { return mileageFormat.format(item[column.dataField]); } public static function formatSpeed(item:Object, column:DataGridColumn):Object { return speedFormat.format(item[column.dataField]); } public static function formatTimeElapsed(item:Object, column:DataGridColumn):Object { function format(i:int, force:Boolean=false, includeColon:Boolean=true):String { if (i == 0 && ! force) { return ""; } var s:String = "" + i; while (s.length < 2) { s = "0" + s; } return s + (includeColon ? ":" : ""); } var sec:int = int(item[column.dataField]); var result:String = format(Math.floor(sec / 86400)); result += format(Math.floor(sec % 86400 / 3600), result.length) result += format(Math.floor(sec % 3600 / 60), result.length) result += format(sec % 60, result.length, false); return result; } public static function formatTimestamp(item:Object, column:DataGridColumn):Object { return timestampFormat.format(item[column.dataField]); } public function Model() { if (_instance != null) { throw new Error("You can't create multiple model instances"); } _instance = this; } public var sessionInfo:SessionInfo; public var userList:ArrayCollection = new ArrayCollection(); public var userGroupList:ArrayCollection = new ArrayCollection(); public var pointList:ArrayCollection = new ArrayCollection(); public var rideList:ArrayCollection = new ArrayCollection(); public var routeList:ArrayCollection = new ArrayCollection(); public var bikeList:ArrayCollection = new ArrayCollection(); public var lastReportArgs:Array; public var reportData:ArrayCollection = new ArrayCollection(); public var reportSeries:ArrayCollection = new ArrayCollection(); public var dayOfWeekList:ArrayCollection = new ArrayCollection([ "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" ]); private var colors:Array = [ [0xfff0f0, 0xf7e8e8], [0xf0f0ff, 0xe8e8f7], [0xf0fff0, 0xe8f7e8], [0xfffff0, 0xf7f7e8], [0xfff0ff, 0xf7e8f7], [0xf0ffff, 0xe8f7f7] ]; private var routeColorUses:uint = 0; private var routeColorMap:Object = {}; public function getRouteColors(routeId:int):Array { if (! routeColorMap.hasOwnProperty(routeId)) { routeColorMap[routeId] = colors[routeColorUses % colors.length]; routeColorUses += 1; } return routeColorMap[routeId]; } } }