Provide a new 'View data' button in the upload dialogue.
[potlatch2.git] / potlatch2.mxml
index a00c46d..9ad7000 100644 (file)
@@ -7,9 +7,11 @@
        horizontalScrollPolicy="off"
        verticalScrollPolicy="off"
        horizontalAlign="center" 
-       addedToStage="initApp()"
+       addedToStage="startInit()"
        preloader="net.systemeD.potlatch2.Preloader">
-       
+
+    <map:LocalizationMap xmlns:map="l10n.map.*" />
+
        <mx:Style source="styles/Application.css"/>
 
     <mx:Glow id="glowImage" duration="100" 
                          else if (event.index==1) { new MyGpxDialog().init(); } 
                                              else { mx.controls.Menu(gpsButton.popUp).selectedIndex=0; 
                                                     mx.controls.Menu(gpsButton.popUp).selectedIndex=0; /* Yes, we do need to call it twice */
-                                                    theMap.removeVectorLayer(theMap.findVectorLayer('GPS tracks')); }">
+                                                    theMap.removeLayerByName('GPS tracks'); }">
                        <mx:dataProvider>
                                <mx:Array>
-                                       <mx:Object label="GPS data" />
-                                       <mx:Object label="My tracks" />
-                                       <mx:Object label="Clear" />
+                                       <mx:Object label="GPS data" id="gpsData" />
+                                       <mx:Object label="My tracks" id="myTracks" />
+                                       <mx:Object label="Clear" id="clearGps" />
                                </mx:Array>
                        </mx:dataProvider>
                </mx:PopUpMenuButton>
         <mx:Button id="redo" label="Redo" click="MainUndoStack.getGlobalStack().redo();" styleName="appBarButton"
             enabled="{MainUndoStack.getGlobalStack().canRedo()}"/>
         <mx:Spacer width="100%"/>
-        <mx:Button label="Help" click="new HelpDialog().init();" styleName="appBarButton" />
-        <mx:Button label="Options" click="new OptionsDialog().init();" styleName="appBarButton" /> 
+        <mx:Button id="helpButton" label="Help" click="new HelpDialog().init();" styleName="appBarButton" />
+        <mx:Button id="optionsButton" label="Options" click="new OptionsDialog().init();" styleName="appBarButton" />
         <mx:Button label="Save" icon="@Embed('embedded/upload.svg')" disabledIcon="@Embed('embedded/upload_disabled.svg')" styleName="appBarButton"
-                       click="SaveManager.saveChanges();" id="saveButton" enabled="false"/>
+                       click="SaveManager.saveChanges(theMap.editableLayer.connection);" id="saveButton" enabled="false"/>
     </mx:ApplicationControlBar>
-
-    <map:LocalizationMap xmlns:map="l10n.map.*" />
     
     <mx:HDividedBox width="100%" height="100%">
 
@@ -72,6 +72,8 @@
             rollOverEffect="glowImage" rollOutEffect="unglowImage" toolTip="{'Zoom in - currently z'+getScale()}" />
         <mx:Image source="@Embed('embedded/zoomOut.svg')" right="3" top="20" click="theMap.zoomOut();"
             rollOverEffect="glowImage" rollOutEffect="unglowImage" toolTip="{'Zoom out - currently z'+getScale()}" />
+        <mx:Image source="@Embed('embedded/search.svg')" right="3" top="40" click="new FindDialog().init();"
+            rollOverEffect="glowImage" rollOutEffect="unglowImage" toolTip="Find place" />
         <mx:TextArea id="dataWorking" text="" right="20" top="3" disabledColor="black" backgroundDisabledColor="0xFFFFEA" height="18"
             enabled="false" borderThickness="0"
             showEffect="{wipeIn}" hideEffect="{wipeOut}"/>
@@ -87,6 +89,7 @@
                import net.systemeD.halcyon.connection.*;
                import net.systemeD.potlatch2.*;
                import net.systemeD.potlatch2.save.SaveManager;
+               import net.systemeD.potlatch2.find.*;
                import net.systemeD.potlatch2.collections.*;
                import net.systemeD.potlatch2.controller.*;
                import net.systemeD.potlatch2.help.*;
 
         include "version.as";
 
