Allow users to load traces that aren't theirs
authorRichard Fairhurst <richard@systemeD.net>
Thu, 14 Jul 2011 15:59:07 +0000 (16:59 +0100)
committerRichard Fairhurst <richard@systemeD.net>
Thu, 14 Jul 2011 15:59:07 +0000 (16:59 +0100)
net/systemeD/halcyon/connection/Connection.as
net/systemeD/halcyon/connection/Trace.as
net/systemeD/halcyon/connection/XMLConnection.as
potlatch2.mxml

index 3e762ff..9a85062 100644 (file)
@@ -439,7 +439,7 @@ package net.systemeD.halcyon.connection {
             return changeset;
         }
 
-        protected function addTrace(t:Object):void {
+        public function addTrace(t:Object):void {
             traces.push(t);
         }
 
@@ -447,6 +447,13 @@ package net.systemeD.halcyon.connection {
             traces = [];
         }
 
+               public function findTrace(id:int):Trace {
+                       for each (var t:Trace in traces) {
+                               if (t.id == id) return t;
+                       }
+                       return null;
+               }
+
         public function getTraces():Array {
             return traces;
         }
index 22a0b64..8a4199e 100644 (file)
@@ -24,7 +24,7 @@ package net.systemeD.halcyon.connection {
         private var _traceData:String; // the trace data, saved as a string
         private var map:Map;
         private var _layer:MapPaint;
-        private var masterConnection:XMLConnection; // The authenticated connection
+        private var masterConnection:Connection; // The authenticated connection
         private var _connection:Connection; // The one we store our fake nodes/ways in.
         private var simplify:Boolean = false;
 
@@ -33,9 +33,10 @@ package net.systemeD.halcyon.connection {
         /** Create a new trace.
         * @param masterConnection The authenticated connection to communicate with the server
         */
-        public function Trace(masterConnection:XMLConnection) {
+        public function Trace(masterConnection:Connection, id:int=0) {
             this.masterConnection = masterConnection;
             map = Globals.vars.root; // REFACTOR this prevents traces being added to arbitrary maps
+                       if (id!=0) _id=id;
         }
 
         /** Create a new trace, from the XML description given by the user/traces call.
@@ -112,6 +113,7 @@ package net.systemeD.halcyon.connection {
 
         private function processEvent(e:Event):void {
             removeEventListener("loaded_data", processEvent);
+            _isLoaded=true;
             process();
         }
 
index 3a4c5ef..97f1e76 100644 (file)
@@ -439,17 +439,17 @@ package net.systemeD.halcyon.connection {
             }
         }
 
-        private function tracesLoadComplete(event:Event):void {
-            clearTraces();
-            var files:XML = new XML(URLLoader(event.target).data);
-            for each(var traceData:XML in files.gpx_file) {
-              var t:Trace = new Trace(this).fromXML(traceData);
-              addTrace(t);
-            }
-            traces_loaded = true;
-            dispatchEvent(new Event(LOAD_COMPLETED));
-            dispatchEvent(new Event(TRACES_LOADED));
-        }
+               private function tracesLoadComplete(event:Event):void {
+                       var files:XML = new XML(URLLoader(event.target).data);
+                       for each(var traceData:XML in files.gpx_file) {
+                               var t:Trace = findTrace(traceData.@id);
+                               if (!t) { t=new Trace(this); addTrace(t); }
+                               t.fromXML(traceData);
+                       }
+                       traces_loaded = true;
+                       dispatchEvent(new Event(LOAD_COMPLETED));
+                       dispatchEvent(new Event(TRACES_LOADED));
+               }
 
         override public function fetchTrace(id:Number, callback:Function):void {
             sendOAuthGet(apiBaseURL+"gpx/"+id+"/data.xml", 
index 0c3e996..c02febf 100644 (file)
         }
 
         private function loadTrace(id:Number):void {
-            var conn:Connection = theMap.editableLayer.connection;
-            conn.addEventListener(Connection.TRACES_LOADED, function (e:Event):void {
-                for each (var trace:Trace in conn.getTraces()) {
-                    if (trace.id == id) { trace.addToMap(); }
-                }
-            });
-            conn.fetchUserTraces();
+                       var conn:Connection = theMap.editableLayer.connection;
+                       conn.addEventListener(Connection.TRACES_LOADED, function (e:Event):void {
+                               var t:Trace = conn.findTrace(id);
+                               if (!t) { t=new Trace(conn,id); conn.addTrace(t); }
+                               t.addToMap();
+                               conn.removeEventListener(Connection.TRACES_LOADED, arguments.callee);
+                       });
+                       conn.fetchUserTraces();
         }
 
        ]]></mx:Script>