kill stupid tab bug
[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.*;
6     import net.systemeD.halcyon.connection.actions.*;
7     import flash.net.*;
8     import flash.events.*;
9     import com.adobe.serialization.json.JSON;
10
11     public class BugLayer extends VectorLayer {
12
13         private var baseUrl:String;
14         private var apiKey:String;
15         private var detailsUrl:String;
16
17         // as strings, since that's how they are in tags and http calls
18         public static var BUG_STATUS_OPEN:String = "1";
19         public static var BUG_STATUS_FIXED:String = "2";
20         public static var BUG_STATUS_INVALID:String = "3"; // or 'non-reproduceable'
21         public static const status:Array = ["", "open", "fixed", "invalid"];
22
23         public function BugLayer(n:String, map:Map, s:String, baseUrl:String, apiKey:String, detailsURL:String) {
24             this.baseUrl = baseUrl;
25             this.apiKey = apiKey;
26             this.detailsUrl = detailsURL;
27             super(n,map,s);
28         }
29
30         public function closeBug(m:Marker, nickname:String, comment:String, status:String = null):void {
31             var id:String = m.getTag('bug_id');
32             nickname ||= 'NoName';
33             // nicknames have length and character restictions. The character restrictions should be taken care of
34             // by the BugPanel.mxml restriction.
35             if (nickname.length < 3 || nickname.length > 16) {
36               nickname = 'NoName';
37             }
38             comment ||= 'No Comment';
39             if (comment.length > 1000) {
40               comment = comment.substr(0,1000); // that's index, length
41             }
42             status ||= BUG_STATUS_FIXED;
43             var urlReq:URLRequest = new URLRequest(baseUrl+"changeBugStatus?id="+id+"&status="+status+"&comment="+encodeURIComponent(comment)+"&nickname="+encodeURIComponent(nickname)+"&key="+apiKey);
44             urlReq.method = "POST";
45             urlReq.data = '    '; // dear Adobe, this is nuts, kthxbye (you can't POST with an empty payload)
46             var loader:URLLoader = new URLLoader();
47             loader.load(urlReq);
48             loader.addEventListener(Event.COMPLETE, function(e:Event):void { bugClosed(e, m, status); } );
49         }
50
51         private function bugClosed(event:Event, marker:Marker, s:String):void {
52             var action:UndoableEntityAction = new SetTagAction(marker, "status", status[int(s)]);
53             action.doAction(); // just do it, don't add to undo stack
54         }
55
56         public override function loadBbox(left:Number, right:Number,
57                                 top:Number, bottom:Number):void {
58             var loader:URLLoader = new URLLoader();
59             loader.load(new URLRequest(baseUrl+"getBugs?bbox="+map.edge_l+","+map.edge_b+","+map.edge_r+","+map.edge_t+"&key="+apiKey));
60             loader.addEventListener(Event.COMPLETE, parseJSON);
61         }
62
63         private function parseJSON(event:Event):void {
64             var result:String = String(event.target.data);
65             if (result) { // api returns 204 no content for no bugs, and the JSON parser treats '' as an error
66               var featureCollection:Object = JSON.decode(result);
67
68               for each (var feature:Object in featureCollection.features) {
69                 // geoJSON spec is x,y,z i.e. lon, lat, ele
70                 var lon:Number = feature.geometry.coordinates[0];
71                 var lat:Number = feature.geometry.coordinates[1];
72                 var tags:Object = {};
73                 tags["name"] = String(feature.properties.description).substr(0,10)+'...';
74                 tags["description"] = feature.properties.description;
75                 tags["bug_id"] = feature.id;
76                 tags["nickname"] = feature.properties.nickname;
77                 tags["type"] = feature.properties.type;
78                 tags["date_created"] = feature.properties.date_created;
79                 tags["date_updated"] = feature.properties.date_updated;
80                 tags["source"] = feature.properties.source;
81                 tags["status"] = status[int(feature.properties.status)];
82                 var marker:Marker = createMarker(tags, lat, lon, Number(feature.id));
83               }
84               paint.updateEntityUIs(getObjectsByBbox(map.edge_l,map.edge_r,map.edge_t,map.edge_b), true, false);
85             }
86         }
87
88         public function bugDetailsUrl(m:Marker):String {
89             if (detailsUrl == '')
90               return null;
91             return detailsUrl+m.id;
92         }
93
94     }
95 }