Created
April 18, 2010 02:50
-
-
Save trxcllnt/369969 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.pt.components.containers.data | |
{ | |
import flash.utils.Dictionary; | |
import mx.utils.UIDUtil; | |
public class RepeaterData | |
{ | |
public function RepeaterData() | |
{ | |
} | |
private var tree:XML = <_/>; | |
private var map:Object = {}; | |
private var map2:Dictionary = new Dictionary(true); | |
public function clear():void | |
{ | |
tree = <_/>; | |
for(var s:String in map) | |
{ | |
delete map[s]; | |
} | |
} | |
public function addItem(item:Object, length:int = 1):Object | |
{ | |
var uid:String = UIDUtil.createUID(); | |
tree.appendChild(<item index={items.length()} position={totalLength} length={length} id={uid} />); | |
map[uid] = item; | |
map2[item] = uid; | |
return item; | |
} | |
public function getItemAt(index:int):Object | |
{ | |
if(index > totalLength) | |
index = totalLength; | |
var len:int = 0; | |
for each(var child:XML in items) | |
{ | |
len = Number(child.@position); | |
if(index <= len) | |
{ | |
return map[String(child.@id)]; | |
} | |
} | |
return null; | |
} | |
public function getItemIndex(item:Object):int | |
{ | |
var uid:String = map2[item]; | |
return items.(attribute("id") == uid).attribute("index"); | |
} | |
public function getItemLength(item:Object):int | |
{ | |
var uid:String = map2[item]; | |
return items.(attribute("id") == uid).attribute("length"); | |
} | |
public function getItemPosition(item:Object):int | |
{ | |
var uid:String = map2[item]; | |
return items.(attribute("id") == uid).attribute("position"); | |
} | |
public function getItems(beginIndex:int, endIndex:int):Array | |
{ | |
if(beginIndex < 0) | |
beginIndex = 0; | |
if(endIndex > totalLength) | |
endIndex = totalLength; | |
var a:Array = []; | |
var pos:int = 0; | |
var len:int = 0; | |
for each(var child:XML in items) | |
{ | |
pos = Number(child.@position); | |
len = Number(child.@length); | |
if(beginIndex < (pos + len) && endIndex >= (pos + len)) | |
{ | |
a.push(map[String(child.@id)]); | |
} | |
else if((pos + len) > endIndex) | |
{ | |
a.push(map[String(child.@id)]); | |
break; | |
} | |
} | |
return a; | |
} | |
public function get totalLength():Number | |
{ | |
var total:Number = 0; | |
for each(var child:XML in items) | |
{ | |
total += Number(child.@length); | |
} | |
return total; | |
} | |
protected function get items():XMLList | |
{ | |
return tree.descendants().(attribute("length").length() > 0); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment