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