-               private function initApp():void {
+               private function startInit():void {
+            /* For reasons that I don't yet understand, the en_US locale is the default and doesn't work at all. */
+            /* Change this to another locale (e.g. fr_FR) to see the differences. */
+            if (loaderInfo.parameters['locale']) {
+              dispatchEvent(new LocaleEvent(LocaleEvent.LOAD_LOCALE,loaderInfo.parameters['locale']));
+            }
 
-            /* For reasons that I don't yet understand, the en_US locale is the default and doesn't work at all. Use en_GB instead. */
-            /* Change this to another locale (e.g. fr_FR) to see the differences. This will obviously become done differently at some point. */
-            dispatchEvent(new LocaleEvent(LocaleEvent.LOAD_LOCALE,'en_GB'));
+                       // Load font and don't do anything until that's done
+                       var loader:Loader = new Loader();
+                       loader.contentLoaderInfo.addEventListener(Event.COMPLETE, continueInit);
+                       loader.load(new URLRequest("FontLibrary.swf"));
+               }
+               
+               private function continueInit(r:Event):void {
+                       // Set font
+                       var FontLibrary:Class = r.target.applicationDomain.getDefinition("FontLibrary") as Class;
+                       Font.registerFont(FontLibrary.DejaVu);
 
                        Globals.vars.map_area = map_area;
                        Globals.vars.root = map_area.rawChildren;                       // set up global reference to root level
                        var _root:IChildList = map_area.rawChildren;            // convenient local shorthand
                        Globals.vars.nocache = loaderInfo.parameters['nocache'] == 'true';
+                       Globals.vars.flashvars = loaderInfo.parameters;
 
                        // populate sharedObject with loaderInfo parameters if supplied
                        var obj:SharedObject = SharedObject.getLocal("user_state");
 
                        // pass additional parameters to map
                        var params:Object={}; var k:String;
-                       for (k in this.loaderInfo.parameters) {
-                               params[k]=this.loaderInfo.parameters[k];
-                       }
-                       for (k in SharedObject.getLocal('user_state').data) {
-                               params[k]=SharedObject.getLocal('user_state').data[k];
-                       }
-                       params['tileblocks']=[new RegExp("google","i")];        // hard-coded block on Google tiles
-
-                       // create map and Yahoo
-                       theMap=new Map(params);
-
-            // Auto-load vector backgrounds from config
-            // This could go spectacularly wrong if map.init completes before the event listener below is added
-            theMap.addEventListener(MapEvent.INITIALISED, function(e:Event):void { VectorBackgrounds.instance().init(theMap); });
+                       for (k in this.loaderInfo.parameters) params[k]=this.loaderInfo.parameters[k];
+                       for (k in SharedObject.getLocal('user_state').data) params[k]=SharedObject.getLocal('user_state').data[k];
 
+                       // Create map
+                       theMap=new Map();
                        theMap.backdrop=b;
-                       yahoo=new Yahoo(w,h,theMap);
+            theMap.updateSize(w,h);
+                       theMap.addEventListener(MapEvent.SCALE, scaleHandler);
+
+                       yahoo=new Yahoo(theMap);
                        yahoo.hide();
                        _root.addChild(yahoo);
                        _root.addChild(theMap);
-            theMap.updateSize(w,h);
-                       theMap.addEventListener(MapEvent.SCALE, scaleHandler);
+
+                       // Initialise 900913 background
+                       // >>>> REFACTOR: something odd about accessing map.tileparams here...
+                       theMap.tileset.blocks=[new RegExp("google","i")];       // hard-coded block on Google tiles
+                       theMap.tileset.init(theMap.tileparams, false, 
+                                           params['background_dim']    ==null ? true  : params['background_dim'],
+                                           params['background_sharpen']==null ? false : params['background_sharpen']);
+
+                       // Add core data layer
+                       // >>>> REFACTOR: shouldn't be hardcoded to XMLConnection
+                       var conn:Connection = new XMLConnection("Main", params['api'], params['policy'], params);
+            conn.addEventListener(Connection.LOAD_STARTED, onDataStart);
+            conn.addEventListener(Connection.LOAD_COMPLETED, onDataComplete);
+            conn.addEventListener(Connection.SAVE_STARTED, onDataStart);
+            conn.addEventListener(Connection.SAVE_COMPLETED, onDataComplete);
+            conn.addEventListener(Connection.DATA_DIRTY, onDataDirty);
+            conn.addEventListener(Connection.DATA_CLEAN, onDataClean);
+                       conn.addEventListener(MapEvent.ERROR, onMapError);
+                       conn.addEventListener(AttentionEvent.ATTENTION, onAttention);
+                       conn.addEventListener(AttentionEvent.ALERT, onAlert);
+                       // >>>> REFACTOR: shouldn't be trying to find out stylesheet from here
+                       theMap.addLayer(conn,params['styleurl'] ? params['styleurl'] : 'stylesheets/potlatch.css',false);
+
+            // Auto-load vector backgrounds from config
+            theMap.addEventListener(MapEvent.INITIALISED, function(e:Event):void { VectorBackgrounds.instance().init(theMap); });
+
+                       // Set start position of map
+                       // >>>> REFACTOR: what happens if lat/lon/zoom not supplied? (Hurleston GPX case)
+                       theMap.init(params['lat'], params['lon'], params['zoom']);
 
                        // add attribution/logo sprite
                        var overlay:Sprite=new Sprite();
 
             theController = new EditController(theMap, tagViewer, toolbox);
             theController.setActive();
+                       theController.addEventListener(AttentionEvent.ATTENTION, onAttention);
+                       theController.addEventListener(AttentionEvent.ALERT, onAlert);
             
-            var conn:Connection = Connection.getConnectionInstance();
-            conn.addEventListener(Connection.LOAD_STARTED, onDataStart);
-            conn.addEventListener(Connection.LOAD_COMPLETED, onDataComplete);
-            conn.addEventListener(Connection.SAVE_STARTED, onDataStart);
-            conn.addEventListener(Connection.SAVE_COMPLETED, onDataComplete);
-            conn.addEventListener(Connection.DATA_DIRTY, onDataDirty);
-            conn.addEventListener(Connection.DATA_CLEAN, onDataClean);
-                       conn.addEventListener(MapEvent.ERROR, onMapError);
-                       conn.addEventListener(AttentionEvent.ATTENTION, onAttention);
-                       conn.addEventListener(AttentionEvent.ALERT, onAlert);
-
             // set the access token from saved cookie
             var tokenObject:SharedObject = SharedObject.getLocal("access_token");
             conn.setAccessToken(tokenObject.data["oauth_token"], tokenObject.data["oauth_token_secret"]);
                 if (conn.hasAccessToken()) { 
                     loadTrace(id);
                 } else {
-                    SaveManager.ensureAccess(function ():void { loadTrace(id); });
+                    SaveManager.ensureAccess(function ():void { loadTrace(id); }, conn);
                 }
             }
 
                        // Load arbitrary GPX from provided URL
                        if (loaderInfo.parameters['gpx_url']) {
-                               var vectorlayer:VectorLayer=new VectorLayer(name,theMap,"stylesheets/gpx.css");
-                               vectorlayer.url=loaderInfo.parameters['gpx_url'];
-                               var gpx:GpxImporter=new GpxImporter(vectorlayer, vectorlayer.paint, [vectorlayer.url], 
-                                                                   function(success:Boolean,message:String=null):void {
-                                       if (!success) return;
-                                       theMap.addVectorLayer(vectorlayer);
-                                       dispatchEvent(new Event("layers_changed"));
-                                                                   }, false);
+
+                var gpx_url:String = loaderInfo.parameters['gpx_url'];
+
+                var connection:Connection = new Connection(name, gpx_url, null, null);
+                var gpx:GpxImporter=new GpxImporter(connection, theMap, [gpx_url],
+                                                function(success:Boolean,message:String=null):void {
+                                                    if (!success) return;
+                                                    var paint:MapPaint = theMap.addLayer(connection, "stylesheets/gpx.css");
+                                                    paint.updateEntityUIs(false, false);
+                                                    dispatchEvent(new Event("layers_changed"));
+                                                }, false);
                        }
 
                        // create GPS trackloader
             // force_auth == force => checks for access token, and displays OAuth panel if needed
             var force_auth:String = loaderInfo.parameters["force_auth"];
             if (!conn.hasAccessToken() && force_auth == 'force') {
-              SaveManager.ensureAccess(onAccessChecked);
+              SaveManager.ensureAccess(onAccessChecked, conn);
             }
 
             // show help dialog on startup, if required
             
             var createAction:CompositeUndoableAction = new CompositeUndoableAction("Create POI");
             
-            var node:Node = Connection.getConnectionInstance().createNode({}, lat, lon, createAction.push);
+                       var conn:Connection = theMap.editableLayer.connection;
+            var node:Node = conn.createNode({}, lat, lon, createAction.push);
             for each ( var tag:Object in tags ) {
                 node.setTag(tag.k, tag.v, createAction.push);
             }
-                       Connection.getConnectionInstance().registerPOI(node);
+                       conn.registerPOI(node);
                        MainUndoStack.getGlobalStack().addAction(createAction);
             theController.setState(new SelectedPOINode(node));
         }
         }
 
         private function loadTrace(id:Number):void {
-            var conn:Connection = Connection.getConnectionInstance();
+            var conn:Connection = theMap.editableLayer.connection;
             conn.addEventListener(Connection.TRACES_LOADED, function (e:Event):void {
                 for each (var trace:Trace in conn.getTraces()) {
                     if (trace.id == id) { trace.addToMap(); }