Show+remove (but not add) entity at specific relation indexes
[potlatch2.git] / net / systemeD / halcyon / connection / Relation.as
1 package net.systemeD.halcyon.connection {
2
3     public class Relation extends Entity {
4         private var members:Array;
5                 public static var entity_type:String = 'relation';
6
7         public function Relation(id:Number, version:uint, tags:Object, loaded:Boolean, members:Array) {
8             super(id, version, tags, loaded);
9             this.members = members;
10                         for each (var member:RelationMember in members) { member.entity.addParent(this); }
11         }
12
13         public function update(version:uint, tags:Object, loaded:Boolean, members:Array):void {
14                         var member:RelationMember;
15                         for each (member in this.members) { member.entity.removeParent(this); }
16                         updateEntityProperties(version,tags,loaded); this.members=members;
17                         for each (member in members) { member.entity.addParent(this); }
18                 }
19                 
20         public function get length():uint {
21             return members.length;
22         }
23
24         public function findEntityMemberIndex(entity:Entity):int {
25             for (var index:uint = 0; index < members.length; index++) {
26                 var member:RelationMember = members[index];
27                 if ( member.entity == entity )
28                     return index;
29             }
30             return -1;
31         }
32
33         public function findEntityMemberIndexes(entity:Entity):Array {
34             var indexes:Array = [];
35             for (var index:uint = 0; index < members.length; index++) {
36                 var member:RelationMember = members[index];
37                 if ( member.entity == entity )
38                     indexes.push(index);
39             }
40             return indexes;
41         }
42         
43         public function getMember(index:uint):RelationMember {
44             return members[index];
45         }
46
47         public function setMember(index:uint, member:RelationMember):void {
48                         member.entity.addParent(this);
49                         members.splice(index, 1, member);
50                         markDirty();
51         }
52
53         public function insertMember(index:uint, member:RelationMember):void {
54                         member.entity.addParent(this);
55             members.splice(index, 0, member);
56                         markDirty();
57         }
58
59         public function appendMember(member:RelationMember):uint {
60                         member.entity.addParent(this);
61             members.push(member);
62                         markDirty();
63             return members.length;
64         }
65
66                 public function removeMember(entity:Entity):void {
67                         var i:int;
68                         while ((i=findEntityMemberIndex(entity))>-1) {
69                                 members.splice(i, 1);
70                         }
71                         entity.removeParent(this);
72                         markDirty();
73                 }
74
75         public function removeMemberByIndex(index:uint):void {
76             var removed:Array=members.splice(index, 1);
77                         var entity:Entity=removed[0].entity;
78                         if (findEntityMemberIndex(entity)==-1) { entity.removeParent(this); }
79                         markDirty();
80         }
81
82                 public override function remove():void {
83                         removeFromParents();
84                         for each (var member:RelationMember in members) { member.entity.removeParent(this); }
85                         members=[];
86                         deleted=true;
87             dispatchEvent(new EntityEvent(Connection.RELATION_DELETED, this));
88                 }
89
90                 internal override function isEmpty():Boolean {
91                         return (deleted || (members.length==0));
92                 }
93
94         public function getDescription():String {
95             var desc:String = "";
96             var relTags:Object = getTagsHash();
97             if ( relTags["type"] ) {
98                 desc = relTags["type"];
99                 if ( relTags[desc] )
100                     desc += " " + relTags[desc];
101             }
102             if ( relTags["ref"] )
103                 desc += " " + relTags["ref"];
104             if ( relTags["name"] )
105                 desc += " " + relTags["name"];
106             return desc;
107         }
108
109                 public override function getType():String {
110                         return 'relation';
111                 }
112                 
113                 public override function toString():String {
114             return "Relation("+id+"@"+version+"): "+members.length+" members "+getTagList();
115         }
116
117     }
118
119 }