Merge branch 'master' of github.com:systemed/potlatch2
[potlatch2.git] / net / systemeD / halcyon / connection / actions / RemoveMemberByIndexAction.as
1 package net.systemeD.halcyon.connection.actions {
2
3     import net.systemeD.halcyon.connection.*;
4
5     public class RemoveMemberByIndexAction extends UndoableEntityAction {
6
7         private var relation:Relation;
8         private var members:Array;
9         private var index:uint;
10         private var removed:Array;
11         private var fireEvent:Boolean;
12
13         public function RemoveMemberByIndexAction(relation:Relation, members:Array, index:uint, fireEvent:Boolean = true) {
14             super(relation, "Remove member at index "+index+" from "+relation);
15             this.relation = relation;
16             this.members = members;
17             this.index = index;
18             this.fireEvent = fireEvent;
19         }
20
21         public override function doAction():uint {
22             removed = members.splice(index,1);
23             var e:Entity = removed[0].entity;
24
25             if (relation.findEntityMemberIndex(e)==-1)
26                 e.removeParent(relation);
27
28             markDirty();
29             if (fireEvent) {
30               relation.dispatchEvent(new RelationMemberEvent(Connection.RELATION_MEMBER_REMOVED, e, relation, index));
31             }
32
33             return SUCCESS;
34         }
35
36         public override function undoAction():uint {
37             members.splice(index, 0, removed[0]);
38             var e:Entity = removed[0].entity;
39             e.addParent(relation);
40
41             markClean();
42             if (fireEvent) {
43               relation.dispatchEvent(new RelationMemberEvent(Connection.RELATION_MEMBER_ADDED, e, relation, index));
44             }
45
46             return SUCCESS;
47         }
48
49     }
50
51 }