more turn restriction stuff, and checkboxes, and a bunch of little fixes
[potlatch2.git] / net / systemeD / halcyon / connection / Connection.as
1 package net.systemeD.halcyon.connection {
2
3     import flash.net.*;
4
5     import flash.events.EventDispatcher;
6     import flash.events.Event;
7         import net.systemeD.halcyon.Globals;
8         import net.systemeD.halcyon.connection.actions.*;
9
10         public class Connection extends EventDispatcher {
11
12         private static var connectionInstance:Connection = null;
13
14         protected static var policyURL:String;
15         protected static var apiBaseURL:String;
16         protected static var params:Object;
17
18         public static function getConnection(initparams:Object=null):Connection {
19             if ( connectionInstance == null ) {
20             
21                 params = initparams == null ? new Object() : initparams;
22                 policyURL = getParam("policy", "http://127.0.0.1:3000/api/crossdomain.xml");
23                 apiBaseURL = getParam("api", "http://127.0.0.1:3000/api/0.6/");
24                 var connectType:String = getParam("connection", "XML");
25                 
26                 if ( connectType == "XML" )
27                     connectionInstance = new XMLConnection();
28                 else if ( connectType == "OSM" )
29                     connectionInstance = new OSMConnection();
30                 else
31                     connectionInstance = new AMFConnection();
32             }
33             return connectionInstance;
34         }
35
36         public static function getParam(name:String, defaultValue:String):String {
37             trace("Returning param "+name+" as "+(params[name] == null ? defaultValue : params[name]));
38             return params[name] == null ? defaultValue : params[name];
39         }
40
41         public static function get apiBase():String {
42             return apiBaseURL;
43         }
44
45         public static function get serverName():String {
46             return getParam("serverName", "Localhost");
47         }
48                 
49                 public static function getConnectionInstance():Connection {
50             return connectionInstance;
51                 }
52
53                 public function getEnvironment(responder:Responder):void {}
54
55         // connection events
56         public static var LOAD_STARTED:String = "load_started";
57         public static var LOAD_COMPLETED:String = "load_completed";
58         public static var SAVE_STARTED:String = "save_started";
59         public static var SAVE_COMPLETED:String = "save_completed";
60         public static var DATA_DIRTY:String = "data_dirty";
61         public static var DATA_CLEAN:String = "data_clean";
62         public static var NEW_CHANGESET:String = "new_changeset";
63         public static var NEW_CHANGESET_ERROR:String = "new_changeset_error";
64         public static var NEW_NODE:String = "new_node";
65         public static var NEW_WAY:String = "new_way";
66         public static var NEW_RELATION:String = "new_relation";
67         public static var NEW_POI:String = "new_poi";
68         public static var NODE_RENUMBERED:String = "node_renumbered";
69         public static var WAY_RENUMBERED:String = "way_renumbered";
70         public static var RELATION_RENUMBERED:String = "relation_renumbered";
71         public static var TAG_CHANGED:String = "tag_change";
72         public static var NODE_MOVED:String = "node_moved";
73         public static var WAY_NODE_ADDED:String = "way_node_added";
74         public static var WAY_NODE_REMOVED:String = "way_node_removed";
75         public static var WAY_REORDERED:String = "way_reordered";
76         public static var WAY_DRAGGED:String = "way_dragged";
77                 public static var NODE_DELETED:String = "node_deleted";
78                 public static var WAY_DELETED:String = "way_deleted";
79                 public static var RELATION_DELETED:String = "relation_deleted";
80                 public static var RELATION_MEMBER_ADDED:String = "relation_member_added";
81                 public static var RELATION_MEMBER_REMOVED:String = "relation_member_deleted";
82                 public static var ADDED_TO_RELATION:String = "added_to_relation";
83                 public static var REMOVED_FROM_RELATION:String = "removed_from_relation";
84                 public static var SUSPEND_REDRAW:String = "suspend_redraw";
85                 public static var RESUME_REDRAW:String = "resume_redraw";
86
87         // store the data we download
88         private var negativeID:Number = -1;
89         private var nodes:Object = {};
90         private var ways:Object = {};
91         private var relations:Object = {};
92         private var pois:Array = [];
93         private var changeset:Changeset = null;
94                 private var modified:Boolean = false;
95                 public var nodecount:int=0;
96                 public var waycount:int=0;
97                 public var relationcount:int=0;
98
99         protected function get nextNegative():Number {
100             return negativeID--;
101         }
102
103         protected function setNode(node:Node, queue:Boolean):void {
104                         if (!nodes[node.id]) { nodecount++; }
105             nodes[node.id] = node;
106             if (node.loaded) { sendEvent(new EntityEvent(NEW_NODE, node),queue); }
107         }
108
109         protected function setWay(way:Way, queue:Boolean):void {
110                         if (!ways[way.id]) { waycount++; }
111             ways[way.id] = way;
112             if (way.loaded) { sendEvent(new EntityEvent(NEW_WAY, way),queue); }
113         }
114
115         protected function setRelation(relation:Relation, queue:Boolean):void {
116                         if (!relations[relation.id]) { relationcount++; }
117             relations[relation.id] = relation;
118             if (relation.loaded) { sendEvent(new EntityEvent(NEW_RELATION, relation),queue); }
119         }
120
121         protected function renumberNode(oldID:Number, node:Node, queue:Boolean):void {
122             nodes[node.id] = node;
123             if (node.loaded) { sendEvent(new EntityRenumberedEvent(NODE_RENUMBERED, node, oldID),queue); }
124             delete nodes[oldID];
125         }
126
127         protected function renumberWay(oldID:Number, way:Way, queue:Boolean):void {
128             ways[way.id] = way;
129             if (way.loaded) { sendEvent(new EntityRenumberedEvent(WAY_RENUMBERED, way, oldID),queue); }
130             delete ways[oldID];
131         }
132
133         protected function renumberRelation(oldID:Number, relation:Relation, queue:Boolean):void {
134             relations[relation.id] = relation;
135             if (relation.loaded) { sendEvent(new EntityRenumberedEvent(RELATION_RENUMBERED, relation, oldID),queue); }
136             delete relations[oldID];
137         }
138
139
140                 public function sendEvent(e:*,queue:Boolean):void {
141                         // queue is only used for AMFConnection
142                         dispatchEvent(e);
143                 }
144
145         public function registerPOI(node:Node):void {
146             if ( pois.indexOf(node) < 0 ) {
147                 pois.push(node);
148                 sendEvent(new EntityEvent(NEW_POI, node),false);
149             }
150         }
151
152         public function unregisterPOI(node:Node):void {
153             var index:uint = pois.indexOf(node);
154             if ( index >= 0 ) {
155                 pois.splice(index,1);
156             }
157         }
158
159         protected function setActiveChangeset(changeset:Changeset):void {
160             this.changeset = changeset;
161             sendEvent(new EntityEvent(NEW_CHANGESET, changeset),false);
162         }
163         
164         public function getNode(id:Number):Node {
165             return nodes[id];
166         }
167
168         public function getWay(id:Number):Way {
169             return ways[id];
170         }
171
172         public function getRelation(id:Number):Relation {
173             return relations[id];
174         }
175
176                 // Remove data from Connection
177                 // These functions are used only internally to stop redundant data hanging around
178                 // (either because it's been deleted on the server, or because we have panned away
179                 //  and need to reduce memory usage)
180
181                 protected function killNode(id:Number):void {
182                         if (!nodes[id]) return;
183             nodes[id].dispatchEvent(new EntityEvent(Connection.NODE_DELETED, nodes[id]));
184                         if (nodes[id].parentRelations.length>0) {
185                                 nodes[id]=new Node(id,0,{},false,0,0);
186                         } else {
187                                 delete nodes[id];
188                         }
189                         nodecount--;
190                 }
191
192                 protected function killWay(id:Number):void {
193                         if (!ways[id]) return;
194             ways[id].dispatchEvent(new EntityEvent(Connection.WAY_DELETED, ways[id]));
195                         if (ways[id].parentRelations.length>0) {
196                                 ways[id]=new Way(id,0,{},false,[]);
197                         } else {
198                                 delete ways[id];
199                         }
200                         waycount--;
201                 }
202
203                 protected function killRelation(id:Number):void {
204                         if (!relations[id]) return;
205             relations[id].dispatchEvent(new EntityEvent(Connection.RELATION_DELETED, relations[id]));
206                         if (relations[id].parentRelations.length>0) {
207                                 relations[id]=new Relation(id,0,{},false,[]);
208                         } else {
209                                 delete relations[id];
210                         }
211                         relationcount--;
212                 }
213
214                 protected function killWayWithNodes(id:Number):void {
215                         var way:Way=ways[id];
216                         var node:Node;
217                         for (var i:uint=0; i<way.length; i++) {
218                                 node=way.getNode(i);
219                                 if (node.isDirty) { continue; }
220                                 if (node.parentWays.length>1) {
221                                         node.removeParent(way);
222                                 } else {
223                                         killNode(node.id);
224                                 }
225                         }
226                         killWay(id);
227                 }
228                 
229
230
231         public function createNode(tags:Object, lat:Number, lon:Number, performCreate:Function):Node {
232             var node:Node = new Node(nextNegative, 0, tags, true, lat, lon);
233             performCreate(new CreateEntityAction(node, setNode));
234                         markDirty();
235             return node;
236         }
237
238         public function createWay(tags:Object, nodes:Array, performCreate:Function):Way {
239             var way:Way = new Way(nextNegative, 0, tags, true, nodes.concat());
240             performCreate(new CreateEntityAction(way, setWay));
241                         markDirty();
242             return way;
243         }
244
245         public function createRelation(tags:Object, members:Array, performCreate:Function):Relation {
246             var relation:Relation = new Relation(nextNegative, 0, tags, true, members.concat());
247             performCreate(new CreateEntityAction(relation, setRelation));
248                         markDirty();
249             return relation;
250         }
251
252         public function getAllNodeIDs():Array {
253             var list:Array = [];
254             for each (var node:Node in nodes)
255                 list.push(node.id);
256             return list;
257         }
258
259         public function getAllWayIDs():Array {
260             var list:Array = [];
261             for each (var way:Way in ways)
262                 list.push(way.id);
263             return list;
264         }
265
266         public function getAllRelationIDs():Array {
267             var list:Array = [];
268             for each (var relation:Relation in relations)
269                 list.push(relation.id);
270             return list;
271         }
272
273         public function getMatchingRelationIDs(match:Object):Array {
274             var list:Array = [];
275                         var ok:Boolean;
276             for each (var relation:Relation in relations) {
277                                 ok=true;
278                                 if (relation.deleted) { ok=false; }
279                                 for (var k:String in match) {
280                                         if (!relation.getTagsHash()[k] || relation.getTagsHash()[k]!=match[k]) { ok=false; }
281                                 }
282                                 if (ok) { list.push(relation.id); }
283                         }
284             return list;
285         }
286
287                 public function getObjectsByBbox(left:Number, right:Number, top:Number, bottom:Number):Object {
288                         var o:Object = { poisInside: [], poisOutside: [], waysInside: [], waysOutside: [] };
289                         for each (var way:Way in ways) {
290                                 if (way.within(left,right,top,bottom)) { o.waysInside.push(way); }
291                                                                   else { o.waysOutside.push(way); }
292                         }
293                         for each (var node:Node in nodes) {
294                                 if (!node.hasParentWays) {
295                                         if (node.within(left,right,top,bottom)) { o.poisInside.push(node); }
296                                                                            else { o.poisOutside.push(node); }
297                                 }
298                         }
299                         return o;
300                 }
301
302         public function getActiveChangeset():Changeset {
303             return changeset;
304         }
305         
306                 public function purgeOutside(left:Number, right:Number, top:Number, bottom:Number):void {
307                         for each (var way:Way in ways) {
308                                 if (!way.within(left,right,top,bottom) && !way.isDirty) {
309                                         killWayWithNodes(way.id);
310                                 }
311                         }
312                         // ** should purge POIs and relations too
313                 }
314
315                 public function markDirty():void {
316             if (!modified) { dispatchEvent(new Event(DATA_DIRTY)); }
317                         modified=true;
318                 }
319                 public function markClean():void {
320             if (modified) { dispatchEvent(new Event(DATA_CLEAN)); }
321                         modified=false;
322                 }
323                 public function get isDirty():Boolean {
324                         return modified;
325                 }
326
327         // these are functions that the Connection implementation is expected to
328         // provide. This class has some generic helpers for the implementation.
329                 public function loadBbox(left:Number, right:Number,
330                                                                 top:Number, bottom:Number):void {
331             }
332             
333             public function setAppID(id:Object):void {}
334             public function setAuthToken(id:Object):void {}
335             public function createChangeset(tags:Object):void {}
336             public function uploadChanges():void {}
337     }
338
339 }
340