Merge branch 'master' of github.com:systemed/potlatch2
[potlatch2.git] / net / systemeD / halcyon / connection / actions / RemoveEntityFromRelationAction.as
1 package net.systemeD.halcyon.connection.actions {
2
3     import net.systemeD.halcyon.connection.*;
4     
5     public class RemoveEntityFromRelationAction extends UndoableEntityAction {
6         private var member:Entity;
7         private var memberList:Array;
8         private var memberRemovedFrom:Array;
9         private var removedMembers:Array;
10         
11         public function RemoveEntityFromRelationAction(rel:Relation, member:Entity, memberList:Array) {
12             super(rel, "Remove "+member.getType()+" "+member.id+" from ");
13             this.member = member;
14             this.memberList = memberList;
15         }
16             
17         public override function doAction():uint {
18             memberRemovedFrom = [];
19             removedMembers = [];
20             
21             var rel:Relation = entity as Relation;
22                         var i:int;
23                         while ((i=rel.findEntityMemberIndex(member))>-1) {
24                                 var removed:RelationMember = memberList.splice(i, 1)[0];
25                                 memberRemovedFrom.push(i);
26                                 removedMembers.push(removed);
27                         }
28                         
29                         if ( removedMembers.length > 0 ) {
30                             member.removeParent(rel);
31                             markDirty();
32                             rel.dispatchEvent(new RelationMemberEvent(
33                                 Connection.RELATION_MEMBER_REMOVED, member, rel, memberRemovedFrom[0]));
34                             return SUCCESS;
35                         }
36             
37             return NO_CHANGE;
38         }
39             
40         public override function undoAction():uint {
41             member.addParent(entity);
42             
43             var last:int = 0;
44             for (var i:int = removedMembers.length - 1; i >= 0; i--) {
45                 var removed:RelationMember = removedMembers[i];
46                 var index:int = memberRemovedFrom[i];
47                 memberList.splice(index, 0, removed);
48                 last = index;
49             }
50
51             markClean();
52             
53                         entity.dispatchEvent(new RelationMemberEvent(
54                                 Connection.RELATION_MEMBER_ADDED, member, Relation(entity), memberRemovedFrom[0]));
55             
56             return SUCCESS;
57         }
58     }
59 }
60