Fixed Stuff
[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                         Globals.vars.nocache = loaderInfo.parameters['nocache'] == 'true';
104
105                         // populate sharedObject with loaderInfo parameters if supplied
106                         var obj:SharedObject = SharedObject.getLocal("user_state");
107                         var objChanged:Boolean = false;
108                         if (loaderInfo.parameters['tileurl']) {
109                                 obj.setProperty('background_url',loaderInfo.parameters['tileurl']); 
110                                 obj.setProperty('background_name','Custom'); 
111                                 objChanged=true;
112                         }
113                         if (loaderInfo.parameters['style']) { 
114                                 obj.setProperty('stylesheet_url',loaderInfo.parameters['style']);
115                                 obj.setProperty('stylesheet_name','Custom');
116                                 objChanged=true; 
117                         }
118                         if (objChanged) { obj.flush(); }
119
120                         // load imagery and style XML
121                 var request:DebugURLRequest = new DebugURLRequest("imagery.xml");
122                 var loader:URLLoader = new URLLoader();
123                 loader.addEventListener(Event.COMPLETE, onImageryLoad);
124                 loader.load(request.request);
125
126                         var request2:DebugURLRequest = new DebugURLRequest("stylesheets.xml");
127                         var loader2:URLLoader = new URLLoader();
128                 loader2.addEventListener(Event.COMPLETE, onStylesheetsLoad);
129                 loader2.load(request2.request);
130
131                         // map backdrop object
132             var w:uint = map_area.width;
133             var h:uint = map_area.height;
134             var b:Sprite = new Sprite();
135             b.height=h; b.width=w;
136             b.graphics.beginFill(0xFFFFEA,100); 
137             b.graphics.drawRect(0,0,w,h); 
138             b.graphics.endFill();
139                         _root.addChild(b);
140
141                         // create map and Yahoo
142                         theMap=new Map(this.loaderInfo.parameters);
143                         theMap.backdrop=b;
144                         yahoo=new Yahoo(w,h,theMap);
145                         yahoo.hide();
146                         _root.addChild(yahoo);
147                         _root.addChild(theMap);
148             theMap.updateSize(w,h);
149
150                         // add mask for map
151                         var s:Sprite=new Sprite();
152                         s.graphics.beginFill(0xFFFFFF,100);
153                         s.graphics.drawRect(0,0,w,h);
154                         s.graphics.endFill();
155                         _root.addChild(s);
156                         theMap.mask=s;
157
158                         // mouse-up handler attached to stage, so the user can release outside the map
159                         stage.addEventListener(MouseEvent.MOUSE_UP, theMap.mouseUpHandler);
160                         Globals.vars.map_area.addEventListener(MouseEvent.MOUSE_MOVE, theMap.mouseMoveHandler);
161                         Globals.vars.map_area.addEventListener(MouseEvent.MOUSE_DOWN, theMap.mouseDownHandler);
162
163                         // keyboard event attached to stage
164                         stage.addEventListener(KeyboardEvent.KEY_UP, theMap.keyUpHandler);
165
166                         // position toolbox
167                         toolbox=Toolbox(PopUpManager.createPopUp(this,Toolbox,false));
168                         toolbox.init(theController);
169                         toolbox.x=stage.stageWidth-toolbox.width-15;
170                         toolbox.y=stage.stageHeight-toolbox.height-15;
171
172                         // add debug field
173                         var t:TextField=new TextField();
174                         t.width=500; t.height=150; t.border=true;
175                         t.multiline=true;
176                         _root.addChild(t);
177                         Globals.vars.debug=t;
178             t.visible = loaderInfo.parameters["show_debug"] == 'true';
179                         Globals.vars.root=theMap;       // just for the addDebug function
180
181             theController = new EditController(theMap, tagViewer, toolbox);
182             theController.setActive();
183             
184             var conn:Connection = Connection.getConnectionInstance();
185             conn.addEventListener(Connection.LOAD_STARTED, onDataStart);
186             conn.addEventListener(Connection.LOAD_COMPLETED, onDataComplete);
187             conn.addEventListener(Connection.SAVE_STARTED, onDataStart);
188             conn.addEventListener(Connection.SAVE_COMPLETED, onDataComplete);
189             conn.addEventListener(Connection.DATA_DIRTY, onDataDirty);
190             conn.addEventListener(Connection.DATA_CLEAN, onDataClean);
191
192                         // create GPS trackloader
193                         trackLoader=new TrackLoader(theMap,conn.apiBase);
194                 }
195                 
196         public function onResizeMap():void {
197             if ( theMap != null )
198                 theMap.updateSize(map_area.width, map_area.height);
199
200                         if (toolbox) {
201                                 toolbox.x=Math.min(toolbox.x,stage.stageWidth-toolbox.width-15);
202                                 toolbox.y=Math.min(toolbox.y,stage.stageHeight-toolbox.height-15);
203                         }
204         }
205
206         private function onDataStart(event:Event):void {
207                         switch (event.type) {
208                                 case Connection.LOAD_STARTED:   loadcount++; break;
209                                 case Connection.SAVE_STARTED:   savecount++; break;
210                         }
211                         updateDataWorking();
212         }
213         private function onDataComplete(event:Event):void {
214                         switch (event.type) {
215                                 case Connection.LOAD_COMPLETED: loadcount--; break;
216                                 case Connection.SAVE_COMPLETED: savecount--; break;
217                         }
218             updateDataWorking();
219         }
220                 private function updateDataWorking():void {
221                         if (loadcount>0 && savecount>0) { dataWorking.text="Loading/saving..."; }
222                         else if (loadcount>0)           { dataWorking.text="Loading data..."; }
223                         else if (savecount>0)           { dataWorking.text="Saving data..."; }
224                         else                            { dataWorking.text=""; }
225                         dataWorking.visible=(dataWorking.text!="");
226                 }
227         private function onDataDirty(event:Event):void {
228                         saveButton.enabled=true;
229                 }
230                 private function onDataClean(event:Event):void {
231                         saveButton.enabled=false;
232                 }
233                 
234         private function onImageryLoad(event:Event):void {
235                         var xml:XML = new XML(URLLoader(event.target).data);
236                         var saved_url:String = SharedObject.getLocal("user_state").data['background_url'];
237                         var saved_name:String= SharedObject.getLocal("user_state").data['background_name'];
238                         var xml_url:String, xml_name:String, isSet:Boolean=false;
239             theController.imagery=new Array(
240                                 { label: "None", data: "" },
241                                 { label: "Yahoo", data: "yahoo" } );
242                         for each(var set:XML in xml.set) {
243                                 xml_url= set.child("url");
244                                 xml_name=set.child("name");
245                 theController.imagery.push({ label:xml_name, data:xml_url });
246                                 if (xml_url==saved_url || (xml_name==saved_name && xml_name!='Custom')) { isSet=true; }
247                         }
248                         if (!isSet) { theController.imagery.push({ label:saved_name, data:saved_url }); }
249
250                         for each (var bg:Object in theController.imagery) {
251                                 if (bg['label']==saved_nameĀ || bg['data']==saved_url) {
252                                         setBackground(bg['label'], bg['data']);
253                                 }
254                         }
255                 }
256                 
257                 public function setBackground(name:String,url:String):void {
258                         // ** this should take an object with all parameters (source tag, etc.)
259                         if (url=='yahoo') { theMap.setBackground('') ; yahoo.show(); }
260                                      else { theMap.setBackground(url); yahoo.hide(); }
261                         var obj:SharedObject = SharedObject.getLocal("user_state");
262                         obj.setProperty("background_url",url );
263                         obj.setProperty("background_name",name);
264                         obj.flush();
265                 }
266
267                 private function onStylesheetsLoad(event:Event):void {
268                         var xml:XML = new XML(URLLoader(event.target).data);
269                         var saved_url:String = SharedObject.getLocal("user_state").data['stylesheet_url'];
270                         var saved_name:String= SharedObject.getLocal("user_state").data['stylesheet_name'];
271                         var xml_url:String, xml_name:String, isSet:Boolean=false;
272                         if (!saved_url) { saved_url='potlatch.css'; saved_name='Potlatch'; }
273                         
274                         // create menu
275             theController.stylesheets=new Array();
276                         for each(var set:XML in xml.stylesheet) {
277                                 xml_url= set.child("url");
278                                 xml_name=set.child("name");
279                 theController.stylesheets.push({ label:xml_name, data:xml_url });
280                                 if (xml_url==saved_url || (xml_name==saved_name && xml_name!='Custom')) { isSet=true; }
281                         }
282                         if (!isSet) { theController.stylesheets.push({ label:saved_name, data:saved_url }); }
283
284                         for each (var bg:Object in theController.stylesheets) {
285                                 if (bg['label']==saved_nameĀ || bg['data']==saved_url) { 
286                                         setStylesheet(bg['label'], bg['data']);
287                                 }
288                         }
289                 }
290
291                 public function setStylesheet(name:String,url:String):void {
292                         theMap.setStyle(url);
293                         var obj:SharedObject = SharedObject.getLocal("user_state");
294                         obj.setProperty("stylesheet_url",url);
295                         obj.setProperty("stylesheet_name",name);
296                         obj.flush();
297                 }
298
299
300         private function dragEnterHandler(event:DragEvent):void {
301             // Get the drop target component from the event object.
302             var dropTarget:Canvas=event.currentTarget as Canvas;
303             // Accept the drag only if the user is dragging poi with tags
304             if (event.dragSource.hasFormat('tags')) 
305             {
306                 DragManager.acceptDragDrop(dropTarget);
307             }
308         }
309         
310         private function dragDropHandler(event:DragEvent):void {
311                         // Deselect the dragged icon
312                         if (event.dragSource.dataForFormat('container')) {
313                                 event.dragSource.dataForFormat('container').selectedItem=-1;
314                         }
315
316                         // Get the tags
317             var tags:Array = event.dragSource.dataForFormat('tags') as Array;
318             var mapLoc:Point = Globals.vars.root.globalToLocal(new Point(event.stageX, event.stageY));
319             var lat:Number = Globals.vars.root.coord2lat(mapLoc.y);
320             var lon:Number = Globals.vars.root.coord2lon(mapLoc.x);
321             
322             var createAction:CompositeUndoableAction = new CompositeUndoableAction("Create POI");
323             
324             var node:Node = Connection.getConnectionInstance().createNode({}, lat, lon, createAction.push);
325             for each ( var tag:Object in tags ) {
326                 node.setTag(tag.k, tag.v, createAction.push);
327             }
328                         Connection.getConnectionInstance().registerPOI(node);
329                         MainUndoStack.getGlobalStack().addAction(createAction);
330             theController.setState(new SelectedPOINode(node));
331         }
332
333         ]]></mx:Script>
334
335 </mx:Application>
336