Select new POIs when they are dropped on the map.
[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="Help" click="new HelpDialog().init();" />
32         <mx:Button label="Options" click="new OptionsDialog().init();" />
33         <mx:Button label="Save" icon="@Embed('embedded/save.svg')" click="SaveManager.saveChanges();"/>
34     </mx:ApplicationControlBar>
35     
36     <mx:HDividedBox width="100%" height="100%">
37
38           <!-- Tag viewer -->
39           <potlatch2:TagViewer width="25%" height="100%" id="tagViewer"/>
40
41       <mx:Canvas width="75%" height="100%">
42         <mx:Canvas id="map_area" resize="onResizeMap()"
43             top="0" left="0" width="100%" height="100%" dragEnter="dragEnterHandler(event);" dragDrop="dragDropHandler(event);">
44         </mx:Canvas>
45         <mx:Image source="@Embed('embedded/zoomIn.svg')" right="3" top="3" click="theMap.zoomIn();"
46             rollOverEffect="glowImage" rollOutEffect="unglowImage"/>
47         <mx:Image source="@Embed('embedded/zoomOut.svg')" right="3" top="20" click="theMap.zoomOut();"
48             rollOverEffect="glowImage" rollOutEffect="unglowImage"/>
49         <mx:TextArea id="dataWorking" text="" right="20" top="3" disabledColor="black" backgroundDisabledColor="0xFFFFEA" height="18"
50             enabled="false" borderThickness="0"
51             showEffect="{wipeIn}" hideEffect="{wipeOut}"/>
52       </mx:Canvas>
53     </mx:HDividedBox>
54
55         <mx:Script><![CDATA[
56                 import net.systemeD.halcyon.*;
57                 import net.systemeD.halcyon.connection.*;
58                 import net.systemeD.potlatch2.*;
59                 import net.systemeD.potlatch2.save.SaveManager;
60                 import net.systemeD.potlatch2.controller.*;
61                 import net.systemeD.potlatch2.help.*;
62                 import net.systemeD.potlatch2.options.*;
63                 import net.systemeD.potlatch2.utils.*;
64                 import mx.managers.PopUpManager;
65                 import flash.system.Security;
66                 import flash.net.*;
67                 import flash.events.MouseEvent;
68                 import flash.display.Sprite;
69                 import mx.core.IChildList;
70         import mx.containers.Canvas;
71                 import mx.core.Application;
72                 import com.yahoo.maps.api.YahooMap;
73                 import com.yahoo.maps.api.YahooMapEvent;
74                 import com.yahoo.maps.api.core.location.LatLon;
75         import mx.events.DragEvent;
76         import mx.managers.DragManager;
77         import mx.core.DragSource;
78
79         public var theMap:Map;
80         public var theController:EditController;
81                 public var yahooListener:Object = new Object();
82                 public var yahoo:YahooMap = new YahooMap(); 
83
84                 private function initApp():void {
85
86                         Globals.vars.map_area = map_area;
87                         Globals.vars.yahoo = yahoo;
88                         Globals.vars.root = map_area.rawChildren;                       // set up global reference to root level
89                         var _root:IChildList = map_area.rawChildren;            // convenient local shorthand
90
91                         // map backdrop object
92             var w:uint = map_area.width;
93             var h:uint = map_area.height;
94             var b:Sprite = new Sprite();
95             b.height=h; b.width=w;
96             b.graphics.beginFill(0xFFFFEA,100); 
97             b.graphics.drawRect(0,0,w,h); 
98             b.graphics.endFill();
99                         _root.addChild(b);
100
101                         // Add Yahoo! background
102                         yahoo.init("f0a.sejV34HnhgIbNSmVHmndXFpijgGeun0fSIMG9428hW_ifF3pYKwbV6r9iaXojl1lU_dakekR", w, h);  
103                         yahoo.mapType="satellite";
104                         _root.addChild(yahoo);
105                         yahoo.visible=false;
106                         yahoo.alpha=0.5;
107
108                         // Yahoo! listeners
109                         yahooListener.yahooInit=function(event:YahooMapEvent):void {
110                                 yahoo.zoomLevel = 18-theMap.scale;
111                                 yahoo.centerLatLon = new LatLon(theMap.centre_lat, theMap.centre_lon);
112
113                                 yahooListener.moveHandler=function(event:MapEvent):void {
114                                         if (!yahoo.visible) { return; }
115                                         yahoo.zoomLevel=18-event.params.scale;
116                                         yahoo.centerLatLon=new LatLon(event.params.lat, event.params.lon);
117                                 };
118                                 theMap.addEventListener(MapEvent.MOVE, yahooListener.moveHandler);
119
120                                 yahooListener.resizeHandler=function(event:MapEvent):void {
121                                         yahoo.setSize(event.params.width, event.params.height);
122                                 };
123                                 theMap.addEventListener(MapEvent.RESIZE, yahooListener.resizeHandler);
124                         }
125                         yahoo.addEventListener(YahooMapEvent.MAP_INITIALIZE, yahooListener.yahooInit);
126
127                         // add map
128                         theMap=new Map(this.loaderInfo.parameters);
129                         theMap.backdrop=b;
130                         _root.addChild(theMap);
131             theMap.updateSize(w, h);
132
133                         // add mask for map
134                         var s:Sprite=new Sprite();
135                         s.graphics.beginFill(0xFFFFFF,100);
136                         s.graphics.drawRect(0,0,w,h);
137                         s.graphics.endFill();
138                         _root.addChild(s);
139                         theMap.mask=s;
140
141                         // add crosshair
142                         /*s=new Sprite();
143                         s.graphics.lineStyle(2,0);
144                         s.graphics.moveTo(stage.stageWidth/2-20,stage.stageHeight/2);
145                         s.graphics.lineTo(stage.stageWidth/2+20,stage.stageHeight/2);
146                         s.graphics.moveTo(stage.stageWidth/2,stage.stageHeight/2-20);
147                         s.graphics.lineTo(stage.stageWidth/2,stage.stageHeight/2+20);
148                         _root.addChild(s);*/
149
150                         // mouse-up handler attached to stage, so the user can release outside the map
151                         stage.addEventListener(MouseEvent.MOUSE_UP, theMap.mouseUpHandler);
152                         Globals.vars.map_area.addEventListener(MouseEvent.MOUSE_MOVE, theMap.mouseMoveHandler);
153                         Globals.vars.map_area.addEventListener(MouseEvent.MOUSE_DOWN, theMap.mouseDownHandler);
154
155                         // keyboard event attached to stage
156                         stage.addEventListener(KeyboardEvent.KEY_UP, theMap.keyUpHandler);
157
158                         // example listener event
159                         var myListenerObj:Object = new Object();
160                         myListenerObj.mapHandler=function(event:MapEvent):void {
161                                 Globals.vars.debug.appendText("Download event fired - "+event.params.minlat+","+event.params.minlon+"\n");
162                         };
163                         theMap.addEventListener(MapEvent.DOWNLOAD, myListenerObj.mapHandler);
164
165                         // add debug field
166                         var t:TextField=new TextField();
167                         t.width=500; t.height=150; t.border=true;
168                         t.multiline=true;
169                         _root.addChild(t);
170                         Globals.vars.debug=t;
171             t.visible = loaderInfo.parameters["show_debug"] == 'true';
172                         Globals.vars.root=theMap;       // just for the addDebug function
173
174             theController = new EditController(theMap, tagViewer);
175             theController.setActive();
176             
177             var conn:Connection = Connection.getConnectionInstance();
178             conn.addEventListener(Connection.LOAD_STARTED, onDataStart);
179             conn.addEventListener(Connection.LOAD_COMPLETED, onDataComplete);
180
181                         // and to import a shapefile...
182 //                      var importer:ShpImporter = new ShpImporter(theMap, ["http://127.0.0.1/~richard/shp/wildrnp020.shp","http://127.0.0.1/~richard/shp/wildrnp020.shx","http://127.0.0.1/~richard/shp/wildrnp020.dbf"]);
183                 }
184
185         public function onResizeMap():void {
186             if ( theMap != null )
187                 theMap.updateSize(map_area.width, map_area.height);
188         }
189
190         private function onDataStart(event:Event):void {
191             dataWorking.text = event.type == Connection.LOAD_STARTED ? "Loading data..." : "Saving Data...";
192             dataWorking.visible = true;
193         }
194         private function onDataComplete(event:Event):void {
195             dataWorking.visible = false;
196         }
197         
198
199         
200         private function dragEnterHandler(event:DragEvent):void {
201             // Get the drop target component from the event object.
202             var dropTarget:Canvas=event.currentTarget as Canvas;
203             // Accept the drag only if the user is dragging poi with tags
204             if (event.dragSource.hasFormat('tags')) 
205             {
206                 DragManager.acceptDragDrop(dropTarget);
207             }
208         }
209         
210         private function dragDropHandler(event:DragEvent):void {
211             // Get the data identified by the color format from the drag source.
212             // Blame http://www.adobe.com/devnet/flex/quickstart/adding_drag_and_drop/#manual
213             // for whatever horrid abuse of "color format" this is doing
214             var tags:Array = event.dragSource.dataForFormat('tags') as Array;
215             var mapLoc:Point = Globals.vars.root.globalToLocal(new Point(event.stageX, event.stageY));
216             var lat:Number = Globals.vars.root.coord2lat(mapLoc.y);
217             var lon:Number = Globals.vars.root.coord2lon(mapLoc.x);
218             var node:Node = Connection.getConnectionInstance().createNode({}, lat, lon);
219             for each ( var tag:Object in tags ) {
220               node.setTag(tag.k, tag.v);
221             }
222                         Connection.getConnectionInstance().registerPOI(node);
223             theController.setState(new SelectedPOINode(node));
224         }
225
226         ]]></mx:Script>
227
228 </mx:Application>
229