the bug layer should autmatically download when panning. Move the loading/parsing...
[potlatch2.git] / net / systemeD / potlatch2 / BugLayer.as
1 package net.systemeD.potlatch2 {
2
3     import net.systemeD.halcyon.VectorLayer;
4     import net.systemeD.halcyon.Map;
5     import net.systemeD.halcyon.connection.Marker;
6     import flash.net.*;
7     import flash.events.*;
8     import com.adobe.serialization.json.JSON;
9
10     public class BugLayer extends VectorLayer {
11
12         private var baseUrl:String;
13         private var apiKey:String;
14
15         // as strings, since that's how they are in tags and http calls
16         private var BUG_STATUS_OPEN:String = "1";
17         private var BUG_STATUS_FIXED:String = "2";
18         private var BUG_STATUS_INVALID:String = "3"; // or 'non-reproduceable'
19         private static const status:Array = ["", "open", "fixed", "invalid"];
20
21         public function BugLayer(n:String, map:Map, s:String, baseUrl:String, apiKey:String) {
22             this.baseUrl = baseUrl;
23             this.apiKey = apiKey;
24             super(n,map,s);
25         }
26
27         public function closeBug(m:Marker):void {
28             var id:String = m.getTag('bug_id');
29             var status:String = BUG_STATUS_FIXED;
30             var comment:String = "NoComment";
31             var nickname:String = "NoName";
32
33             //TODO urlencode stuff
34             var urlReq:URLRequest = new URLRequest(baseUrl+"changeBugStatus?id="+id+"&status="+status+"&comment="+comment+"&nickname="+nickname+"&key="+apiKey);
35             urlReq.method = "POST";
36             urlReq.data = '    '; // dear Adobe, this is nuts, kthxbye (you can't POST with an empty payload)
37             var loader:URLLoader = new URLLoader();
38             loader.load(urlReq);
39             loader.addEventListener(Event.COMPLETE, bugClosed);
40         }
41
42         private function bugClosed(event:Event):void {
43             trace("bug closed");
44             // remove it from the layer, redraw, fix selection etc.
45         }
46
47         public override function loadBbox(left:Number, right:Number,
48                                 top:Number, bottom:Number):void {
49             var loader:URLLoader = new URLLoader();
50             loader.load(new URLRequest(baseUrl+"getBugs?bbox="+map.edge_l+","+map.edge_b+","+map.edge_r+","+map.edge_t+"&key="+apiKey));
51             loader.addEventListener(Event.COMPLETE, parseJSON);
52         }
53
54         private function parseJSON(event:Event):void {
55             var result:String = String(event.target.data);
56             if (result) { // api returns 204 no content for no bugs, and the JSON parser treats '' as an error
57               var featureCollection:Object = JSON.decode(result);
58
59               for each (var feature:Object in featureCollection.features) {
60                 // geoJSON spec is x,y,z i.e. lon, lat, ele
61                 var lon:Number = feature.geometry.coordinates[0];
62                 var lat:Number = feature.geometry.coordinates[1];
63                 var tags:Object = {};
64                 tags["name"] = String(feature.properties.description).substr(0,10)+'...';
65                 tags["description"] = feature.properties.description;
66                 tags["bug_id"] = feature.id;
67                 tags["nickname"] = feature.properties.nickname;
68                 tags["type"] = feature.properties.type;
69                 tags["date_created"] = feature.properties.date_created;
70                 tags["date_updated"] = feature.properties.date_updated;
71                 tags["source"] = feature.properties.source;
72                 tags["status"] = status[int(feature.properties.status)];
73                 var marker:Marker = createMarker(tags, lat, lon);
74               }
75               paint.updateEntityUIs(getObjectsByBbox(map.edge_l,map.edge_r,map.edge_t,map.edge_b), true, false);
76             }
77         }
78
79     }
80 }