Complete support for loading relations by ID
[potlatch2.git] / net / systemeD / potlatch2 / RelationLoaderPanel.mxml
index b5c305e..53c9624 100644 (file)
@@ -2,10 +2,11 @@
 <mx:TitleWindow
        xmlns:mx="http://www.adobe.com/2006/mxml"
        xmlns:potlatch2="net.systemeD.potlatch2.*"
-       title="Load Relation" width="350" height="200"
+       title="Load Relation" width="350" height="160"
+       creationComplete="requestedID.setFocus()"
        showCloseButton="true" close="PopUpManager.removePopUp(this);">
        
-       <mx:HBox>
+       <mx:HBox paddingLeft="3" paddingTop="4">
                <mx:Label text="Relation ID: " />
                <mx:TextInput id="requestedID" restrict="0-9" />
        </mx:HBox>
                import net.systemeD.potlatch2.*;
                import mx.managers.PopUpManager;
 
-               // ** FIXME: should automatically focus on id field (like the Changeset dialogue)
+               private var entity:Entity;
+               private var relid:Number;
+               
+               public function setEntity(e:Entity):void {
+                       entity=e;
+               }
+
+               private function loadRelation():void {
+                       relid = Number(requestedID.text);
+                       PopUpManager.removePopUp(this);
 
-               public function loadRelation():void {
-            var conn:Connection = Connection.getConnectionInstance();
-                       var id:uint = uint(requestedID.text);
-                       if (requestedID.text) {
-                               var rel:Relation=conn.getRelation(id);
-                               if (!rel) {
-                                       rel = conn.createRelation({}, [], MainUndoStack.getGlobalStack().addAction);
-                                       rel.renumber(id,1);
+                       var conn:Connection = Connection.getConnectionInstance();
+                       if (!relid) return;
+                       if (conn.getRelation(relid)) {
+                               relationLoaded(null);
+                       } else {
+                               conn.loadEntityByID("relation",relid);
+                               conn.addEventListener(Connection.LOAD_COMPLETED, relationLoaded);
+                       }
+               }
+               
+               private function relationLoaded(event:Event):void {
+                       var conn:Connection = Connection.getConnectionInstance();
+                       var relation:Relation = conn.getRelation(relid);
+                       conn.removeEventListener(Connection.LOAD_COMPLETED, relationLoaded);
+                       if (!relation) return;
+
+                       var undo:CompositeUndoableAction = new CompositeUndoableAction("Add to relation");
+                       if (entity is EntityCollection) {
+                               for each (var e:Entity in EntityCollection(entity).entities) {
+                                       if (relation.findEntityMemberIndex(e)==-1) {
+                                               relation.appendMember(new RelationMember(e, ''), undo.push);
+                                       }
                                }
-                               conn.loadEntity(rel);
-                               // add an eventListener of some sort to pop up the panel again once it's loaded
+                       } else {
+                               relation.appendMember(new RelationMember(entity, ''), undo.push);
                        }
-                       PopUpManager.removePopUp(this);
+                       MainUndoStack.getGlobalStack().addAction(undo);
                }
+               
     ]]></mx:Script>    
 </mx:TitleWindow>