Complete support for loading relations by ID
authorRichard Fairhurst <richard@systemeD.net>
Sat, 2 Apr 2011 21:35:13 +0000 (22:35 +0100)
committerRichard Fairhurst <richard@systemeD.net>
Sat, 2 Apr 2011 21:35:13 +0000 (22:35 +0100)
net/systemeD/halcyon/connection/Connection.as
net/systemeD/halcyon/connection/XMLConnection.as
net/systemeD/potlatch2/RelationLoaderPanel.mxml
net/systemeD/potlatch2/RelationSelectPanel.mxml

index 0821a5b..1e9b6f9 100644 (file)
@@ -544,7 +544,7 @@ package net.systemeD.halcyon.connection {
                public function loadBbox(left:Number, right:Number,
                                                                top:Number, bottom:Number):void {
            }
-           public function loadEntity(entity:Entity):void {}
+           public function loadEntityByID(type:String, id:Number):void {}
            public function setAuthToken(id:Object):void {}
         public function setAccessToken(key:String, secret:String):void {}
            public function createChangeset(tags:Object):void {}
@@ -553,6 +553,11 @@ package net.systemeD.halcyon.connection {
         public function fetchUserTraces(refresh:Boolean=false):void {}
         public function fetchTrace(id:Number, callback:Function):void {}
         public function hasAccessToken():Boolean { return false; }
+
+               public function loadEntity(entity:Entity):void {
+                       loadEntityByID(entity.getType(),entity.id);
+               }
+
     }
 
 }
index b94345b..20d5e43 100644 (file)
@@ -46,9 +46,9 @@ package net.systemeD.halcyon.connection {
             sendLoadRequest(mapRequest);
                }
 
-               override public function loadEntity(entity:Entity):void {
-                       var url:String=Connection.apiBaseURL + entity.getType() + "/" + entity.id;
-                       if (entity is Way) url+="/full";
+               override public function loadEntityByID(type:String, id:Number):void {
+                       var url:String=Connection.apiBaseURL + type + "/" + id;
+                       if (type=='way') url+="/full";
                        sendLoadRequest(new URLRequest(url));
                }
 
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>
 
index 8db158d..6cd409e 100644 (file)
@@ -85,6 +85,7 @@
                        PopUpManager.removePopUp(this);
                        var panel:RelationLoaderPanel = RelationLoaderPanel(
                            PopUpManager.createPopUp(Application(Application.application), RelationLoaderPanel, true));
+                       panel.setEntity(entity);
                        PopUpManager.centerPopUp(panel);
                }