package com.exanimo.gallery { import com.exanimo.data.DynamicDTO; import com.exanimo.utils.IEquatable; import flash.display.Bitmap; import flash.display.DisplayObject; import flash.display.Loader; import flash.events.Event; import flash.events.EventDispatcher; import flash.net.URLRequest; /** * * .. * * @langversion ActionScript 3 * @playerversion Flash 9.0.0 * * @author Eric Eldredge * @author Rich Perez * @author Matthew Tretter * @since 2009.01.12 * */ public class GalleryDataProvider extends EventDispatcher implements IEquatable { private var _data:XML; /** * * */ public function GalleryDataProvider(data:XML):void { this._init(data); } // // accessors // /** * * */ public function get name():String { return String(this._data.@name) || null; } /** * * */ public function get length():uint { return this._data.item.length(); } // // public methods // /** * * */ public function equals(obj:Object):Boolean { var equal:Boolean; if (obj == this) { equal = true; } else if (!(obj is GalleryDataProvider)) { equal = false; } else if (obj.length != this.length) { equal = false; } else { equal = true; for (var i:int = 0; i < this.length; i++) { if (!this.getItemAt(i).equals(obj.getItemAt(i))) { equal = false; break; } } } return equal; } public function getItemAt(index:uint):Object { if ((index < 0) || (index >= this.length)) { throw new RangeError('Index out of bounds'); } // TODO: Create a class for this so we can compare equality with .equals() var data:XML = this._data.item[index]; return this._xml2Object(data); } public function getItemByProperty(prop:String, value:String):Object { return this._xml2Object(this._data.item.(attribute(prop) == value)[0]); } public function toArray():Array { var a:Array = []; for (var i:uint = 0; i < this.length; i++) { a.push(this.getItemAt(i)); } return a; } // // private methods // private function _xml2Object(xml:XML):Object { var o:Object = {}; for each (var attr:XML in xml.attributes()) { o[attr.localName().toString()] = attr.toString(); } return new DynamicDTO(o); } /** * * */ private function _init(data:XML):void { data = data.copy(); var i:uint = 0; for each (var item:XML in data..item) { item.@name = item.@name.toString() || item.@source.toString().split('/').pop().toString().split('.')[0]; item.@index = i++; } this._data = data; } } }