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