1 <?xml version="1.0" encoding="utf-8"?>
3 xmlns:mx="http://www.adobe.com/2006/mxml"
4 xmlns:halcyon="net.systemeD.halcyon.*"
5 xmlns:potlatch2="net.systemeD.potlatch2.*"
7 horizontalScrollPolicy="off"
8 verticalScrollPolicy="off"
9 horizontalAlign="center"
10 addedToStage="initApp()">
12 <mx:Style source="styles/Application.css"/>
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"
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"
24 <mx:WipeLeft id="wipeOut" duration="250"/>
25 <mx:WipeRight id="wipeIn" duration="250"/>
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:PopUpMenuButton id="gpsButton" itemClick="if (event.index==0) { trackLoader.load(); } else { new MyGpxDialog().init(); }">
35 <mx:Object label="GPS data" />
36 <mx:Object label="My tracks" />
40 <mx:Spacer width="100%"/>
41 <mx:Button label="Undo" click="MainUndoStack.getGlobalStack().undo();"
42 enabled="{MainUndoStack.getGlobalStack().canUndo()}"/>
43 <mx:Button label="Redo" click="MainUndoStack.getGlobalStack().redo();"
44 enabled="{MainUndoStack.getGlobalStack().canRedo()}"/>
45 <mx:Spacer width="100%"/>
46 <mx:Button label="Help" click="new HelpDialog().init();" />
47 <mx:Button label="Options" click="new OptionsDialog().init();" />
48 <mx:Button label="Save" icon="@Embed('embedded/save.svg')" click="SaveManager.saveChanges();" id="saveButton"
50 </mx:ApplicationControlBar>
52 <mx:HDividedBox width="100%" height="100%">
55 <potlatch2:TagViewer width="25%" height="100%" id="tagViewer"/>
57 <mx:Canvas width="75%" height="100%">
58 <mx:Canvas id="map_area" resize="onResizeMap()"
59 top="0" left="0" width="100%" height="100%" dragEnter="dragEnterHandler(event);" dragDrop="dragDropHandler(event);">
61 <mx:Image source="@Embed('embedded/zoomIn.svg')" right="3" top="3" click="theMap.zoomIn();"
62 rollOverEffect="glowImage" rollOutEffect="unglowImage" toolTip="{'Zoom in - currently z'+getScale()}" />
63 <mx:Image source="@Embed('embedded/zoomOut.svg')" right="3" top="20" click="theMap.zoomOut();"
64 rollOverEffect="glowImage" rollOutEffect="unglowImage" toolTip="{'Zoom out - currently z'+getScale()}" />
65 <mx:TextArea id="dataWorking" text="" right="20" top="3" disabledColor="black" backgroundDisabledColor="0xFFFFEA" height="18"
66 enabled="false" borderThickness="0"
67 showEffect="{wipeIn}" hideEffect="{wipeOut}"/>
74 import net.systemeD.halcyon.*;
75 import net.systemeD.halcyon.connection.*;
76 import net.systemeD.potlatch2.*;
77 import net.systemeD.potlatch2.save.SaveManager;
78 import net.systemeD.potlatch2.controller.*;
79 import net.systemeD.potlatch2.help.*;
80 import net.systemeD.potlatch2.options.*;
81 import net.systemeD.potlatch2.utils.*;
82 import net.systemeD.potlatch2.mygpx.*;
83 import mx.managers.PopUpManager;
84 import flash.system.Security;
86 import flash.events.MouseEvent;
87 import flash.display.Sprite;
88 import mx.core.IChildList;
89 import mx.containers.Canvas;
90 import mx.core.Application;
91 import mx.events.DragEvent;
92 import mx.managers.DragManager;
93 import mx.core.DragSource;
94 import mx.controls.Alert;
96 public var theMap:Map;
97 public var theController:EditController;
98 public var yahoo:Yahoo;
99 public var trackLoader:TrackLoader;
100 public var toolbox:Toolbox;
102 private var savecount:uint=0;
103 private var loadcount:uint=0;
105 public var version:String="(Foxbase) Alpha"; // mxml won't read it if it's a const. Go figure
107 private function initApp():void {
109 Globals.vars.map_area = map_area;
110 Globals.vars.root = map_area.rawChildren; // set up global reference to root level
111 var _root:IChildList = map_area.rawChildren; // convenient local shorthand
112 Globals.vars.nocache = loaderInfo.parameters['nocache'] == 'true';
114 // populate sharedObject with loaderInfo parameters if supplied
115 var obj:SharedObject = SharedObject.getLocal("user_state");
116 var objChanged:Boolean = false;
117 if (loaderInfo.parameters['tileurl']) {
118 obj.setProperty('background_url',loaderInfo.parameters['tileurl']);
119 obj.setProperty('background_name','Custom');
122 if (loaderInfo.parameters['style']) {
123 obj.setProperty('stylesheet_url',loaderInfo.parameters['style']);
124 obj.setProperty('stylesheet_name','Custom');
127 if (objChanged) { obj.flush(); }
129 // load imagery and style XML
130 var request:DebugURLRequest = new DebugURLRequest("imagery.xml");
131 var loader:URLLoader = new URLLoader();
132 loader.addEventListener(Event.COMPLETE, onImageryLoad);
133 loader.load(request.request);
135 var request2:DebugURLRequest = new DebugURLRequest("stylesheets.xml");
136 var loader2:URLLoader = new URLLoader();
137 loader2.addEventListener(Event.COMPLETE, onStylesheetsLoad);
138 loader2.load(request2.request);
140 // map backdrop object
141 var w:uint = map_area.width;
142 var h:uint = map_area.height;
143 var b:Sprite = new Sprite();
144 b.height=h; b.width=w;
145 b.graphics.beginFill(0xFFFFEA,100);
146 b.graphics.drawRect(0,0,w,h);
147 b.graphics.endFill();
150 // create map and Yahoo
151 theMap=new Map(this.loaderInfo.parameters);
153 yahoo=new Yahoo(w,h,theMap);
155 _root.addChild(yahoo);
156 _root.addChild(theMap);
157 theMap.updateSize(w,h);
158 theMap.addEventListener(MapEvent.SCALE, scaleHandler);
161 var s:Sprite=new Sprite();
162 s.graphics.beginFill(0xFFFFFF,100);
163 s.graphics.drawRect(0,0,w,h);
164 s.graphics.endFill();
168 // mouse-up handler attached to stage, so the user can release outside the map
169 stage.addEventListener(MouseEvent.MOUSE_UP, theMap.mouseUpHandler);
170 Globals.vars.map_area.addEventListener(MouseEvent.MOUSE_MOVE, theMap.mouseMoveHandler);
171 Globals.vars.map_area.addEventListener(MouseEvent.MOUSE_DOWN, theMap.mouseDownHandler);
173 // keyboard event attached to stage
174 stage.addEventListener(KeyboardEvent.KEY_UP, theMap.keyUpHandler);
177 toolbox=Toolbox(PopUpManager.createPopUp(this,Toolbox,false));
178 toolbox.init(theController);
179 toolbox.x=stage.stageWidth-toolbox.width-15;
180 toolbox.y=stage.stageHeight-toolbox.height-15;
183 var t:TextField=new TextField();
184 t.width=500; t.height=150; t.border=true;
187 Globals.vars.debug=t;
188 t.visible = loaderInfo.parameters["show_debug"] == 'true';
189 Globals.vars.root=theMap; // just for the addDebug function
191 theController = new EditController(theMap, tagViewer, toolbox);
192 theController.setActive();
194 var conn:Connection = Connection.getConnectionInstance();
195 conn.addEventListener(Connection.LOAD_STARTED, onDataStart);
196 conn.addEventListener(Connection.LOAD_COMPLETED, onDataComplete);
197 conn.addEventListener(Connection.SAVE_STARTED, onDataStart);
198 conn.addEventListener(Connection.SAVE_COMPLETED, onDataComplete);
199 conn.addEventListener(Connection.DATA_DIRTY, onDataDirty);
200 conn.addEventListener(Connection.DATA_CLEAN, onDataClean);
201 conn.addEventListener(MapEvent.ERROR, onMapError);
203 // set the access token from saved cookie
204 var tokenObject:SharedObject = SharedObject.getLocal("access_token");
205 conn.setAccessToken(tokenObject.data["oauth_token"], tokenObject.data["oauth_token_secret"]);
207 // create GPS trackloader
208 trackLoader=new TrackLoader(theMap,conn.apiBase);
210 // Force authentication on startup, if required
211 // force_auth == force => checks for access token, and displays OAuth panel if needed
212 var force_auth:String = loaderInfo.parameters["force_auth"];
213 if (!conn.hasAccessToken() && force_auth == 'force') {
214 SaveManager.ensureAccess(onAccessChecked);
217 // show help dialog on startup, if required
218 // show_help == always => on every startup
219 // show_help == once => show on first startup only
220 var show_help:String = loaderInfo.parameters["show_help"];
222 if (show_help == 'always' || (show_help == 'once' && obj.data["help_shown"] != "true")) {
223 new HelpDialog().init();
226 // Check if Tiger highlighting should be enabled from saved object
227 Globals.vars.highlightTiger = obj.data['tiger_highlighted'];
230 public function onMapError(event:MapEvent):void {
231 Alert.show(event.params.message, 'Error', mx.controls.Alert.OK);
234 public function onResizeMap():void {
235 if ( theMap != null )
236 theMap.updateSize(map_area.width, map_area.height);
239 toolbox.x=Math.min(toolbox.x,stage.stageWidth-toolbox.width-15);
240 toolbox.y=Math.min(toolbox.y,stage.stageHeight-toolbox.height-15);
244 private function onDataStart(event:Event):void {
245 switch (event.type) {
246 case Connection.LOAD_STARTED: loadcount++; break;
247 case Connection.SAVE_STARTED: savecount++; break;
251 private function onDataComplete(event:Event):void {
252 switch (event.type) {
253 case Connection.LOAD_COMPLETED: loadcount--; break;
254 case Connection.SAVE_COMPLETED: savecount--; break;
258 private function updateDataWorking():void {
259 if (loadcount>0 && savecount>0) { dataWorking.text="Loading/saving..."; }
260 else if (loadcount>0) { dataWorking.text="Loading data..."; }
261 else if (savecount>0) { dataWorking.text="Saving data..."; }
262 else { dataWorking.text=""; }
263 dataWorking.visible=(dataWorking.text!="");
265 private function onDataDirty(event:Event):void {
266 saveButton.enabled=true;
267 if (ExternalInterface.available) {
268 ExternalInterface.call("markChanged", false);
271 private function onDataClean(event:Event):void {
272 saveButton.enabled=false;
273 if (ExternalInterface.available) {
274 ExternalInterface.call("markChanged", true);
278 private function onImageryLoad(event:Event):void {
279 var xml:XML = new XML(URLLoader(event.target).data);
280 var saved_url:String = SharedObject.getLocal("user_state").data['background_url'];
281 var saved_name:String= SharedObject.getLocal("user_state").data['background_name'];
282 var xml_url:String, xml_name:String, isSet:Boolean=false;
283 var backgroundSet:Boolean = false;
285 theController.imagery=new Array(
286 { label: "None", data: "" },
287 { label: "Yahoo", data: "yahoo" } );
288 for each(var set:XML in xml.set) {
289 xml_url= set.child("url");
290 xml_name=set.child("name");
291 theController.imagery.push({ label:xml_name, data:xml_url });
292 if (xml_url==saved_url || (xml_name==saved_name && xml_name!='Custom')) { isSet=true; }
295 if (!isSet && saved_name != null && saved_url != null && saved_url != '' && saved_url != 'yahoo') {
296 theController.imagery.push({ label:saved_name, data:saved_url });
300 for each (var bg:Object in theController.imagery) {
301 if (bg['label']==saved_nameĀ || bg['data']==saved_url) {
302 setBackground(bg['label'], bg['data']);
303 backgroundSet = true;
307 // For most contributors it's useful to set the background to yahoo by default, I reckon, but lets make it a config
308 if (!backgroundSet && loaderInfo.parameters['yahoo_default'] == 'true') {
309 setBackground("Yahoo", "yahoo");
313 public function setBackground(name:String,url:String):void {
314 // ** this should take an object with all parameters (source tag, etc.)
315 if (url=='yahoo') { theMap.setBackground('') ; yahoo.show(); }
316 else { theMap.setBackground(url); yahoo.hide(); }
317 var obj:SharedObject = SharedObject.getLocal("user_state");
318 obj.setProperty("background_url",url );
319 obj.setProperty("background_name",name);
323 private function onStylesheetsLoad(event:Event):void {
324 var xml:XML = new XML(URLLoader(event.target).data);
325 var saved_url:String = SharedObject.getLocal("user_state").data['stylesheet_url'];
326 var saved_name:String= SharedObject.getLocal("user_state").data['stylesheet_name'];
327 var xml_url:String, xml_name:String, isInMenu:Boolean=false, isSet:Boolean=false;
329 // first, build the menu from the stylesheet list.
330 // Also ensure the saved_url is in the menu (might be either saved from before, or supplied via loaderInfo)
331 theController.stylesheets=new Array();
332 for each(var set:XML in xml.stylesheet) {
333 xml_url= set.child("url");
334 xml_name=set.child("name");
335 theController.stylesheets.push({ label:xml_name, data:xml_url });
336 if (xml_url==saved_url || (xml_name==saved_name && xml_name!='Custom')) { isInMenu=true; }
338 if (saved_url && !isInMenu) { theController.stylesheets.push({ label:saved_name, data:saved_url }); }
340 // pick a stylesheet to be set. It should be the saved one, if it is in the menu
341 // or alternatively the first one on the menu,
342 // or finally try 'potlatch.css'
343 for each (var ss:Object in theController.stylesheets) {
344 if (ss['label']==saved_nameĀ || ss['data']==saved_url) {
345 setStylesheet(ss['label'], ss['data']);
351 if(theController.stylesheets.length > 0) {
352 var s:Object = theController.stylesheets[0];
353 setStylesheet(s['label'], s['data']);
355 //hit and hope. FIXME should this be an error state?
356 theController.stylesheets.push({ label:'Potlatch', data:'potlatch.css'});
357 setStylesheet('Potlatch','potlatch.css');
362 public function setStylesheet(name:String,url:String):void {
363 theMap.setStyle(url);
364 var obj:SharedObject = SharedObject.getLocal("user_state");
365 obj.setProperty("stylesheet_url",url);
366 obj.setProperty("stylesheet_name",name);
370 private function scaleHandler(event:MapEvent):void {
371 dispatchEvent(new Event("rescale"));
374 [Bindable(event="rescale")]
375 private function getScale():String {
376 return String(theMap.scale);
379 private function dragEnterHandler(event:DragEvent):void {
380 // Get the drop target component from the event object.
381 var dropTarget:Canvas=event.currentTarget as Canvas;
382 // Accept the drag only if the user is dragging poi with tags
383 if (event.dragSource.hasFormat('tags'))
385 DragManager.acceptDragDrop(dropTarget);
389 private function dragDropHandler(event:DragEvent):void {
390 // Deselect the dragged icon
391 if (event.dragSource.dataForFormat('container')) {
392 event.dragSource.dataForFormat('container').selectedItem=-1;
396 var tags:Array = event.dragSource.dataForFormat('tags') as Array;
397 var mapLoc:Point = Globals.vars.root.globalToLocal(new Point(event.stageX, event.stageY));
398 var lat:Number = Globals.vars.root.coord2lat(mapLoc.y);
399 var lon:Number = Globals.vars.root.coord2lon(mapLoc.x);
401 var createAction:CompositeUndoableAction = new CompositeUndoableAction("Create POI");
403 var node:Node = Connection.getConnectionInstance().createNode({}, lat, lon, createAction.push);
404 for each ( var tag:Object in tags ) {
405 node.setTag(tag.k, tag.v, createAction.push);
407 Connection.getConnectionInstance().registerPOI(node);
408 MainUndoStack.getGlobalStack().addAction(createAction);
409 theController.setState(new SelectedPOINode(node));
412 /* Called when forcing an initial access token check. */
413 private function onAccessChecked():void {
414 // Floaty panel message "You now have access";