load GPS tracks from server, needs a bit of refinement still but the basics are there
[potlatch2.git] / potlatch2.mxml
1 <?xml version="1.0" encoding="utf-8"?>
2 <mx:Application 
3         xmlns:mx="http://www.adobe.com/2006/mxml" 
4         xmlns:halcyon="net.systemeD.halcyon.*"
5         xmlns:potlatch2="net.systemeD.potlatch2.*"
6         layout="vertical"
7         horizontalScrollPolicy="off"
8         verticalScrollPolicy="off"
9         horizontalAlign="center" 
10         addedToStage="initApp()">
11         
12         <mx:Style source="styles/Application.css"/>
13
14     <mx:Glow id="glowImage" duration="100" 
15         alphaFrom="0.3" alphaTo="1.0" 
16         blurXFrom="0.0" blurXTo="5.0" 
17         blurYFrom="0.0" blurYTo="5.0" 
18         color="0xFF8000"/>
19     <mx:Glow id="unglowImage" duration="100" 
20         alphaFrom="1.0" alphaTo="0.3" 
21         blurXFrom="5.0" blurXTo="0.0" 
22         blurYFrom="5.0" blurYTo="0.0" 
23         color="0xFF8000"/>
24     <mx:WipeLeft id="wipeOut" duration="250"/>
25     <mx:WipeRight id="wipeIn" duration="250"/>
26
27     <mx:ApplicationControlBar dock="true">
28         <mx:PopUpButton id="bgButton" label="Background" openAlways="true"
29             creationComplete="bgButton.popUp = new BackgroundSelector();"/>
30         <mx:PopUpButton id="styleButton" label="Map Style" openAlways="true"
31             creationComplete="styleButton.popUp = new StyleSelector();"/>
32         <mx:Button label="GPS" click="trackLoader.load();"/>
33         <mx:Spacer width="100%"/>
34         <mx:Button label="Undo" click="MainUndoStack.getGlobalStack().undo();"
35             enabled="{MainUndoStack.getGlobalStack().canUndo()}"/>
36         <mx:Button label="Redo" click="MainUndoStack.getGlobalStack().redo();"
37             enabled="{MainUndoStack.getGlobalStack().canRedo()}"/>
38         <mx:Spacer width="100%"/>
39         <mx:Button label="Help" click="new HelpDialog().init();" />
40         <mx:Button label="Options" click="new OptionsDialog().init();" /> 
41         <mx:Button label="Save" icon="@Embed('embedded/save.svg')" click="SaveManager.saveChanges();" id="saveButton" 
42                         enabled="false"/>
43     </mx:ApplicationControlBar>
44     
45     <mx:HDividedBox width="100%" height="100%">
46
47           <!-- Tag viewer -->
48           <potlatch2:TagViewer width="25%" height="100%" id="tagViewer"/>
49
50       <mx:Canvas width="75%" height="100%">
51         <mx:Canvas id="map_area" resize="onResizeMap()"
52             top="0" left="0" width="100%" height="100%" dragEnter="dragEnterHandler(event);" dragDrop="dragDropHandler(event);">
53         </mx:Canvas>
54         <mx:Image source="@Embed('embedded/zoomIn.svg')" right="3" top="3" click="theMap.zoomIn();"
55             rollOverEffect="glowImage" rollOutEffect="unglowImage"/>
56         <mx:Image source="@Embed('embedded/zoomOut.svg')" right="3" top="20" click="theMap.zoomOut();"
57             rollOverEffect="glowImage" rollOutEffect="unglowImage"/>
58         <mx:TextArea id="dataWorking" text="" right="20" top="3" disabledColor="black" backgroundDisabledColor="0xFFFFEA" height="18"
59             enabled="false" borderThickness="0"
60             showEffect="{wipeIn}" hideEffect="{wipeOut}"/>
61
62     </mx:Canvas>
63
64     </mx:HDividedBox>
65
66         <mx:Script><![CDATA[
67                 import net.systemeD.halcyon.*;
68                 import net.systemeD.halcyon.connection.*;
69                 import net.systemeD.potlatch2.*;
70                 import net.systemeD.potlatch2.save.SaveManager;
71                 import net.systemeD.potlatch2.controller.*;
72                 import net.systemeD.potlatch2.help.*;
73                 import net.systemeD.potlatch2.options.*;
74                 import net.systemeD.potlatch2.utils.*;
75                 import mx.managers.PopUpManager;
76                 import flash.system.Security;
77                 import flash.net.*;
78                 import flash.events.MouseEvent;
79                 import flash.display.Sprite;
80                 import mx.core.IChildList;
81         import mx.containers.Canvas;
82                 import mx.core.Application;
83         import mx.events.DragEvent;
84         import mx.managers.DragManager;
85         import mx.core.DragSource;
86
87         public var theMap:Map;
88         public var theController:EditController;
89                 public var yahoo:Yahoo;
90                 public var trackLoader:TrackLoader;
91                 public var toolbox:Toolbox;
92
93                 private var savecount:uint=0;
94                 private var loadcount:uint=0;
95                 
96                 public var version:String="(Foxbase) Alpha";                    // mxml won't read it if it's a const. Go figure
97
98                 private function initApp():void {
99
100                         Globals.vars.map_area = map_area;
101                         Globals.vars.root = map_area.rawChildren;                       // set up global reference to root level
102                         var _root:IChildList = map_area.rawChildren;            // convenient local shorthand
103
104                         // populate sharedObject with loaderInfo parameters if supplied
105                         var obj:SharedObject = SharedObject.getLocal("user_state");
106                         var objChanged:Boolean = false;
107                         if (loaderInfo.parameters['tileurl']) {
108                                 obj.setProperty('background_url',loaderInfo.parameters['tileurl']); 
109                                 obj.setProperty('background_name','Custom'); 
110                                 objChanged=true;
111                         }
112                         if (loaderInfo.parameters['style']) { 
113                                 obj.setProperty('stylesheet_url',loaderInfo.parameters['style']);
114                                 obj.setProperty('stylesheet_name','Custom');
115                                 objChanged=true; 
116                         }
117                         if (objChanged) { obj.flush(); }
118
119                         // load imagery and style XML
120                 var request:URLRequest = new URLRequest("imagery.xml?"+Math.random());
121                 var loader:URLLoader = new URLLoader();
122                 loader.addEventListener(Event.COMPLETE, onImageryLoad);
123                 loader.load(request);
124
125                         var request2:URLRequest = new URLRequest("stylesheets.xml?"+Math.random());
126                         var loader2:URLLoader = new URLLoader();
127                 loader2.addEventListener(Event.COMPLETE, onStylesheetsLoad);
128                 loader2.load(request2);
129
130                         // map backdrop object
131             var w:uint = map_area.width;
132             var h:uint = map_area.height;
133             var b:Sprite = new Sprite();
134             b.height=h; b.width=w;
135             b.graphics.beginFill(0xFFFFEA,100); 
136             b.graphics.drawRect(0,0,w,h); 
137             b.graphics.endFill();
138                         _root.addChild(b);
139
140                         // create map and Yahoo
141                         theMap=new Map(this.loaderInfo.parameters);
142                         theMap.backdrop=b;
143                         yahoo=new Yahoo(w,h,theMap);
144                         yahoo.hide();
145                         _root.addChild(yahoo);
146                         _root.addChild(theMap);
147             theMap.updateSize(w,h);
148
149                         // add mask for map
150                         var s:Sprite=new Sprite();
151                         s.graphics.beginFill(0xFFFFFF,100);
152                         s.graphics.drawRect(0,0,w,h);
153                         s.graphics.endFill();
154                         _root.addChild(s);
155                         theMap.mask=s;
156
157                         // mouse-up handler attached to stage, so the user can release outside the map
158                         stage.addEventListener(MouseEvent.MOUSE_UP, theMap.mouseUpHandler);
159                         Globals.vars.map_area.addEventListener(MouseEvent.MOUSE_MOVE, theMap.mouseMoveHandler);
160                         Globals.vars.map_area.addEventListener(MouseEvent.MOUSE_DOWN, theMap.mouseDownHandler);
161
162                         // keyboard event attached to stage
163                         stage.addEventListener(KeyboardEvent.KEY_UP, theMap.keyUpHandler);
164
165                         // position toolbox
166                         toolbox=Toolbox(PopUpManager.createPopUp(this,Toolbox,false));
167                         toolbox.init(theController);
168                         toolbox.x=stage.stageWidth-toolbox.width-15;
169                         toolbox.y=stage.stageHeight-toolbox.height-15;
170
171                         // add debug field
172                         var t:TextField=new TextField();
173                         t.width=500; t.height=150; t.border=true;
174                         t.multiline=true;
175                         _root.addChild(t);
176                         Globals.vars.debug=t;
177             t.visible = loaderInfo.parameters["show_debug"] == 'true';
178                         Globals.vars.root=theMap;       // just for the addDebug function
179
180             theController = new EditController(theMap, tagViewer, toolbox);
181             theController.setActive();
182             
183             var conn:Connection = Connection.getConnectionInstance();
184             conn.addEventListener(Connection.LOAD_STARTED, onDataStart);
185             conn.addEventListener(Connection.LOAD_COMPLETED, onDataComplete);
186             conn.addEventListener(Connection.SAVE_STARTED, onDataStart);
187             conn.addEventListener(Connection.SAVE_COMPLETED, onDataComplete);
188             conn.addEventListener(Connection.DATA_DIRTY, onDataDirty);
189             conn.addEventListener(Connection.DATA_CLEAN, onDataClean);
190
191                         // create GPS trackloader
192                         trackLoader=new TrackLoader(theMap,conn.apiBase);
193                 }
194                 
195         public function onResizeMap():void {
196             if ( theMap != null )
197                 theMap.updateSize(map_area.width, map_area.height);
198
199                         if (toolbox) {
200                                 toolbox.x=Math.min(toolbox.x,stage.stageWidth-toolbox.width-15);
201                                 toolbox.y=Math.min(toolbox.y,stage.stageHeight-toolbox.height-15);
202                         }
203         }
204
205         private function onDataStart(event:Event):void {
206                         switch (event.type) {
207                                 case Connection.LOAD_STARTED:   loadcount++; break;
208                                 case Connection.SAVE_STARTED:   savecount++; break;
209                         }
210                         updateDataWorking();
211         }
212         private function onDataComplete(event:Event):void {
213                         switch (event.type) {
214                                 case Connection.LOAD_COMPLETED: loadcount--; break;
215                                 case Connection.SAVE_COMPLETED: savecount--; break;
216                         }
217             updateDataWorking();
218         }
219                 private function updateDataWorking():void {
220                         if (loadcount>0 && savecount>0) { dataWorking.text="Loading/saving..."; }
221                         else if (loadcount>0)           { dataWorking.text="Loading data..."; }
222                         else if (savecount>0)           { dataWorking.text="Saving data..."; }
223                         else                            { dataWorking.text=""; }
224                         dataWorking.visible=(dataWorking.text!="");
225                 }
226         private function onDataDirty(event:Event):void {
227                         saveButton.enabled=true;
228                 }
229                 private function onDataClean(event:Event):void {
230                         saveButton.enabled=false;
231                 }
232                 
233         private function onImageryLoad(event:Event):void {
234                         var xml:XML = new XML(URLLoader(event.target).data);
235                         var saved_url:String = SharedObject.getLocal("user_state").data['background_url'];
236                         var saved_name:String= SharedObject.getLocal("user_state").data['background_name'];
237                         var xml_url:String, xml_name:String, isSet:Boolean=false;
238             theController.imagery=new Array(
239                                 { label: "None", data: "" },
240                                 { label: "Yahoo", data: "yahoo" } );
241                         for each(var set:XML in xml.set) {
242                                 xml_url= set.child("url");
243                                 xml_name=set.child("name");
244                 theController.imagery.push({ label:xml_name, data:xml_url });
245                                 if (xml_url==saved_url || (xml_name==saved_name && xml_name!='Custom')) { isSet=true; }
246                         }
247                         if (!isSet) { theController.imagery.push({ label:saved_name, data:saved_url }); }
248
249                         for each (var bg:Object in theController.imagery) {
250                                 if (bg['label']==saved_nameĀ || bg['data']==saved_url) {
251                                         setBackground(bg['label'], bg['data']);
252                                 }
253                         }
254                 }
255                 
256                 public function setBackground(name:String,url:String):void {
257                         // ** this should take an object with all parameters (source tag, etc.)
258                         if (url=='yahoo') { theMap.setBackground('') ; yahoo.show(); }
259                                      else { theMap.setBackground(url); yahoo.hide(); }
260                         var obj:SharedObject = SharedObject.getLocal("user_state");
261                         obj.setProperty("background_url",url );
262                         obj.setProperty("background_name",name);
263                         obj.flush();
264                 }
265
266                 private function onStylesheetsLoad(event:Event):void {
267                         var xml:XML = new XML(URLLoader(event.target).data);
268                         var saved_url:String = SharedObject.getLocal("user_state").data['stylesheet_url'];
269                         var saved_name:String= SharedObject.getLocal("user_state").data['stylesheet_name'];
270                         var xml_url:String, xml_name:String, isSet:Boolean=false;
271                         if (!saved_url) { saved_url='potlatch.css'; saved_name='Potlatch'; }
272                         
273                         // create menu
274             theController.stylesheets=new Array();
275                         for each(var set:XML in xml.stylesheet) {
276                                 xml_url= set.child("url");
277                                 xml_name=set.child("name");
278                 theController.stylesheets.push({ label:xml_name, data:xml_url });
279                                 if (xml_url==saved_url || (xml_name==saved_name && xml_name!='Custom')) { isSet=true; }
280                         }
281                         if (!isSet) { theController.stylesheets.push({ label:saved_name, data:saved_url }); }
282
283                         for each (var bg:Object in theController.stylesheets) {
284                                 if (bg['label']==saved_nameĀ || bg['data']==saved_url) { 
285                                         setStylesheet(bg['label'], bg['data']);
286                                 }
287                         }
288                 }
289
290                 public function setStylesheet(name:String,url:String):void {
291                         theMap.setStyle(url);
292                         var obj:SharedObject = SharedObject.getLocal("user_state");
293                         obj.setProperty("stylesheet_url",url);
294                         obj.setProperty("stylesheet_name",name);
295                         obj.flush();
296                 }
297
298
299         private function dragEnterHandler(event:DragEvent):void {
300             // Get the drop target component from the event object.
301             var dropTarget:Canvas=event.currentTarget as Canvas;
302             // Accept the drag only if the user is dragging poi with tags
303             if (event.dragSource.hasFormat('tags')) 
304             {
305                 DragManager.acceptDragDrop(dropTarget);
306             }
307         }
308         
309         private function dragDropHandler(event:DragEvent):void {
310                         // Deselect the dragged icon
311                         if (event.dragSource.dataForFormat('container')) {
312                                 event.dragSource.dataForFormat('container').selectedItem=-1;
313                         }
314
315                         // Get the tags
316             var tags:Array = event.dragSource.dataForFormat('tags') as Array;
317             var mapLoc:Point = Globals.vars.root.globalToLocal(new Point(event.stageX, event.stageY));
318             var lat:Number = Globals.vars.root.coord2lat(mapLoc.y);
319             var lon:Number = Globals.vars.root.coord2lon(mapLoc.x);
320             
321             var createAction:CompositeUndoableAction = new CompositeUndoableAction("Create POI");
322             
323             var node:Node = Connection.getConnectionInstance().createNode({}, lat, lon, createAction.push);
324             for each ( var tag:Object in tags ) {
325                 node.setTag(tag.k, tag.v, createAction.push);
326             }
327                         Connection.getConnectionInstance().registerPOI(node);
328                         MainUndoStack.getGlobalStack().addAction(createAction);
329             theController.setState(new SelectedPOINode(node));
330         }
331
332         ]]></mx:Script>
333
334 </mx:Application>
335