Skip to content

Instantly share code, notes, and snippets.

@codeinvain
Created July 17, 2010 17:13
Show Gist options
  • Save codeinvain/479664 to your computer and use it in GitHub Desktop.
Save codeinvain/479664 to your computer and use it in GitHub Desktop.
package net.codeinvain.data
{
import flash.events.TimerEvent;
import flash.utils.Dictionary;
import flash.utils.Timer;
import mx.collections.ArrayCollection;
import mx.collections.ArrayList;
public class IndexedArrayCollection extends ArrayCollection
{
private var indexField:String;
private var hash:Dictionary;
private var refreshTimer:Timer;
public function IndexedArrayCollection(source:Array=null, indexField:String="id")
{
this.indexField = indexField;
hash = new Dictionary();
refreshTimer = new Timer(1000,1);
refreshTimer.addEventListener(TimerEvent.TIMER_COMPLETE,refreshTimerComplete);
super(source);
}
private function refreshTimerComplete(e:TimerEvent):void
{
this.refresh_internal();
}
public override function addItemAt(item:Object, index:int):void
{
hash[item[indexField]] = index;
super.addItemAt(item, index);
refresh_internal();
}
public override function setItemAt(item:Object, index:int):Object
{
var oldItem:Object = getItemAt(index);
delete hash[oldItem[indexField]];
hash[item[indexField]] = index;
return super.setItemAt(item, index);
}
public override function itemUpdated(item:Object, property:Object=null, oldValue:Object=null, newValue:Object=null):void
{
if (property.toString()==indexField)
{
hash[newValue] = hash[oldValue];
delete hash[oldValue];
}
super.itemUpdated(item, property, oldValue, newValue);
}
public override function refresh():Boolean
{
this.refreshTimer.reset();
this.refreshTimer.start();
return super.refresh();
}
private function refresh_internal():void
{
this.refreshTimer.stop();
this.hash = new Dictionary();
var len:int = this.source.length;
for(var i:int=0; i<len;i++)
{
if (this.source[i]!=null)
{
hash[this.source[i][indexField]] = i;
}
}
}
public override function removeAll():void
{
this.hash = new Dictionary();
super.removeAll();
}
public function removeItems(itemsToDelete:Array):void
{
var indices:Array=[];
for (var deleteIndex:int=0;deleteIndex<itemsToDelete.length;deleteIndex++)
{
var sourceIndex :int = hash[itemsToDelete[deleteIndex][indexField]];
if (sourceIndex>-1)
{
indices.push(sourceIndex);
this.source[sourceIndex] = null;
delete hash[itemsToDelete[deleteIndex][indexField]];
}
}
indices = indices.sort(function (a:int,b:int):int{return b-a});
for (var i:int = 0 ;i<indices.length;i++){
this.source.splice(indices[i],1);
}
refresh_internal();
}
public override function removeItemAt(index:int):Object
{
var item:Object = this.getItemAt(index);
delete hash[item[indexField]];
return super.removeItemAt(index);
refresh_internal();
}
public function addItems(items:Array):void
{
var tempArr:Array = this.source.concat(items);
this.list = new ArrayList(tempArr);
refresh_internal();
}
public function find(key:Object):*
{
var result:Object = null;
if (hash[key]!=null)
{
result = this.source[hash[key]];
}
return result;
}
public function findIndex(key:Object):int
{
var result:int = -1;
if (hash[key]!=null)
{
result = hash[key];
}
return result;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment